mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-19 14:00:35 +00:00
Fix find routines' api to default to last=-1 (#19761)
This changes the default for the `last` parameter of various `find` routines from `0` to `-1`. Previous default prevents limiting the search to the first character. This is a logic error, as full text search was performed for 2 *valid* values of `last`: `0` and `last.high()`. Adds an overload for `initSkipTable` which returns a newly initialized table. This encapsulates every single usage of a `var`-acting original func in this module. Co-authored-by: flywind <xzsflywind@gmail.com>
This commit is contained in:
@@ -232,18 +232,22 @@ template main() =
|
||||
{.pop.}
|
||||
|
||||
block: # find
|
||||
doAssert "0123456789ABCDEFGH".find('A') == 10
|
||||
doAssert "0123456789ABCDEFGH".find('A', 5) == 10
|
||||
doAssert "0123456789ABCDEFGH".find('A', 5, 10) == 10
|
||||
doAssert "0123456789ABCDEFGH".find('A', 5, 9) == -1
|
||||
doAssert "0123456789ABCDEFGH".find("A") == 10
|
||||
doAssert "0123456789ABCDEFGH".find("A", 5) == 10
|
||||
doAssert "0123456789ABCDEFGH".find("A", 5, 10) == 10
|
||||
doAssert "0123456789ABCDEFGH".find("A", 5, 9) == -1
|
||||
doAssert "0123456789ABCDEFGH".find({'A'..'C'}) == 10
|
||||
doAssert "0123456789ABCDEFGH".find({'A'..'C'}, 5) == 10
|
||||
doAssert "0123456789ABCDEFGH".find({'A'..'C'}, 5, 10) == 10
|
||||
doAssert "0123456789ABCDEFGH".find({'A'..'C'}, 5, 9) == -1
|
||||
const haystack: string = "0123456789ABCDEFGH"
|
||||
doAssert haystack.find('A') == 10
|
||||
doAssert haystack.find('A', 5) == 10
|
||||
doAssert haystack.find('A', 5, 10) == 10
|
||||
doAssert haystack.find('A', 5, 9) == -1
|
||||
doAssert haystack.find("A") == 10
|
||||
doAssert haystack.find("A", 5) == 10
|
||||
doAssert haystack.find("A", 5, 10) == 10
|
||||
doAssert haystack.find("A", 5, 9) == -1
|
||||
doAssert haystack.find({'A'..'C'}) == 10
|
||||
doAssert haystack.find({'A'..'C'}, 5) == 10
|
||||
doAssert haystack.find({'A'..'C'}, 5, 10) == 10
|
||||
doAssert haystack.find({'A'..'C'}, 5, 9) == -1
|
||||
doAssert haystack.find('A', 0, 0) == -1 # search limited to the first char
|
||||
doAssert haystack.find('A', 5, 0) == -1 # last < start
|
||||
doAssert haystack.find('A', 5, 4) == -1 # last < start
|
||||
|
||||
block:
|
||||
const haystack: string = "ABCABABABABCAB"
|
||||
@@ -290,17 +294,17 @@ template main() =
|
||||
|
||||
# when last <= start, searching for non-empty string
|
||||
block:
|
||||
let last: int = -1
|
||||
doAssert "abcd".find("ab", start=0, last=last) == -1
|
||||
doAssert "abcd".find("ab", start=1, last=last) == -1
|
||||
doAssert "abcd".find("bc", start=1, last=last) == -1
|
||||
doAssert "abcd".find("bc", start=2, last=last) == -1
|
||||
block:
|
||||
let last: int = 0
|
||||
let last: int = -1 # searching through whole line
|
||||
doAssert "abcd".find("ab", start=0, last=last) == 0
|
||||
doAssert "abcd".find("ab", start=1, last=last) == -1
|
||||
doAssert "abcd".find("bc", start=1, last=last) == 1
|
||||
doAssert "abcd".find("bc", start=2, last=last) == -1
|
||||
block:
|
||||
let last: int = 0
|
||||
doAssert "abcd".find("ab", start=0, last=last) == -1
|
||||
doAssert "abcd".find("ab", start=1, last=last) == -1
|
||||
doAssert "abcd".find("bc", start=1, last=last) == -1
|
||||
doAssert "abcd".find("bc", start=2, last=last) == -1
|
||||
block:
|
||||
let last: int = 1
|
||||
doAssert "abcd".find("ab", start=0, last=last) == 0
|
||||
|
||||
Reference in New Issue
Block a user