string comparisons don't have to know the strings representation
This commit is contained in:
parent
48b98cf701
commit
8f0c9b2fdd
1 changed files with 8 additions and 21 deletions
|
|
@ -9,36 +9,23 @@
|
||||||
|
|
||||||
## Compilerprocs for strings that do not depend on the string implementation.
|
## Compilerprocs for strings that do not depend on the string implementation.
|
||||||
|
|
||||||
proc cmpStrings(a, b: NimString): int {.inline, compilerProc.} =
|
proc cmpStrings(a, b: string): int {.inline, compilerProc.} =
|
||||||
if a == b: return 0
|
let alen = a.len
|
||||||
when defined(nimNoNil):
|
let blen = b.len
|
||||||
let alen = if a == nil: 0 else: a.len
|
|
||||||
let blen = if b == nil: 0 else: b.len
|
|
||||||
else:
|
|
||||||
if a == nil: return -1
|
|
||||||
if b == nil: return 1
|
|
||||||
let alen = a.len
|
|
||||||
let blen = b.len
|
|
||||||
let minlen = min(alen, blen)
|
let minlen = min(alen, blen)
|
||||||
if minlen > 0:
|
if minlen > 0:
|
||||||
result = c_memcmp(addr a.data, addr b.data, minlen.csize)
|
result = c_memcmp(unsafeAddr a[0], unsafeAddr b[0], minlen.csize)
|
||||||
if result == 0:
|
if result == 0:
|
||||||
result = alen - blen
|
result = alen - blen
|
||||||
else:
|
else:
|
||||||
result = alen - blen
|
result = alen - blen
|
||||||
|
|
||||||
proc eqStrings(a, b: NimString): bool {.inline, compilerProc.} =
|
proc eqStrings(a, b: string): bool {.inline, compilerProc.} =
|
||||||
if a == b: return true
|
let alen = a.len
|
||||||
when defined(nimNoNil):
|
let blen = b.len
|
||||||
let alen = if a == nil: 0 else: a.len
|
|
||||||
let blen = if b == nil: 0 else: b.len
|
|
||||||
else:
|
|
||||||
if a == nil or b == nil: return false
|
|
||||||
let alen = a.len
|
|
||||||
let blen = b.len
|
|
||||||
if alen == blen:
|
if alen == blen:
|
||||||
if alen == 0: return true
|
if alen == 0: return true
|
||||||
return equalMem(addr(a.data), addr(b.data), alen)
|
return equalMem(unsafeAddr(a[0]), unsafeAddr(b[0]), alen)
|
||||||
|
|
||||||
proc hashString(s: string): int {.compilerproc.} =
|
proc hashString(s: string): int {.compilerproc.} =
|
||||||
# the compiler needs exactly the same hash function!
|
# the compiler needs exactly the same hash function!
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue