public static implicit operator StandardFormat(char symbol) => new StandardFormat(symbol);
/// <summary>
- /// Converts a classic .NET format string into a StandardFormat
+ /// Converts a ReadOnlySpan<char> into a StandardFormat
/// </summary>
public static StandardFormat Parse(ReadOnlySpan<char> format)
{
+ ParseHelper(format, out StandardFormat standardFormat, throws: true);
+
+ return standardFormat;
+ }
+
+ /// <summary>
+ /// Converts a classic .NET format string into a StandardFormat
+ /// </summary>
+ public static StandardFormat Parse(string format) => format == null ? default : Parse(format.AsSpan());
+
+ /// <summary>
+ /// Tries to convert a ReadOnlySpan<char> into a StandardFormat. A return value indicates whether the conversion succeeded or failed.
+ /// </summary>
+ public static bool TryParse(ReadOnlySpan<char> format, out StandardFormat result)
+ {
+ return ParseHelper(format, out result);
+ }
+
+ private static bool ParseHelper(ReadOnlySpan<char> format, out StandardFormat standardFormat, bool throws = false)
+ {
+ standardFormat = default;
+
if (format.Length == 0)
- return default;
+ return true;
char symbol = format[0];
byte precision;
{
uint digit = format[srcIndex] - 48u; // '0'
if (digit > 9)
- throw new FormatException(SR.Format(SR.Argument_CannotParsePrecision, MaxPrecision));
-
+ {
+ return throws ? throw new FormatException(SR.Format(SR.Argument_CannotParsePrecision, MaxPrecision)) : false;
+ }
parsedPrecision = parsedPrecision * 10 + digit;
if (parsedPrecision > MaxPrecision)
- throw new FormatException(SR.Format(SR.Argument_PrecisionTooLarge, MaxPrecision));
+ {
+ return throws ? throw new FormatException(SR.Format(SR.Argument_PrecisionTooLarge, MaxPrecision)) : false;
+ }
}
precision = (byte)parsedPrecision;
}
- return new StandardFormat(symbol, precision);
+ standardFormat = new StandardFormat(symbol, precision);
+ return true;
}
/// <summary>
- /// Converts a classic .NET format string into a StandardFormat
- /// </summary>
- public static StandardFormat Parse(string format) => format == null ? default : Parse(format.AsSpan());
-
- /// <summary>
/// Returns true if both the Symbol and Precision are equal.
/// </summary>
public override bool Equals(object obj) => obj is StandardFormat other && Equals(other);