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