authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-12-10 14:53:41-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-12-10 14:53:41-05:00
log023b597ab4a74adbbe658bd7476fa5d20b02a133
tree3b6234ccf647312eb55561218555a08f2ea35e8a
parentac0488430fd4ab35cab972d6409a0e244ad9637c
parenta01993e908b636835d4c2905928af700da23ea8e
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #13370 from r00ster91/newascii

std.ascii: remove LUT and deprecations

9 files changed, 89 insertions(+), 284 deletions(-)

lib/std/SemanticVersion.zig+2-2
......@@ -114,7 +114,7 @@ pub fn parse(text: []const u8) !Version {
114114 if (id.len == 0) return error.InvalidVersion;
115115
116116 // Identifiers MUST comprise only ASCII alphanumerics and hyphens [0-9A-Za-z-].
117 for (id) |c| if (!std.ascii.isAlNum(c) and c != '-') return error.InvalidVersion;
117 for (id) |c| if (!std.ascii.isAlphanumeric(c) and c != '-') return error.InvalidVersion;
118118
119119 // Numeric identifiers MUST NOT include leading zeroes.
120120 const is_num = for (id) |c| {
......@@ -133,7 +133,7 @@ pub fn parse(text: []const u8) !Version {
133133 if (id.len == 0) return error.InvalidVersion;
134134
135135 // Identifiers MUST comprise only ASCII alphanumerics and hyphens [0-9A-Za-z-].
136 for (id) |c| if (!std.ascii.isAlNum(c) and c != '-') return error.InvalidVersion;
136 for (id) |c| if (!std.ascii.isAlphanumeric(c) and c != '-') return error.InvalidVersion;
137137 }
138138 }
139139
lib/std/ascii.zig+72-268
......@@ -10,83 +10,10 @@
1010
1111const std = @import("std");
1212
13// TODO: remove all decls marked as DEPRECATED after 0.10.0's release
14
1513/// The C0 control codes of the ASCII encoding.
1614///
17/// See also: https://en.wikipedia.org/wiki/C0_and_C1_control_codes and `isControl`.
15/// See also: https://en.wikipedia.org/wiki/C0_and_C1_control_codes and `isControl`
1816pub const control_code = struct {
19 // DEPRECATED: use the lowercase variant
20 pub const NUL = 0x00;
21 // DEPRECATED: use the lowercase variant
22 pub const SOH = 0x01;
23 // DEPRECATED: use the lowercase variant
24 pub const STX = 0x02;
25 // DEPRECATED: use the lowercase variant
26 pub const ETX = 0x03;
27 // DEPRECATED: use the lowercase variant
28 pub const EOT = 0x04;
29 // DEPRECATED: use the lowercase variant
30 pub const ENQ = 0x05;
31 // DEPRECATED: use the lowercase variant
32 pub const ACK = 0x06;
33 // DEPRECATED: use the lowercase variant
34 pub const BEL = 0x07;
35 // DEPRECATED: use the lowercase variant
36 pub const BS = 0x08;
37 // DEPRECATED: use `ht`
38 pub const TAB = 0x09;
39 // DEPRECATED: use the lowercase variant
40 pub const LF = 0x0A;
41 // DEPRECATED: use the lowercase variant
42 pub const VT = 0x0B;
43 // DEPRECATED: use the lowercase variant
44 pub const FF = 0x0C;
45 // DEPRECATED: use the lowercase variant
46 pub const CR = 0x0D;
47 // DEPRECATED: use the lowercase variant
48 pub const SO = 0x0E;
49 // DEPRECATED: use the lowercase variant
50 pub const SI = 0x0F;
51 // DEPRECATED: use the lowercase variant
52 pub const DLE = 0x10;
53 // DEPRECATED: use the lowercase variant
54 pub const DC1 = 0x11;
55 // DEPRECATED: use the lowercase variant
56 pub const DC2 = 0x12;
57 // DEPRECATED: use the lowercase variant
58 pub const DC3 = 0x13;
59 // DEPRECATED: use the lowercase variant
60 pub const DC4 = 0x14;
61 // DEPRECATED: use the lowercase variant
62 pub const NAK = 0x15;
63 // DEPRECATED: use the lowercase variant
64 pub const SYN = 0x16;
65 // DEPRECATED: use the lowercase variant
66 pub const ETB = 0x17;
67 // DEPRECATED: use the lowercase variant
68 pub const CAN = 0x18;
69 // DEPRECATED: use the lowercase variant
70 pub const EM = 0x19;
71 // DEPRECATED: use the lowercase variant
72 pub const SUB = 0x1A;
73 // DEPRECATED: use the lowercase variant
74 pub const ESC = 0x1B;
75 // DEPRECATED: use the lowercase variant
76 pub const FS = 0x1C;
77 // DEPRECATED: use the lowercase variant
78 pub const GS = 0x1D;
79 // DEPRECATED: use the lowercase variant
80 pub const RS = 0x1E;
81 // DEPRECATED: use the lowercase variant
82 pub const US = 0x1F;
83 // DEPRECATED: use the lowercase variant
84 pub const DEL = 0x7F;
85 // DEPRECATED: use the lowercase variant
86 pub const XON = 0x11;
87 // DEPRECATED: use the lowercase variant
88 pub const XOFF = 0x13;
89
9017 /// Null.
9118 pub const nul = 0x00;
9219 /// Start of Heading.
......@@ -161,211 +88,63 @@ pub const control_code = struct {
16188 pub const xoff = dc3;
16289};
16390
164const tIndex = enum(u3) {
165 Alpha,
166 Hex,
167 Space,
168 Digit,
169 Lower,
170 Upper,
171 // Ctrl, < 0x20 || == DEL
172 // Print, = Graph || == ' '. NOT '\t' et cetera
173 Punct,
174 Graph,
175 //ASCII, | ~0b01111111
176 //isBlank, == ' ' || == '\x09'
177};
178
179const combinedTable = init: {
180 comptime var table: [256]u8 = undefined;
181
182 const mem = std.mem;
183
184 const alpha = [_]u1{
185 // 0, 1, 2, 3, 4, 5, 6, 7 ,8, 9,10,11,12,13,14,15
186 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
187 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
188 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
189 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
190
191 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
192 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
193 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
194 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
195 };
196 const lower = [_]u1{
197 // 0, 1, 2, 3, 4, 5, 6, 7 ,8, 9,10,11,12,13,14,15
198 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
278 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
279 };
280
281 comptime var i = 0;
282 inline while (i < 128) : (i += 1) {
283 table[i] =
284 @as(u8, alpha[i]) << @enumToInt(tIndex.Alpha) |
285 @as(u8, hex[i]) << @enumToInt(tIndex.Hex) |
286 @as(u8, space[i]) << @enumToInt(tIndex.Space) |
287 @as(u8, digit[i]) << @enumToInt(tIndex.Digit) |
288 @as(u8, lower[i]) << @enumToInt(tIndex.Lower) |
289 @as(u8, upper[i]) << @enumToInt(tIndex.Upper) |
290 @as(u8, punct[i]) << @enumToInt(tIndex.Punct) |
291 @as(u8, graph[i]) << @enumToInt(tIndex.Graph);
292 }
293 mem.set(u8, table[128..256], 0);
294 break :init table;
295};
296
297fn inTable(c: u8, t: tIndex) bool {
298 return (combinedTable[c] & (@as(u8, 1) << @enumToInt(t))) != 0;
299}
300
301/// DEPRECATED: use `isAlphanumeric`
302pub const isAlNum = isAlphanumeric;
303/// DEPRECATED: use `isAlphabetic`
304pub const isAlpha = isAlphabetic;
305/// DEPRECATED: use `isControl`
306pub const isCntrl = isControl;
307/// DEPRECATED: use `isWhitespace`.
308pub const isSpace = isWhitespace;
309/// DEPRECATED: use `whitespace`.
310pub const spaces = whitespace;
311/// DEPRECATED: use `isHex`.
312pub const isXDigit = isHex;
313
314/// Returns whether the character is alphanumeric.
91/// Returns whether the character is alphanumeric: A-Z, a-z, or 0-9.
31592pub fn isAlphanumeric(c: u8) bool {
316 return (combinedTable[c] & ((@as(u8, 1) << @enumToInt(tIndex.Alpha)) |
317 @as(u8, 1) << @enumToInt(tIndex.Digit))) != 0;
93 return switch (c) {
94 '0'...'9', 'A'...'Z', 'a'...'z' => true,
95 else => false,
96 };
31897}
31998
320/// Returns whether the character is alphabetic.
99/// Returns whether the character is alphabetic: A-Z or a-z.
321100pub fn isAlphabetic(c: u8) bool {
322 return inTable(c, tIndex.Alpha);
101 return switch (c) {
102 'A'...'Z', 'a'...'z' => true,
103 else => false,
104 };
323105}
324106
325107/// Returns whether the character is a control character.
326/// This is the same as `!isPrint(c)`.
327108///
328/// See also: `control_code`.
109/// See also: `control_code`
329110pub fn isControl(c: u8) bool {
330111 return c <= control_code.us or c == control_code.del;
331112}
332113
333114/// Returns whether the character is a digit.
334115pub fn isDigit(c: u8) bool {
335 return inTable(c, tIndex.Digit);
336}
337
338/// DEPRECATED: use `isPrint(c) and c != ' '` instead
339pub fn isGraph(c: u8) bool {
340 return inTable(c, tIndex.Graph);
116 return switch (c) {
117 '0'...'9' => true,
118 else => false,
119 };
341120}
342121
343/// Returns whether the character is a lowercased letter.
122/// Returns whether the character is a lowercase letter.
344123pub fn isLower(c: u8) bool {
345 return inTable(c, tIndex.Lower);
124 return switch (c) {
125 'a'...'z' => true,
126 else => false,
127 };
346128}
347129
348/// Returns whether the character has some graphical representation and can be printed.
349/// This also returns `true` for the space character.
350/// This is the same as `!isControl(c)`.
130/// Returns whether the character is printable and has some graphical representation,
131/// including the space character.
351132pub fn isPrint(c: u8) bool {
352 return inTable(c, tIndex.Graph) or c == ' ';
353}
354
355/// DEPRECATED: create your own function based on your needs and what you want to do.
356pub fn isPunct(c: u8) bool {
357 return inTable(c, tIndex.Punct);
133 return isASCII(c) and !isControl(c);
358134}
359135
360136/// Returns whether this character is included in `whitespace`.
361137pub fn isWhitespace(c: u8) bool {
362 return inTable(c, tIndex.Space);
138 return for (whitespace) |other| {
139 if (c == other)
140 break true;
141 } else false;
363142}
364143
365144/// Whitespace for general use.
366145/// This may be used with e.g. `std.mem.trim` to trim whitespace.
367146///
368/// See also: `isWhitespace`.
147/// See also: `isWhitespace`
369148pub const whitespace = [_]u8{ ' ', '\t', '\n', '\r', control_code.vt, control_code.ff };
370149
371150test "whitespace" {
......@@ -377,14 +156,20 @@ test "whitespace" {
377156 }
378157}
379158
380/// Returns whether the character is an uppercased letter.
159/// Returns whether the character is an uppercase letter.
381160pub fn isUpper(c: u8) bool {
382 return inTable(c, tIndex.Upper);
161 return switch (c) {
162 'A'...'Z' => true,
163 else => false,
164 };
383165}
384166
385/// Returns whether the character is a hexadecimal digit. This is case-insensitive.
167/// Returns whether the character is a hexadecimal digit: A-F, a-f, or 0-9.
386168pub fn isHex(c: u8) bool {
387 return inTable(c, tIndex.Hex);
169 return switch (c) {
170 '0'...'9', 'A'...'F', 'a'...'f' => true,
171 else => false,
172 };
388173}
389174
390175/// Returns whether the character is a 7-bit ASCII character.
......@@ -392,12 +177,7 @@ pub fn isASCII(c: u8) bool {
392177 return c < 128;
393178}
394179
395/// DEPRECATED: use `c == ' ' or c == '\t'` or try `isWhitespace`
396pub fn isBlank(c: u8) bool {
397 return (c == ' ') or (c == '\x09');
398}
399
400/// Uppercases the character and returns it as-is if it's already uppercased or not a letter.
180/// Uppercases the character and returns it as-is if already uppercase or not a letter.
401181pub fn toUpper(c: u8) u8 {
402182 if (isLower(c)) {
403183 return c & 0b11011111;
......@@ -406,7 +186,7 @@ pub fn toUpper(c: u8) u8 {
406186 }
407187}
408188
409/// Lowercases the character and returns it as-is if it's already lowercased or not a letter.
189/// Lowercases the character and returns it as-is if already lowercase or not a letter.
410190pub fn toLower(c: u8) u8 {
411191 if (isUpper(c)) {
412192 return c | 0b00100000;
......@@ -415,53 +195,77 @@ pub fn toLower(c: u8) u8 {
415195 }
416196}
417197
418test "ascii character classes" {
198test "ASCII character classes" {
419199 const testing = std.testing;
420200
421201 try testing.expect(!isControl('a'));
422202 try testing.expect(!isControl('z'));
203 try testing.expect(!isControl(' '));
423204 try testing.expect(isControl(control_code.nul));
424205 try testing.expect(isControl(control_code.ff));
425206 try testing.expect(isControl(control_code.us));
207 try testing.expect(isControl(control_code.del));
208 try testing.expect(!isControl(0x80));
209 try testing.expect(!isControl(0xff));
426210
427211 try testing.expect('C' == toUpper('c'));
428212 try testing.expect(':' == toUpper(':'));
429213 try testing.expect('\xab' == toUpper('\xab'));
430214 try testing.expect(!isUpper('z'));
215 try testing.expect(!isUpper(0x80));
216 try testing.expect(!isUpper(0xff));
431217
432218 try testing.expect('c' == toLower('C'));
433219 try testing.expect(':' == toLower(':'));
434220 try testing.expect('\xab' == toLower('\xab'));
435221 try testing.expect(!isLower('Z'));
222 try testing.expect(!isLower(0x80));
223 try testing.expect(!isLower(0xff));
436224
437225 try testing.expect(isAlphanumeric('Z'));
438226 try testing.expect(isAlphanumeric('z'));
439227 try testing.expect(isAlphanumeric('5'));
440 try testing.expect(isAlphanumeric('5'));
228 try testing.expect(isAlphanumeric('a'));
441229 try testing.expect(!isAlphanumeric('!'));
230 try testing.expect(!isAlphanumeric(0x80));
231 try testing.expect(!isAlphanumeric(0xff));
442232
443 try testing.expect(!isAlpha('5'));
444 try testing.expect(isAlpha('c'));
445 try testing.expect(!isAlpha('5'));
233 try testing.expect(!isAlphabetic('5'));
234 try testing.expect(isAlphabetic('c'));
235 try testing.expect(!isAlphabetic('@'));
236 try testing.expect(isAlphabetic('Z'));
237 try testing.expect(!isAlphabetic(0x80));
238 try testing.expect(!isAlphabetic(0xff));
446239
447240 try testing.expect(isWhitespace(' '));
448241 try testing.expect(isWhitespace('\t'));
449242 try testing.expect(isWhitespace('\r'));
450243 try testing.expect(isWhitespace('\n'));
244 try testing.expect(isWhitespace(control_code.ff));
451245 try testing.expect(!isWhitespace('.'));
246 try testing.expect(!isWhitespace(control_code.us));
247 try testing.expect(!isWhitespace(0x80));
248 try testing.expect(!isWhitespace(0xff));
452249
453250 try testing.expect(!isHex('g'));
454251 try testing.expect(isHex('b'));
252 try testing.expect(isHex('F'));
455253 try testing.expect(isHex('9'));
254 try testing.expect(!isHex(0x80));
255 try testing.expect(!isHex(0xff));
456256
457257 try testing.expect(!isDigit('~'));
458258 try testing.expect(isDigit('0'));
459259 try testing.expect(isDigit('9'));
260 try testing.expect(!isDigit(0x80));
261 try testing.expect(!isDigit(0xff));
460262
461263 try testing.expect(isPrint(' '));
462264 try testing.expect(isPrint('@'));
463265 try testing.expect(isPrint('~'));
464266 try testing.expect(!isPrint(control_code.esc));
267 try testing.expect(!isPrint(0x80));
268 try testing.expect(!isPrint(0xff));
465269}
466270
467271/// Writes a lower case copy of `ascii_string` to `output`.
......@@ -541,7 +345,7 @@ pub fn startsWithIgnoreCase(haystack: []const u8, needle: []const u8) bool {
541345 return if (needle.len > haystack.len) false else eqlIgnoreCase(haystack[0..needle.len], needle);
542346}
543347
544test "ascii.startsWithIgnoreCase" {
348test "startsWithIgnoreCase" {
545349 try std.testing.expect(startsWithIgnoreCase("boB", "Bo"));
546350 try std.testing.expect(!startsWithIgnoreCase("Needle in hAyStAcK", "haystack"));
547351}
......@@ -550,7 +354,7 @@ pub fn endsWithIgnoreCase(haystack: []const u8, needle: []const u8) bool {
550354 return if (needle.len > haystack.len) false else eqlIgnoreCase(haystack[haystack.len - needle.len ..], needle);
551355}
552356
553test "ascii.endsWithIgnoreCase" {
357test "endsWithIgnoreCase" {
554358 try std.testing.expect(endsWithIgnoreCase("Needle in HaYsTaCk", "haystack"));
555359 try std.testing.expect(!endsWithIgnoreCase("BoB", "Bo"));
556360}
lib/std/fmt.zig+2-1
......@@ -2198,8 +2198,9 @@ test "slice" {
21982198}
21992199
22002200test "escape non-printable" {
2201 try expectFmt("abc", "{s}", .{fmtSliceEscapeLower("abc")});
2201 try expectFmt("abc 123", "{s}", .{fmtSliceEscapeLower("abc 123")});
22022202 try expectFmt("ab\\xffc", "{s}", .{fmtSliceEscapeLower("ab\xffc")});
2203 try expectFmt("abc 123", "{s}", .{fmtSliceEscapeUpper("abc 123")});
22032204 try expectFmt("ab\\xFFc", "{s}", .{fmtSliceEscapeUpper("ab\xffc")});
22042205}
22052206
lib/std/net.zig+1-1
......@@ -1192,7 +1192,7 @@ pub fn isValidHostName(hostname: []const u8) bool {
11921192 if (hostname.len >= 254) return false;
11931193 if (!std.unicode.utf8ValidateSlice(hostname)) return false;
11941194 for (hostname) |byte| {
1195 if (byte >= 0x80 or byte == '.' or byte == '-' or std.ascii.isAlNum(byte)) {
1195 if (!std.ascii.isASCII(byte) or byte == '.' or byte == '-' or std.ascii.isAlphanumeric(byte)) {
11961196 continue;
11971197 }
11981198 return false;
lib/std/zig/parse.zig+2-2
......@@ -1531,7 +1531,7 @@ const Parser = struct {
15311531 // without types we don't know if '&&' was intended as 'bitwise_and address_of', or a c-style logical_and
15321532 // The best the parser can do is recommend changing it to 'and' or ' & &'
15331533 try p.warnMsg(.{ .tag = .invalid_ampersand_ampersand, .token = oper_token });
1534 } else if (std.ascii.isSpace(char_before) != std.ascii.isSpace(char_after)) {
1534 } else if (std.ascii.isWhitespace(char_before) != std.ascii.isWhitespace(char_after)) {
15351535 try p.warnMsg(.{ .tag = .mismatched_binary_op_whitespace, .token = oper_token });
15361536 }
15371537 }
......@@ -1728,7 +1728,7 @@ const Parser = struct {
17281728 var sentinel: Node.Index = 0;
17291729 if (p.eatToken(.identifier)) |ident| {
17301730 const ident_slice = p.source[p.token_starts[ident]..p.token_starts[ident + 1]];
1731 if (!std.mem.eql(u8, std.mem.trimRight(u8, ident_slice, &std.ascii.spaces), "c")) {
1731 if (!std.mem.eql(u8, std.mem.trimRight(u8, ident_slice, &std.ascii.whitespace), "c")) {
17321732 p.tok_i -= 1;
17331733 }
17341734 } else if (p.eatToken(.colon)) |_| {
lib/std/zig/render.zig+4-4
......@@ -2648,7 +2648,7 @@ fn renderComments(ais: *Ais, tree: Ast, start: usize, end: usize) Error!bool {
26482648 const newline = if (newline_index) |i| comment_start + i else null;
26492649
26502650 const untrimmed_comment = tree.source[comment_start .. newline orelse tree.source.len];
2651 const trimmed_comment = mem.trimRight(u8, untrimmed_comment, &std.ascii.spaces);
2651 const trimmed_comment = mem.trimRight(u8, untrimmed_comment, &std.ascii.whitespace);
26522652
26532653 // Don't leave any whitespace at the start of the file
26542654 if (index != 0) {
......@@ -2669,7 +2669,7 @@ fn renderComments(ais: *Ais, tree: Ast, start: usize, end: usize) Error!bool {
26692669
26702670 index = 1 + (newline orelse end - 1);
26712671
2672 const comment_content = mem.trimLeft(u8, trimmed_comment["//".len..], &std.ascii.spaces);
2672 const comment_content = mem.trimLeft(u8, trimmed_comment["//".len..], &std.ascii.whitespace);
26732673 if (ais.disabled_offset != null and mem.eql(u8, comment_content, "zig fmt: on")) {
26742674 // Write the source for which formatting was disabled directly
26752675 // to the underlying writer, fixing up invaild whitespace.
......@@ -2716,7 +2716,7 @@ fn renderExtraNewlineToken(ais: *Ais, tree: Ast, token_index: Ast.TokenIndex) Er
27162716 // non-whitespace character is encountered or two newlines have been found.
27172717 var i = token_start - 1;
27182718 var newlines: u2 = 0;
2719 while (std.ascii.isSpace(tree.source[i])) : (i -= 1) {
2719 while (std.ascii.isWhitespace(tree.source[i])) : (i -= 1) {
27202720 if (tree.source[i] == '\n') newlines += 1;
27212721 if (newlines == 2) return ais.insertNewline();
27222722 if (i == prev_token_end) break;
......@@ -2778,7 +2778,7 @@ fn tokenSliceForRender(tree: Ast, token_index: Ast.TokenIndex) []const u8 {
27782778 ret.len -= 1;
27792779 },
27802780 .container_doc_comment, .doc_comment => {
2781 ret = mem.trimRight(u8, ret, &std.ascii.spaces);
2781 ret = mem.trimRight(u8, ret, &std.ascii.whitespace);
27822782 },
27832783 else => {},
27842784 }
lib/std/zig/tokenizer.zig+1-1
......@@ -1232,7 +1232,7 @@ pub const Tokenizer = struct {
12321232 fn getInvalidCharacterLength(self: *Tokenizer) u3 {
12331233 const c0 = self.buffer[self.index];
12341234 if (std.ascii.isASCII(c0)) {
1235 if (std.ascii.isCntrl(c0)) {
1235 if (std.ascii.isControl(c0)) {
12361236 // ascii control codes are never allowed
12371237 // (note that \n was checked before we got here)
12381238 return 1;
src/DepTokenizer.zig+4-4
......@@ -866,7 +866,7 @@ test "error target - continuation expecting end-of-line" {
866866 );
867867 try depTokenizer("foo.o: \\ ",
868868 \\target = {foo.o}
869 \\ERROR: illegal char \x20 at position 8: continuation expecting end-of-line
869 \\ERROR: illegal char ' ' at position 8: continuation expecting end-of-line
870870 );
871871 try depTokenizer("foo.o: \\x",
872872 \\target = {foo.o}
......@@ -1053,10 +1053,10 @@ fn printCharValues(out: anytype, bytes: []const u8) !void {
10531053}
10541054
10551055fn printUnderstandableChar(out: anytype, char: u8) !void {
1056 if (!std.ascii.isPrint(char) or char == ' ') {
1057 try out.print("\\x{X:0>2}", .{char});
1056 if (std.ascii.isPrint(char)) {
1057 try out.print("'{c}'", .{char});
10581058 } else {
1059 try out.print("'{c}'", .{printable_char_tab[char]});
1059 try out.print("\\x{X:0>2}", .{char});
10601060 }
10611061}
10621062
src/translate_c.zig+1-1
......@@ -5738,7 +5738,7 @@ fn parseCNumLit(c: *Context, m: *MacroCtx) ParseError!Node {
57385738 if (mem.indexOfScalar(u8, lit_bytes, '.')) |dot_index| {
57395739 if (dot_index == 2) {
57405740 lit_bytes = try std.fmt.allocPrint(c.arena, "0x0{s}", .{lit_bytes[2..]});
5741 } else if (dot_index + 1 == lit_bytes.len or !std.ascii.isXDigit(lit_bytes[dot_index + 1])) {
5741 } else if (dot_index + 1 == lit_bytes.len or !std.ascii.isHex(lit_bytes[dot_index + 1])) {
57425742 // If the literal lacks a digit after the `.`, we need to
57435743 // add one since `0x1.p10` would be invalid syntax in Zig.
57445744 lit_bytes = try std.fmt.allocPrint(c.arena, "0x{s}0{s}", .{