
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#1197 · main · 10 min 35 s
1Étoiles
0Forks
0Issues
| 1 | /// Immutable binary data (`Bytes`, a prelude type; ADR 0012). |
| 2 | /// |
| 3 | /// let data = bytes.from_string("héllo") |
| 4 | /// bytes.length(data) // 6 |
| 5 | /// bytes.get(data, 0) // Some(104) |
| 6 | /// bytes.to_string(bytes.slice(data, 0, 2)) // Error("invalid UTF-8 at byte 1") |
| 7 | /// |
| 8 | /// Every operation returns new `Bytes` (slices are copies). `==`, `<` and |
| 9 | /// `debug` work on `Bytes`. To build `Bytes` from many pieces, append them |
| 10 | /// to a `Builder`: appending does not copy what was appended before. |
| 11 | |
| 12 | /// The empty byte string. |
| 13 | pub fn empty :: -> Bytes |
| 14 | { |
| 15 | from_list [] |
| 16 | } |
| 17 | |
| 18 | /// The number of bytes. |
| 19 | @extern c "tl_bytes_length" |
| 20 | pub fn length :: b: Bytes -> Int |
| 21 | |
| 22 | /// The byte (0 to 255) at `index`, if it is in range. |
| 23 | pub fn get :: b: Bytes index: Int -> Option<Int> |
| 24 | { |
| 25 | let byte = byte_at b index |
| 26 | if byte < 0 |
| 27 | { |
| 28 | None |
| 29 | } |
| 30 | else |
| 31 | { |
| 32 | Some(byte) |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | @extern c "tl_bytes_at" |
| 37 | fn byte_at :: b: Bytes index: Int -> Int |
| 38 | |
| 39 | /// The byte (0 to 255) at `index`, or -1 when `index` is out of range: |
| 40 | /// `get` without the `Option`, for decoders that check bounds themselves. |
| 41 | pub fn read_u8 :: b: Bytes index: Int -> Int |
| 42 | { |
| 43 | byte_at b index |
| 44 | } |
| 45 | |
| 46 | /// The unsigned big-endian 16-bit integer at `off`, or -1 when |
| 47 | /// `off..off + 2` is out of range. Reading allocates nothing. |
| 48 | @extern c "tl_bytes_read_u16be" |
| 49 | pub fn read_u16be :: b: Bytes off: Int -> Int |
| 50 | |
| 51 | /// The unsigned big-endian 32-bit integer at `off`, or -1 when |
| 52 | /// `off..off + 4` is out of range. Reading allocates nothing. |
| 53 | @extern c "tl_bytes_read_u32be" |
| 54 | pub fn read_u32be :: b: Bytes off: Int -> Int |
| 55 | |
| 56 | /// The unsigned big-endian 64-bit integer at `off` (a big integer above |
| 57 | /// 2^62), or -1 when `off..off + 8` is out of range. |
| 58 | @extern c "tl_bytes_read_u64be" |
| 59 | pub fn read_u64be :: b: Bytes off: Int -> Int |
| 60 | |
| 61 | /// The bytes from `start` up to `end` (excluded). Both are clamped to |
| 62 | /// `0..length(b)`; the result is empty when `end <= start`. |
| 63 | @extern c "tl_bytes_slice" |
| 64 | pub fn slice :: b: Bytes start: Int end: Int -> Bytes |
| 65 | |
| 66 | /// `a` followed by `b`. |
| 67 | @extern c "tl_bytes_concat" |
| 68 | pub fn concat :: a: Bytes b: Bytes -> Bytes |
| 69 | |
| 70 | /// All of `parts`, in order (one copy). |
| 71 | @extern c "tl_bytes_concat_all" |
| 72 | pub fn concat_all :: parts: List<Bytes> -> Bytes |
| 73 | |
| 74 | /// The UTF-8 encoding of `s`. |
| 75 | @extern c "tl_bytes_from_string" |
| 76 | pub fn from_string :: s: String -> Bytes |
| 77 | |
| 78 | /// The text `b` encodes, or an error naming the first byte that is not |
| 79 | /// valid UTF-8. |
| 80 | @extern c "tl_bytes_to_string" |
| 81 | pub fn to_string :: b: Bytes -> Result<String, String> |
| 82 | |
| 83 | /// `to_string` of `slice(b, off, off + len)` without the intermediate |
| 84 | /// `Bytes`: the bounds are clamped the same way, and the error names the |
| 85 | /// first invalid byte of the range. |
| 86 | @extern c "tl_bytes_to_string_range" |
| 87 | pub fn to_string_range :: b: Bytes off: Int len: Int -> Result<String, String> |
| 88 | |
| 89 | /// The text `b` encodes, with each invalid sequence replaced by U+FFFD. |
| 90 | @extern c "tl_bytes_to_string_lossy" |
| 91 | pub fn to_string_lossy :: b: Bytes -> String |
| 92 | |
| 93 | /// Bytes from Ints in `0..=255` (panics on any other Int). |
| 94 | @extern c "tl_bytes_from_list" |
| 95 | pub fn from_list :: items: List<Int> -> Bytes |
| 96 | |
| 97 | /// The bytes as Ints. |
| 98 | @extern c "tl_bytes_to_list" |
| 99 | pub fn to_list :: b: Bytes -> List<Int> |
| 100 | |
| 101 | /// A growable buffer: append pieces, then `build` the `Bytes` once. |
| 102 | /// Appending copies only the piece: a builder that is unique (no other |
| 103 | /// reference to it remains, as with `bytes.append(acc, piece)` in a loop) |
| 104 | /// grows in place, so building is linear in the total size, and `build` |
| 105 | /// hands its buffer over without copying it. A builder is still a value: |
| 106 | /// appending to one builder twice gives two independent builders (the |
| 107 | /// second append copies it). |
| 108 | @builtin |
| 109 | pub type Builder |
| 110 | |
| 111 | pub fn builder :: -> Builder |
| 112 | { |
| 113 | builder_with_capacity 0 |
| 114 | } |
| 115 | |
| 116 | /// An empty builder with room for `capacity` bytes before it grows. |
| 117 | @extern c "tl_bytes_builder" |
| 118 | pub fn builder_with_capacity :: capacity: Int -> Builder |
| 119 | |
| 120 | @extern c "tl_bytes_append" owned |
| 121 | pub fn append :: b: Builder data: Bytes -> Builder |
| 122 | |
| 123 | /// Append the UTF-8 encoding of `s`. |
| 124 | @extern c "tl_bytes_append_string" owned |
| 125 | pub fn append_string :: b: Builder s: String -> Builder |
| 126 | |
| 127 | /// Append one byte (`0..=255`; panics on any other Int). |
| 128 | @extern c "tl_bytes_append_byte" owned |
| 129 | pub fn append_byte :: b: Builder byte: Int -> Builder |
| 130 | |
| 131 | /// Everything appended, in order. |
| 132 | @extern c "tl_bytes_build" owned |
| 133 | pub fn build :: b: Builder -> Bytes |
| 134 | |
| 135 | /// Everything appended, in order, as text. Panics if the bytes are not |
| 136 | /// valid UTF-8 (the message names the first invalid byte, as |
| 137 | /// `to_string`'s error does). Like `build`, a unique builder hands its |
| 138 | /// buffer over without copying it. |
| 139 | @extern c "tl_bytes_build_string" owned |
| 140 | pub fn build_string :: b: Builder -> String |