-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstdx.zig
107 lines (95 loc) · 3.61 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
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
//! Extensions to the standard library -- things which could have been in std, but aren't.
const std = @import("std");
const builtin = @import("builtin");
const assert = std.debug.assert;
/// `maybe` is the dual of `assert`: it signals that condition is sometimes true
/// and sometimes false.
///
/// Currently we use it for documentation, but maybe one day we plug it into
/// coverage.
pub fn maybe(ok: bool) void {
assert(ok or !ok);
}
pub const log = if (builtin.is_test)
// Downgrade `err` to `warn` for tests.
// Zig fails any test that does `log.err`, but we want to test those code paths here.
struct {
pub fn scoped(comptime scope: @Type(.EnumLiteral)) type {
const base = std.log.scoped(scope);
return struct {
pub const err = warn;
pub const warn = base.warn;
pub const info = base.info;
pub const debug = base.debug;
};
}
}
else
std.log;
// 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);
}
}
/// Like std.posix's `unexpectedErrno()` but log unconditionally, not just when mode=Debug.
/// The added `label` argument works around the absence of stack traces in ReleaseSafe builds.
pub fn unexpected_errno(label: []const u8, err: std.posix.system.E) std.posix.UnexpectedError {
log.scoped(.stdx).err("unexpected errno: {s}: code={d} name={?s}", .{
label,
@intFromEnum(err),
std.enums.tagName(std.posix.system.E, err),
});
if (builtin.mode == .Debug) {
std.debug.dumpCurrentStackTrace(null);
}
return error.Unexpected;
}