mirror of
https://github.com/nim-lang/Nim.git
synced 2025-12-28 08:54:53 +00:00
adds nimPreviewCStringComparisons for cstring comparisons (#24946)
todo: We can also give a deprecation message for `ltPtr`/`lePtr` matching for cstring in `magicsAfterOverloadResolution` follow up https://github.com/nim-lang/Nim/pull/24942
This commit is contained in:
@@ -21,6 +21,8 @@ errors.
|
||||
|
||||
- The bare `except:` now panics on `Defect`. Use `except Exception:` or `except Defect:` to catch `Defect`. `--legacy:noPanicOnExcept` is provided for a transition period.
|
||||
|
||||
- With `-d:nimPreviewCStringComparisons`, comparsions (`<`, `>`, `<=`, `>=`) between cstrings switch from reference semantics to value semantics like `==` and `!=`.
|
||||
|
||||
## Standard library additions and changes
|
||||
|
||||
[//]: # "Additions:"
|
||||
|
||||
@@ -11,6 +11,7 @@ define:nimPreviewRangeDefault
|
||||
define:nimPreviewNonVarDestructor
|
||||
define:nimPreviewCheckedClose
|
||||
define:nimPreviewAsmSemSymbol
|
||||
define:nimPreviewCStringComparisons
|
||||
threads:off
|
||||
|
||||
#import:"$projectpath/testability"
|
||||
|
||||
@@ -2738,36 +2738,37 @@ func ltCStringVm(x, y: cstring): bool {.inline.} =
|
||||
func leCStringVm(x, y: cstring): bool {.inline.} =
|
||||
discard "implemented in the vm ops"
|
||||
|
||||
func `<`*(x, y: cstring): bool {.inline.} =
|
||||
if x == y:
|
||||
result = false
|
||||
elif x == nil:
|
||||
result = true
|
||||
elif y == nil:
|
||||
result = false
|
||||
else:
|
||||
when nimvm:
|
||||
result = ltCStringVm(x, y)
|
||||
when defined(nimPreviewCStringComparisons):
|
||||
func `<`*(x, y: cstring): bool {.inline.} =
|
||||
if x == y:
|
||||
result = false
|
||||
elif x == nil:
|
||||
result = true
|
||||
elif y == nil:
|
||||
result = false
|
||||
else:
|
||||
when defined(js):
|
||||
result = pointer(x) < pointer(y)
|
||||
when nimvm:
|
||||
result = ltCStringVm(x, y)
|
||||
else:
|
||||
result = strcmp(x, y) < 0
|
||||
when defined(js):
|
||||
result = pointer(x) < pointer(y)
|
||||
else:
|
||||
result = strcmp(x, y) < 0
|
||||
|
||||
func `<=`*(x, y: cstring): bool {.inline.} =
|
||||
if x == y: result = true
|
||||
elif x == nil:
|
||||
result = true
|
||||
elif y == nil:
|
||||
result = false
|
||||
else:
|
||||
when nimvm:
|
||||
result = leCStringVm(x, y)
|
||||
func `<=`*(x, y: cstring): bool {.inline.} =
|
||||
if x == y: result = true
|
||||
elif x == nil:
|
||||
result = true
|
||||
elif y == nil:
|
||||
result = false
|
||||
else:
|
||||
when defined(js):
|
||||
result = pointer(x) <= pointer(y)
|
||||
when nimvm:
|
||||
result = leCStringVm(x, y)
|
||||
else:
|
||||
result = strcmp(x, y) <= 0
|
||||
when defined(js):
|
||||
result = pointer(x) <= pointer(y)
|
||||
else:
|
||||
result = strcmp(x, y) <= 0
|
||||
|
||||
template closureScope*(body: untyped): untyped =
|
||||
## Useful when creating a closure in a loop to capture local loop variables by
|
||||
|
||||
@@ -46,3 +46,5 @@ when not defined(testsConciseTypeMismatch):
|
||||
switch("experimental", "vtables")
|
||||
switch("experimental", "openSym")
|
||||
switch("experimental", "typeBoundOps")
|
||||
|
||||
switch("define", "nimPreviewCStringComparisons")
|
||||
|
||||
Reference in New Issue
Block a user