commit 860eff72eb9039802ad792a9d9607fb6c131fcfe
parent f297519576ce33f2f78b98e559fc1f69d7a661c1
Author: Drew DeVault <sir@cmpwn.com>
Date: Sat, 30 Jan 2021 18:48:54 -0500
bytes: add equal, index
Diffstat:
2 files changed, 21 insertions(+), 0 deletions(-)
diff --git a/bytes/equal.ha b/bytes/equal.ha
@@ -0,0 +1,12 @@
+// Returns true if the two byte sequences are identical.
+export fn equal(a: []u8, b: []u8) bool = {
+ if (len(a) != len(b)) {
+ return false;
+ };
+ for (let i = 0z; i < len(a); i += 1z) {
+ if (a[i] != b[i]) {
+ return false;
+ };
+ };
+ return true;
+};
diff --git a/bytes/index.ha b/bytes/index.ha
@@ -0,0 +1,9 @@
+// Returns the offset of the first instance of 'needle' in a 'haystack' of
+// bytes, or void if it is not found.
+export fn index(haystack: []u8, needle: u8) (size | void) = {
+ for (let i = 0z; i < len(haystack); i += 1z) {
+ if (haystack[i] == needle) {
+ return i;
+ };
+ };
+};