aboutsummaryrefslogtreecommitdiff
path: root/core/bytes
diff options
context:
space:
mode:
authorYawning Angel <yawning@schwanenlied.me>2024-05-12 20:58:51 +0900
committerYawning Angel <yawning@schwanenlied.me>2024-07-16 01:29:43 +0900
commitdcaf085bfa68a355eb937469ae245ee41aeeeac5 (patch)
treefc8dae7fb6bc0d1a6d7d69f122832b266ca6b9f0 /core/bytes
parent1a30d47ee8d6e80d8cd76afc24595cd6e6f62d4c (diff)
core/bytes: Add `alias` and `alias_inexactly`
Diffstat (limited to 'core/bytes')
-rw-r--r--core/bytes/bytes.odin25
1 files changed, 25 insertions, 0 deletions
diff --git a/core/bytes/bytes.odin b/core/bytes/bytes.odin
index 208949fd8..7cbf092ac 100644
--- a/core/bytes/bytes.odin
+++ b/core/bytes/bytes.odin
@@ -1167,3 +1167,28 @@ fields_proc :: proc(s: []byte, f: proc(rune) -> bool, allocator := context.alloc
return subslices[:]
}
+
+// alias returns true iff a and b have a non-zero length, and any part of
+// a overlaps with b.
+alias :: proc "contextless" (a, b: []byte) -> bool {
+ a_len, b_len := len(a), len(b)
+ if a_len == 0 || b_len == 0 {
+ return false
+ }
+
+ a_start, b_start := uintptr(raw_data(a)), uintptr(raw_data(b))
+ a_end, b_end := a_start + uintptr(a_len-1), b_start + uintptr(b_len-1)
+
+ return a_start <= b_end && b_start <= a_end
+}
+
+// alias_inexactly returns true iff a and b have a non-zero length,
+// the base pointer of a and b are NOT equal, and any part of a overlaps
+// with b (ie: `alias(a, b)` with an exception that returns false for
+// `a == b`, `b = a[:len(a)-69]` and similar conditions).
+alias_inexactly :: proc "contextless" (a, b: []byte) -> bool {
+ if raw_data(a) == raw_data(b) {
+ return false
+ }
+ return alias(a, b)
+}