aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2023-02-25 18:22:29 -0800
committerGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2023-02-25 18:23:01 -0800
commit693be3d1c2fe63cb5961a0023ad4c301209495bc (patch)
treec56159b91e01438a416b5b10dc114f9f51fef61a /src
parent8ba65a4f0bbd38b5c6a677d93d1fa13d0042bb35 (diff)
downloadbun-693be3d1c2fe63cb5961a0023ad4c301209495bc.tar.gz
bun-693be3d1c2fe63cb5961a0023ad4c301209495bc.tar.zst
bun-693be3d1c2fe63cb5961a0023ad4c301209495bc.zip
Faster `eqlCaseInsensitiveASCII`
Diffstat (limited to 'src')
-rw-r--r--src/bun.js/api/bun.zig8
-rw-r--r--src/bun.js/bindings/bindings.zig6
-rw-r--r--src/c.zig2
-rw-r--r--src/string_immutable.zig34
4 files changed, 26 insertions, 24 deletions
diff --git a/src/bun.js/api/bun.zig b/src/bun.js/api/bun.zig
index 5e92a470d..1c991eece 100644
--- a/src/bun.js/api/bun.zig
+++ b/src/bun.js/api/bun.zig
@@ -1541,13 +1541,7 @@ pub const Crypto = struct {
}
pub fn byNameAndEngine(engine: *BoringSSL.ENGINE, name: []const u8) ?EVP {
-
- // none of the names are longer than 255
- var buf: [256]u8 = undefined;
- const len = @min(name.len, buf.len - 1);
- _ = strings.copyLowercase(name, &buf);
-
- if (Algorithm.map.get(buf[0..len])) |algorithm| {
+ if (Algorithm.map.getWithEql(name, strings.eqlCaseInsensitiveASCIIIgnoreLength)) |algorithm| {
if (algorithm == .blake2b256) {
return EVP.init(algorithm, BoringSSL.EVP_blake2b256(), engine);
}
diff --git a/src/bun.js/bindings/bindings.zig b/src/bun.js/bindings/bindings.zig
index 54d54f644..ebe5f83ed 100644
--- a/src/bun.js/bindings/bindings.zig
+++ b/src/bun.js/bindings/bindings.zig
@@ -295,14 +295,14 @@ pub const ZigString = extern struct {
}
pub fn eqlComptime(this: ZigString, comptime other: []const u8) bool {
- if (this.len != other.len)
- return false;
-
if (this.is16Bit()) {
return strings.eqlComptimeUTF16(this.utf16SliceAligned(), other);
}
if (comptime strings.isAllASCIISimple(other)) {
+ if (this.len != other.len)
+ return false;
+
return strings.eqlComptimeIgnoreLen(this.slice(), other);
}
diff --git a/src/c.zig b/src/c.zig
index 54be0878c..695ee6b34 100644
--- a/src/c.zig
+++ b/src/c.zig
@@ -441,3 +441,5 @@ pub fn dlsym(comptime Type: type, comptime name: [:0]const u8) ?Type {
// set in c-bindings.cpp
pub extern fn get_process_priority(pid: c_uint) i32;
pub extern fn set_process_priority(pid: c_uint, priority: c_int) i32;
+
+pub extern fn strncasecmp(s1: [*]const u8, s2: [*]const u8, n: usize) i32;
diff --git a/src/string_immutable.zig b/src/string_immutable.zig
index ea81de06c..e5a37006a 100644
--- a/src/string_immutable.zig
+++ b/src/string_immutable.zig
@@ -785,25 +785,31 @@ pub inline fn eqlComptimeCheckLenWithType(comptime Type: type, a: []const Type,
return eqlComptimeCheckLenWithKnownType(comptime Type, a, if (@typeInfo(@TypeOf(b)) != .Pointer) &b else b, comptime check_len);
}
-pub fn eqlCaseInsensitiveASCII(a: string, comptime b: anytype, comptime check_len: bool) bool {
+pub fn eqlCaseInsensitiveASCIIIgnoreLength(
+ a: string,
+ b: string,
+) bool {
+ return eqlCaseInsensitiveASCII(a, b, false);
+}
+
+pub fn eqlCaseInsensitiveASCIIICheckLength(
+ a: string,
+ b: string,
+) bool {
+ return eqlCaseInsensitiveASCII(a, b, true);
+}
+
+pub fn eqlCaseInsensitiveASCII(a: string, b: string, comptime check_len: bool) bool {
if (comptime check_len) {
- if (comptime b.len == 0) {
- return a.len == 0;
- }
+ if (a.len != b.len) return false;
- switch (a.len) {
- b.len => void{},
- else => return false,
- }
+ if (a.len == 0) return true;
}
- // pray to the auto vectorization gods
- inline for (b, 0..) |c, i| {
- const char = comptime std.ascii.toLower(c);
- if (char != std.ascii.toLower(a[i])) return false;
- }
+ std.debug.assert(b.len > 0);
+ std.debug.assert(a.len > 0);
- return true;
+ return bun.C.strncasecmp(a.ptr, b.ptr, a.len) == 0;
}
pub fn eqlLong(a_str: string, b_str: string, comptime check_len: bool) bool {