authorgravatar for 35231115+Rocknest@users.noreply.github.comRocknest <35231115+Rocknest@users.noreply.github.com> 2020-10-11 16:29:19+03:00
committergravatar for 35231115+Rocknest@users.noreply.github.comRocknest <35231115+Rocknest@users.noreply.github.com> 2020-10-11 16:29:19+03:00
logcd4200ccef99867f49bc9b5b27d367fa667fecf2
tree4317241561148d185ad8a8b74329ed87660b6364
parent53c63bdb73d9fbc5a54afb4977bb975b03c4c9cc

make Version.parse less strict


1 files changed, 19 insertions(+), 4 deletions(-)

lib/std/builtin.zig+19-4
...@@ -488,11 +488,26 @@ pub const Version = struct {...@@ -488,11 +488,26 @@ pub const Version = struct {
488 }488 }
489489
490 pub fn parse(text: []const u8) !Version {490 pub fn parse(text: []const u8) !Version {
491 var it = std.mem.split(text, ".");491 var end: usize = 0;
492 while (end < text.len) : (end += 1) {
493 const c = text[end];
494 if (!std.ascii.isDigit(c) and c != '.') break;
495 }
496 // found no digits or '.' before unexpected character
497 if (end == 0) return error.InvalidVersion;
498
499 var it = std.mem.split(text[0..end], ".");
500 // substring is not empty, first call will succeed
501 const major = it.next().?;
502 if (major.len == 0) return error.InvalidVersion;
503 const minor = it.next() orelse "0";
504 // ignore 'patch' if 'minor' is invalid
505 const patch = if (minor.len == 0) "0" else (it.next() orelse "0");
506
492 return Version{507 return Version{
493 .major = try std.fmt.parseInt(u32, it.next() orelse return error.InvalidVersion, 10),508 .major = try std.fmt.parseUnsigned(u32, major, 10),
494 .minor = try std.fmt.parseInt(u32, it.next() orelse "0", 10),509 .minor = try std.fmt.parseUnsigned(u32, if (minor.len == 0) "0" else minor, 10),
495 .patch = try std.fmt.parseInt(u32, it.next() orelse "0", 10),510 .patch = try std.fmt.parseUnsigned(u32, if (patch.len == 0) "0" else patch, 10),
496 };511 };
497 }512 }
498513