From d9c1a18776a9e692083b590a5d04b30efd9a4c03 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Sun, 29 Jan 2023 09:54:47 +0200 Subject: [bun add] fix more corner cases (#1930) --- src/string_immutable.zig | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) (limited to 'src/string_immutable.zig') 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 { -- cgit v1.2.3