// Bytes (std/bytes), its builder, and linear string joins. Large Bytes // sent to another actor while the sender keeps them are shared by the // native runtime, not copied (invisible to the program). import std/bytes.{type Builder} import std/io import std/list import std/process.{type Subject, type proc} import std/string fn checksum :: b: Bytes -> Int { list.fold (bytes.to_list b) 0 fn acc x { (acc * 31 + x) % 1000003 } } /// `n` copies of `piece`, through a builder. fn repeated :: piece: Bytes n: Int acc: Builder -> Bytes { if n == 0 { bytes.build acc } else { repeated piece (n - 1) (bytes.append acc piece) } } fn summarize :: inbox: Subject<(Bytes, Subject<(Int, Int)>)> left: Int -> () / { if left > 0 { let (data, reply) = process.receive_forever inbox process.send reply (bytes.length data, checksum data) summarize inbox (left - 1) } else { () } } fn show_pair :: p: (Int, Int) -> String { show p.0 <> " bytes, checksum " <> show p.1 } fn show_result :: r: Result -> String { match r { Ok(s) -> "Ok(" <> s <> ")", Error(e) -> "Error(" <> e <> ")" } } pub fn main :: -> () / { basics () comparisons () builders_and_joins () cursor_reads () sending () } fn basics :: -> () / { let hello = bytes.from_string "héllo" io.println ( show (bytes.length hello) <> " " <> show (bytes.get hello 1) <> " " <> show (bytes.get hello 6) <> " " <> show (bytes.get hello (-1)) ) io.println (debug hello <> " " <> debug (bytes.from_list [0, 9, 10, 34, 92, 127, 255])) io.println ( show_result (bytes.to_string hello) <> " " <> show_result (bytes.to_string (bytes.slice hello 0 2)) ) io.println (bytes.to_string_lossy (bytes.from_list [104, 255, 105])) io.println ( debug (bytes.slice hello 3 100) <> " " <> debug (bytes.slice hello (-5) 1) <> " " <> debug (bytes.slice hello 4 2) ) io.println ( show ( bytes.to_list (bytes.concat (bytes.from_list [1, 2]) (bytes.from_list [3])) ) ) io.println ( debug ( bytes.concat_all [bytes.from_string "a", bytes.empty (), bytes.from_string "bc"] ) ) } // Equality, order and hashes agree with the contents. fn comparisons :: -> () / { let abc = bytes.from_string "abc" io.println ( show (abc == bytes.from_list [97, 98, 99]) <> " " <> show (abc == bytes.from_string "abd") <> " " <> show (hash abc == hash (bytes.from_string "abc")) ) io.println ( show (abc < bytes.from_string "abd") <> " " <> show (abc > bytes.from_string "ab") <> " " <> show (abc < bytes.from_list [98]) ) } // A builder is a value: appending to it twice gives two builders. fn builders_and_joins :: -> () / { let start = bytes.append_string (bytes.builder ()) "key=" let one = bytes.append_byte (bytes.append_string start "one") 33 let two = bytes.append start (bytes.from_string "two") io.println ( debug (bytes.build one) <> " " <> debug (bytes.build two) <> " " <> debug (bytes.build (bytes.builder ())) ) io.println ( string.join ["a", "b", "", "c"] ", " <> "|" <> string.join [] "-" <> "|" <> string.concat ["x", "y", "z"] ) let many = string.join (list.map (list.range 1 20000) fn i { show i }) "," io.println (show (string.byte_length many)) } // Big-endian cursor reads in place (-1 out of range), and string ranges // without the intermediate slice. fn cursor_reads :: -> () / { let b = bytes.from_list [18, 52, 86, 120, 154, 188, 222, 240, 255] io.println ( show (bytes.read_u8 b 0) <> " " <> show (bytes.read_u8 b 8) <> " " <> show (bytes.read_u8 b 9) <> " " <> show (bytes.read_u8 b (-1)) ) io.println ( show (bytes.read_u16be b 0) <> " " <> show (bytes.read_u16be b 7) <> " " <> show (bytes.read_u16be b 8) ) io.println (show (bytes.read_u32be b 1) <> " " <> show (bytes.read_u32be b 6)) io.println (show (bytes.read_u64be b 0) <> " " <> show (bytes.read_u64be b 2)) // Above 2^62 the read is a big integer. io.println ( show ( bytes.read_u64be (bytes.from_list [255, 255, 255, 255, 255, 255, 255, 255]) 0 ) ) let hello = bytes.from_string "héllo" io.println ( show_result (bytes.to_string_range hello 0 6) <> " " <> show_result (bytes.to_string_range hello 3 2) <> " " <> show_result (bytes.to_string_range hello 3 100) ) io.println ( show_result (bytes.to_string_range hello 0 2) <> " " <> show_result (bytes.to_string_range hello 1 1) <> " " <> show_result (bytes.to_string_range hello (-2) 3) <> " " <> show_result (bytes.to_string_range hello 4 (-9)) ) } // Sent to another actor: large and kept (shared), large and not kept // (moved), small and kept (copied). fn sending :: -> () / { let inbox = process.subject () let reply = process.subject () let _ = process.spawn fn { let mine = process.subject () process.send inbox mine summarize mine 3 } let worker = process.receive_forever inbox let large = repeated (bytes.from_list (list.range 0 255)) 64 (bytes.builder ()) process.send worker (large, reply) io.println ( "large " <> show_pair (process.receive_forever reply) <> " " <> show (checksum large) ) process.send worker (repeated (bytes.from_string "abc") 5000 (bytes.builder ()), reply) io.println ("moved " <> show_pair (process.receive_forever reply)) let abc = bytes.from_string "abc" process.send worker (abc, reply) io.println ( "small " <> show_pair (process.receive_forever reply) <> " " <> show (checksum abc) ) } //> 6 Some(195) None None //> b"h\xc3\xa9llo" b"\x00\t\n\"\\\x7f\xff" //> Ok(héllo) Error(invalid UTF-8 at byte 1) //> h�i //> b"llo" b"h" b"" //> [1, 2, 3] //> b"abc" //> True False True //> True True True //> b"key=one!" b"key=two" b"" //> a, b, , c||xyz //> 108893 //> 18 255 -1 -1 //> 4660 61695 -1 //> 878082202 -1 //> 1311768467463790320 -1 //> 18446744073709551615 //> Ok(héllo) Ok(ll) Ok(llo) //> Error(invalid UTF-8 at byte 1) Error(invalid UTF-8 at byte 0) Ok(h) Ok() //> large 16384 bytes, checksum 531177 531177 //> moved 15000 bytes, checksum 567322 //> small 3 bytes, checksum 96354 96354