| ... | @@ -79,8 +79,6 @@ fn zigifyEscapeSequences(allocator: *std.mem.Allocator, tok: CToken) !CToken { | ... | @@ -79,8 +79,6 @@ fn zigifyEscapeSequences(allocator: *std.mem.Allocator, tok: CToken) !CToken { |
| 79 | Escape, | 79 | Escape, |
| 80 | Hex, | 80 | Hex, |
| 81 | Octal, | 81 | Octal, |
| 82 | HexZero, | | |
| 83 | OctalZero, | | |
| 84 | } = .Start; | 82 | } = .Start; |
| 85 | var i: usize = 0; | 83 | var i: usize = 0; |
| 86 | var count: u8 = 0; | 84 | var count: u8 = 0; |
| ... | @@ -92,20 +90,15 @@ fn zigifyEscapeSequences(allocator: *std.mem.Allocator, tok: CToken) !CToken { | ... | @@ -92,20 +90,15 @@ fn zigifyEscapeSequences(allocator: *std.mem.Allocator, tok: CToken) !CToken { |
| 92 | 'n', 'r', 't', '\\', '\'', '\"' => { | 90 | 'n', 'r', 't', '\\', '\'', '\"' => { |
| 93 | bytes[i] = c; | 91 | bytes[i] = c; |
| 94 | }, | 92 | }, |
| 95 | '0' => { | 93 | '0'...'7' => { |
| 96 | state = .OctalZero; | | |
| 97 | bytes[i] = 'x'; | | |
| 98 | }, | | |
| 99 | '1'...'7' => { | | |
| 100 | count += 1; | 94 | count += 1; |
| 101 | num *= 8; | | |
| 102 | num += c - '0'; | 95 | num += c - '0'; |
| 103 | state = .Octal; | 96 | state = .Octal; |
| 104 | bytes[i] = 'x'; | 97 | bytes[i] = 'x'; |
| 105 | }, | 98 | }, |
| 106 | 'x' => { | 99 | 'x' => { |
| 107 | state = .HexZero; | 100 | state = .Hex; |
| 108 | bytes[i] = c; | 101 | bytes[i] = 'x'; |
| 109 | }, | 102 | }, |
| 110 | 'a' => { | 103 | 'a' => { |
| 111 | bytes[i] = 'x'; | 104 | bytes[i] = 'x'; |
| ... | @@ -159,83 +152,37 @@ fn zigifyEscapeSequences(allocator: *std.mem.Allocator, tok: CToken) !CToken { | ... | @@ -159,83 +152,37 @@ fn zigifyEscapeSequences(allocator: *std.mem.Allocator, tok: CToken) !CToken { |
| 159 | bytes[i] = c; | 152 | bytes[i] = c; |
| 160 | i += 1; | 153 | i += 1; |
| 161 | }, | 154 | }, |
| 162 | .HexZero => { | | |
| 163 | switch (c) { | | |
| 164 | '0' => { continue; }, | | |
| 165 | '1'...'9' => { | | |
| 166 | count += 1; | | |
| 167 | num *= 16; | | |
| 168 | num += c - '0'; | | |
| 169 | }, | | |
| 170 | 'a'...'f' => { | | |
| 171 | count += 1; | | |
| 172 | num *= 16; | | |
| 173 | num += c - 'a' + 10; | | |
| 174 | }, | | |
| 175 | 'A'...'F' => { | | |
| 176 | count += 1; | | |
| 177 | num *= 16; | | |
| 178 | num += c - 'A' + 10; | | |
| 179 | }, | | |
| 180 | else => {}, | | |
| 181 | } | | |
| 182 | state = .Hex; | | |
| 183 | }, | | |
| 184 | .Hex => { | 155 | .Hex => { |
| 185 | switch (c) { | 156 | switch (c) { |
| 186 | '0'...'9' => { | 157 | '0'...'9' => { |
| 187 | count += 1; | 158 | num = std.math.mul(u8, num, 16) catch return error.TokenizingFailed; |
| 188 | num *= 16; | | |
| 189 | num += c - '0'; | 159 | num += c - '0'; |
| 190 | if (count < 2) | | |
| 191 | continue; | | |
| 192 | }, | 160 | }, |
| 193 | 'a'...'f' => { | 161 | 'a'...'f' => { |
| 194 | count += 1; | 162 | num = std.math.mul(u8, num, 16) catch return error.TokenizingFailed; |
| 195 | num *= 16; | | |
| 196 | num += c - 'a' + 10; | 163 | num += c - 'a' + 10; |
| 197 | if (count < 2) | | |
| 198 | continue; | | |
| 199 | }, | 164 | }, |
| 200 | 'A'...'F' => { | 165 | 'A'...'F' => { |
| 201 | count += 1; | 166 | num = std.math.mul(u8, num, 16) catch return error.TokenizingFailed; |
| 202 | num *= 16; | | |
| 203 | num += c - 'A' + 10; | 167 | num += c - 'A' + 10; |
| 204 | if (count < 2) | | |
| 205 | continue; | | |
| 206 | }, | 168 | }, |
| 207 | else => {}, | | |
| 208 | } | | |
| 209 | i += std.fmt.formatIntBuf(bytes[i..], num, 16, false, std.fmt.FormatOptions{.fill = '0', .width = 2}); | | |
| 210 | switch (c) { | | |
| 211 | '\\' => state = .Escape, | | |
| 212 | '0'...'9', 'a'...'f','A'...'F' => state = .Start, | | |
| 213 | else => { | 169 | else => { |
| 214 | state = .Start; | 170 | i += std.fmt.formatIntBuf(bytes[i..], num, 16, false, std.fmt.FormatOptions{.fill = '0', .width = 2}); |
| | 171 | num = 0; |
| | 172 | if (c == '\\') |
| | 173 | state = .Escape |
| | 174 | else |
| | 175 | state = .Start; |
| 215 | bytes[i] = c; | 176 | bytes[i] = c; |
| 216 | i += 1; | 177 | i += 1; |
| 217 | }, | 178 | }, |
| 218 | } | 179 | } |
| 219 | count = 0; | | |
| 220 | num = 0; | | |
| 221 | }, | | |
| 222 | .OctalZero => { | | |
| 223 | switch (c) { | | |
| 224 | '0' => { continue; }, | | |
| 225 | '1'...'7' => { | | |
| 226 | count += 1; | | |
| 227 | num *= 8; | | |
| 228 | num += c - '0'; | | |
| 229 | }, | | |
| 230 | else => {}, | | |
| 231 | } | | |
| 232 | state = .Octal; | | |
| 233 | }, | 180 | }, |
| 234 | .Octal => { | 181 | .Octal => { |
| 235 | switch (c) { | 182 | switch (c) { |
| 236 | '0'...'7' => { | 183 | '0'...'7' => { |
| 237 | count += 1; | 184 | count += 1; |
| 238 | num *= 8; | 185 | num = std.math.mul(u8, num, 8) catch return error.TokenizingFailed; |
| 239 | num += c - '0'; | 186 | num += c - '0'; |
| 240 | if (count < 3) | 187 | if (count < 3) |
| 241 | continue; | 188 | continue; |
| ... | @@ -243,15 +190,7 @@ fn zigifyEscapeSequences(allocator: *std.mem.Allocator, tok: CToken) !CToken { | ... | @@ -243,15 +190,7 @@ fn zigifyEscapeSequences(allocator: *std.mem.Allocator, tok: CToken) !CToken { |
| 243 | else => {}, | 190 | else => {}, |
| 244 | } | 191 | } |
| 245 | i += std.fmt.formatIntBuf(bytes[i..], num, 16, false, std.fmt.FormatOptions{.fill = '0', .width = 2}); | 192 | i += std.fmt.formatIntBuf(bytes[i..], num, 16, false, std.fmt.FormatOptions{.fill = '0', .width = 2}); |
| 246 | switch (c) { | 193 | state = .Start; |
| 247 | '\\' => state = .Escape, | | |
| 248 | '0'...'7' => state = .Start, | | |
| 249 | else => { | | |
| 250 | state = .Start; | | |
| 251 | bytes[i] = c; | | |
| 252 | i += 1; | | |
| 253 | }, | | |
| 254 | } | | |
| 255 | count = 0; | 194 | count = 0; |
| 256 | num = 0; | 195 | num = 0; |
| 257 | }, | 196 | }, |
| ... | @@ -799,12 +738,12 @@ test "escape sequences" { | ... | @@ -799,12 +738,12 @@ test "escape sequences" { |
| 799 | })).bytes, "\\x77")); | 738 | })).bytes, "\\x77")); |
| 800 | expect(std.mem.eql(u8, (try zigifyEscapeSequences(a, .{ | 739 | expect(std.mem.eql(u8, (try zigifyEscapeSequences(a, .{ |
| 801 | .id = .StrLit, | 740 | .id = .StrLit, |
| 802 | .bytes = "\\00245", | 741 | .bytes = "\\24500", |
| 803 | })).bytes, "\\xa5")); | 742 | })).bytes, "\\xa500")); |
| 804 | expect(std.mem.eql(u8, (try zigifyEscapeSequences(a, .{ | 743 | expect(std.mem.eql(u8, (try zigifyEscapeSequences(a, .{ |
| 805 | .id = .StrLit, | 744 | .id = .StrLit, |
| 806 | .bytes = "\\x0077abc", | 745 | .bytes = "\\x0077 abc", |
| 807 | })).bytes, "\\x77abc")); | 746 | })).bytes, "\\x77 abc")); |
| 808 | expect(std.mem.eql(u8, (try zigifyEscapeSequences(a, .{ | 747 | expect(std.mem.eql(u8, (try zigifyEscapeSequences(a, .{ |
| 809 | .id = .StrLit, | 748 | .id = .StrLit, |
| 810 | .bytes = "\\045abc", | 749 | .bytes = "\\045abc", |