fix: reject invalid SemVer identifiers when parsing

Per SemVer 2.0.0 the core version numbers and numeric prerelease
identifiers must not include leading zeroes, and prerelease/build-metadata
identifiers must be composed only of ASCII alphanumerics and hyphens.

Add IsValidIdentifiers using a vectorized SearchValues lookup for the
allowed character set and ulong.TryParse to detect numeric identifiers
for the leading-zero check. Add failing-then-passing tests for both
invalid and still-valid inputs.
This commit is contained in:
Louis Seubert 2026-07-11 21:56:56 +02:00
commit b4ca01580a
3 changed files with 99 additions and 0 deletions

View file

@ -186,6 +186,32 @@ internal sealed class SemanticVersionTests
await Assert.That(v.Metadata).IsEqualTo("789");
}
[Test]
[Arguments("1.2.3-@#")]
[Arguments("01.2.3")]
[Arguments("1.2.3-01")]
[Arguments("1.2.3-alpha_1")]
[Arguments("1.2.3+build.@")]
[Arguments("1.2.3-")]
[Arguments("1.2.3-..")]
public async Task I_can_not_parse_invalid_versions(string input)
{
await Assert.That(SemanticVersion.TryParse(input, out _)).IsFalse();
}
[Test]
[Arguments("1.2.3")]
[Arguments("1.2.3-alpha.1")]
[Arguments("1.0.0-0.3.7")]
[Arguments("1.0.0-x.7.z.92")]
[Arguments("10.20.30")]
[Arguments("1.2.3+build.01")]
[Arguments("1.0.0-alpha-b")]
public async Task I_can_parse_valid_versions(string input)
{
await Assert.That(SemanticVersion.TryParse(input, out _)).IsTrue();
}
[Test]
public async Task I_can_handle_invalid_json_token()
{