diff options
author | 2023-01-29 09:54:47 +0200 | |
---|---|---|
committer | 2023-01-28 23:54:47 -0800 | |
commit | d9c1a18776a9e692083b590a5d04b30efd9a4c03 (patch) | |
tree | 026899cd7e17a67b569fde2ec8c5a42e7a054db4 /src/string_immutable.zig | |
parent | f087388ebc6314c2852d553f4f4ea3074369dfbe (diff) | |
download | bun-d9c1a18776a9e692083b590a5d04b30efd9a4c03.tar.gz bun-d9c1a18776a9e692083b590a5d04b30efd9a4c03.tar.zst bun-d9c1a18776a9e692083b590a5d04b30efd9a4c03.zip |
[bun add] fix more corner cases (#1930)
Diffstat (limited to 'src/string_immutable.zig')
-rw-r--r-- | src/string_immutable.zig | 32 |
1 files changed, 13 insertions, 19 deletions
diff --git a/src/string_immutable.zig b/src/string_immutable.zig index b02cf1249..a69802a1b 100644 --- a/src/string_immutable.zig +++ b/src/string_immutable.zig @@ -91,34 +91,28 @@ pub inline fn containsAny(in: anytype, target: string) bool { /// a folder name. Therefore, the name can't contain any non-URL-safe /// characters. pub inline fn isNPMPackageName(target: string) bool { - if (target.len >= 215) return false; - switch (target[0]) { - 'a'...'z', - '0'...'9', - '$', - '@', - '-', - => {}, - else => return false, - } - if (target.len == 1) return true; + if (target.len == 0) return false; + if (target.len > 214) return false; - var slash_count: usize = 0; - - for (target[1..]) |c| { + const scoped = switch (target[0]) { + 'a'...'z', '0'...'9', '$', '-' => false, + '@' => true, + else => return false, + }; + var slash_index: usize = 0; + for (target[1..]) |c, i| { switch (c) { 'A'...'Z', 'a'...'z', '0'...'9', '$', '-', '_', '.' => {}, '/' => { - if (slash_count > 0) { - return false; - } - slash_count += 1; + if (!scoped) return false; + if (slash_index > 0) return false; + slash_index = i + 1; }, else => return false, } } - return true; + return !scoped or slash_index > 0 and slash_index + 1 < target.len; } pub inline fn indexAny(in: anytype, target: string) ?usize { |