summaryrefslogtreecommitdiff
path: root/src/System.Private.CoreLib/shared/System/Buffers
diff options
context:
space:
mode:
authorAlexej Liebenthal <13908853+AlexejLiebenthal@users.noreply.github.com>2018-11-30 01:58:38 +0100
committerAhson Khan <ahkha@microsoft.com>2018-11-29 16:58:38 -0800
commit50e2a0eac6f9cd3bf620c336079b4eb497cc0a99 (patch)
tree6aebfb713f928f1ff3148e1505336c8f9e46b1a4 /src/System.Private.CoreLib/shared/System/Buffers
parentb6c346828eb393d93dd6204a1e88a651e68cdb3e (diff)
downloadcoreclr-50e2a0eac6f9cd3bf620c336079b4eb497cc0a99.tar.gz
coreclr-50e2a0eac6f9cd3bf620c336079b4eb497cc0a99.tar.bz2
coreclr-50e2a0eac6f9cd3bf620c336079b4eb497cc0a99.zip
Added TryParse to StandardFormat (#21216)
Diffstat (limited to 'src/System.Private.CoreLib/shared/System/Buffers')
-rw-r--r--src/System.Private.CoreLib/shared/System/Buffers/StandardFormat.cs43
1 files changed, 32 insertions, 11 deletions
diff --git a/src/System.Private.CoreLib/shared/System/Buffers/StandardFormat.cs b/src/System.Private.CoreLib/shared/System/Buffers/StandardFormat.cs
index 6975c204f1..826032514c 100644
--- a/src/System.Private.CoreLib/shared/System/Buffers/StandardFormat.cs
+++ b/src/System.Private.CoreLib/shared/System/Buffers/StandardFormat.cs
@@ -67,12 +67,34 @@ namespace System.Buffers
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;
@@ -87,25 +109,24 @@ namespace System.Buffers
{
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);