From c1c02ffefdf41d2ad4fdd2fc9bdfedcd85bc85f2 Mon Sep 17 00:00:00 2001
From: koniifer <koniifer@proton.me>
Date: Sat, 23 Nov 2024 16:51:39 +0000
Subject: [PATCH] silly string

---
 Cargo.lock                          |  50 ++++-----
 sysdata/libraries/stn/src/math.hb   |  10 ++
 sysdata/libraries/stn/src/string.hb | 153 ++++++++++++++++++++++++++++
 3 files changed, 188 insertions(+), 25 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index 2f07974..8dc4baa 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -213,12 +213,12 @@ dependencies = [
 [[package]]
 name = "hbbytecode"
 version = "0.1.0"
-source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#37dd13cab295aa9e74d704b3345685b4428d149a"
+source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#86ca959ea3eae1cb32298e135a444820583d24a0"
 
 [[package]]
 name = "hblang"
 version = "0.1.0"
-source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#37dd13cab295aa9e74d704b3345685b4428d149a"
+source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#86ca959ea3eae1cb32298e135a444820583d24a0"
 dependencies = [
  "hashbrown",
  "hbbytecode",
@@ -229,7 +229,7 @@ dependencies = [
 [[package]]
 name = "hbvm"
 version = "0.1.0"
-source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#37dd13cab295aa9e74d704b3345685b4428d149a"
+source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#86ca959ea3eae1cb32298e135a444820583d24a0"
 dependencies = [
  "hbbytecode",
 ]
@@ -424,9 +424,9 @@ checksum = "02034f8f6b3e7bf050f310fbaf6db0018b8e54b75598d0a4c97172054752fede"
 
 [[package]]
 name = "litemap"
-version = "0.7.3"
+version = "0.7.4"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "643cb0b8d4fcc284004d5fd0d67ccf61dfffadb7f75e1e71bc420f4688a3a704"
+checksum = "4ee93343901ab17bd981295f2cf0026d4ad018c7c31ba84549a4ddbb47a45104"
 
 [[package]]
 name = "lock_api"
@@ -503,9 +503,9 @@ checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e"
 
 [[package]]
 name = "proc-macro2"
-version = "1.0.89"
+version = "1.0.92"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "f139b0662de085916d1fb67d2b4169d1addddda1919e696f3252b740b629986e"
+checksum = "37d3544b3f2748c54e147655edb5025752e2303145b5aefb3c3ea2c78b973bb0"
 dependencies = [
  "unicode-ident",
 ]
@@ -584,9 +584,9 @@ dependencies = [
 
 [[package]]
 name = "rustls"
-version = "0.23.17"
+version = "0.23.18"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "7f1a745511c54ba6d4465e8d5dfbd81b45791756de28d4981af70d6dca128f1e"
+checksum = "9c9cc1d47e243d655ace55ed38201c19ae02c148ae56412ab8750e8f0166ab7f"
 dependencies = [
  "log",
  "once_cell",
@@ -717,9 +717,9 @@ checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292"
 
 [[package]]
 name = "syn"
-version = "2.0.87"
+version = "2.0.89"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "25aa4ce346d03a6dcd68dd8b4010bcb74e54e62c90c573f394c46eae99aba32d"
+checksum = "44d46482f1c1c87acd84dea20c1bf5ebff4c757009ed6bf19cfd36fb10e92c4e"
 dependencies = [
  "proc-macro2",
  "quote",
@@ -800,9 +800,9 @@ dependencies = [
 
 [[package]]
 name = "unicode-ident"
-version = "1.0.13"
+version = "1.0.14"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe"
+checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83"
 
 [[package]]
 name = "unicode-xid"
@@ -833,9 +833,9 @@ dependencies = [
 
 [[package]]
 name = "url"
-version = "2.5.3"
+version = "2.5.4"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "8d157f1b96d14500ffdc1f10ba712e780825526c03d9a49b4d0324b0d9113ada"
+checksum = "32f8b686cadd1473f4bd0117a5d28d36b1ade384ea9b5069a1c40aefed7fda60"
 dependencies = [
  "form_urlencoded",
  "idna",
@@ -876,9 +876,9 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
 
 [[package]]
 name = "webpki-roots"
-version = "0.26.6"
+version = "0.26.7"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "841c67bff177718f1d4dfefde8d8f0e78f9b6589319ba88312f567fc5841a958"
+checksum = "5d642ff16b7e79272ae451b7322067cdc17cadf68c23264be9d94a32319efe7e"
 dependencies = [
  "rustls-pki-types",
 ]
@@ -1035,9 +1035,9 @@ dependencies = [
 
 [[package]]
 name = "yoke"
-version = "0.7.4"
+version = "0.7.5"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "6c5b1314b079b0930c31e3af543d8ee1757b1951ae1e1565ec704403a7240ca5"
+checksum = "120e6aef9aa629e3d4f52dc8cc43a015c7724194c97dfaf45180d2daf2b77f40"
 dependencies = [
  "serde",
  "stable_deref_trait",
@@ -1047,9 +1047,9 @@ dependencies = [
 
 [[package]]
 name = "yoke-derive"
-version = "0.7.4"
+version = "0.7.5"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "28cc31741b18cb6f1d5ff12f5b7523e3d6eb0852bbbad19d73905511d9849b95"
+checksum = "2380878cad4ac9aac1e2435f3eb4020e8374b5f13c296cb75b4620ff8e229154"
 dependencies = [
  "proc-macro2",
  "quote",
@@ -1059,18 +1059,18 @@ dependencies = [
 
 [[package]]
 name = "zerofrom"
-version = "0.1.4"
+version = "0.1.5"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "91ec111ce797d0e0784a1116d0ddcdbea84322cd79e5d5ad173daeba4f93ab55"
+checksum = "cff3ee08c995dee1859d998dea82f7374f2826091dd9cd47def953cae446cd2e"
 dependencies = [
  "zerofrom-derive",
 ]
 
 [[package]]
 name = "zerofrom-derive"
-version = "0.1.4"
+version = "0.1.5"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "0ea7b4a3637ea8669cedf0f1fd5c286a17f3de97b8dd5a70a6c167a1730e63a5"
+checksum = "595eed982f7d355beb85837f651fa22e90b3c044842dc7f2c2842c086f295808"
 dependencies = [
  "proc-macro2",
  "quote",
diff --git a/sysdata/libraries/stn/src/math.hb b/sysdata/libraries/stn/src/math.hb
index d60495c..b868cd8 100644
--- a/sysdata/libraries/stn/src/math.hb
+++ b/sysdata/libraries/stn/src/math.hb
@@ -10,6 +10,16 @@ $max := fn($Expr: type, a: Expr, b: Expr): Expr {
 $sign := fn($Expr: type, x: Expr): i8 {
 	return @bitcast(x > 0) - @bitcast(x < 0)
 }
+$log := fn($Expr: type, base: uint, x: Expr): uint {
+	// if x <= 0 {}
+	// if base <= 1 {}
+	result := 0
+	loop if x < base break else {
+		x /= base
+		result += 1
+	}
+	return result
+}
 
 Vec2 := fn($Expr: type): type {
 	return struct {x: Expr, y: Expr}
diff --git a/sysdata/libraries/stn/src/string.hb b/sysdata/libraries/stn/src/string.hb
index e617498..cb78cd4 100644
--- a/sysdata/libraries/stn/src/string.hb
+++ b/sysdata/libraries/stn/src/string.hb
@@ -60,4 +60,157 @@ equals := fn(lhs: ^u8, rhs: ^u8): bool {
 		lhs += 1
 		rhs += 1
 	}
+}
+
+clear := fn(ptr: ^u8): void {
+	loop if *ptr == 0 break else {
+		*ptr = 0
+		ptr += 1
+	}
+}
+
+split_once := fn(haystack: ^u8, needle: u8): ?^u8 {
+	loop if *haystack == needle return haystack else if *haystack == 0 return null else haystack += 1
+}
+
+split_once_str := fn(haystack: ^u8, needle: ^u8): ?^u8 {
+	if *needle == 0 return null
+
+	loop if *haystack == 0 return null else {
+		if *haystack == *needle {
+			h := haystack
+			n := needle
+
+			loop {
+				n += 1
+				h += 1
+				if *n == 0 {
+					return haystack
+				} else if *h == 0 | *h != *n {
+					break
+				}
+			}
+		}
+		haystack += 1
+	}
+}
+
+Split := struct {
+	str: ^u8,
+	needle: u8,
+	done: bool,
+}
+
+split := fn(iter: ^u8, needle: u8): Split {
+	return .(
+		iter,
+		needle,
+		false,
+	)
+}
+
+iter_split := fn(iter: ^Split): ?^u8 {
+	if iter.done | *iter.str == 0 {
+		return null
+	}
+
+	next := split_once(iter.str + 1, iter.needle)
+
+	if next == null {
+		iter.done = true
+		return iter.str
+	}
+	s := iter.str
+	iter.str = next + 1
+
+	return s
+}
+
+SplitStr := struct {
+	str: ^u8,
+	needle: ^u8,
+	done: bool,
+}
+
+split_str := fn(iter: ^u8, needle: ^u8): SplitStr {
+	return .(
+		iter,
+		needle,
+		false,
+	)
+}
+
+iter_split_str := fn(iter: ^SplitStr): ?^u8 {
+	if iter.done | *iter.str == 0 {
+		return null
+	}
+
+	next := split_once_str(iter.str, iter.needle)
+
+	if next == null {
+		iter.done = true
+		return iter.str
+	}
+
+	s := iter.str
+
+	iter.str = next + length(iter.needle)
+
+	return s
+}
+
+find_once := fn(haystack: ^u8, needle: u8): ?uint {
+	return @bitcast(@inline(split_once, haystack, needle) - haystack)
+}
+
+find_once_str := fn(haystack: ^u8, needle: ^u8): ?uint {
+	return @bitcast(@inline(split_once_str, haystack, needle) - haystack)
+}
+
+count := fn(haystack: ^u8, needle: ^u8): uint {
+	c := 0
+	loop if *haystack == needle {
+		c += 1
+		haystack += 1
+	} else if *haystack == 0 return c else haystack += 1
+}
+
+count_str := fn(haystack: ^u8, needle: ^u8): uint {
+	if *needle == 0 return 0
+	c := 0
+	loop if *haystack == 0 return c else {
+		if *haystack == *needle {
+			h := haystack
+			n := needle
+
+			loop {
+				n += 1
+				h += 1
+				if *n == 0 {
+					c += 1
+					break
+				} else if *h == 0 | *h != *n {
+					break
+				}
+			}
+		}
+		haystack += 1
+	}
+}
+
+left_trim := fn(str: ^u8, sub: ^u8): ^u8 {
+	original := str
+	if *str == *sub {
+		loop if *sub == 0 {
+			return str
+		} else if *str != *sub {
+			return original
+		} else if *str == 0 {
+			return original
+		} else {
+			str += 1
+			sub += 1
+		}
+	}
+	return str
 }
\ No newline at end of file