diff options
author | 2021-12-18 21:07:07 -0800 | |
---|---|---|
committer | 2021-12-18 21:07:07 -0800 | |
commit | 0cee57f1d997fe21e519d5e771df0877ab489d5f (patch) | |
tree | 417d044ebbc47cc9b6ef49213620c07ae2927e0a /src/string_immutable.zig | |
parent | d1783babd99ff2a8020765837b3b9b3099137024 (diff) | |
parent | eab99b3bae9a810d76e6eb16afd9fb32cd7672bd (diff) | |
download | bun-0cee57f1d997fe21e519d5e771df0877ab489d5f.tar.gz bun-0cee57f1d997fe21e519d5e771df0877ab489d5f.tar.zst bun-0cee57f1d997fe21e519d5e771df0877ab489d5f.zip |
Merge pull request #80 from Jarred-Sumner/jarred/npm-install
bun install
Diffstat (limited to 'src/string_immutable.zig')
-rw-r--r-- | src/string_immutable.zig | 66 |
1 files changed, 65 insertions, 1 deletions
diff --git a/src/string_immutable.zig b/src/string_immutable.zig index cb33e9d59..2f14d7e28 100644 --- a/src/string_immutable.zig +++ b/src/string_immutable.zig @@ -91,6 +91,14 @@ pub const StringOrTinyString = struct { // allocator.free(slice_); } + pub fn initAppendIfNeeded(stringy: string, comptime Appender: type, appendy: Appender) !StringOrTinyString { + if (stringy.len <= StringOrTinyString.Max) { + return StringOrTinyString.init(stringy); + } + + return StringOrTinyString.init(try appendy.append(string, stringy)); + } + pub fn init(stringy: string) StringOrTinyString { switch (stringy.len) { 0 => { @@ -102,7 +110,7 @@ pub const StringOrTinyString = struct { .is_tiny_string = 1, .remainder_len = @truncate(u7, stringy.len), }; - std.mem.copy(u8, &tiny.remainder_buf, stringy); + @memcpy(&tiny.remainder_buf, stringy.ptr, tiny.remainder_len); return tiny; }, else => { @@ -233,6 +241,7 @@ test "StringOrTinyString Lowercase" { try std.testing.expectEqualStrings("hello!!!!!", str.slice()); } +/// startsWith except it checks for non-empty strings pub fn hasPrefix(self: string, str: string) bool { return str.len > 0 and startsWith(self, str); } @@ -257,6 +266,10 @@ pub inline fn endsWith(self: string, str: string) bool { return str.len == 0 or @call(.{ .modifier = .always_inline }, std.mem.endsWith, .{ u8, self, str }); } +pub inline fn endsWithComptime(self: string, comptime str: anytype) bool { + return self.len >= str.len and eqlComptimeIgnoreLen(self[self.len - str.len .. self.len], comptime str); +} + pub inline fn startsWithChar(self: string, char: u8) bool { return self.len > 0 and self[0] == char; } @@ -400,6 +413,57 @@ inline fn eqlComptimeCheckLen(a: string, comptime b: anytype, comptime check_len return true; } +pub fn eqlLong(a_: string, b: string, comptime check_len: bool) bool { + if (comptime check_len) { + if (a_.len == 0) { + return b.len == 0; + } + + if (a_.len != b.len) { + return false; + } + } + + const len = b.len; + var dword_length = b.len >> 3; + var b_ptr: usize = 0; + const a = a_.ptr; + + while (dword_length > 0) : (dword_length -= 1) { + const slice = b.ptr; + if (@bitCast(usize, a[b_ptr..len][0..@sizeOf(usize)].*) != @bitCast(usize, (slice[b_ptr..b.len])[0..@sizeOf(usize)].*)) + return false; + b_ptr += @sizeOf(usize); + if (b_ptr == b.len) return true; + } + + if (comptime @sizeOf(usize) == 8) { + if ((len & 4) != 0) { + const slice = b.ptr; + if (@bitCast(u32, a[b_ptr..len][0..@sizeOf(u32)].*) != @bitCast(u32, (slice[b_ptr..b.len])[0..@sizeOf(u32)].*)) + return false; + + b_ptr += @sizeOf(u32); + + if (b_ptr == b.len) return true; + } + } + + if ((len & 2) != 0) { + const slice = b.ptr; + if (@bitCast(u16, a[b_ptr..len][0..@sizeOf(u16)].*) != @bitCast(u16, b.ptr[b_ptr..len][0..@sizeOf(u16)].*)) + return false; + + b_ptr += @sizeOf(u16); + + if (b_ptr == b.len) return true; + } + + if (((len & 1) != 0) and a[b_ptr] != b[b_ptr]) return false; + + return true; +} + pub inline fn append(allocator: *std.mem.Allocator, self: string, other: string) !string { return std.fmt.allocPrint(allocator, "{s}{s}", .{ self, other }); } |