Zig 0.17.0-dev (Split by item)
This is an example of documentation generated by
ZigDoc , an alternative to Zig's built-in
Auto Doc feature. See also
examples in other modes/formats . The project being documented here (as the example) is the Zig library itself.
Zig › std/ › SemanticVersion.zig › parse
parse
SemanticVersion.parse
pub fn parse (text : []const u8 ) !Version
File
Code
pub fn parse (text : []const u8 ) !Version {
const extra_index = std .mem .findAny (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 ;
const extra : []const u8 = text [extra_index .?..text .len ];
if (extra [0 ] == '-' ) {
const build_index = std .mem .findScalar (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 ..];
}
// See: https://semver.org/#spec-item-9
if (ver .pre ) |pre | {
it = std .mem .splitScalar (u8 , pre , '.' );
while (it .next ()) |id | {
if (id .len == 0 ) return error .InvalidVersion ;
for (id ) |c | if (!std .ascii .isAlphanumeric (c ) and c != '-' ) return error .InvalidVersion ;
const is_num = for (id ) |c | {
if (!std .ascii .isDigit (c )) break false ;
} else true ;
if (is_num ) _ = try parseNum (id );
}
}
// See: https://semver.org/#spec-item-10
if (ver .build ) |build | {
it = std .mem .splitScalar (u8 , build , '.' );
while (it .next ()) |id | {
if (id .len == 0 ) return error .InvalidVersion ;
for (id ) |c | if (!std .ascii .isAlphanumeric (c ) and c != '-' ) return error .InvalidVersion ;
}
}
return ver ;
}