public static bool TryParse(string value, out SemanticVersion version, bool preserveMissingComponents = false)
{
version = null;
if (value != null)
{
Version systemVersion = null;
// trim the value before passing it in since we not strict here
var sections = ParseSections(value.Trim());
// null indicates the string did not meet the rules
if (sections != null
&& !string.IsNullOrEmpty(sections.Item1))
{
var versionPart = sections.Item1;
if (versionPart.IndexOf('.') < 0)
{
// System.Version requires at least a 2 part version to parse.
versionPart += ".0";
}
if (Version.TryParse(versionPart, out systemVersion))
{
// labels
if (sections.Item2 != null
&& !sections.Item2.All(s => IsValidPart(s, false)))
{
return false;
}
// build metadata
if (sections.Item3 != null
&& !IsValid(sections.Item3, true))
{
return false;
}
var ver = preserveMissingComponents
? systemVersion
: NormalizeVersionValue(systemVersion);
var originalVersion = value;
if (originalVersion.IndexOf(' ') > -1)
{
originalVersion = value.Replace(" ", "");
}
version = new SemanticVersion(version: ver,
releaseLabels: sections.Item2,
metadata: sections.Item3 ?? string.Empty,
originalVersion: originalVersion);
return true;
}
}
}
return false;
}