authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2022-03-09 16:38:47-08:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-03-09 19:38:47-05:00
log017d3864de1f337d01726e87534a4e2093c7265f
tree52276b6096f00a53b7861bbb842d69d55fd0e0d4
parentf32a77b30df48552897a5c9023c39d8ba6610821
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

std: fix false positive for `zig.isValidId` with empty string (#11104)

* std: fix false positive for `zig.isValidId` with empty string, fixes #11099 * std: add zig.isValidId tests

1 files changed, 9 insertions(+), 0 deletions(-)

lib/std/zig/fmt.zig+9
...@@ -23,6 +23,7 @@ pub fn fmtId(bytes: []const u8) std.fmt.Formatter(formatId) {...@@ -23,6 +23,7 @@ pub fn fmtId(bytes: []const u8) std.fmt.Formatter(formatId) {
23}23}
2424
25pub fn isValidId(bytes: []const u8) bool {25pub fn isValidId(bytes: []const u8) bool {
26 if (bytes.len == 0) return false;
26 if (mem.eql(u8, bytes, "_")) return false;27 if (mem.eql(u8, bytes, "_")) return false;
27 for (bytes) |c, i| {28 for (bytes) |c, i| {
28 switch (c) {29 switch (c) {
...@@ -34,6 +35,14 @@ pub fn isValidId(bytes: []const u8) bool {...@@ -34,6 +35,14 @@ pub fn isValidId(bytes: []const u8) bool {
34 return std.zig.Token.getKeyword(bytes) == null;35 return std.zig.Token.getKeyword(bytes) == null;
35}36}
3637
38test "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/// Print the string as escaped contents of a double quoted or single-quoted string.46/// Print the string as escaped contents of a double quoted or single-quoted string.
38/// Format `{}` treats contents as a double-quoted string.47/// Format `{}` treats contents as a double-quoted string.
39/// Format `{'}` treats contents as a single-quoted string.48/// Format `{'}` treats contents as a single-quoted string.