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/core | |
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/core')
-rw-r--r-- | lib/core/macros.nim | 19 | ||||
-rw-r--r-- | lib/core/typeinfo.nim | 26 |
2 files changed, 9 insertions, 36 deletions
diff --git a/lib/core/macros.nim b/lib/core/macros.nim index 97c3a46c5..748464061 100644 --- a/lib/core/macros.nim +++ b/lib/core/macros.nim @@ -1389,25 +1389,10 @@ when defined(nimVmEqIdent): ## these nodes will be unwrapped. else: + from std/private/strimpl import cmpIgnoreStyleImpl # this procedure is optimized for native code, it should not be compiled to nimVM bytecode. proc cmpIgnoreStyle(a, b: cstring): int {.noSideEffect.} = - proc toLower(c: char): char {.inline.} = - if c in {'A'..'Z'}: result = chr(ord(c) + (ord('a') - ord('A'))) - else: result = c - var i = 0 - var j = 0 - # first char is case sensitive - if a[0] != b[0]: return 1 - while true: - while a[i] == '_': inc(i) - while b[j] == '_': inc(j) # BUGFIX: typo - var aa = toLower(a[i]) - var bb = toLower(b[j]) - result = ord(aa) - ord(bb) - if result != 0 or aa == '\0': break - inc(i) - inc(j) - + cmpIgnoreStyleImpl(a, b, true) proc eqIdent*(a, b: string): bool = cmpIgnoreStyle(a, b) == 0 ## Check if two idents are equal. diff --git a/lib/core/typeinfo.nim b/lib/core/typeinfo.nim index c15f6dc1f..9b7e32466 100644 --- a/lib/core/typeinfo.nim +++ b/lib/core/typeinfo.nim @@ -91,6 +91,8 @@ when not defined(gcDestructors): else: include system/seqs_v2_reimpl +from std/private/strimpl import cmpIgnoreStyleImpl + when not defined(js): template rawType(x: Any): PNimType = cast[PNimType](x.rawTypePtr) @@ -366,36 +368,22 @@ iterator fields*(x: Any): tuple[name: string, any: Any] = for name, any in items(ret): yield ($name, any) -proc cmpIgnoreStyle(a, b: cstring): int {.noSideEffect.} = - proc toLower(c: char): char {.inline.} = - if c in {'A'..'Z'}: result = chr(ord(c) + (ord('a') - ord('A'))) - else: result = c - var i = 0 - var j = 0 - if a[0] != b[0]: return 1 - while true: - while a[i] == '_': inc(i) - while b[j] == '_': inc(j) # BUGFIX: typo - var aa = toLower(a[i]) - var bb = toLower(b[j]) - result = ord(aa) - ord(bb) - if result != 0 or aa == '\0': break - inc(i) - inc(j) +proc cmpNimIdentifier(a, b: cstring): int {.noSideEffect.} = + cmpIgnoreStyleImpl(a, b, true) proc getFieldNode(p: pointer, n: ptr TNimNode, name: cstring): ptr TNimNode = case n.kind of nkNone: assert(false) of nkSlot: - if cmpIgnoreStyle(n.name, name) == 0: + if cmpNimIdentifier(n.name, name) == 0: result = n of nkList: for i in 0..n.len-1: result = getFieldNode(p, n.sons[i], name) if result != nil: break of nkCase: - if cmpIgnoreStyle(n.name, name) == 0: + if cmpNimIdentifier(n.name, name) == 0: result = n else: var m = selectBranch(p, n) @@ -599,7 +587,7 @@ proc getEnumOrdinal*(x: Any, name: string): int = var n = typ.node var s = n.sons for i in 0 .. n.len-1: - if cmpIgnoreStyle($s[i].name, name) == 0: + if cmpNimIdentifier($s[i].name, name) == 0: if ntfEnumHole notin typ.flags: return i else: |