diff --git a/changelog.md b/changelog.md index dc225844b8..e12265d169 100644 --- a/changelog.md +++ b/changelog.md @@ -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:" diff --git a/compiler/nim.cfg b/compiler/nim.cfg index 21faf37836..0cc8c476ec 100644 --- a/compiler/nim.cfg +++ b/compiler/nim.cfg @@ -11,6 +11,7 @@ define:nimPreviewRangeDefault define:nimPreviewNonVarDestructor define:nimPreviewCheckedClose define:nimPreviewAsmSemSymbol +define:nimPreviewCStringComparisons threads:off #import:"$projectpath/testability" diff --git a/lib/system.nim b/lib/system.nim index a77b59e74e..128759ecf8 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -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 diff --git a/tests/config.nims b/tests/config.nims index 71825774c8..f19ff92220 100644 --- a/tests/config.nims +++ b/tests/config.nims @@ -46,3 +46,5 @@ when not defined(testsConciseTypeMismatch): switch("experimental", "vtables") switch("experimental", "openSym") switch("experimental", "typeBoundOps") + +switch("define", "nimPreviewCStringComparisons")