aboutsummaryrefslogtreecommitdiff
path: root/src/install/semver.zig
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2021-11-29 23:56:35 -0800
committerGravatar Jarred Sumner <jarred@jarredsumner.com> 2021-12-16 19:18:51 -0800
commit128e9861f9916bb30cdaaf5d9665b6c14c67d58f (patch)
tree3a93b2b67afe6ea1549d7e3adab967313bafb752 /src/install/semver.zig
parent5d2cb27562c2aee1d21a16ef3848cc3c3f936056 (diff)
downloadbun-128e9861f9916bb30cdaaf5d9665b6c14c67d58f.tar.gz
bun-128e9861f9916bb30cdaaf5d9665b6c14c67d58f.tar.zst
bun-128e9861f9916bb30cdaaf5d9665b6c14c67d58f.zip
[bun install] Most of the Lockfile format (not done yet)
Diffstat (limited to 'src/install/semver.zig')
-rw-r--r--src/install/semver.zig69
1 files changed, 63 insertions, 6 deletions
diff --git a/src/install/semver.zig b/src/install/semver.zig
index 71eb2073c..6e36f2c2d 100644
--- a/src/install/semver.zig
+++ b/src/install/semver.zig
@@ -6,25 +6,59 @@ pub const ExternalString = extern struct {
len: u32 = 0,
hash: u64 = 0,
- pub fn from(in: string) ExternalString {
+ /// ExternalString but without the hash
+ pub const Small = extern struct {
+ off: u32 = 0,
+ len: u32 = 0,
+
+ pub inline fn slice(this: Small, buf: string) string {
+ return buf[this.off..][0..this.len];
+ }
+
+ pub inline fn from(in: string) Small {
+ return Small{
+ .off = 0,
+ .len = @truncate(u32, in.len),
+ .hash = std.hash.Wyhash.hash(0, in),
+ };
+ }
+
+ pub inline fn init(buf: string, in: string) Small {
+ std.debug.assert(@ptrToInt(buf.ptr) <= @ptrToInt(in.ptr) and ((@ptrToInt(in.ptr) + in.len) <= (@ptrToInt(buf.ptr) + buf.len)));
+
+ return Small{
+ .off = @truncate(u32, @ptrToInt(in.ptr) - @ptrToInt(buf.ptr)),
+ .len = @truncate(u32, in.len),
+ };
+ }
+ };
+
+ pub inline fn from(in: string) ExternalString {
return ExternalString{
.off = 0,
- .len = @truncate(u16, in.len),
+ .len = @truncate(u32, in.len),
.hash = std.hash.Wyhash.hash(0, in),
};
}
+ pub inline fn small(this: ExternalString) ExternalString.Small {
+ return ExternalString.Small{
+ .off = this.off,
+ .len = this.len,
+ };
+ }
+
pub inline fn init(buf: string, in: string, hash: u64) ExternalString {
std.debug.assert(@ptrToInt(buf.ptr) <= @ptrToInt(in.ptr) and ((@ptrToInt(in.ptr) + in.len) <= (@ptrToInt(buf.ptr) + buf.len)));
return ExternalString{
.off = @truncate(u32, @ptrToInt(in.ptr) - @ptrToInt(buf.ptr)),
- .len = @truncate(u16, in.len),
+ .len = @truncate(u32, in.len),
.hash = hash,
};
}
- pub fn slice(this: ExternalString, buf: string) string {
+ pub inline fn slice(this: ExternalString, buf: string) string {
return buf[this.off..][0..this.len];
}
};
@@ -66,9 +100,18 @@ pub const SlicedString = struct {
}
pub inline fn external(this: SlicedString) ExternalString {
- std.debug.assert(@ptrToInt(this.buf.ptr) <= @ptrToInt(this.slice.ptr) and ((@ptrToInt(this.slice.ptr) + this.slice.len) <= (@ptrToInt(this.buf.ptr) + this.buf.len)));
+ if (comptime Environment.isDebug or Environment.isTest) std.debug.assert(@ptrToInt(this.buf.ptr) <= @ptrToInt(this.slice.ptr) and ((@ptrToInt(this.slice.ptr) + this.slice.len) <= (@ptrToInt(this.buf.ptr) + this.buf.len)));
+
+ return ExternalString{ .off = @truncate(u32, @ptrToInt(this.slice.ptr) - @ptrToInt(this.buf.ptr)), .len = @truncate(u32, this.slice.len), .hash = std.hash.Wyhash.hash(0, this.slice) };
+ }
+
+ pub inline fn small(this: SlicedString) ExternalString.Small {
+ if (comptime Environment.isDebug or Environment.isTest) std.debug.assert(@ptrToInt(this.buf.ptr) <= @ptrToInt(this.slice.ptr) and ((@ptrToInt(this.slice.ptr) + this.slice.len) <= (@ptrToInt(this.buf.ptr) + this.buf.len)));
- return ExternalString{ .off = @truncate(u32, @ptrToInt(this.slice.ptr) - @ptrToInt(this.buf.ptr)), .len = @truncate(u16, this.slice.len), .hash = std.hash.Wyhash.hash(0, this.slice) };
+ return ExternalString.Small{
+ .off = @truncate(u32, @ptrToInt(this.slice.ptr) - @ptrToInt(this.buf.ptr)),
+ .len = @truncate(u32, this.slice.len),
+ };
}
pub inline fn sub(this: SlicedString, input: string) SlicedString {
@@ -89,6 +132,20 @@ pub const Version = extern struct {
return Formatter{ .version = this, .input = input };
}
+ pub fn count(this: Version, buf: []const u8, comptime StringBuilder: type, builder: StringBuilder) void {
+ if (this.tag.hasPre()) builder.count(this.tag.pre.slice(buf));
+ if (this.tag.hasBuild()) builder.count(this.tag.build.slice(buf));
+ }
+
+ pub fn clone(this: Version, comptime StringBuilder: type, builder: StringBuilder) Version {
+ var that = this;
+
+ if (this.tag.hasPre()) that.tag.pre = builder.append(ExternalString, this.tag.pre.slice(buf));
+ if (this.tag.hasBuild()) that.tag.build = builder.append(ExternalString, this.tag.build.slice(buf));
+
+ return that;
+ }
+
const HashableVersion = extern struct { major: u32, minor: u32, patch: u32, pre: u64, build: u64 };
pub fn hash(this: Version) u64 {