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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
|
const std = @import("std");
const bun = @import("root").bun;
const JSC = bun.JSC;
const JSValue = bun.JSC.JSValue;
const Parent = @This();
pub const BufferOwnership = enum {
BufferInternal,
BufferOwned,
BufferSubstring,
BufferExternal,
};
pub const WTFStringImpl = *WTFStringImplStruct;
pub const WTFStringImplStruct = extern struct {
m_refCount: u32 = 0,
m_length: u32 = 0,
m_ptr: extern union { latin1: [*]const u8, utf16: [*]const u16 },
m_hashAndFlags: u32 = 0,
// ---------------------------------------------------------------------
// These details must stay in sync with WTFStringImpl.h in WebKit!
// ---------------------------------------------------------------------
const s_flagCount: u32 = 8;
const s_flagMask: u32 = (1 << s_flagCount) - 1;
const s_flagStringKindCount: u32 = 4;
const s_hashZeroValue: u32 = 0;
const s_hashFlagStringKindIsAtom: u32 = @as(1, u32) << (s_flagStringKindCount);
const s_hashFlagStringKindIsSymbol: u32 = @as(1, u32) << (s_flagStringKindCount + 1);
const s_hashMaskStringKind: u32 = s_hashFlagStringKindIsAtom | s_hashFlagStringKindIsSymbol;
const s_hashFlagDidReportCost: u32 = @as(1, u32) << 3;
const s_hashFlag8BitBuffer: u32 = 1 << 2;
const s_hashMaskBufferOwnership: u32 = (1 << 0) | (1 << 1);
/// The bottom bit in the ref count indicates a static (immortal) string.
const s_refCountFlagIsStaticString = 0x1;
/// This allows us to ref / deref without disturbing the static string flag.
const s_refCountIncrement = 0x2;
// ---------------------------------------------------------------------
pub fn refCount(this: WTFStringImpl) u32 {
return this.m_refCount / s_refCountIncrement;
}
pub fn isStatic(this: WTFStringImpl) bool {
return this.m_refCount & s_refCountIncrement != 0;
}
pub fn byteLength(this: WTFStringImpl) usize {
return if (this.is8Bit()) this.m_length else this.m_length * 2;
}
pub fn byteSlice(this: WTFStringImpl) []const u8 {
return this.m_ptr.latin1[0..this.byteLength()];
}
pub inline fn is8Bit(self: WTFStringImpl) bool {
return (self.m_hashAndFlags & s_hashFlag8BitBuffer) != 0;
}
pub inline fn length(self: WTFStringImpl) u32 {
return self.m_length;
}
pub inline fn utf16Slice(self: WTFStringImpl) []const u16 {
std.debug.assert(!is8Bit(self));
return self.m_ptr.utf16[0..length(self)];
}
pub inline fn latin1Slice(self: WTFStringImpl) []const u8 {
std.debug.assert(is8Bit(self));
return self.m_ptr.latin1[0..length(self)];
}
/// Caller must ensure that the string is 8-bit and ASCII.
pub inline fn utf8Slice(self: WTFStringImpl) []const u8 {
if (comptime bun.Environment.allow_assert)
std.debug.assert(canUseAsUTF8(self));
return self.m_ptr.latin1[0..length(self)];
}
pub fn toZigString(this: WTFStringImpl) ZigString {
if (this.is8Bit()) {
return ZigString.init(this.latin1Slice());
} else {
return ZigString.init16(this.utf16Slice());
}
}
pub inline fn deref(self: WTFStringImpl) void {
JSC.markBinding(@src());
const current_count = self.refCount();
std.debug.assert(current_count > 0);
Bun__WTFStringImpl__deref(self);
if (comptime bun.Environment.allow_assert) {
if (current_count > 1) {
std.debug.assert(self.refCount() < current_count or self.isStatic());
}
}
}
pub inline fn ref(self: WTFStringImpl) void {
JSC.markBinding(@src());
const current_count = self.refCount();
std.debug.assert(current_count > 0);
Bun__WTFStringImpl__ref(self);
std.debug.assert(self.refCount() > current_count or self.isStatic());
}
pub fn toUTF8(this: WTFStringImpl, allocator: std.mem.Allocator) ZigString.Slice {
if (this.is8Bit()) {
if (bun.strings.toUTF8FromLatin1(allocator, this.latin1Slice()) catch null) |utf8| {
return ZigString.Slice.init(allocator, utf8.items);
}
this.ref();
return ZigString.Slice.init(this.refCountAllocator(), this.latin1Slice());
}
if (bun.strings.toUTF8Alloc(allocator, this.utf16Slice()) catch null) |utf8| {
return ZigString.Slice.init(allocator, utf8);
}
return .{};
}
pub fn toUTF8IfNeeded(this: WTFStringImpl, allocator: std.mem.Allocator) ?ZigString.Slice {
if (this.is8Bit()) {
if (bun.strings.toUTF8FromLatin1(allocator, this.latin1Slice()) catch null) |utf8| {
return ZigString.Slice.init(allocator, utf8.items);
}
return null;
}
if (bun.strings.toUTF8Alloc(allocator, this.utf16Slice()) catch null) |utf8| {
return ZigString.Slice.init(allocator, utf8);
}
return null;
}
/// Avoid using this in code paths that are about to get the string as a UTF-8
/// In that case, use toUTF8IfNeeded instead.
pub fn canUseAsUTF8(this: WTFStringImpl) bool {
return this.is8Bit() and bun.strings.isAllASCII(this.latin1Slice());
}
pub fn utf8ByteLength(this: WTFStringImpl) usize {
if (this.is8Bit()) {
const input = this.latin1Slice();
return if (input.len > 0) JSC.WebCore.Encoder.byteLengthU8(input.ptr, input.len, .utf8) else 0;
} else {
const input = this.utf16Slice();
return if (input.len > 0) JSC.WebCore.Encoder.byteLengthU16(input.ptr, input.len, .utf8) else 0;
}
}
pub fn refCountAllocator(self: WTFStringImpl) std.mem.Allocator {
return std.mem.Allocator{ .ptr = self, .vtable = StringImplAllocator.VTablePtr };
}
extern fn Bun__WTFStringImpl__deref(self: WTFStringImpl) void;
extern fn Bun__WTFStringImpl__ref(self: WTFStringImpl) void;
};
pub const StringImplAllocator = struct {
fn alloc(ptr: *anyopaque, len: usize, _: u8, _: usize) ?[*]u8 {
var this = bun.cast(WTFStringImpl, ptr);
const len_ = this.byteLength();
if (len_ != len) {
// we don't actually allocate, we just reference count
return null;
}
this.ref();
// we should never actually allocate
return @constCast(this.m_ptr.latin1);
}
fn resize(_: *anyopaque, _: []u8, _: u8, _: usize, _: usize) bool {
return false;
}
pub fn free(
ptr: *anyopaque,
buf: []u8,
_: u8,
_: usize,
) void {
var this = bun.cast(WTFStringImpl, ptr);
std.debug.assert(this.byteSlice().ptr == buf.ptr);
std.debug.assert(this.byteSlice().len == buf.len);
this.deref();
}
pub const VTable = std.mem.Allocator.VTable{
.alloc = &alloc,
.resize = &resize,
.free = &free,
};
pub const VTablePtr = &VTable;
};
pub const Tag = enum(u8) {
Dead = 0,
WTFStringImpl = 1,
ZigString = 2,
StaticZigString = 3,
Empty = 4,
};
const ZigString = bun.JSC.ZigString;
pub const StringImpl = extern union {
ZigString: ZigString,
WTFStringImpl: WTFStringImpl,
StaticZigString: ZigString,
Dead: void,
Empty: void,
};
/// Prefer using String instead of ZigString in new code.
pub const String = extern struct {
pub const name = "BunString";
tag: Tag,
value: StringImpl,
pub const empty = String{ .tag = .Empty, .value = .{ .Empty = {} } };
pub const dead = String{ .tag = .Dead, .value = .{ .Dead = {} } };
pub const StringImplAllocator = Parent.StringImplAllocator;
extern fn BunString__fromLatin1(bytes: [*]const u8, len: usize) String;
extern fn BunString__fromBytes(bytes: [*]const u8, len: usize) String;
pub fn createLatin1(bytes: []const u8) String {
JSC.markBinding(@src());
return BunString__fromLatin1(bytes.ptr, bytes.len);
}
pub fn create(bytes: []const u8) String {
JSC.markBinding(@src());
return BunString__fromBytes(bytes.ptr, bytes.len);
}
pub fn isEmpty(this: String) bool {
return this.tag == .Empty or this.length() == 0;
}
pub fn dupeRef(this: String) String {
this.ref();
return this;
}
pub fn initWithType(comptime Type: type, value: Type) String {
switch (comptime Type) {
ZigString => return String{ .tag = .ZigString, .value = .{ .ZigString = value } },
[:0]u8, []u8, [:0]const u8, []const u8 => return String{ .tag = .ZigString, .value = .{ .ZigString = ZigString.fromBytes(value) } },
[:0]u16, []u16, [:0]const u16, []const u16 => return String{ .tag = .ZigString, .value = .{ .ZigString = ZigString.from16Slice(value) } },
WTFStringImpl => return String{ .tag = .WTFStringImpl, .value = .{ .WTFStringImpl = value } },
*const ZigString, *ZigString => return String{ .tag = .ZigString, .value = .{ .ZigString = value.* } },
*const [0:0]u8 => return String{ .tag = .Empty, .value = .{ .Empty = {} } },
String => return value,
else => {
if (comptime std.meta.trait.isZigString(Type)) {
return static(value);
}
@compileError("Unsupported type for String " ++ @typeName(Type));
},
}
}
pub fn toErrorInstance(this: String, globalObject: *JSC.JSGlobalObject) JSC.JSValue {
return this.toZigString().toErrorInstance(globalObject);
}
pub fn static(input: []const u8) String {
return .{
.tag = .StaticZigString,
.value = .{ .StaticZigString = ZigString.init(input) },
};
}
pub fn init(value: anytype) String {
return initWithType(@TypeOf(value), value);
}
extern fn BunString__createExternal(
bytes: [*]const u8,
len: usize,
isLatin1: bool,
ptr: ?*anyopaque,
callback: ?*const fn (*anyopaque, *anyopaque, u32) callconv(.C) void,
) String;
pub fn createExternal(bytes: []const u8, isLatin1: bool, ctx: ?*anyopaque, callback: ?*const fn (*anyopaque, *anyopaque, u32) callconv(.C) void) String {
JSC.markBinding(@src());
return BunString__createExternal(bytes.ptr, bytes.len, isLatin1, ctx, callback);
}
pub fn fromUTF8(value: []const u8) String {
return String.initWithType(ZigString, ZigString.initUTF8(value));
}
pub fn fromBytes(value: []const u8) String {
return String.initWithType(ZigString, ZigString.fromBytes(value));
}
pub fn format(self: String, comptime fmt: []const u8, opts: std.fmt.FormatOptions, writer: anytype) !void {
try self.toZigString().format(fmt, opts, writer);
}
pub fn fromJS(value: bun.JSC.JSValue, globalObject: *JSC.JSGlobalObject) String {
JSC.markBinding(@src());
var out: String = String.dead;
if (BunString__fromJS(globalObject, value, &out)) {
return out;
} else {
return String.dead;
}
}
pub fn tryFromJS(value: bun.JSC.JSValue, globalObject: *JSC.JSGlobalObject) ?String {
JSC.markBinding(@src());
var out: String = String.dead;
if (BunString__fromJS(globalObject, value, &out)) {
return out;
} else {
return null;
}
}
pub fn toJS(this: *String, globalObject: *bun.JSC.JSGlobalObject) JSC.JSValue {
JSC.markBinding(@src());
return BunString__toJS(globalObject, this);
}
pub fn toJSConst(this: *const String, globalObject: *bun.JSC.JSGlobalObject) JSC.JSValue {
JSC.markBinding(@src());
var a = this.*;
return toJS(&a, globalObject);
}
extern fn BunString__createArray(
globalObject: *bun.JSC.JSGlobalObject,
ptr: [*]const String,
len: usize,
) JSC.JSValue;
pub fn toJSArray(globalObject: *bun.JSC.JSGlobalObject, array: []const bun.String) JSC.JSValue {
JSC.markBinding(@src());
return BunString__createArray(globalObject, array.ptr, array.len);
}
pub fn toZigString(this: String) ZigString {
if (this.tag == .StaticZigString or this.tag == .ZigString) {
return this.value.ZigString;
}
if (this.tag == .WTFStringImpl)
return this.value.WTFStringImpl.toZigString();
return ZigString.Empty;
}
pub fn toWTF(this: *String) void {
JSC.markBinding(@src());
BunString__toWTFString(this);
}
pub inline fn length(this: String) usize {
return if (this.tag == .WTFStringImpl)
this.value.WTFStringImpl.length()
else
this.toZigString().length();
}
pub inline fn utf16(self: String) []const u16 {
if (self.tag == .Empty)
return &[_]u16{};
std.debug.assert(self.tag == .WTFStringImpl);
return self.value.WTFStringImpl.utf16Slice();
}
pub inline fn latin1(self: String) []const u8 {
if (self.tag == .Empty)
return &[_]u8{};
std.debug.assert(self.tag == .WTFStringImpl);
return self.value.WTFStringImpl.latin1Slice();
}
pub fn isUTF8(self: String) bool {
if (!self.tag == .ZigString or self.tag == .StaticZigString)
return false;
return self.value.ZigString.isUTF8();
}
pub fn encoding(self: String) bun.strings.Encoding {
if (self.isUTF16()) {
return .utf16;
}
if (self.isUTF8()) {
return .utf8;
}
return .latin1;
}
pub fn byteSlice(this: String) []const u8 {
return switch (this.tag) {
.ZigString, .StaticZigString => this.value.ZigString.byteSlice(),
.WTFStringImpl => this.value.WTFStringImpl.byteSlice(),
else => &[_]u8{},
};
}
pub fn isUTF16(self: String) bool {
if (self.tag == .WTFStringImpl)
return !self.value.WTFStringImpl.is8Bit();
if (self.tag == .ZigString or self.tag == .StaticZigString)
return self.value.ZigString.isUTF16();
return false;
}
pub inline fn utf8(self: String) []const u8 {
if (comptime bun.Environment.allow_assert)
std.debug.assert(self.canBeUTF8());
return self.value.ZigString.slice();
}
pub fn canBeUTF8(self: String) bool {
if (self.tag == .WTFStringImpl)
return self.value.WTFStringImpl.is8Bit() and bun.strings.isAllASCII(self.value.WTFStringImpl.latin1());
if (self.tag == .ZigString or self.tag == .StaticZigString)
return self.value.ZigString.isUTF8();
return self.tag == .Empty;
}
pub fn substring(self: String, offset: usize) String {
return String.init(self.toZigString().substring(offset, 0));
}
pub fn toUTF8(this: String, allocator: std.mem.Allocator) ZigString.Slice {
if (this.tag == .WTFStringImpl) {
return this.value.WTFStringImpl.toUTF8(allocator);
}
if (this.tag == .ZigString) {
return this.value.ZigString.toSlice(allocator);
}
if (this.tag == .StaticZigString) {
return ZigString.Slice.fromUTF8NeverFree(this.value.StaticZigString.slice());
}
return ZigString.Slice.empty;
}
pub fn toSlice(this: String, allocator: std.mem.Allocator) SliceWithUnderlyingString {
return SliceWithUnderlyingString{
.utf8 = this.toUTF8(allocator),
.underlying = this,
};
}
extern fn BunString__fromJS(globalObject: *JSC.JSGlobalObject, value: bun.JSC.JSValue, out: *String) bool;
extern fn BunString__toJS(globalObject: *JSC.JSGlobalObject, in: *String) JSC.JSValue;
extern fn BunString__toWTFString(this: *String) void;
pub fn ref(this: String) void {
switch (this.tag) {
.WTFStringImpl => this.value.WTFStringImpl.ref(),
else => {},
}
}
pub fn deref(this: String) void {
switch (this.tag) {
.WTFStringImpl => this.value.WTFStringImpl.deref(),
else => {},
}
}
pub const unref = deref;
pub fn eqlComptime(this: String, comptime value: []const u8) bool {
return this.toZigString().eqlComptime(value);
}
pub fn is8Bit(this: String) bool {
return switch (this.tag) {
.WTFStringImpl => this.value.WTFStringImpl.is8Bit(),
.ZigString => !this.value.ZigString.is16Bit(),
else => true,
};
}
pub fn indexOfComptimeWithCheckLen(this: String, comptime values: []const []const u8, comptime check_len: usize) ?usize {
if (this.is8Bit()) {
const bytes = this.byteSlice();
for (values, 0..) |val, i| {
if (bun.strings.eqlComptimeCheckLenWithType(u8, bytes, val, check_len)) {
return i;
}
}
return null;
}
const u16_bytes = this.byteSlice();
inline for (values, 0..) |val, i| {
if (bun.strings.eqlComptimeCheckLenWithType(u16, u16_bytes, comptime bun.strings.toUTF16Literal(val), check_len)) {
return i;
}
}
return null;
}
pub fn indexOfComptimeArrayAssumeSameLength(this: String, comptime values: []const []const u8) ?usize {
if (this.is8Bit()) {
const bytes = this.byteSlice();
inline for (0..values.len) |i| {
std.debug.assert(bytes.len == values[i].len);
if (bun.strings.eqlComptimeCheckLenWithType(u8, bytes, values[i], false)) {
return i;
}
}
return null;
}
const u16_bytes = this.utf16();
var buffer: [values[0].len]u8 = undefined;
inline for (0..values[0].len) |i| {
const uchar = u16_bytes[i];
if (uchar > 255)
return null;
buffer[i] = @intCast(u8, uchar);
}
inline for (0..values.len) |i| {
if (bun.strings.eqlComptimeCheckLenWithType(u8, &buffer, values[i], false)) {
return i;
}
}
return null;
}
pub fn inMap(this: String, comptime ComptimeStringMap: anytype) ?ComptimeStringMap.Value {
return ComptimeStringMap.getWithEqlList(this, indexOfComptimeArrayAssumeSameLength);
}
pub fn inMapCaseInsensitive(this: String, comptime ComptimeStringMap: anytype) ?ComptimeStringMap.Value {
return ComptimeStringMap.getWithEqlList(this, indexOfComptimeArrayCaseInsensitiveSameLength);
}
pub fn indexOfComptimeArrayCaseInsensitiveSameLength(this: String, comptime values: []const []const u8) ?usize {
if (this.is8Bit()) {
const bytes = this.byteSlice();
inline for (0..values.len) |i| {
std.debug.assert(bytes.len == values[i].len);
if (bun.strings.eqlCaseInsensitiveASCIIIgnoreLength(bytes, values[i])) {
return i;
}
}
return null;
}
const u16_bytes = this.utf16();
const buffer: [values[0].len]u8 = brk: {
var bytes: [values[0].len]u8 = undefined;
for (&bytes, u16_bytes) |*byte, uchar| {
if (uchar > 255)
return null;
byte.* = @intCast(u8, uchar);
}
break :brk bytes;
};
inline for (0..values.len) |i| {
if (bun.strings.eqlCaseInsensitiveASCIIIgnoreLength(&buffer, values[i])) {
return i;
}
}
return null;
}
pub fn hasPrefixComptime(this: String, comptime value: []const u8) bool {
return this.toZigString().substring(0, value.len).eqlComptime(value);
}
pub fn isWTFAllocator(this: std.mem.Allocator) bool {
return this.vtable == @This().StringImplAllocator.VTablePtr;
}
pub fn eqlBytes(this: String, value: []const u8) bool {
return bun.strings.eqlLong(this.byteSlice(), value, true);
}
pub fn eql(this: String, other: String) bool {
return this.toZigString().eql(other.toZigString());
}
};
pub const SliceWithUnderlyingString = struct {
utf8: ZigString.Slice,
underlying: String,
pub fn deinit(this: SliceWithUnderlyingString) void {
this.utf8.deinit();
this.underlying.deref();
}
pub fn slice(this: SliceWithUnderlyingString) []const u8 {
return this.utf8.slice();
}
pub fn toJS(this: SliceWithUnderlyingString, globalObject: *JSC.JSGlobalObject) JSC.JSValue {
return this.underlying.toJS(globalObject);
}
};
|