From 9e1d7a011345a6d495f8c59d90c9599920775a34 Mon Sep 17 00:00:00 2001 From: Julien Marrec Date: Thu, 26 Mar 2020 00:24:04 +0100 Subject: [PATCH 1/4] [OPTIONAL] Bump dependency versions --- jaro_winkler.gemspec | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/jaro_winkler.gemspec b/jaro_winkler.gemspec index 5731593..eabef75 100644 --- a/jaro_winkler.gemspec +++ b/jaro_winkler.gemspec @@ -25,8 +25,8 @@ Gem::Specification.new do |spec| 'source_code_uri' => "https://github.com/tonytonyjan/jaro_winkler/tree/v#{spec.version}", } spec.files = Dir['lib/**/*.rb', 'ext/**/*.{h,c}', 'LICENSE.txt'] - spec.add_development_dependency 'bundler', '~> 1.7' - spec.add_development_dependency 'rake', '~> 12.0' + spec.add_development_dependency 'bundler', '~> 2.1' + spec.add_development_dependency 'rake', '~> 13.0' spec.add_development_dependency 'rake-compiler' spec.add_development_dependency 'minitest' end From a742b861114b93a92f5a5f62a060e8b5483911e3 Mon Sep 17 00:00:00 2001 From: Julien Marrec Date: Wed, 25 Mar 2020 23:51:43 +0100 Subject: [PATCH 2/4] Allow compiling the gem with MSVC (which is not C99 compliant) * Do not use Variable-length arrays (VLA) but instead use a heap array with malloc & free * Redefine a macro --- ext/jaro_winkler/jaro.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/ext/jaro_winkler/jaro.c b/ext/jaro_winkler/jaro.c index 78ba27a..92e48f9 100644 --- a/ext/jaro_winkler/jaro.c +++ b/ext/jaro_winkler/jaro.c @@ -8,9 +8,9 @@ #define DEFAULT_WEIGHT 0.1 #define DEFAULT_THRESHOLD 0.7 -#define SWAP(x, y) \ +#define SWAP(type, x, y) \ do { \ - __typeof__(x) SWAP = x; \ + type SWAP = x; \ x = y; \ y = SWAP; \ } while (0) @@ -27,8 +27,8 @@ double jaro_distance_from_codes(uint32_t *codepoints1, size_t len1, return 0.0; if (len1 > len2) { - SWAP(codepoints1, codepoints2); - SWAP(len1, len2); + SWAP(uint32_t*, codepoints1, codepoints2); + SWAP(size_t, len1, len2); } if (opt->ignore_case) { @@ -42,8 +42,8 @@ double jaro_distance_from_codes(uint32_t *codepoints1, size_t len1, if (window_size < 0) window_size = 0; - char short_codes_flag[len1]; - char long_codes_flag[len2]; + char * short_codes_flag = malloc(len1*sizeof(char)); + char * long_codes_flag = malloc(len2*sizeof(char)); memset(short_codes_flag, 0, len1); memset(long_codes_flag, 0, len2); @@ -64,8 +64,11 @@ double jaro_distance_from_codes(uint32_t *codepoints1, size_t len1, } } - if (!match_count) + if (!match_count) { + free(short_codes_flag); + free(long_codes_flag); return 0.0; + } // count number of transpositions size_t transposition_count = 0, j = 0, k = 0; @@ -99,6 +102,10 @@ double jaro_distance_from_codes(uint32_t *codepoints1, size_t len1, double t = (double)(transposition_count / 2); if (opt->adj_table) m = similar_count / 10.0 + m; + + free(short_codes_flag); + free(long_codes_flag); + return (m / len1 + m / len2 + (m - t) / m) / 3; } From 12dc642cd62cdec8c4b518eee6ffc95249869b57 Mon Sep 17 00:00:00 2001 From: Julien Marrec Date: Thu, 26 Mar 2020 01:20:29 +0100 Subject: [PATCH 3/4] Us alloca (stack allocation) instead of malloc (heap) --- ext/jaro_winkler/jaro.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/ext/jaro_winkler/jaro.c b/ext/jaro_winkler/jaro.c index 92e48f9..2f47a7d 100644 --- a/ext/jaro_winkler/jaro.c +++ b/ext/jaro_winkler/jaro.c @@ -42,8 +42,8 @@ double jaro_distance_from_codes(uint32_t *codepoints1, size_t len1, if (window_size < 0) window_size = 0; - char * short_codes_flag = malloc(len1*sizeof(char)); - char * long_codes_flag = malloc(len2*sizeof(char)); + char * short_codes_flag = alloca(len1); + char * long_codes_flag = alloca(len2); memset(short_codes_flag, 0, len1); memset(long_codes_flag, 0, len2); @@ -65,8 +65,6 @@ double jaro_distance_from_codes(uint32_t *codepoints1, size_t len1, } if (!match_count) { - free(short_codes_flag); - free(long_codes_flag); return 0.0; } @@ -103,9 +101,6 @@ double jaro_distance_from_codes(uint32_t *codepoints1, size_t len1, if (opt->adj_table) m = similar_count / 10.0 + m; - free(short_codes_flag); - free(long_codes_flag); - return (m / len1 + m / len2 + (m - t) / m) / 3; } From f1ca425fdef06603e5c65b09c5b681f805e1e297 Mon Sep 17 00:00:00 2001 From: Julien Marrec Date: Thu, 26 Mar 2020 01:35:07 +0100 Subject: [PATCH 4/4] Obviously this doesn't work on MSVC, so use macros Ripped off https://www.gnu.org/software/autoconf/manual/autoconf-2.60/html_node/Particular-Functions.html --- ext/jaro_winkler/jaro.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/ext/jaro_winkler/jaro.c b/ext/jaro_winkler/jaro.c index 2f47a7d..046c2db 100644 --- a/ext/jaro_winkler/jaro.c +++ b/ext/jaro_winkler/jaro.c @@ -6,6 +6,23 @@ #include #include +#if HAVE_ALLOCA_H +# include +#elif defined __GNUC__ +# define alloca __builtin_alloca +#elif defined _AIX +# define alloca __alloca +#elif defined _MSC_VER +# include +# define alloca _alloca +#else +# include +# ifdef __cplusplus +extern "C" +# endif +void *alloca (size_t); +#endif + #define DEFAULT_WEIGHT 0.1 #define DEFAULT_THRESHOLD 0.7 #define SWAP(type, x, y) \