plum
git clone https://git.pyrossh.dev/plum
A statically typed, imperative programming language inspired by rust, python
ce93ac0
— Peter John
2026-11-10T22:01:10+05:30
Str: a byte that cannot start a character counts as one
- std/Str.plum +23 -22
std/Str.plum
CHANGED
|
@@ -36,11 +36,10 @@ struct Str(data: Array<Byte>, start: Int, len: Int) =
|
|
|
36
36
|
fun clone(self) -> Str =
|
|
37
37
|
fromBytes(self.data, self.start, self.len)
|
|
38
38
|
|
|
39
|
-
# FNV-1a over the raw UTF-8 bytes —
|
|
39
|
+
# FNV-1a over the raw UTF-8 bytes — what `Hamt` (std/collections/Hamt) buckets `Str`
|
|
40
|
-
# `
|
|
40
|
+
# keys by (`Map` orders keys with `compare` and needs no hash). Not cryptographic, just
|
|
41
|
-
# decently-distributed general-purpose hash; wraps silently on overflow
|
|
41
|
+
# a well-known, decently-distributed general-purpose hash; wraps silently on overflow
|
|
42
|
-
# (ordinary 64-bit multiplication), which is fine — only the bit pattern
|
|
42
|
+
# (ordinary 64-bit multiplication), which is fine — only the bit pattern matters.
|
|
43
|
-
# matters, not the numeric value itself.
|
|
44
43
|
fun hash(self) -> Int =
|
|
45
44
|
# FNV offset basis 0xcbf29ce484222325 as a signed Int
|
|
46
45
|
hashFrom(self, 0, -0x340d631b7bdddcdb)
|
|
@@ -51,33 +50,25 @@ struct Str(data: Array<Byte>, start: Int, len: Int) =
|
|
|
51
50
|
else
|
|
52
51
|
hashFrom(s, i + 1, {h ^ s.byteAtUnchecked(i)} * 0x100000001b3)
|
|
53
52
|
|
|
54
|
-
# The
|
|
53
|
+
# The one-byte substring at byte index `i` (crashes out of range, as `byteAt` does). For
|
|
55
|
-
# `Char` type, but `Char` was never actually defined anywhere in `libs/std` —
|
|
56
|
-
# returning a length-1 `Str` instead makes it immediately usable with every
|
|
57
|
-
#
|
|
54
|
+
# a whole character use `runeAt`.
|
|
58
55
|
fun get(self, i: Int) -> Str =
|
|
59
56
|
byteToStr(self.byteAt(i))
|
|
60
57
|
|
|
61
58
|
# `Str` is a byte array (UTF-8 encoded), NOT a sequence of Unicode
|
|
62
59
|
# characters — `length`/`byteAt`/`get`/`sub`/`slice` all operate on raw
|
|
63
60
|
# bytes, so indexing into (or slicing) a string containing non-ASCII text
|
|
64
|
-
# with them can land in the MIDDLE of a multi-byte character
|
|
61
|
+
# with them can land in the MIDDLE of a multi-byte character, as in Go and
|
|
65
|
-
# same design real languages with an efficient string type use (Go, Rust:
|
|
66
|
-
#
|
|
62
|
+
# Rust. The methods below are the character-aware layer on top:
|
|
67
|
-
# a `Slice` of codepoints/bytes would need a heap-allocated node PER
|
|
68
|
-
# CHARACTER with only sequential access, several orders of magnitude
|
|
69
|
-
# slower for something as basic as string length, and `Slice` is currently
|
|
70
|
-
# broken for any generic-with-args field regardless (see
|
|
71
|
-
# `plum_generic_field_type_gap`). These three methods are that
|
|
72
|
-
# "codepoint-aware iteration built on top" layer:
|
|
73
63
|
#
|
|
74
64
|
# How many bytes the UTF-8 character STARTING at byte index `i` occupies
|
|
75
|
-
# (1-4), decoded from its leading byte's high bits.
|
|
65
|
+
# (1-4), decoded from its leading byte's high bits. A byte that cannot start
|
|
76
|
-
#
|
|
66
|
+
# a character (a stray continuation byte `10xxxxxx`, or 0xF8 and up) counts
|
|
77
|
-
#
|
|
67
|
+
# as one, so a walk over text that isn't valid UTF-8 still visits every byte
|
|
68
|
+
# once instead of swallowing the next three.
|
|
78
69
|
fun codePointByteLength(self, i: Int) -> Int =
|
|
79
70
|
b := self.byteAt(i)
|
|
80
|
-
n := if b <
|
|
71
|
+
n := if b < 0xC0 || b >= 0xF8
|
|
81
72
|
1
|
|
82
73
|
else if b & 0xE0 == 0xC0
|
|
83
74
|
2
|
|
@@ -947,3 +938,13 @@ test "compare and < go byte by byte, a prefix first"
|
|
|
947
938
|
assert "a" < "ab" && "ab" < "b" && "" < "a" && !{"b" < "b"} && "b" <= "b" && "c" > "b" && "c" >= "c"
|
|
948
939
|
assert "Z" < "a" && "é" > "z"
|
|
949
940
|
assert "xab".sub(1, 3).compare("ab") == 0
|
|
941
|
+
|
|
942
|
+
test "a byte that cannot start a character counts as one: runeLength, runeAt and reverse stay byte-true"
|
|
943
|
+
stray := "a{Str.byteToStr(0x80)}bcd"
|
|
944
|
+
assert stray.runeLength() == 5 && stray.runeAt(2)! == "b" && stray.codePointByteLength(1) == 1
|
|
945
|
+
assert stray.reverse().length() == 5 && stray.reverse().byteAt(0) == 100 && stray.reverse().byteAt(3) == 0x80
|
|
946
|
+
high := "{Str.byteToStr(0xFF)}xyz"
|
|
947
|
+
assert high.runeLength() == 4 && high.runeAt(1)! == "x"
|
|
948
|
+
cut := "ab{Str.byteToStr(0xE4)}{Str.byteToStr(0xB8)}"
|
|
949
|
+
assert cut.runeLength() == 3 && cut.runeAt(2)!.length() == 2 && cut.runeAt(3).isNone()
|
|
950
|
+
assert "h\u00e9\u4e16".runeLength() == 3 && "h\u00e9\u4e16".runeAt(2)! == "\u4e16"
|