Function parse [src]

Prototype

pub fn parse(text: []const u8) !Version

Parameters

text: []const u8

Source

pub fn parse(text: []const u8) !Version { // Parse the required major, minor, and patch numbers. const extra_index = std.mem.indexOfAny(u8, text, "-+"); const required = text[0..(extra_index orelse text.len)]; var it = std.mem.splitScalar(u8, required, '.'); var ver = Version{ .major = try parseNum(it.first()), .minor = try parseNum(it.next() orelse return error.InvalidVersion), .patch = try parseNum(it.next() orelse return error.InvalidVersion), }; if (it.next() != null) return error.InvalidVersion; if (extra_index == null) return ver; // Slice optional pre-release or build metadata components. const extra: []const u8 = text[extra_index.?..text.len]; if (extra[0] == '-') { const build_index = std.mem.indexOfScalar(u8, extra, '+'); ver.pre = extra[1..(build_index orelse extra.len)]; if (build_index) |idx| ver.build = extra[(idx + 1)..]; } else { ver.build = extra[1..]; } // Check validity of optional pre-release identifiers. // See: https://semver.org/#spec-item-9 if (ver.pre) |pre| { it = std.mem.splitScalar(u8, pre, '.'); while (it.next()) |id| { // Identifiers MUST NOT be empty. if (id.len == 0) return error.InvalidVersion; // Identifiers MUST comprise only ASCII alphanumerics and hyphens [0-9A-Za-z-]. for (id) |c| if (!std.ascii.isAlphanumeric(c) and c != '-') return error.InvalidVersion; // Numeric identifiers MUST NOT include leading zeroes. const is_num = for (id) |c| { if (!std.ascii.isDigit(c)) break false; } else true; if (is_num) _ = try parseNum(id); } } // Check validity of optional build metadata identifiers. // See: https://semver.org/#spec-item-10 if (ver.build) |build| { it = std.mem.splitScalar(u8, build, '.'); while (it.next()) |id| { // Identifiers MUST NOT be empty. if (id.len == 0) return error.InvalidVersion; // Identifiers MUST comprise only ASCII alphanumerics and hyphens [0-9A-Za-z-]. for (id) |c| if (!std.ascii.isAlphanumeric(c) and c != '-') return error.InvalidVersion; } } return ver; }