use Error panic use Str Str use Iter to SmolStr SmolStr Size fn max-sz (): (Mem.get-typesize() as Mem.TypeSize Size).size - 1 fn from-byte-array(chs Array n U8) -> SmolStr max-sz = max-sz() if n Mem.i32-size() > max-sz panic('too big for smol str. given \(n), but gotten \(max-sz)') backing = 0 Cnile.memcpy(Mem.cast-ptr(&backing), Mem.cast-ptr(&chs), n Mem.i32-size()) return SmolStr(backing) fn from-const-str(c ConstStr) -> SmolStr max-sz = max-sz() backing = 0 # zero backing out! strlen = Cnile.strlen(c) Mem.i32-size() if strlen >= max-sz panic('(SmolStr.from-const-str) const str too large yo') Cnile.memcpy(Mem.cast-ptr(&backing), Mem.cast(c), strlen) return SmolStr(backing) fn from-bytes(it it) -> SmolStr <= IntoIter it maxsz = max-sz() Error.assert(max-sz() == 7, '(SmolStr.from-iter) current backing size is assumed to be 7, but got \(maxsz)') return (Array.fill-iter(it, Char.nullchar() Char.char-u8()) as Array 7 U8) from-byte-array() fn from-chars (it it) -> SmolStr <= IntoIter it buf =& Array.fill-default(Char.nullchar() Char.char-u8()) as Array 7 U8 # TODO: use BoundedList i = 0 for c in it if i > 7 panic('(SmolStr.from-chars) string too long') s = c Char.as-u8-slice(&@undefined) for ii in 0 to (Mem.size-i32(s.count) - 1) ii = Mem.i32-size(ii) buf Array.set(i + ii, s[ii]) i <= i + s.count return buf& from-byte-array() inst Str SmolStr print-str (self _) cs = Mem.cast(&self) as ConstStr Str.print-str(cs) chars (self _) cs = Mem.cast(&self) as ConstStr return cs Str.chars() inst Eq SmolStr eq (SmolStr(l), SmolStr(r)) -> Bool: l == r inst FromString SmolStr from-string (ptr Ptr U8, num-bytes Size) -> _ if num-bytes > max-sz() panic('(SmolStr.from-string) String too long for SmolStr') slice = Slice { ptr, count: num-bytes } return from-bytes(slice)