diff options
| author | Rocknest <35231115+Rocknest@users.noreply.github.com> | 2020-10-11 16:29:19 +0300 |
|---|---|---|
| committer | Rocknest <35231115+Rocknest@users.noreply.github.com> | 2020-10-11 16:29:19 +0300 |
| commit | cd4200ccef99867f49bc9b5b27d367fa667fecf2 (patch) | |
| tree | 4317241561148d185ad8a8b74329ed87660b6364 /lib | |
| parent | 53c63bdb73d9fbc5a54afb4977bb975b03c4c9cc (diff) | |
| download | zig-cd4200ccef99867f49bc9b5b27d367fa667fecf2.tar.gz zig-cd4200ccef99867f49bc9b5b27d367fa667fecf2.zip | |
make Version.parse less strict
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/std/builtin.zig | 23 |
1 files changed, 19 insertions, 4 deletions
diff --git a/lib/std/builtin.zig b/lib/std/builtin.zig index 8543461f33..fd641c2ee2 100644 --- a/lib/std/builtin.zig +++ b/lib/std/builtin.zig @@ -488,11 +488,26 @@ pub const Version = struct { } pub fn parse(text: []const u8) !Version { - var it = std.mem.split(text, "."); + var end: usize = 0; + while (end < text.len) : (end += 1) { + const c = text[end]; + if (!std.ascii.isDigit(c) and c != '.') break; + } + // found no digits or '.' before unexpected character + if (end == 0) return error.InvalidVersion; + + var it = std.mem.split(text[0..end], "."); + // substring is not empty, first call will succeed + const major = it.next().?; + if (major.len == 0) return error.InvalidVersion; + const minor = it.next() orelse "0"; + // ignore 'patch' if 'minor' is invalid + const patch = if (minor.len == 0) "0" else (it.next() orelse "0"); + return Version{ - .major = try std.fmt.parseInt(u32, it.next() orelse return error.InvalidVersion, 10), - .minor = try std.fmt.parseInt(u32, it.next() orelse "0", 10), - .patch = try std.fmt.parseInt(u32, it.next() orelse "0", 10), + .major = try std.fmt.parseUnsigned(u32, major, 10), + .minor = try std.fmt.parseUnsigned(u32, if (minor.len == 0) "0" else minor, 10), + .patch = try std.fmt.parseUnsigned(u32, if (patch.len == 0) "0" else patch, 10), }; } |
