diff options
| author | gingerBill <bill@gingerbill.org> | 2023-09-26 12:21:43 +0100 |
|---|---|---|
| committer | gingerBill <bill@gingerbill.org> | 2023-09-26 12:21:43 +0100 |
| commit | c08bf1204f49207454edb82f0328d42d64a6bc05 (patch) | |
| tree | 98c1d066b09fea7415c7743b9a482018665c1717 /core/runtime | |
| parent | ecde06e3a31179bd8f86383fd65cfbce31ab6d9a (diff) | |
Add `cstring` specific comparison procedures to fix comparisons like `cstring("") != cstring(nil)`
Diffstat (limited to 'core/runtime')
| -rw-r--r-- | core/runtime/internal.odin | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/core/runtime/internal.odin b/core/runtime/internal.odin index 4d166bef0..5e4805153 100644 --- a/core/runtime/internal.odin +++ b/core/runtime/internal.odin @@ -410,6 +410,48 @@ cstring_to_string :: proc "contextless" (s: cstring) -> string { } +cstring_eq :: proc "contextless" (lhs, rhs: cstring) -> bool { + x := ([^]byte)(lhs) + y := ([^]byte)(rhs) + if x == y { + return true + } + if (x == nil) ~ (y == nil) { + return false + } + xn := cstring_len(lhs) + yn := cstring_len(rhs) + if xn != yn { + return false + } + return #force_inline memory_equal(x, y, xn) +} + +cstring_cmp :: proc "contextless" (lhs, rhs: cstring) -> int { + x := ([^]byte)(lhs) + y := ([^]byte)(rhs) + if x == y { + return 0 + } + if (x == nil) ~ (y == nil) { + return -1 if x == nil else +1 + } + xn := cstring_len(lhs) + yn := cstring_len(rhs) + ret := memory_compare(x, y, min(xn, yn)) + if ret == 0 && xn != yn { + return -1 if xn < yn else +1 + } + return ret +} + +cstring_ne :: #force_inline proc "contextless" (a, b: cstring) -> bool { return !cstring_eq(a, b) } +cstring_lt :: #force_inline proc "contextless" (a, b: cstring) -> bool { return cstring_cmp(a, b) < 0 } +cstring_gt :: #force_inline proc "contextless" (a, b: cstring) -> bool { return cstring_cmp(a, b) > 0 } +cstring_le :: #force_inline proc "contextless" (a, b: cstring) -> bool { return cstring_cmp(a, b) <= 0 } +cstring_ge :: #force_inline proc "contextless" (a, b: cstring) -> bool { return cstring_cmp(a, b) >= 0 } + + complex32_eq :: #force_inline proc "contextless" (a, b: complex32) -> bool { return real(a) == real(b) && imag(a) == imag(b) } complex32_ne :: #force_inline proc "contextless" (a, b: complex32) -> bool { return real(a) != real(b) || imag(a) != imag(b) } |