Added TryParse to StandardFormat (#21216)
authorAlexej Liebenthal <13908853+AlexejLiebenthal@users.noreply.github.com>
Fri, 30 Nov 2018 00:58:38 +0000 (01:58 +0100)
committerAhson Khan <ahkha@microsoft.com>
Fri, 30 Nov 2018 00:58:38 +0000 (16:58 -0800)
src/System.Private.CoreLib/shared/System/Buffers/StandardFormat.cs

index 6975c20..8260325 100644 (file)
@@ -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);