forked from oven-sh/bun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring_types.zig
309 lines (259 loc) · 9.77 KB
/
string_types.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
const std = @import("std");
const BabyList = @import("./baby_list.zig").BabyList;
pub const string = []const u8;
pub const stringZ = [:0]const u8;
pub const stringMutable = []u8;
pub const CodePoint = i32;
const bun = @import("root").bun;
// macOS sets file path limit to 1024
// Since a pointer on x64 is 64 bits and only 46 bits are used
// We can safely store the entire path slice in a single u64.
pub const PathString = packed struct {
const PathIntLen = std.math.IntFittingRange(0, bun.MAX_PATH_BYTES);
pub const use_small_path_string = @bitSizeOf(usize) - @bitSizeOf(PathIntLen) >= 53;
pub const PathInt = if (use_small_path_string) PathIntLen else usize;
pub const PointerIntType = if (use_small_path_string) u53 else usize;
ptr: PointerIntType = 0,
len: PathInt = 0,
const JSC = bun.JSC;
pub fn estimatedSize(this: *const PathString) usize {
return @as(usize, this.len);
}
pub inline fn slice(this: anytype) string {
@setRuntimeSafety(false); // "cast causes pointer to be null" is fine here. if it is null, the len will be 0.
return @as([*]u8, @ptrFromInt(@as(usize, @intCast(this.ptr))))[0..this.len];
}
pub inline fn sliceAssumeZ(this: anytype) stringZ {
@setRuntimeSafety(false); // "cast causes pointer to be null" is fine here. if it is null, the len will be 0.
return @as([*:0]u8, @ptrFromInt(@as(usize, @intCast(this.ptr))))[0..this.len :0];
}
pub inline fn init(str: string) @This() {
@setRuntimeSafety(false); // "cast causes pointer to be null" is fine here. if it is null, the len will be 0.
return .{
.ptr = @as(PointerIntType, @truncate(@intFromPtr(str.ptr))),
.len = @as(PathInt, @truncate(str.len)),
};
}
pub inline fn isEmpty(this: anytype) bool {
return this.len == 0;
}
pub fn format(self: PathString, comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void {
try writer.writeAll(self.slice());
}
pub const empty = @This(){ .ptr = 0, .len = 0 };
comptime {
if (!bun.Environment.isWasm) {
if (use_small_path_string and @bitSizeOf(@This()) != 64) {
@compileError("PathString must be 64 bits");
} else if (!use_small_path_string and @bitSizeOf(@This()) != 128) {
@compileError("PathString must be 128 bits");
}
}
}
};
pub const HashedString = struct {
ptr: [*]const u8,
len: u32,
hash: u32,
pub const empty = HashedString{ .ptr = @as([*]const u8, @ptrFromInt(0xDEADBEEF)), .len = 0, .hash = 0 };
pub fn init(buf: string) HashedString {
return HashedString{
.ptr = buf.ptr,
.len = @as(u32, @truncate(buf.len)),
.hash = @as(u32, @truncate(bun.hash(buf))),
};
}
pub fn initNoHash(buf: string) HashedString {
return HashedString{
.ptr = buf.ptr,
.len = @as(u32, @truncate(buf.len)),
.hash = 0,
};
}
pub fn eql(this: HashedString, other: anytype) bool {
return Eql(this, @TypeOf(other), other);
}
pub fn Eql(this: HashedString, comptime Other: type, other: Other) bool {
switch (comptime Other) {
HashedString, *HashedString, *const HashedString => {
return ((@max(this.hash, other.hash) > 0 and this.hash == other.hash) or (this.ptr == other.ptr)) and this.len == other.len;
},
else => {
return @as(usize, this.len) == other.len and @as(u32, @truncate(bun.hash(other[0..other.len]))) == this.hash;
},
}
}
pub fn str(this: HashedString) string {
return this.ptr[0..this.len];
}
};
/// This is a string type that stores up to 15 bytes inline on the stack, and heap allocates if it is longer
pub const SmolStr = packed struct {
__len: u32,
cap: u32,
__ptr: [*]u8,
const Tag: usize = 0x8000000000000000;
const NegatedTag: usize = ~Tag;
pub fn jsonStringify(self: *const SmolStr, writer: anytype) !void {
try writer.write(self.slice());
}
pub const Inlined = packed struct {
data: u120,
__len: u7,
_tag: u1,
pub fn len(this: Inlined) u8 {
return @intCast(this.__len);
}
pub fn setLen(this: *Inlined, new_len: u7) void {
this.__len = new_len;
}
pub fn slice(this: *Inlined) []const u8 {
return this.allChars()[0..this.__len];
}
pub fn allChars(this: *Inlined) *[15]u8 {
return @as([*]u8, @ptrCast(@as(*u128, @ptrCast(this))))[0..15];
}
};
comptime {
bun.assert(@sizeOf(SmolStr) == @sizeOf(Inlined));
}
pub fn empty() SmolStr {
const inlined = Inlined{
.data = 0,
.__len = 0,
._tag = 1,
};
return SmolStr.fromInlined(inlined);
}
pub fn len(this: *const SmolStr) u32 {
if (this.isInlined()) {
return @intCast((@intFromPtr(this.__ptr) >> 56) & 0b01111111);
}
return this.__len;
}
pub fn ptr(this: *SmolStr) [*]u8 {
return @ptrFromInt(@as(usize, @intFromPtr(this.__ptr)) & NegatedTag);
}
pub fn ptrConst(this: *const SmolStr) [*]const u8 {
return @ptrFromInt(@as(usize, @intFromPtr(this.__ptr)) & NegatedTag);
}
pub fn markInlined(this: *SmolStr) void {
this.__ptr = @ptrFromInt(@as(usize, @intFromPtr(this.__ptr)) | Tag);
}
pub fn markHeap(this: *SmolStr) void {
this.__ptr = @ptrFromInt(@as(usize, @intFromPtr(this.__ptr)) & NegatedTag);
}
pub fn isInlined(this: *const SmolStr) bool {
return @as(usize, @intFromPtr(this.__ptr)) & Tag != 0;
}
pub fn toInlined(this: *const SmolStr) Inlined {
var inlined: Inlined = @bitCast(@as(u128, @bitCast(this.*)));
inlined._tag = 1;
return inlined;
}
pub fn fromBabyList(baby_list: BabyList(u8)) SmolStr {
var smol_str: SmolStr = .{
.__len = baby_list.len,
.cap = baby_list.cap,
.__ptr = baby_list.ptr,
};
smol_str.markHeap();
return smol_str;
}
pub fn fromInlined(inlined: Inlined) SmolStr {
var smol_str: SmolStr = @bitCast(inlined);
smol_str.markInlined();
return smol_str;
}
pub fn fromChar(char: u8) SmolStr {
var inlined = Inlined{
.data = 0,
.__len = 1,
._tag = 1,
};
inlined.allChars()[0] = char;
inlined.setLen(1);
return SmolStr.fromInlined(inlined);
}
pub fn fromSlice(allocator: std.mem.Allocator, values: []const u8) !SmolStr {
if (values.len > 15) {
var baby_list = try BabyList(u8).initCapacity(allocator, values.len);
baby_list.appendSliceAssumeCapacity(values);
return SmolStr.fromBabyList(baby_list);
}
var inlined = Inlined{
.data = 0,
.__len = 0,
._tag = 1,
};
if (values.len > 0) {
@memcpy(inlined.allChars()[0..values.len], values[0..values.len]);
inlined.setLen(@intCast(values.len));
}
return SmolStr.fromInlined(inlined);
}
pub fn slice(this: *const SmolStr) []const u8 {
if (this.isInlined()) {
const bytes: [*]const u8 = @ptrCast(this);
return bytes[0..this.len()];
}
return this.ptrConst()[0..this.__len];
}
pub fn appendChar(this: *SmolStr, allocator: std.mem.Allocator, char: u8) !void {
if (this.isInlined()) {
var inlined = this.toInlined();
if (inlined.len() + 1 > 15) {
var baby_list = try BabyList(u8).initCapacity(allocator, inlined.len() + 1);
baby_list.appendSliceAssumeCapacity(inlined.slice());
try baby_list.push(allocator, char);
// this.* = SmolStr.fromBabyList(baby_list);
this.__len = baby_list.len;
this.__ptr = baby_list.ptr;
this.cap = baby_list.cap;
this.markHeap();
return;
}
inlined.allChars()[inlined.len()] = char;
inlined.setLen(@intCast(inlined.len() + 1));
// this.* = SmolStr.fromInlined(inlined);
this.* = @bitCast(inlined);
this.markInlined();
return;
}
var baby_list = BabyList(u8){
.ptr = this.ptr(),
.len = this.__len,
.cap = this.cap,
};
try baby_list.push(allocator, char);
// this.* = SmolStr.fromBabyList(baby_list);
this.__len = baby_list.len;
this.__ptr = baby_list.ptr;
this.cap = baby_list.cap;
return;
}
pub fn appendSlice(this: *SmolStr, allocator: std.mem.Allocator, values: []const u8) !void {
if (this.isInlined()) {
var inlined = this.toInlined();
if (inlined.len() + values.len > 15) {
var baby_list = try BabyList(u8).initCapacity(allocator, inlined.len() + values.len);
baby_list.appendSliceAssumeCapacity(inlined.slice());
baby_list.appendSliceAssumeCapacity(values);
this.* = SmolStr.fromBabyList(baby_list);
return;
}
@memcpy(inlined.allChars()[inlined.len() .. inlined.len() + values.len], values);
inlined.setLen(@intCast(inlined.len() + values.len));
this.* = SmolStr.fromInlined(inlined);
return;
}
var baby_list = BabyList(u8){
.ptr = this.ptr(),
.len = this.__len,
.cap = this.cap,
};
try baby_list.append(allocator, values);
this.* = SmolStr.fromBabyList(baby_list);
return;
}
};