forked from oven-sh/bun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsemver.zig
2675 lines (2323 loc) · 90.8 KB
/
semver.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
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
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const std = @import("std");
const Allocator = std.mem.Allocator;
const bun = @import("root").bun;
const string = bun.string;
const Output = bun.Output;
const Global = bun.Global;
const Environment = bun.Environment;
const strings = bun.strings;
const MutableString = bun.MutableString;
const stringZ = bun.stringZ;
const default_allocator = bun.default_allocator;
const C = bun.C;
const JSC = bun.JSC;
const IdentityContext = @import("../identity_context.zig").IdentityContext;
/// String type that stores either an offset/length into an external buffer or a string inline directly
pub const String = extern struct {
pub const max_inline_len: usize = 8;
/// This is three different types of string.
/// 1. Empty string. If it's all zeroes, then it's an empty string.
/// 2. If the final bit is set, then it's a string that is stored inline.
/// 3. If the final bit is not set, then it's a string that is stored in an external buffer.
bytes: [max_inline_len]u8 = [8]u8{ 0, 0, 0, 0, 0, 0, 0, 0 },
/// Create an inline string
pub fn from(comptime inlinable_buffer: []const u8) String {
comptime {
if (inlinable_buffer.len > max_inline_len or
inlinable_buffer.len == max_inline_len and
inlinable_buffer[max_inline_len - 1] >= 0x80)
{
@compileError("string constant too long to be inlined");
}
}
return String.init(inlinable_buffer, inlinable_buffer);
}
pub const Tag = enum {
small,
big,
};
pub inline fn fmt(self: *const String, buf: []const u8) Formatter {
return Formatter{
.buf = buf,
.str = self,
};
}
pub inline fn init(
buf: string,
in: string,
) String {
if (comptime Environment.allow_assert) {
const out = realInit(buf, in);
if (!out.isInline()) {
std.debug.assert(@as(u64, @bitCast(out.slice(buf)[0..8].*)) != undefined);
}
return out;
} else {
return realInit(buf, in);
}
}
pub const Formatter = struct {
str: *const String,
buf: string,
pub fn format(formatter: Formatter, comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void {
const str = formatter.str;
try writer.writeAll(str.slice(formatter.buf));
}
};
pub fn Sorter(comptime direction: enum { asc, desc }) type {
return struct {
lhs_buf: []const u8,
rhs_buf: []const u8,
pub fn lessThan(this: @This(), lhs: String, rhs: String) bool {
return lhs.order(&rhs, this.lhs_buf, this.rhs_buf) == if (comptime direction == .asc) .lt else .gt;
}
};
}
pub inline fn order(
lhs: *const String,
rhs: *const String,
lhs_buf: []const u8,
rhs_buf: []const u8,
) std.math.Order {
return strings.order(lhs.slice(lhs_buf), rhs.slice(rhs_buf));
}
pub inline fn canInline(buf: []const u8) bool {
return switch (buf.len) {
0...max_inline_len - 1 => true,
max_inline_len => buf[max_inline_len - 1] & 0x80 == 0,
else => false,
};
}
pub inline fn isInline(this: String) bool {
return this.bytes[max_inline_len - 1] & 0x80 == 0;
}
pub inline fn sliced(this: *const String, buf: []const u8) SlicedString {
return if (this.isInline())
SlicedString.init(this.slice(""), this.slice(""))
else
SlicedString.init(buf, this.slice(buf));
}
// https://en.wikipedia.org/wiki/Intel_5-level_paging
// https://developer.arm.com/documentation/101811/0101/Address-spaces-in-AArch64#:~:text=0%2DA%2C%20the%20maximum%20size,2%2DA.
// X64 seems to need some of the pointer bits
const max_addressable_space = u63;
comptime {
if (@sizeOf(usize) != 8) {
@compileError("This code needs to be updated for non-64-bit architectures");
}
}
pub const HashContext = struct {
a_buf: []const u8,
b_buf: []const u8,
pub fn eql(ctx: HashContext, a: String, b: String) bool {
return a.eql(b, ctx.a_buf, ctx.b_buf);
}
pub fn hash(ctx: HashContext, a: String) u64 {
const str = a.slice(ctx.a_buf);
return bun.hash(str);
}
};
pub const ArrayHashContext = struct {
a_buf: []const u8,
b_buf: []const u8,
pub fn eql(ctx: ArrayHashContext, a: String, b: String, _: usize) bool {
return a.eql(b, ctx.a_buf, ctx.b_buf);
}
pub fn hash(ctx: ArrayHashContext, a: String) u32 {
const str = a.slice(ctx.a_buf);
return @as(u32, @truncate(bun.hash(str)));
}
};
fn realInit(
buf: string,
in: string,
) String {
return switch (in.len) {
0 => String{},
1 => String{ .bytes = .{ in[0], 0, 0, 0, 0, 0, 0, 0 } },
2 => String{ .bytes = .{ in[0], in[1], 0, 0, 0, 0, 0, 0 } },
3 => String{ .bytes = .{ in[0], in[1], in[2], 0, 0, 0, 0, 0 } },
4 => String{ .bytes = .{ in[0], in[1], in[2], in[3], 0, 0, 0, 0 } },
5 => String{ .bytes = .{ in[0], in[1], in[2], in[3], in[4], 0, 0, 0 } },
6 => String{ .bytes = .{ in[0], in[1], in[2], in[3], in[4], in[5], 0, 0 } },
7 => String{ .bytes = .{ in[0], in[1], in[2], in[3], in[4], in[5], in[6], 0 } },
max_inline_len =>
// If they use the final bit, then it's a big string.
// This should only happen for non-ascii strings that are exactly 8 bytes.
// so that's an edge-case
if ((in[max_inline_len - 1]) >= 128)
@as(String, @bitCast((@as(
u64,
0,
) | @as(
u64,
@as(
max_addressable_space,
@truncate(@as(
u64,
@bitCast(Pointer.init(buf, in)),
)),
),
)) | 1 << 63))
else
String{ .bytes = .{ in[0], in[1], in[2], in[3], in[4], in[5], in[6], in[7] } },
else => @as(
String,
@bitCast((@as(
u64,
0,
) | @as(
u64,
@as(
max_addressable_space,
@truncate(@as(
u64,
@bitCast(Pointer.init(buf, in)),
)),
),
)) | 1 << 63),
),
};
}
pub fn eql(this: String, that: String, this_buf: []const u8, that_buf: []const u8) bool {
if (this.isInline() and that.isInline()) {
return @as(u64, @bitCast(this.bytes)) == @as(u64, @bitCast(that.bytes));
} else if (this.isInline() != that.isInline()) {
return false;
} else {
const a = this.ptr();
const b = that.ptr();
return strings.eql(this_buf[a.off..][0..a.len], that_buf[b.off..][0..b.len]);
}
}
pub inline fn isEmpty(this: String) bool {
return @as(u64, @bitCast(this.bytes)) == @as(u64, 0);
}
pub fn len(this: String) usize {
switch (this.bytes[max_inline_len - 1] & 128) {
0 => {
// Edgecase: string that starts with a 0 byte will be considered empty.
switch (this.bytes[0]) {
0 => {
return 0;
},
else => {
comptime var i: usize = 0;
inline while (i < this.bytes.len) : (i += 1) {
if (this.bytes[i] == 0) return i;
}
return 8;
},
}
},
else => {
const ptr_ = this.ptr();
return ptr_.len;
},
}
}
pub const Pointer = extern struct {
off: u32 = 0,
len: u32 = 0,
pub inline fn init(
buf: string,
in: string,
) Pointer {
if (Environment.allow_assert) {
std.debug.assert(bun.isSliceInBuffer(in, buf));
}
return Pointer{
.off = @as(u32, @truncate(@intFromPtr(in.ptr) - @intFromPtr(buf.ptr))),
.len = @as(u32, @truncate(in.len)),
};
}
};
pub inline fn ptr(this: String) Pointer {
return @as(Pointer, @bitCast(@as(u64, @as(u63, @truncate(@as(u64, @bitCast(this)))))));
}
// String must be a pointer because we reference it as a slice. It will become a dead pointer if it is copied.
pub fn slice(this: *const String, buf: string) string {
switch (this.bytes[max_inline_len - 1] & 128) {
0 => {
// Edgecase: string that starts with a 0 byte will be considered empty.
switch (this.bytes[0]) {
0 => {
return "";
},
else => {
comptime var i: usize = 0;
inline while (i < this.bytes.len) : (i += 1) {
if (this.bytes[i] == 0) return this.bytes[0..i];
}
return &this.bytes;
},
}
},
else => {
const ptr_ = this.*.ptr();
return buf[ptr_.off..][0..ptr_.len];
},
}
}
pub const Builder = struct {
len: usize = 0,
cap: usize = 0,
ptr: ?[*]u8 = null,
string_pool: StringPool = undefined,
pub const StringPool = std.HashMap(u64, String, IdentityContext(u64), 80);
pub inline fn stringHash(buf: []const u8) u64 {
return bun.Wyhash11.hash(0, buf);
}
pub inline fn count(this: *Builder, slice_: string) void {
return countWithHash(this, slice_, if (slice_.len >= String.max_inline_len) stringHash(slice_) else std.math.maxInt(u64));
}
pub inline fn countWithHash(this: *Builder, slice_: string, hash: u64) void {
if (slice_.len <= String.max_inline_len) return;
if (!this.string_pool.contains(hash)) {
this.cap += slice_.len;
}
}
pub inline fn allocatedSlice(this: *Builder) []u8 {
return if (this.cap > 0)
this.ptr.?[0..this.cap]
else
&[_]u8{};
}
pub fn allocate(this: *Builder, allocator: Allocator) !void {
const ptr_ = try allocator.alloc(u8, this.cap);
this.ptr = ptr_.ptr;
}
pub fn append(this: *Builder, comptime Type: type, slice_: string) Type {
return @call(bun.callmod_inline, appendWithHash, .{ this, Type, slice_, stringHash(slice_) });
}
pub fn appendUTF8WithoutPool(this: *Builder, comptime Type: type, slice_: string, hash: u64) Type {
if (slice_.len <= String.max_inline_len) {
if (strings.isAllASCII(slice_)) {
switch (Type) {
String => {
return String.init(this.allocatedSlice(), slice_);
},
ExternalString => {
return ExternalString.init(this.allocatedSlice(), slice_, hash);
},
else => @compileError("Invalid type passed to StringBuilder"),
}
}
}
if (comptime Environment.allow_assert) {
std.debug.assert(this.len <= this.cap); // didn't count everything
std.debug.assert(this.ptr != null); // must call allocate first
}
bun.copy(u8, this.ptr.?[this.len..this.cap], slice_);
const final_slice = this.ptr.?[this.len..this.cap][0..slice_.len];
this.len += slice_.len;
if (comptime Environment.allow_assert) std.debug.assert(this.len <= this.cap);
switch (Type) {
String => {
return String.init(this.allocatedSlice(), final_slice);
},
ExternalString => {
return ExternalString.init(this.allocatedSlice(), final_slice, hash);
},
else => @compileError("Invalid type passed to StringBuilder"),
}
}
// SlicedString is not supported due to inline strings.
pub fn appendWithoutPool(this: *Builder, comptime Type: type, slice_: string, hash: u64) Type {
if (slice_.len <= String.max_inline_len) {
switch (Type) {
String => {
return String.init(this.allocatedSlice(), slice_);
},
ExternalString => {
return ExternalString.init(this.allocatedSlice(), slice_, hash);
},
else => @compileError("Invalid type passed to StringBuilder"),
}
}
if (comptime Environment.allow_assert) {
std.debug.assert(this.len <= this.cap); // didn't count everything
std.debug.assert(this.ptr != null); // must call allocate first
}
bun.copy(u8, this.ptr.?[this.len..this.cap], slice_);
const final_slice = this.ptr.?[this.len..this.cap][0..slice_.len];
this.len += slice_.len;
if (comptime Environment.allow_assert) std.debug.assert(this.len <= this.cap);
switch (Type) {
String => {
return String.init(this.allocatedSlice(), final_slice);
},
ExternalString => {
return ExternalString.init(this.allocatedSlice(), final_slice, hash);
},
else => @compileError("Invalid type passed to StringBuilder"),
}
}
pub fn appendWithHash(this: *Builder, comptime Type: type, slice_: string, hash: u64) Type {
if (slice_.len <= String.max_inline_len) {
switch (Type) {
String => {
return String.init(this.allocatedSlice(), slice_);
},
ExternalString => {
return ExternalString.init(this.allocatedSlice(), slice_, hash);
},
else => @compileError("Invalid type passed to StringBuilder"),
}
}
if (comptime Environment.allow_assert) {
std.debug.assert(this.len <= this.cap); // didn't count everything
std.debug.assert(this.ptr != null); // must call allocate first
}
const string_entry = this.string_pool.getOrPut(hash) catch unreachable;
if (!string_entry.found_existing) {
bun.copy(u8, this.ptr.?[this.len..this.cap], slice_);
const final_slice = this.ptr.?[this.len..this.cap][0..slice_.len];
this.len += slice_.len;
string_entry.value_ptr.* = String.init(this.allocatedSlice(), final_slice);
}
if (comptime Environment.allow_assert) std.debug.assert(this.len <= this.cap);
switch (Type) {
String => {
return string_entry.value_ptr.*;
},
ExternalString => {
return ExternalString{
.value = string_entry.value_ptr.*,
.hash = hash,
};
},
else => @compileError("Invalid type passed to StringBuilder"),
}
}
};
comptime {
if (@sizeOf(String) != @sizeOf(Pointer)) {
@compileError("String types must be the same size");
}
}
};
test "String works" {
{
var buf: string = "hello world";
const world: string = buf[6..];
var str = String.init(
buf,
world,
);
try std.testing.expectEqualStrings("world", str.slice(buf));
}
{
const buf: string = "hello";
const world: string = buf;
var str = String.init(
buf,
world,
);
try std.testing.expectEqualStrings("hello", str.slice(buf));
try std.testing.expectEqual(@as(u64, @bitCast(str)), @as(u64, @bitCast([8]u8{ 'h', 'e', 'l', 'l', 'o', 0, 0, 0 })));
}
{
const buf: string = &[8]u8{ 'h', 'e', 'l', 'l', 'o', 'k', 'k', 129 };
const world: string = buf;
var str = String.init(
buf,
world,
);
try std.testing.expectEqualStrings(buf, str.slice(buf));
}
}
pub const ExternalString = extern struct {
value: String = String{},
hash: u64 = 0,
pub inline fn fmt(this: *const ExternalString, buf: []const u8) String.Formatter {
return this.value.fmt(buf);
}
pub fn order(lhs: *const ExternalString, rhs: *const ExternalString, lhs_buf: []const u8, rhs_buf: []const u8) std.math.Order {
if (lhs.hash == rhs.hash and lhs.hash > 0) return .eq;
return lhs.value.order(&rhs.value, lhs_buf, rhs_buf);
}
/// ExternalString but without the hash
pub inline fn from(in: string) ExternalString {
return ExternalString{
.value = String.init(in, in),
.hash = bun.Wyhash.hash(0, in),
};
}
pub inline fn isInline(this: ExternalString) bool {
return this.value.isInline();
}
pub inline fn isEmpty(this: ExternalString) bool {
return this.value.isEmpty();
}
pub inline fn len(this: ExternalString) usize {
return this.value.len();
}
pub inline fn init(buf: string, in: string, hash: u64) ExternalString {
return ExternalString{
.value = String.init(buf, in),
.hash = hash,
};
}
pub inline fn slice(this: *const ExternalString, buf: string) string {
return this.value.slice(buf);
}
};
pub const BigExternalString = extern struct {
off: u32 = 0,
len: u32 = 0,
hash: u64 = 0,
pub fn from(in: string) BigExternalString {
return BigExternalString{
.off = 0,
.len = @as(u32, @truncate(in.len)),
.hash = bun.Wyhash.hash(0, in),
};
}
pub inline fn init(buf: string, in: string, hash: u64) BigExternalString {
std.debug.assert(@intFromPtr(buf.ptr) <= @intFromPtr(in.ptr) and ((@intFromPtr(in.ptr) + in.len) <= (@intFromPtr(buf.ptr) + buf.len)));
return BigExternalString{
.off = @as(u32, @truncate(@intFromPtr(in.ptr) - @intFromPtr(buf.ptr))),
.len = @as(u32, @truncate(in.len)),
.hash = hash,
};
}
pub fn slice(this: BigExternalString, buf: string) string {
return buf[this.off..][0..this.len];
}
};
pub const SlicedString = struct {
buf: string,
slice: string,
pub inline fn init(buf: string, slice: string) SlicedString {
if (Environment.allow_assert) {
if (@intFromPtr(buf.ptr) > @intFromPtr(slice.ptr)) {
@panic("SlicedString.init buf is not in front of slice");
}
}
return SlicedString{ .buf = buf, .slice = slice };
}
pub inline fn external(this: SlicedString) ExternalString {
if (comptime Environment.allow_assert) {
std.debug.assert(@intFromPtr(this.buf.ptr) <= @intFromPtr(this.slice.ptr) and ((@intFromPtr(this.slice.ptr) + this.slice.len) <= (@intFromPtr(this.buf.ptr) + this.buf.len)));
}
return ExternalString.init(this.buf, this.slice, bun.Wyhash11.hash(0, this.slice));
}
pub inline fn value(this: SlicedString) String {
if (comptime Environment.allow_assert) {
std.debug.assert(@intFromPtr(this.buf.ptr) <= @intFromPtr(this.slice.ptr) and ((@intFromPtr(this.slice.ptr) + this.slice.len) <= (@intFromPtr(this.buf.ptr) + this.buf.len)));
}
return String.init(this.buf, this.slice);
}
pub inline fn sub(this: SlicedString, input: string) SlicedString {
if (Environment.allow_assert) {
if (!(@intFromPtr(this.buf.ptr) <= @intFromPtr(this.buf.ptr) and ((@intFromPtr(input.ptr) + input.len) <= (@intFromPtr(this.buf.ptr) + this.buf.len)))) {
@panic("SlicedString.sub input is not a substring of the slice");
}
}
return SlicedString{ .buf = this.buf, .slice = input };
}
};
const RawType = void;
pub const Version = extern struct {
major: u32 = 0,
minor: u32 = 0,
patch: u32 = 0,
_tag_padding: [4]u8 = .{0} ** 4, // [see padding_checker.zig]
tag: Tag = .{},
// raw: RawType = RawType{},
/// Assumes that there is only one buffer for all the strings
pub fn sortGt(ctx: []const u8, lhs: Version, rhs: Version) bool {
return orderFn(ctx, lhs, rhs) == .gt;
}
pub fn orderFn(ctx: []const u8, lhs: Version, rhs: Version) std.math.Order {
return lhs.order(rhs, ctx, ctx);
}
pub fn cloneInto(this: Version, slice: []const u8, buf: *[]u8) Version {
return .{
.major = this.major,
.minor = this.minor,
.patch = this.patch,
.tag = this.tag.cloneInto(slice, buf),
};
}
pub inline fn len(this: *const Version) u32 {
return this.tag.build.len + this.tag.pre.len;
}
pub fn fmt(this: Version, input: string) Formatter {
return .{ .version = this, .input = input };
}
pub fn count(this: *const Version, buf: []const u8, comptime StringBuilder: type, builder: StringBuilder) void {
if (this.tag.hasPre() and !this.tag.pre.isInline()) builder.count(this.tag.pre.slice(buf));
if (this.tag.hasBuild() and !this.tag.build.isInline()) builder.count(this.tag.build.slice(buf));
}
pub fn clone(this: *const Version, buf: []const u8, comptime StringBuilder: type, builder: StringBuilder) Version {
var that = this.*;
if (this.tag.hasPre() and !this.tag.pre.isInline()) that.tag.pre = builder.append(ExternalString, this.tag.pre.slice(buf));
if (this.tag.hasBuild() and !this.tag.build.isInline()) that.tag.build = builder.append(ExternalString, this.tag.build.slice(buf));
return that;
}
pub const Partial = struct {
major: ?u32 = null,
minor: ?u32 = null,
patch: ?u32 = null,
tag: Tag = .{},
pub fn min(this: Partial) Version {
return .{
.major = this.major orelse 0,
.minor = this.minor orelse 0,
.patch = this.patch orelse 0,
.tag = this.tag,
};
}
pub fn max(this: Partial) Version {
return .{
.major = this.major orelse std.math.maxInt(u32),
.minor = this.minor orelse std.math.maxInt(u32),
.patch = this.patch orelse std.math.maxInt(u32),
.tag = this.tag,
};
}
};
const Hashable = extern struct {
major: u32,
minor: u32,
patch: u32,
pre: u64,
build: u64,
};
pub fn hash(this: Version) u64 {
const hashable = Hashable{
.major = this.major,
.minor = this.minor,
.patch = this.patch,
.pre = this.tag.pre.hash,
.build = this.tag.build.hash,
};
const bytes = std.mem.asBytes(&hashable);
return bun.Wyhash.hash(0, bytes);
}
pub const Formatter = struct {
version: Version,
input: string,
pub fn format(formatter: Formatter, comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void {
const self = formatter.version;
try std.fmt.format(writer, "{?d}.{?d}.{?d}", .{ self.major, self.minor, self.patch });
if (self.tag.hasPre()) {
const pre = self.tag.pre.slice(formatter.input);
try writer.writeAll("-");
try writer.writeAll(pre);
}
if (self.tag.hasBuild()) {
const build = self.tag.build.slice(formatter.input);
try writer.writeAll("+");
try writer.writeAll(build);
}
}
};
pub fn eql(lhs: Version, rhs: Version) bool {
return lhs.major == rhs.major and lhs.minor == rhs.minor and lhs.patch == rhs.patch and rhs.tag.eql(lhs.tag);
}
pub const HashContext = struct {
pub fn hash(_: @This(), lhs: Version) u32 {
return @as(u32, @truncate(lhs.hash()));
}
pub fn eql(_: @This(), lhs: Version, rhs: Version) bool {
return lhs.eql(rhs);
}
};
pub fn orderWithoutTag(
lhs: Version,
rhs: Version,
) std.math.Order {
if (lhs.major < rhs.major) return .lt;
if (lhs.major > rhs.major) return .gt;
if (lhs.minor < rhs.minor) return .lt;
if (lhs.minor > rhs.minor) return .gt;
if (lhs.patch < rhs.patch) return .lt;
if (lhs.patch > rhs.patch) return .gt;
if (lhs.tag.hasPre()) {
if (!rhs.tag.hasPre()) return .lt;
} else {
if (rhs.tag.hasPre()) return .gt;
}
return .eq;
}
pub fn order(
lhs: Version,
rhs: Version,
lhs_buf: []const u8,
rhs_buf: []const u8,
) std.math.Order {
const order_without_tag = orderWithoutTag(lhs, rhs);
if (order_without_tag != .eq) return order_without_tag;
return lhs.tag.order(rhs.tag, lhs_buf, rhs_buf);
}
pub fn orderWithoutBuild(
lhs: Version,
rhs: Version,
lhs_buf: []const u8,
rhs_buf: []const u8,
) std.math.Order {
const order_without_tag = orderWithoutTag(lhs, rhs);
if (order_without_tag != .eq) return order_without_tag;
return lhs.tag.orderWithoutBuild(rhs.tag, lhs_buf, rhs_buf);
}
pub const Tag = extern struct {
pre: ExternalString = ExternalString{},
build: ExternalString = ExternalString{},
pub fn orderPre(lhs: Tag, rhs: Tag, lhs_buf: []const u8, rhs_buf: []const u8) std.math.Order {
const lhs_str = lhs.pre.slice(lhs_buf);
const rhs_str = rhs.pre.slice(rhs_buf);
// 1. split each by '.', iterating through each one looking for integers
// 2. compare as integers, or if not possible compare as string
// 3. whichever is greater is the greater one
//
// 1.0.0-canary.0.0.0.0.0.0 < 1.0.0-canary.0.0.0.0.0.1
var lhs_itr = strings.split(lhs_str, ".");
var rhs_itr = strings.split(rhs_str, ".");
while (true) {
const lhs_part = lhs_itr.next();
const rhs_part = rhs_itr.next();
if (lhs_part == null and rhs_part == null) return .eq;
// if right is null, left is greater than.
if (rhs_part == null) return .gt;
// if left is null, left is less than.
if (lhs_part == null) return .lt;
const lhs_uint: ?u32 = std.fmt.parseUnsigned(u32, lhs_part.?, 10) catch null;
const rhs_uint: ?u32 = std.fmt.parseUnsigned(u32, rhs_part.?, 10) catch null;
// a part that doesn't parse as an integer is greater than a part that does
// https://github.com/npm/node-semver/blob/816c7b2cbfcb1986958a290f941eddfd0441139e/internal/identifiers.js#L12
if (lhs_uint != null and rhs_uint == null) return .lt;
if (lhs_uint == null and rhs_uint != null) return .gt;
if (lhs_uint == null and rhs_uint == null) {
switch (strings.order(lhs_part.?, rhs_part.?)) {
.eq => {
// continue to the next part
continue;
},
else => |not_equal| return not_equal,
}
}
switch (std.math.order(lhs_uint.?, rhs_uint.?)) {
.eq => continue,
else => |not_equal| return not_equal,
}
}
unreachable;
}
pub fn order(
lhs: Tag,
rhs: Tag,
lhs_buf: []const u8,
rhs_buf: []const u8,
) std.math.Order {
if (!lhs.pre.isEmpty() and !rhs.pre.isEmpty()) {
return lhs.orderPre(rhs, lhs_buf, rhs_buf);
}
const pre_order = lhs.pre.order(&rhs.pre, lhs_buf, rhs_buf);
if (pre_order != .eq) return pre_order;
return lhs.build.order(&rhs.build, lhs_buf, rhs_buf);
}
pub fn orderWithoutBuild(
lhs: Tag,
rhs: Tag,
lhs_buf: []const u8,
rhs_buf: []const u8,
) std.math.Order {
if (!lhs.pre.isEmpty() and !rhs.pre.isEmpty()) {
return lhs.orderPre(rhs, lhs_buf, rhs_buf);
}
return lhs.pre.order(&rhs.pre, lhs_buf, rhs_buf);
}
pub fn cloneInto(this: Tag, slice: []const u8, buf: *[]u8) Tag {
var pre: String = this.pre.value;
var build: String = this.build.value;
if (this.pre.isInline()) {
pre = this.pre.value;
} else {
const pre_slice = this.pre.slice(slice);
bun.copy(u8, buf.*, pre_slice);
pre = String.init(buf.*, buf.*[0..pre_slice.len]);
buf.* = buf.*[pre_slice.len..];
}
if (this.build.isInline()) {
build = this.build.value;
} else {
const build_slice = this.build.slice(slice);
bun.copy(u8, buf.*, build_slice);
build = String.init(buf.*, buf.*[0..build_slice.len]);
buf.* = buf.*[build_slice.len..];
}
return .{
.pre = .{
.value = pre,
.hash = this.pre.hash,
},
.build = .{
.value = build,
.hash = this.build.hash,
},
};
}
pub inline fn hasPre(this: Tag) bool {
return !this.pre.isEmpty();
}
pub inline fn hasBuild(this: Tag) bool {
return !this.build.isEmpty();
}
pub fn eql(lhs: Tag, rhs: Tag) bool {
return lhs.pre.hash == rhs.pre.hash;
}
pub const TagResult = struct {
tag: Tag = Tag{},
len: u32 = 0,
};
var multi_tag_warn = false;
// TODO: support multiple tags
pub fn parse(sliced_string: SlicedString) TagResult {
return parseWithPreCount(sliced_string, 0);
}
pub fn parseWithPreCount(sliced_string: SlicedString, initial_pre_count: u32) TagResult {
var input = sliced_string.slice;
var build_count: u32 = 0;
var pre_count: u32 = initial_pre_count;
for (input) |c| {
switch (c) {
' ' => break,
'+' => {
build_count += 1;
},
'-' => {
pre_count += 1;
},
else => {},
}
}
if (build_count == 0 and pre_count == 0) {
return TagResult{
.len = 0,
};
}
const State = enum { none, pre, build };
var result = TagResult{};
// Common case: no allocation is necessary.
var state = State.none;
var start: usize = 0;
var i: usize = 0;
while (i < input.len) : (i += 1) {
const c = input[i];
switch (c) {
' ' => {
switch (state) {
.none => {},
.pre => {
result.tag.pre = sliced_string.sub(input[start..i]).external();
if (comptime Environment.isDebug) {
std.debug.assert(!strings.containsChar(result.tag.pre.slice(sliced_string.buf), '-'));
}
state = State.none;
},
.build => {
result.tag.build = sliced_string.sub(input[start..i]).external();
if (comptime Environment.isDebug) {
std.debug.assert(!strings.containsChar(result.tag.build.slice(sliced_string.buf), '-'));
}
state = State.none;
},
}
result.len = @as(u32, @truncate(i));
break;
},
'+' => {
// qualifier ::= ( '-' pre )? ( '+' build )?
if (state == .pre or state == .none and initial_pre_count > 0) {
result.tag.pre = sliced_string.sub(input[start..i]).external();
}
if (state != .build) {
state = .build;
start = i + 1;
}
},
'-' => {
if (state != .pre) {
state = .pre;
start = i + 1;
}
},
else => {},
}
}
if (state == .none and initial_pre_count > 0) {