Tree-native byte processing

This commit is contained in:
2026-05-06 18:53:17 -05:00
parent 1c4c49e68d
commit 89bb73ed99
3 changed files with 256 additions and 0 deletions

49
lib/bytes.tri Normal file
View File

@@ -0,0 +1,49 @@
!import "base.tri" !Local
!import "list.tri" !Local
nothing = t
just = x : t x
bytesIsNil = emptyList?
bytesHead = matchList nothing (h _ : just h)
bytesTail = matchList nothing (_ r : just r)
byteEq = equal?
bytesLength = length
bytesAppend = append
bytesTake_ = y (self n i remaining :
matchBool
t
(matchList
t
(h r : pair h (self n (succ i) r))
remaining)
(equal? i n))
bytesTake = n bytes : bytesTake_ n 0 bytes
bytesDrop_ = y (self n i remaining :
matchBool
remaining
(matchList
t
(_ r : self n (succ i) r)
remaining)
(equal? i n))
bytesDrop = n bytes : bytesDrop_ n 0 bytes
bytesSplitAt = n bytes : pair (bytesTake n bytes) (bytesDrop n bytes)
bytesEq = y (self xs ys :
matchList
(matchList true (_ _ : false) ys)
(xh xt :
matchList
false
(yh yt : and? (byteEq xh yh) (self xt yt))
ys)
xs)