-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstdx.zig
64 lines (56 loc) · 2.19 KB
/
stdx.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
//! Extensions to the standard library -- things which could have been in std, but aren't.
const std = @import("std");
// std.SemanticVersion requires there be no extra characters after the
// major/minor/patch numbers. But when we try to parse `uname
// --kernel-release` (note: while Linux doesn't follow semantic
// versioning, it doesn't violate it either), some distributions have
// extra characters, such as this Fedora one: 6.3.8-100.fc37.x86_64, and
// this WSL one has more than three dots:
// 5.15.90.1-microsoft-standard-WSL2.
pub fn parse_dirty_semver(dirty_release: []const u8) !std.SemanticVersion {
const release = blk: {
var last_valid_version_character_index: usize = 0;
var dots_found: u8 = 0;
for (dirty_release) |c| {
if (c == '.') dots_found += 1;
if (dots_found == 3) {
break;
}
if (c == '.' or (c >= '0' and c <= '9')) {
last_valid_version_character_index += 1;
continue;
}
break;
}
break :blk dirty_release[0..last_valid_version_character_index];
};
return std.SemanticVersion.parse(release);
}
test "stdx.zig: parse_dirty_semver" {
const SemverTestCase = struct {
dirty_release: []const u8,
expected_version: std.SemanticVersion,
};
const cases = &[_]SemverTestCase{
.{
.dirty_release = "1.2.3",
.expected_version = std.SemanticVersion{ .major = 1, .minor = 2, .patch = 3 },
},
.{
.dirty_release = "1001.843.909",
.expected_version = std.SemanticVersion{ .major = 1001, .minor = 843, .patch = 909 },
},
.{
.dirty_release = "6.3.8-100.fc37.x86_64",
.expected_version = std.SemanticVersion{ .major = 6, .minor = 3, .patch = 8 },
},
.{
.dirty_release = "5.15.90.1-microsoft-standard-WSL2",
.expected_version = std.SemanticVersion{ .major = 5, .minor = 15, .patch = 90 },
},
};
for (cases) |case| {
const version = try parse_dirty_semver(case.dirty_release);
try std.testing.expectEqual(case.expected_version, version);
}
}