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 {...@@ -35,25 +35,25 @@ pub fn utf8Encode(c: u32, out: []u8) !u3 {
35 if (utf8CodepointSequenceLength(c)) |length| {35 if (utf8CodepointSequenceLength(c)) |length| {
36 debug.assert(out.len >= length);36 debug.assert(out.len >= length);
37 switch (length) {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 1 => out[0] = u8(c), // Can just do 0 + codepoint for initial range42 1 => out[0] = u8(c), // Can just do 0 + codepoint for initial range
39 2 => {43 2 => {
40 // 64 to convert the codepoint into its segments44 out[0] = u8(0b11000000 | (c >> 6));
41 out[0] = u8(0b11000000 + c / 64);45 out[1] = u8(0b10000000 | (c & 0b111111));
42 out[1] = u8(0b10000000 + c % 64);
43 },46 },
44 3 => {47 3 => {
45 // Again using 64 as a conversion into their segments48 out[0] = u8(0b11100000 | (c >> 12));
46 // But using C / 4096 (64 * 64) as the first, (C/64) % 64 as the second, and just C % 64 as the last49 out[1] = u8(0b10000000 | ((c >> 6) & 0b111111));
47 out[0] = u8(0b11100000 + c / 4096);50 out[2] = u8(0b10000000 | (c & 0b111111));
48 out[1] = u8(0b10000000 + (c / 64) % 64);
49 out[2] = u8(0b10000000 + c % 64);
50 },51 },
51 4 => {52 4 => {
52 // Same as previously but now its C / 64^3 (262144), (C / 4096) % 64, (C / 64) % 64 and C % 6453 out[0] = u8(0b11110000 | (c >> 18));
53 out[0] = u8(0b11110000 + c / 262144);54 out[1] = u8(0b10000000 | ((c >> 12) & 0b111111));
54 out[1] = u8(0b10000000 + (c / 4096) % 64);55 out[2] = u8(0b10000000 | ((c >> 6) & 0b111111));
55 out[2] = u8(0b10000000 + (c / 64) % 64);56 out[3] = u8(0b10000000 | (c & 0b111111));
56 out[3] = u8(0b10000000 + c % 64);
57 },57 },
58 else => unreachable,58 else => unreachable,
59 }59 }
...@@ -257,7 +257,7 @@ fn testErrorEncode(codePoint: u32, array: []u8, expectedErr: error) void {...@@ -257,7 +257,7 @@ fn testErrorEncode(codePoint: u32, array: []u8, expectedErr: error) void {
257 if (utf8Encode(codePoint, array)) |_| {257 if (utf8Encode(codePoint, array)) |_| {
258 unreachable;258 unreachable;
259 } else |err| {259 } else |err| {
260 assert(err == expectedErr);260 debug.assert(err == expectedErr);
261 }261 }
262}262}
263263