authorgravatar for Braedonww@gmail.comBraedon <Braedonww@gmail.com> 2018-04-25 16:26:57+10:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2018-04-25 16:26:57+10:00
log07af6559d8a883dcb21ff99cddb4a836d2bb66bd
tree09269a870904f7f24fd69878f64fc0f2db26deac
parentf6cbe9a9cca3718501272cea177ab1ad48852ffe
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Changed to use shifting and masking


1 files changed, 14 insertions(+), 14 deletions(-)

std/unicode.zig+14-14
......@@ -35,25 +35,25 @@ pub fn utf8Encode(c: u32, out: []u8) !u3 {
3535 if (utf8CodepointSequenceLength(c)) |length| {
3636 debug.assert(out.len >= length);
3737 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)
3842 1 => out[0] = u8(c), // Can just do 0 + codepoint for initial range
3943 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));
4346 },
4447 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));
5051 },
5152 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));
5757 },
5858 else => unreachable,
5959 }
......@@ -257,7 +257,7 @@ fn testErrorEncode(codePoint: u32, array: []u8, expectedErr: error) void {
257257 if (utf8Encode(codePoint, array)) |_| {
258258 unreachable;
259259 } else |err| {
260 assert(err == expectedErr);
260 debug.assert(err == expectedErr);
261261 }
262262}
263263