| ... | ... | @@ -23,6 +23,7 @@ pub fn fmtId(bytes: []const u8) std.fmt.Formatter(formatId) { |
| 23 | 23 | } |
| 24 | 24 | |
| 25 | 25 | pub fn isValidId(bytes: []const u8) bool { |
| 26 | if (bytes.len == 0) return false; |
| 26 | 27 | if (mem.eql(u8, bytes, "_")) return false; |
| 27 | 28 | for (bytes) |c, i| { |
| 28 | 29 | switch (c) { |
| ... | ... | @@ -34,6 +35,14 @@ pub fn isValidId(bytes: []const u8) bool { |
| 34 | 35 | return std.zig.Token.getKeyword(bytes) == null; |
| 35 | 36 | } |
| 36 | 37 | |
| 38 | test "isValidId" { |
| 39 | try std.testing.expect(!isValidId("")); |
| 40 | try std.testing.expect(isValidId("foobar")); |
| 41 | try std.testing.expect(!isValidId("a b c")); |
| 42 | try std.testing.expect(!isValidId("3d")); |
| 43 | try std.testing.expect(!isValidId("enum")); |
| 44 | } |
| 45 | |
| 37 | 46 | /// Print the string as escaped contents of a double quoted or single-quoted string. |
| 38 | 47 | /// Format `{}` treats contents as a double-quoted string. |
| 39 | 48 | /// Format `{'}` treats contents as a single-quoted string. |