diff options
author | flywind <43030857+xflywind@users.noreply.github.com> | 2020-12-31 04:54:40 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-31 11:54:40 +0100 |
commit | 5fb56a3b2c83b62d72c72a9d56ef1333671bc2b6 (patch) | |
tree | e32cffb18e3180a81f8179130cc99800d6e505df /lib/std/private | |
parent | 5984f7a7dda5e6fb3119cd5705d5758e1b8f3fc7 (diff) | |
download | Nim-5fb56a3b2c83b62d72c72a9d56ef1333671bc2b6.tar.gz |
refactor cmpIgnoreStyle and cmpIgnoreCase (#16399)
* init * support strutils * more * better * Call len once per string/cstring * Change var to let * Compare ternary on first char * More appropriate param name * fix * better * one test * impl * more efficient * minor Co-authored-by: Clyybber <darkmine956@gmail.com>
Diffstat (limited to 'lib/std/private')
-rw-r--r-- | lib/std/private/strimpl.nim | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/lib/std/private/strimpl.nim b/lib/std/private/strimpl.nim new file mode 100644 index 000000000..ae752165a --- /dev/null +++ b/lib/std/private/strimpl.nim @@ -0,0 +1,53 @@ +func toLowerAscii*(c: char): char {.inline.} = + if c in {'A'..'Z'}: + result = chr(ord(c) + (ord('a') - ord('A'))) + else: + result = c + +template firstCharCaseSensitiveImpl(a, b: typed, aLen, bLen: int) = + if aLen == 0 or bLen == 0: + return aLen - bLen + if a[0] != b[0]: return ord(a[0]) - ord(b[0]) + +template cmpIgnoreStyleImpl*(a, b: typed, firstCharCaseSensitive: static bool = false) = + # a, b are string or cstring + let aLen = a.len + let bLen = b.len + var i = 0 + var j = 0 + when firstCharCaseSensitive: + firstCharCaseSensitiveImpl(a, b, aLen, bLen) + inc i + inc j + while true: + while i < aLen and a[i] == '_': inc i + while j < bLen and b[j] == '_': inc j + let aa = if i < aLen: toLowerAscii(a[i]) else: '\0' + let bb = if j < bLen: toLowerAscii(b[j]) else: '\0' + result = ord(aa) - ord(bb) + if result != 0: return result + # the characters are identical: + if i >= aLen: + # both cursors at the end: + if j >= bLen: return 0 + # not yet at the end of 'b': + return -1 + elif j >= bLen: + return 1 + inc i + inc j + +template cmpIgnoreCaseImpl*(a, b: typed, firstCharCaseSensitive: static bool = false) = + # a, b are string or cstring + let aLen = a.len + let bLen = b.len + var i = 0 + when firstCharCaseSensitive: + firstCharCaseSensitiveImpl(a, b, aLen, bLen) + inc i + var m = min(aLen, bLen) + while i < m: + result = ord(toLowerAscii(a[i])) - ord(toLowerAscii(b[i])) + if result != 0: return + inc i + result = aLen - bLen |