| ... | ... | @@ -35,25 +35,25 @@ pub fn utf8Encode(c: u32, out: []u8) !u3 { |
| 35 | 35 | if (utf8CodepointSequenceLength(c)) |length| { |
| 36 | 36 | debug.assert(out.len >= length); |
| 37 | 37 | switch (length) { |
| 38 | // The pattern for each is the same |
| 39 | // - Increasing the initial shift by 6 each time |
| 40 | // - Each time after the first shorten the shifted |
| 41 | // value to a max of 0b111111 (63) |
| 38 | 42 | 1 => out[0] = u8(c), // Can just do 0 + codepoint for initial range |
| 39 | 43 | 2 => { |
| 40 | | // 64 to convert the codepoint into its segments |
| 41 | | out[0] = u8(0b11000000 + c / 64); |
| 42 | | out[1] = u8(0b10000000 + c % 64); |
| 44 | out[0] = u8(0b11000000 | (c >> 6)); |
| 45 | out[1] = u8(0b10000000 | (c & 0b111111)); |
| 43 | 46 | }, |
| 44 | 47 | 3 => { |
| 45 | | // Again using 64 as a conversion into their segments |
| 46 | | // But using C / 4096 (64 * 64) as the first, (C/64) % 64 as the second, and just C % 64 as the last |
| 47 | | out[0] = u8(0b11100000 + c / 4096); |
| 48 | | out[1] = u8(0b10000000 + (c / 64) % 64); |
| 49 | | out[2] = u8(0b10000000 + c % 64); |
| 48 | out[0] = u8(0b11100000 | (c >> 12)); |
| 49 | out[1] = u8(0b10000000 | ((c >> 6) & 0b111111)); |
| 50 | out[2] = u8(0b10000000 | (c & 0b111111)); |
| 50 | 51 | }, |
| 51 | 52 | 4 => { |
| 52 | | // Same as previously but now its C / 64^3 (262144), (C / 4096) % 64, (C / 64) % 64 and C % 64 |
| 53 | | out[0] = u8(0b11110000 + c / 262144); |
| 54 | | out[1] = u8(0b10000000 + (c / 4096) % 64); |
| 55 | | out[2] = u8(0b10000000 + (c / 64) % 64); |
| 56 | | out[3] = u8(0b10000000 + c % 64); |
| 53 | out[0] = u8(0b11110000 | (c >> 18)); |
| 54 | out[1] = u8(0b10000000 | ((c >> 12) & 0b111111)); |
| 55 | out[2] = u8(0b10000000 | ((c >> 6) & 0b111111)); |
| 56 | out[3] = u8(0b10000000 | (c & 0b111111)); |
| 57 | 57 | }, |
| 58 | 58 | else => unreachable, |
| 59 | 59 | } |
| ... | ... | @@ -257,7 +257,7 @@ fn testErrorEncode(codePoint: u32, array: []u8, expectedErr: error) void { |
| 257 | 257 | if (utf8Encode(codePoint, array)) |_| { |
| 258 | 258 | unreachable; |
| 259 | 259 | } else |err| { |
| 260 | | assert(err == expectedErr); |
| 260 | debug.assert(err == expectedErr); |
| 261 | 261 | } |
| 262 | 262 | } |
| 263 | 263 | |