diff options
author | 2023-01-10 15:35:20 +0200 | |
---|---|---|
committer | 2023-01-10 05:35:20 -0800 | |
commit | 270b07e85e82b46491cc713fb2a5bb973d3de4f6 (patch) | |
tree | b8071eaf835f2aefb8ee95c68f9002d84b7b5761 | |
parent | b458abedbb865461005757cac5ea2b458249ef25 (diff) | |
download | bun-270b07e85e82b46491cc713fb2a5bb973d3de4f6.tar.gz bun-270b07e85e82b46491cc713fb2a5bb973d3de4f6.tar.zst bun-270b07e85e82b46491cc713fb2a5bb973d3de4f6.zip |
use `strings.hasPrefixComptime()` (#1755)
-rw-r--r-- | src/bun.js/bindings/bindings.zig | 2 | ||||
-rw-r--r-- | src/bun.js/javascript.zig | 4 | ||||
-rw-r--r-- | src/bun.js/module_loader.zig | 6 | ||||
-rw-r--r-- | src/cli/run_command.zig | 118 | ||||
-rw-r--r-- | src/http.zig | 46 | ||||
-rw-r--r-- | src/http/websocket_http_client.zig | 4 | ||||
-rw-r--r-- | src/install/dependency.zig | 4 | ||||
-rw-r--r-- | src/js_ast.zig | 2 | ||||
-rw-r--r-- | src/linker.zig | 2 | ||||
-rw-r--r-- | src/mdx/mdx_parser.zig | 5 | ||||
-rw-r--r-- | src/resolver/resolve_path.zig | 2 | ||||
-rw-r--r-- | src/resolver/resolver.zig | 2 | ||||
-rw-r--r-- | src/router.zig | 4 | ||||
-rw-r--r-- | src/sourcemap/sourcemap.zig | 6 |
14 files changed, 92 insertions, 115 deletions
diff --git a/src/bun.js/bindings/bindings.zig b/src/bun.js/bindings/bindings.zig index 1afb5a588..41a52956d 100644 --- a/src/bun.js/bindings/bindings.zig +++ b/src/bun.js/bindings/bindings.zig @@ -1506,7 +1506,7 @@ pub const JSPromise = extern struct { ) JSValue { if (value.isEmpty()) { return resolvedPromiseValue(globalObject, JSValue.jsUndefined()); - } else if (value.isEmptyOrUndefinedOrNull() or !value.isCell()) { + } else if (value.isUndefinedOrNull() or !value.isCell()) { return resolvedPromiseValue(globalObject, value); } diff --git a/src/bun.js/javascript.zig b/src/bun.js/javascript.zig index 9ba56fffa..fd2ded108 100644 --- a/src/bun.js/javascript.zig +++ b/src/bun.js/javascript.zig @@ -910,11 +910,11 @@ pub const VirtualMachine = struct { ret.result = null; ret.path = jsc_vm.entry_point.source.path.text; return; - } else if (specifier.len > js_ast.Macro.namespaceWithColon.len and strings.eqlComptimeIgnoreLen(specifier[0..js_ast.Macro.namespaceWithColon.len], js_ast.Macro.namespaceWithColon)) { + } else if (strings.hasPrefixComptime(specifier, js_ast.Macro.namespaceWithColon)) { ret.result = null; ret.path = specifier; return; - } else if (specifier.len > "/bun-vfs/node_modules/".len and strings.eqlComptimeIgnoreLen(specifier[0.."/bun-vfs/node_modules/".len], "/bun-vfs/node_modules/")) { + } else if (strings.hasPrefixComptime(specifier, "/bun-vfs/node_modules/")) { ret.result = null; ret.path = specifier; return; diff --git a/src/bun.js/module_loader.zig b/src/bun.js/module_loader.zig index 46b7d0c09..c4dea6fc4 100644 --- a/src/bun.js/module_loader.zig +++ b/src/bun.js/module_loader.zig @@ -940,7 +940,7 @@ pub const ModuleLoader = struct { } // this should be a cheap lookup because 24 bytes == 8 * 3 so it's read 3 machine words - const is_node_override = specifier.len > "/bun-vfs/node_modules/".len and strings.eqlComptimeIgnoreLen(specifier[0.."/bun-vfs/node_modules/".len], "/bun-vfs/node_modules/"); + const is_node_override = strings.hasPrefixComptime(specifier, "/bun-vfs/node_modules/"); const macro_remappings = if (jsc_vm.macro_mode or !jsc_vm.has_any_macro_remappings or is_node_override) MacroRemap{} @@ -1910,9 +1910,7 @@ pub const ModuleLoader = struct { }; }, } - } else if (specifier.len > js_ast.Macro.namespaceWithColon.len and - strings.eqlComptimeIgnoreLen(specifier[0..js_ast.Macro.namespaceWithColon.len], js_ast.Macro.namespaceWithColon)) - { + } else if (strings.hasPrefixComptime(specifier, js_ast.Macro.namespaceWithColon)) { if (jsc_vm.macro_entry_points.get(MacroEntryPoint.generateIDFromSpecifier(specifier))) |entry| { return ResolvedSource{ .allocator = null, diff --git a/src/cli/run_command.zig b/src/cli/run_command.zig index 8f0919d90..97a0cb51a 100644 --- a/src/cli/run_command.zig +++ b/src/cli/run_command.zig @@ -106,44 +106,42 @@ pub const RunCommand = struct { switch (script[entry_i]) { 'y' => { if (delimiter > 0) { - if (entry_i + "arn".len < script.len) { - var remainder = script[start..]; - if (remainder.len > "yarn ".len and strings.eqlComptimeIgnoreLen(remainder[0.."yarn ".len], "yarn ")) { - const next = remainder["yarn ".len..]; - // We have yarn - // Find the next space - if (strings.indexOfChar(next, ' ')) |space| { - const yarn_cmd = next[0..space]; - if (strings.eqlComptime(yarn_cmd, "run")) { - try copy_script.appendSlice(BUN_RUN); - entry_i += "yarn run".len; - continue; - } + const remainder = script[start..]; + if (strings.hasPrefixComptime(remainder, "yarn ")) { + const next = remainder["yarn ".len..]; + // We have yarn + // Find the next space + if (strings.indexOfChar(next, ' ')) |space| { + const yarn_cmd = next[0..space]; + if (strings.eqlComptime(yarn_cmd, "run")) { + try copy_script.appendSlice(BUN_RUN); + entry_i += "yarn run".len; + continue; + } - // yarn npm is a yarn 2 subcommand - if (strings.eqlComptime(yarn_cmd, "npm")) { - entry_i += "yarn npm ".len; - try copy_script.appendSlice("yarn npm "); - continue; - } + // yarn npm is a yarn 2 subcommand + if (strings.eqlComptime(yarn_cmd, "npm")) { + entry_i += "yarn npm ".len; + try copy_script.appendSlice("yarn npm "); + continue; + } - if (strings.startsWith(yarn_cmd, "-")) { - // Skip the rest of the command - entry_i += "yarn ".len + yarn_cmd.len; - try copy_script.appendSlice("yarn "); - try copy_script.appendSlice(yarn_cmd); - continue; - } + if (strings.startsWith(yarn_cmd, "-")) { + // Skip the rest of the command + entry_i += "yarn ".len + yarn_cmd.len; + try copy_script.appendSlice("yarn "); + try copy_script.appendSlice(yarn_cmd); + continue; + } - // implicit yarn commands - if (std.mem.indexOfScalar(u64, yarn_commands, std.hash.Wyhash.hash(0, yarn_cmd)) == null) { - try copy_script.appendSlice(BUN_RUN); - try copy_script.append(' '); - try copy_script.appendSlice(yarn_cmd); - entry_i += "yarn ".len + yarn_cmd.len; - delimiter = 0; - continue; - } + // implicit yarn commands + if (std.mem.indexOfScalar(u64, yarn_commands, std.hash.Wyhash.hash(0, yarn_cmd)) == null) { + try copy_script.appendSlice(BUN_RUN); + try copy_script.append(' '); + try copy_script.appendSlice(yarn_cmd); + entry_i += "yarn ".len + yarn_cmd.len; + delimiter = 0; + continue; } } } @@ -165,32 +163,18 @@ pub const RunCommand = struct { 'n' => { if (delimiter > 0) { - const npm_i = entry_i + "pm run ".len; - if (npm_i < script.len) { - const base = script[start..]; - if (base.len > "npm run ".len) { - const remainder = base[0.."npm run ".len]; - if (strings.eqlComptimeIgnoreLen(remainder, "npm run ")) { - try copy_script.appendSlice(BUN_RUN ++ " "); - entry_i += remainder.len; - delimiter = 0; - continue; - } - } + if (strings.hasPrefixComptime(script[start..], "npm run ")) { + try copy_script.appendSlice(BUN_RUN ++ " "); + entry_i += "npm run ".len; + delimiter = 0; + continue; } - const npx_i = entry_i + "px ".len; - if (npx_i < script.len) { - const base = script[start..]; - if (base.len > "px ".len) { - const remainder = base[0.."px ".len]; - if (strings.eqlComptimeIgnoreLen(remainder, "px ")) { - try copy_script.appendSlice("bunx" ++ " "); - entry_i += remainder.len; - delimiter = 0; - continue; - } - } + if (strings.hasPrefixComptime(script[start..], "npx ")) { + try copy_script.appendSlice("bunx" ++ " "); + entry_i += "npx ".len; + delimiter = 0; + continue; } } @@ -198,15 +182,11 @@ pub const RunCommand = struct { }, 'p' => { if (delimiter > 0) { - const npm_i = entry_i + "npm run ".len; - if (npm_i < script.len) { - const remainder = script[start .. npm_i + 1]; - if (remainder.len > npm_i and strings.eqlComptimeIgnoreLen(remainder, "pnpm run") and remainder[remainder.len - 1] == delimiter) { - try copy_script.appendSlice(BUN_RUN ++ " "); - entry_i += remainder.len; - delimiter = 0; - continue; - } + if (strings.hasPrefixComptime(script[start..], "pnpm run ")) { + try copy_script.appendSlice(BUN_RUN ++ " "); + entry_i += "pnpm run ".len; + delimiter = 0; + continue; } } @@ -935,7 +915,7 @@ pub const RunCommand = struct { shebang = std.mem.trim(u8, shebang, " \r\n\t"); if (shebang.len == 0) break :possibly_open_with_bun_js; - if (shebang.len > 2 and strings.eqlComptimeIgnoreLen(shebang[0..2], "#!")) { + if (strings.hasPrefixComptime(shebang, "#!")) { const first_arg: string = if (std.os.argv.len > 0) bun.span(std.os.argv[0]) else ""; const filename = std.fs.path.basename(first_arg); // are we attempting to run the script with bun? diff --git a/src/http.zig b/src/http.zig index 588007142..0e11b6803 100644 --- a/src/http.zig +++ b/src/http.zig @@ -3085,37 +3085,37 @@ pub const RequestContext = struct { return true; } - if (ctx.url.path.len > "blob:".len) { - if (strings.eqlComptimeIgnoreLen(ctx.url.path[0.."blob:".len], "blob:")) { - try ctx.handleBlobURL(server); - return true; - } + if (strings.hasPrefixComptime(ctx.url.path, "blob:")) { + try ctx.handleBlobURL(server); + return true; + } - // From HTTP, we serve files with a hash modkey - // The format is - // hash:${hash}/${ORIGINAL_PATH} - // hash:abcdefg123/app/foo/my-file.jpeg - // The hash exists for browser cache invalidation - if (strings.eqlComptimeIgnoreLen(ctx.url.path[0.."hash:".len], "hash:")) { - var current = ctx.url.path; - current = current["hash:".len..]; - if (strings.indexOfChar(current, '/')) |i| { - current = current[i + 1 ..]; - ctx.url.path = current; - return false; - } + // From HTTP, we serve files with a hash modkey + // The format is + // hash:${hash}/${ORIGINAL_PATH} + // hash:abcdefg123/app/foo/my-file.jpeg + // The hash exists for browser cache invalidation + if (strings.hasPrefixComptime(ctx.url.path, "hash:")) { + var current = ctx.url.path; + current = current["hash:".len..]; + if (strings.indexOfChar(current, '/')) |i| { + current = current[i + 1 ..]; + ctx.url.path = current; + return false; } } - const isMaybePrefix = ctx.url.path.len > "bun:".len; - - if (isMaybePrefix and strings.eqlComptimeIgnoreLen(ctx.url.path[0.."bun:".len], "bun:")) { + if (strings.hasPrefixComptime(ctx.url.path, "bun:")) { try ctx.handleBunURL(server); return true; - } else if (isMaybePrefix and strings.eqlComptimeIgnoreLen(ctx.url.path[0.."src:".len], "src:")) { + } + + if (strings.hasPrefixComptime(ctx.url.path, "src:")) { try ctx.handleSrcURL(server); return true; - } else if (isMaybePrefix and strings.eqlComptimeIgnoreLen(ctx.url.path[0.."abs:".len], "abs:")) { + } + + if (strings.hasPrefixComptime(ctx.url.path, "abs:")) { try ctx.handleAbsURL(server); return true; } diff --git a/src/http/websocket_http_client.zig b/src/http/websocket_http_client.zig index 801e11007..77c64cf46 100644 --- a/src/http/websocket_http_client.zig +++ b/src/http/websocket_http_client.zig @@ -346,9 +346,9 @@ pub fn NewHTTPUpgradeClient(comptime ssl: bool) type { var body = this.getBody(); var remain = body[this.body_written..]; const is_first = this.body_written == 0; - if (is_first and data.len >= "HTTP/1.1 101 ".len) { + if (is_first) { // fail early if we receive a non-101 status code - if (!strings.eqlComptimeIgnoreLen(data[0.."HTTP/1.1 101 ".len], "HTTP/1.1 101 ")) { + if (!strings.hasPrefixComptime(data, "HTTP/1.1 101 ")) { this.terminate(ErrorCode.expected_101_status_code); return; } diff --git a/src/install/dependency.zig b/src/install/dependency.zig index 73eeb4264..e85d7cb85 100644 --- a/src/install/dependency.zig +++ b/src/install/dependency.zig @@ -311,7 +311,7 @@ pub const Version = struct { }, 'n' => { - if (dependency.len > 4 and strings.eqlComptimeIgnoreLen(dependency[0..4], "npm:")) { + if (strings.hasPrefixComptime(dependency, "npm:")) { return Tag.npm; } }, @@ -536,7 +536,7 @@ pub fn parse( if (dependency.len == 0) return null; const tag = Version.Tag.infer(dependency); - if (tag == .npm and dependency.len > 4 and strings.eqlComptimeIgnoreLen(dependency[0..4], "npm:")) { + if (tag == .npm and strings.hasPrefixComptime(dependency, "npm:")) { dependency = dependency[4..]; } diff --git a/src/js_ast.zig b/src/js_ast.zig index 691ff791a..e3732afa0 100644 --- a/src/js_ast.zig +++ b/src/js_ast.zig @@ -4873,7 +4873,7 @@ pub const Macro = struct { pub const namespaceWithColon: string = namespace ++ ":"; pub fn isMacroPath(str: string) bool { - return (str.len > namespaceWithColon.len and strings.eqlComptimeIgnoreLen(str[0..namespaceWithColon.len], namespaceWithColon)); + return strings.hasPrefixComptime(str, namespaceWithColon); } pub const MacroContext = struct { diff --git a/src/linker.zig b/src/linker.zig index b9ce293cd..3d53dc1b1 100644 --- a/src/linker.zig +++ b/src/linker.zig @@ -304,7 +304,7 @@ pub const Linker = struct { } } - if (import_record.path.text.len > 4 and strings.eqlComptimeIgnoreLen(import_record.path.text[0.."bun:".len], "bun:")) { + if (strings.hasPrefixComptime(import_record.path.text, "bun:")) { import_record.path = Fs.Path.init(import_record.path.text["bun:".len..]); import_record.path.namespace = "bun"; diff --git a/src/mdx/mdx_parser.zig b/src/mdx/mdx_parser.zig index b44ab239a..3b2064c39 100644 --- a/src/mdx/mdx_parser.zig +++ b/src/mdx/mdx_parser.zig @@ -1043,9 +1043,8 @@ pub const MDParser = struct { } // Scan for end of the line. - while (off + 3 < this.size and - !(strings.eqlComptimeIgnoreLen(this.source.contents.ptr[off..][0..4], "\n\n\n\n") or - strings.eqlComptimeIgnoreLen(this.source.contents.ptr[off..][0..4], "\r\n\r\n"))) + while (!(strings.hasPrefixComptime(this.source.contents.ptr[off..], "\n\n\n\n") or + strings.hasPrefixComptime(this.source.contents.ptr[off..], "\r\n\r\n"))) { off += 4; } diff --git a/src/resolver/resolve_path.zig b/src/resolver/resolve_path.zig index 605c16e6a..4db102f59 100644 --- a/src/resolver/resolve_path.zig +++ b/src/resolver/resolve_path.zig @@ -31,7 +31,7 @@ inline fn isDotSlash(slice: []const u8) bool { } inline fn @"is ../"(slice: []const u8) bool { - return slice.len >= 3 and strings.eqlComptimeIgnoreLen(slice[0..3], "../"); + return strings.hasPrefixComptime(slice, "../"); } // TODO: is it faster to determine longest_common_separator in the while loop diff --git a/src/resolver/resolver.zig b/src/resolver/resolver.zig index 773fe3805..c7035e9eb 100644 --- a/src/resolver/resolver.zig +++ b/src/resolver/resolver.zig @@ -1129,7 +1129,7 @@ pub const Resolver = struct { // "fs/*" // These are disabled! } else if (had_node_prefix or - (import_path_without_node_prefix.len >= 2 and strings.eqlComptimeIgnoreLen(import_path_without_node_prefix[0..2], "fs") and + (strings.hasPrefixComptime(import_path_without_node_prefix, "fs") and (import_path_without_node_prefix.len == 2 or import_path_without_node_prefix[3] == '/'))) { diff --git a/src/router.zig b/src/router.zig index 7f6aba82c..530824650 100644 --- a/src/router.zig +++ b/src/router.zig @@ -1336,7 +1336,7 @@ const Pattern = struct { i += 1; - if (!strings.eqlComptimeIgnoreLen(input[i..][0..3], "...")) return error.InvalidOptionalCatchAllRoute; + if (!strings.hasPrefixComptime(input[i..], "...")) return error.InvalidOptionalCatchAllRoute; i += 3; param.offset = i; }, @@ -1348,7 +1348,7 @@ const Pattern = struct { return error.InvalidCatchAllRoute; } - if (!strings.eqlComptimeIgnoreLen(input[i..][0..2], "..")) return error.InvalidCatchAllRoute; + if (!strings.hasPrefixComptime(input[i..], "..")) return error.InvalidCatchAllRoute; i += 2; param.offset = i; diff --git a/src/sourcemap/sourcemap.zig b/src/sourcemap/sourcemap.zig index 58c781762..592df5d1d 100644 --- a/src/sourcemap/sourcemap.zig +++ b/src/sourcemap/sourcemap.zig @@ -118,8 +118,8 @@ pub const Mapping = struct { if (remain[0] == ';') { generated.columns = 0; - while (remain.len > @sizeOf(usize) / 2 and strings.eqlComptimeIgnoreLen( - remain[0 .. @sizeOf(usize) / 2], + while (strings.hasPrefixComptime( + remain, comptime [_]u8{';'} ** (@sizeOf(usize) / 2), )) { generated.lines += (@sizeOf(usize) / 2); @@ -210,7 +210,7 @@ pub const Mapping = struct { remain = remain[source_index_delta.start..]; // // "AAAA" is extremely common - // if (remain.len > 5 and remain[4] == ';' and strings.eqlComptimeIgnoreLen(remain[0..4], "AAAA")) { + // if (strings.hasPrefixComptime(remain, "AAAA;")) { // } |