
louise
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#1199 · main · 10 min 33 s
1Étoiles
0Forks
0Issues
| 1 | // Bytes (std/bytes), its builder, and linear string joins. Large Bytes |
| 2 | // sent to another actor while the sender keeps them are shared by the |
| 3 | // native runtime, not copied (invisible to the program). |
| 4 | |
| 5 | import std/bytes.{type Builder} |
| 6 | import std/io |
| 7 | import std/list |
| 8 | import std/process.{type Subject, type proc} |
| 9 | import std/string |
| 10 | |
| 11 | fn checksum :: b: Bytes -> Int |
| 12 | { |
| 13 | list.fold (bytes.to_list b) 0 fn acc x { (acc * 31 + x) % 1000003 } |
| 14 | } |
| 15 | |
| 16 | /// `n` copies of `piece`, through a builder. |
| 17 | fn repeated :: piece: Bytes n: Int acc: Builder -> Bytes |
| 18 | { |
| 19 | if n == 0 |
| 20 | { |
| 21 | bytes.build acc |
| 22 | } |
| 23 | else |
| 24 | { |
| 25 | repeated piece (n - 1) (bytes.append acc piece) |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | fn summarize :: |
| 30 | inbox: Subject<(Bytes, Subject<(Int, Int)>)> |
| 31 | left: Int |
| 32 | -> () / <proc> |
| 33 | { |
| 34 | if left > 0 |
| 35 | { |
| 36 | let (data, reply) = process.receive_forever inbox |
| 37 | process.send reply (bytes.length data, checksum data) |
| 38 | summarize inbox (left - 1) |
| 39 | } |
| 40 | else |
| 41 | { |
| 42 | () |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | fn show_pair :: p: (Int, Int) -> String |
| 47 | { |
| 48 | show p.0 <> " bytes, checksum " <> show p.1 |
| 49 | } |
| 50 | |
| 51 | fn show_result :: r: Result<String, String> -> String |
| 52 | { |
| 53 | match r { Ok(s) -> "Ok(" <> s <> ")", Error(e) -> "Error(" <> e <> ")" } |
| 54 | } |
| 55 | |
| 56 | pub fn main :: -> () / <io, proc> |
| 57 | { |
| 58 | basics () |
| 59 | comparisons () |
| 60 | builders_and_joins () |
| 61 | cursor_reads () |
| 62 | sending () |
| 63 | } |
| 64 | |
| 65 | fn basics :: -> () / <io> |
| 66 | { |
| 67 | let hello = bytes.from_string "héllo" |
| 68 | io.println |
| 69 | ( show (bytes.length hello) <> " " <> show (bytes.get hello 1) <> " " |
| 70 | <> show (bytes.get hello 6) |
| 71 | <> " " |
| 72 | <> show (bytes.get hello (-1)) |
| 73 | ) |
| 74 | io.println |
| 75 | (debug hello <> " " <> debug (bytes.from_list [0, 9, 10, 34, 92, 127, 255])) |
| 76 | io.println |
| 77 | ( show_result (bytes.to_string hello) <> " " |
| 78 | <> show_result (bytes.to_string (bytes.slice hello 0 2)) |
| 79 | ) |
| 80 | io.println (bytes.to_string_lossy (bytes.from_list [104, 255, 105])) |
| 81 | io.println |
| 82 | ( debug (bytes.slice hello 3 100) <> " " <> debug (bytes.slice hello (-5) 1) |
| 83 | <> " " |
| 84 | <> debug (bytes.slice hello 4 2) |
| 85 | ) |
| 86 | io.println |
| 87 | ( show |
| 88 | ( bytes.to_list |
| 89 | (bytes.concat (bytes.from_list [1, 2]) (bytes.from_list [3])) |
| 90 | ) |
| 91 | ) |
| 92 | io.println |
| 93 | ( debug |
| 94 | ( bytes.concat_all |
| 95 | [bytes.from_string "a", bytes.empty (), bytes.from_string "bc"] |
| 96 | ) |
| 97 | ) |
| 98 | } |
| 99 | |
| 100 | // Equality, order and hashes agree with the contents. |
| 101 | fn comparisons :: -> () / <io> |
| 102 | { |
| 103 | let abc = bytes.from_string "abc" |
| 104 | io.println |
| 105 | ( show (abc == bytes.from_list [97, 98, 99]) <> " " |
| 106 | <> show (abc == bytes.from_string "abd") |
| 107 | <> " " |
| 108 | <> show (hash abc == hash (bytes.from_string "abc")) |
| 109 | ) |
| 110 | io.println |
| 111 | ( show (abc < bytes.from_string "abd") <> " " |
| 112 | <> show (abc > bytes.from_string "ab") |
| 113 | <> " " |
| 114 | <> show (abc < bytes.from_list [98]) |
| 115 | ) |
| 116 | } |
| 117 | |
| 118 | // A builder is a value: appending to it twice gives two builders. |
| 119 | fn builders_and_joins :: -> () / <io> |
| 120 | { |
| 121 | let start = bytes.append_string (bytes.builder ()) "key=" |
| 122 | let one = bytes.append_byte (bytes.append_string start "one") 33 |
| 123 | let two = bytes.append start (bytes.from_string "two") |
| 124 | io.println |
| 125 | ( debug (bytes.build one) <> " " <> debug (bytes.build two) <> " " |
| 126 | <> debug (bytes.build (bytes.builder ())) |
| 127 | ) |
| 128 | |
| 129 | io.println |
| 130 | ( string.join ["a", "b", "", "c"] ", " <> "|" <> string.join [] "-" <> "|" |
| 131 | <> string.concat ["x", "y", "z"] |
| 132 | ) |
| 133 | let many = string.join (list.map (list.range 1 20000) fn i { show i }) "," |
| 134 | io.println (show (string.byte_length many)) |
| 135 | } |
| 136 | |
| 137 | // Big-endian cursor reads in place (-1 out of range), and string ranges |
| 138 | // without the intermediate slice. |
| 139 | fn cursor_reads :: -> () / <io> |
| 140 | { |
| 141 | let b = bytes.from_list [18, 52, 86, 120, 154, 188, 222, 240, 255] |
| 142 | io.println |
| 143 | ( show (bytes.read_u8 b 0) <> " " <> show (bytes.read_u8 b 8) <> " " |
| 144 | <> show (bytes.read_u8 b 9) |
| 145 | <> " " |
| 146 | <> show (bytes.read_u8 b (-1)) |
| 147 | ) |
| 148 | io.println |
| 149 | ( show (bytes.read_u16be b 0) <> " " <> show (bytes.read_u16be b 7) <> " " |
| 150 | <> show (bytes.read_u16be b 8) |
| 151 | ) |
| 152 | io.println (show (bytes.read_u32be b 1) <> " " <> show (bytes.read_u32be b 6)) |
| 153 | io.println (show (bytes.read_u64be b 0) <> " " <> show (bytes.read_u64be b 2)) |
| 154 | // Above 2^62 the read is a big integer. |
| 155 | io.println |
| 156 | ( show |
| 157 | ( bytes.read_u64be |
| 158 | (bytes.from_list [255, 255, 255, 255, 255, 255, 255, 255]) |
| 159 | 0 |
| 160 | ) |
| 161 | ) |
| 162 | let hello = bytes.from_string "héllo" |
| 163 | io.println |
| 164 | ( show_result (bytes.to_string_range hello 0 6) <> " " |
| 165 | <> show_result (bytes.to_string_range hello 3 2) |
| 166 | <> " " |
| 167 | <> show_result (bytes.to_string_range hello 3 100) |
| 168 | ) |
| 169 | io.println |
| 170 | ( show_result (bytes.to_string_range hello 0 2) <> " " |
| 171 | <> show_result (bytes.to_string_range hello 1 1) |
| 172 | <> " " |
| 173 | <> show_result (bytes.to_string_range hello (-2) 3) |
| 174 | <> " " |
| 175 | <> show_result (bytes.to_string_range hello 4 (-9)) |
| 176 | ) |
| 177 | } |
| 178 | |
| 179 | // Sent to another actor: large and kept (shared), large and not kept |
| 180 | // (moved), small and kept (copied). |
| 181 | fn sending :: -> () / <io, proc> |
| 182 | { |
| 183 | let inbox = process.subject () |
| 184 | let reply = process.subject () |
| 185 | let _ = |
| 186 | process.spawn |
| 187 | fn |
| 188 | { |
| 189 | let mine = process.subject () |
| 190 | process.send inbox mine |
| 191 | summarize mine 3 |
| 192 | } |
| 193 | let worker = process.receive_forever inbox |
| 194 | let large = |
| 195 | repeated (bytes.from_list (list.range 0 255)) 64 (bytes.builder ()) |
| 196 | process.send worker (large, reply) |
| 197 | io.println |
| 198 | ( "large " <> show_pair (process.receive_forever reply) <> " " |
| 199 | <> show (checksum large) |
| 200 | ) |
| 201 | process.send |
| 202 | worker |
| 203 | (repeated (bytes.from_string "abc") 5000 (bytes.builder ()), reply) |
| 204 | io.println ("moved " <> show_pair (process.receive_forever reply)) |
| 205 | let abc = bytes.from_string "abc" |
| 206 | process.send worker (abc, reply) |
| 207 | io.println |
| 208 | ( "small " <> show_pair (process.receive_forever reply) <> " " |
| 209 | <> show (checksum abc) |
| 210 | ) |
| 211 | } |
| 212 | //> 6 Some(195) None None |
| 213 | //> b"h\xc3\xa9llo" b"\x00\t\n\"\\\x7f\xff" |
| 214 | //> Ok(héllo) Error(invalid UTF-8 at byte 1) |
| 215 | //> h�i |
| 216 | //> b"llo" b"h" b"" |
| 217 | //> [1, 2, 3] |
| 218 | //> b"abc" |
| 219 | //> True False True |
| 220 | //> True True True |
| 221 | //> b"key=one!" b"key=two" b"" |
| 222 | //> a, b, , c||xyz |
| 223 | //> 108893 |
| 224 | //> 18 255 -1 -1 |
| 225 | //> 4660 61695 -1 |
| 226 | //> 878082202 -1 |
| 227 | //> 1311768467463790320 -1 |
| 228 | //> 18446744073709551615 |
| 229 | //> Ok(héllo) Ok(ll) Ok(llo) |
| 230 | //> Error(invalid UTF-8 at byte 1) Error(invalid UTF-8 at byte 0) Ok(h) Ok() |
| 231 | //> large 16384 bytes, checksum 531177 531177 |
| 232 | //> moved 15000 bytes, checksum 567322 |
| 233 | //> small 3 bytes, checksum 96354 96354 |