correct comparisons for nil strings/seqs
This commit is contained in:
parent
13a380bca6
commit
f64f9e50cc
7 changed files with 91 additions and 23 deletions
|
|
@ -645,7 +645,6 @@ template replaceImpl(str: string, pattern: Regex,
|
|||
let bounds = match.matchBounds
|
||||
result.add(str.substr(lastIdx, bounds.a - 1))
|
||||
let nextVal = replacement
|
||||
assert(nextVal != nil)
|
||||
result.add(nextVal)
|
||||
|
||||
lastIdx = bounds.b + 1
|
||||
|
|
|
|||
|
|
@ -2412,8 +2412,9 @@ proc `==` *[T](x, y: seq[T]): bool {.noSideEffect.} =
|
|||
if seqToPtr(x) == seqToPtr(y):
|
||||
return true
|
||||
|
||||
if x.isNil or y.isNil:
|
||||
return false
|
||||
when not defined(nimNoNil):
|
||||
if x.isNil or y.isNil:
|
||||
return false
|
||||
|
||||
if x.len != y.len:
|
||||
return false
|
||||
|
|
@ -4010,18 +4011,18 @@ proc addQuoted*[T](s: var string, x: T) =
|
|||
|
||||
when hasAlloc:
|
||||
# XXX: make these the default (or implement the NilObject optimization)
|
||||
proc safeAdd*[T](x: var seq[T], y: T) {.noSideEffect.} =
|
||||
proc safeAdd*[T](x: var seq[T], y: T) {.noSideEffect, deprecated.} =
|
||||
## Adds ``y`` to ``x`` unless ``x`` is not yet initialized; in that case,
|
||||
## ``x`` becomes ``@[y]``
|
||||
if x == nil: x = @[y]
|
||||
else: x.add(y)
|
||||
|
||||
proc safeAdd*(x: var string, y: char) =
|
||||
proc safeAdd*(x: var string, y: char) {.noSideEffect, deprecated.} =
|
||||
## Adds ``y`` to ``x``. If ``x`` is ``nil`` it is initialized to ``""``
|
||||
if x == nil: x = ""
|
||||
x.add(y)
|
||||
|
||||
proc safeAdd*(x: var string, y: string) =
|
||||
proc safeAdd*(x: var string, y: string) {.noSideEffect, deprecated.} =
|
||||
## Adds ``y`` to ``x`` unless ``x`` is not yet initalized; in that
|
||||
## case, ``x`` becomes ``y``
|
||||
if x == nil: x = y
|
||||
|
|
|
|||
|
|
@ -22,21 +22,34 @@ proc resize(old: int): int {.inline.} =
|
|||
|
||||
proc cmpStrings(a, b: NimString): int {.inline, compilerProc.} =
|
||||
if a == b: return 0
|
||||
if a == nil: return -1
|
||||
if b == nil: return 1
|
||||
let minlen = min(a.len, b.len)
|
||||
when defined(nimNoNil):
|
||||
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)
|
||||
if minlen > 0:
|
||||
result = c_memcmp(addr a.data, addr b.data, minlen.csize)
|
||||
if result == 0:
|
||||
result = a.len - b.len
|
||||
result = alen - blen
|
||||
else:
|
||||
result = a.len - b.len
|
||||
result = alen - blen
|
||||
|
||||
proc eqStrings(a, b: NimString): bool {.inline, compilerProc.} =
|
||||
if a == b: return true
|
||||
if a == nil or b == nil: return false
|
||||
return a.len == b.len and
|
||||
equalMem(addr(a.data), addr(b.data), a.len)
|
||||
when defined(nimNoNil):
|
||||
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 == 0: return true
|
||||
return equalMem(addr(a.data), addr(b.data), alen)
|
||||
|
||||
when declared(allocAtomic):
|
||||
template allocStr(size: untyped): untyped =
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue