From 716c12a4365c04fcd164c8e1295805b3311cd517 Mon Sep 17 00:00:00 2001 From: Charles Blake Date: Thu, 19 Nov 2015 06:52:31 -0500 Subject: [PATCH 1/2] Fix loop index bug in scan for a[s] in b[s..s+len2-1]. a, b must both be indexed starting from s after the common prefix "strip" phase. This resolves issue 3477: https://github.com/nim-lang/Nim/issues/3477 --- lib/pure/strutils.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pure/strutils.nim b/lib/pure/strutils.nim index b61df60861..6c561eaf9e 100644 --- a/lib/pure/strutils.nim +++ b/lib/pure/strutils.nim @@ -1281,7 +1281,7 @@ proc editDistance*(a, b: string): int {.noSideEffect, # another special case: if len1 == 1: - for j in s..len2-1: + for j in s..s+len2-1: if a[s] == b[j]: return len2 - 1 return len2 From a1df79d5f2f5df1f6c81e28a91f3750f791d3fbd Mon Sep 17 00:00:00 2001 From: Charles Blake Date: Thu, 19 Nov 2015 07:03:06 -0500 Subject: [PATCH 2/2] Add a unit test that covers issue 3477 https://github.com/nim-lang/Nim/issues/3477 --- tests/stdlib/tstrutil.nim | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/stdlib/tstrutil.nim b/tests/stdlib/tstrutil.nim index b15bf0e688..b97f2b1e96 100644 --- a/tests/stdlib/tstrutil.nim +++ b/tests/stdlib/tstrutil.nim @@ -78,6 +78,7 @@ assert(editDistance("prefix__hallo_suffix", "prefix__ha_suffix") == 3) assert(editDistance("prefix__hallo_suffix", "prefix") == 14) assert(editDistance("prefix__hallo_suffix", "suffix") == 14) assert(editDistance("prefix__hallo_suffix", "prefix__hao_suffix") == 2) +assert(editDistance("main", "malign") == 2) assert "/1/2/3".rfind('/') == 4 assert "/1/2/3".rfind('/', 1) == 0