public sealed class CustomApiVersionParser : IApiVersionParser { private static CustomApiVersionParser? @default; public static CustomApiVersionParser Default => @default ??= new(); public ApiVersion Parse( ReadOnlySpan<char> text ) { if ( text.IsEmpty ) { throw new FormatException( "The specified API version is invalid." ); } var index = text.IndexOf( '.' ); if ( index < 0 || index >= text.Length ) { throw new FormatException( "The specified API version is invalid." ); } var token1 = text[..index]; text = text[( index + 1 )..]; index = text.IndexOf( '.' ); ReadOnlySpan<char> token2; ReadOnlySpan<char> token3; if ( index < 0 || index >= text.Length ) { token2 = text; token3 = default; if ( token2.IsEmpty ) { throw new FormatException( "The specified API version is invalid." ); } } else { token2 = text[..index]; token3 = text[( index + 1 )..]; if ( token3.IsEmpty ) { throw new FormatException( "The specified API version is invalid." ); } } return new CustomApiVersion( token1.ToString(), token2.ToString(), token3.ToString() ); } public bool TryParse( ReadOnlySpan<char> text, [MaybeNullWhen( false )] out ApiVersion apiVersion ) { if ( text.IsEmpty ) { apiVersion = default; return false; } var index = text.IndexOf( '.' ); if ( index < 0 || index >= text.Length ) { apiVersion = default; return false; } var token1 = text[..index]; text = text[( index + 1 )..]; index = text.IndexOf( '.' ); ReadOnlySpan<char> token2; ReadOnlySpan<char> token3; if ( index < 0 || index >= text.Length ) { token2 = text; token3 = default; if ( token2.IsEmpty ) { apiVersion = default; return false; } } else { token2 = text[..index]; token3 = text[( index + 1 )..]; if ( token3.IsEmpty ) { apiVersion = default; return false; } } apiVersion = new CustomApiVersion( token1.ToString(), token2.ToString(), token3.ToString() ); return true; } }