Example #1
0
 public static double Parse(String s, IFormatProvider provider)
 {
     if (s == null)
     {
         ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
     }
     return(Number.ParseDouble(s.AsSpan(), NumberStyles.Float | NumberStyles.AllowThousands, NumberFormatInfo.GetInstance(provider)));
 }
Example #2
0
 // Converts a string to a Decimal. The string must consist of an optional
 // minus sign ("-") followed by a sequence of digits ("0" - "9"). The
 // sequence of digits may optionally contain a single decimal point (".")
 // character. Leading and trailing whitespace characters are allowed.
 // Parse also allows a currency symbol, a trailing negative sign, and
 // parentheses in the number.
 //
 public static Decimal Parse(String s)
 {
     if (s == null)
     {
         ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
     }
     return(Number.ParseDecimal(s.AsSpan(), NumberStyles.Number, NumberFormatInfo.CurrentInfo));
 }
Example #3
0
 public static Decimal Parse(String s, IFormatProvider provider)
 {
     if (s == null)
     {
         ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
     }
     return(Number.ParseDecimal(s.AsSpan(), NumberStyles.Number, provider));
 }
Example #4
0
 public static short Parse(String s)
 {
     if (s == null)
     {
         ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
     }
     return(Parse(s.AsSpan(), NumberStyles.Integer, NumberFormatInfo.CurrentInfo));
 }
Example #5
0
 private static sbyte Parse(String s, NumberStyles style, NumberFormatInfo info)
 {
     if (s == null)
     {
         ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
     }
     return(Parse(s.AsSpan(), style, info));
 }
Example #6
0
 public static Boolean TryParse(String s, out Decimal result)
 {
     if (s == null)
     {
         ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
     }
     return(Number.TryParseDecimal(s.AsSpan(), NumberStyles.Number, null, out result));
 }
Example #7
0
        //
        // Static Methods
        //

        // Determines whether a String represents true or false.
        //
        public static Boolean Parse(String value)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }
            return(Parse(value.AsSpan()));
        }
Example #8
0
 public static double Parse(String s)
 {
     if (s == null)
     {
         ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
     }
     return(Number.ParseDouble(s.AsSpan(), NumberStyles.Float | NumberStyles.AllowThousands, NumberFormatInfo.CurrentInfo));
 }
Example #9
0
 public static short Parse(String s, IFormatProvider provider)
 {
     if (s == null)
     {
         ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
     }
     return(Parse(s.AsSpan(), NumberStyles.Integer, NumberFormatInfo.GetInstance(provider)));
 }
Example #10
0
 public static short Parse(String s, NumberStyles style, IFormatProvider provider)
 {
     NumberFormatInfo.ValidateParseStyleInteger(style);
     if (s == null)
     {
         ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
     }
     return(Parse(s.AsSpan(), style, NumberFormatInfo.GetInstance(provider)));
 }
Example #11
0
 public static Decimal Parse(String s, NumberStyles style, IFormatProvider provider)
 {
     NumberFormatInfo.ValidateParseStyleFloatingPoint(style);
     if (s == null)
     {
         ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
     }
     return(Number.ParseDecimal(s.AsSpan(), style, NumberFormatInfo.GetInstance(provider)));
 }
Example #12
0
 public static double Parse(String s, NumberStyles style)
 {
     NumberFormatInfo.ValidateParseStyleFloatingPoint(style);
     if (s == null)
     {
         ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
     }
     return(Number.ParseDouble(s.AsSpan(), style, NumberFormatInfo.CurrentInfo));
 }
Example #13
0
 public static uint Parse(String s, NumberStyles style)
 {
     NumberFormatInfo.ValidateParseStyleInteger(style);
     if (s == null)
     {
         ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
     }
     return(Number.ParseUInt32(s.AsSpan(), style, NumberFormatInfo.CurrentInfo));
 }
Example #14
0
 public static Boolean TryParse(String s, NumberStyles style, IFormatProvider provider, out Decimal result)
 {
     ValidateParseStyleFloatingPoint(style);
     if (s == null)
     {
         ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
     }
     return(Number.TryParseDecimal(s.AsSpan(), style, provider, out result));
 }
Example #15
0
 public static Decimal Parse(String s, NumberStyles style)
 {
     ValidateParseStyleFloatingPoint(style);
     if (s == null)
     {
         ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
     }
     return(Number.ParseDecimal(s.AsSpan(), style, null));
 }
Example #16
0
        public static Boolean TryParse(String s, out Decimal result)
        {
            if (s == null)
            {
                result = 0;
                return(false);
            }

            return(Number.TryParseDecimal(s.AsSpan(), NumberStyles.Number, NumberFormatInfo.CurrentInfo, out result));
        }
Example #17
0
        // Determines whether a String represents true or false.
        //
        public static Boolean TryParse(String value, out Boolean result)
        {
            if (value == null)
            {
                result = false;
                return(false);
            }

            return(TryParse(value.AsSpan(), out result));
        }
Example #18
0
        public static bool TryParse(String s, out double result)
        {
            if (s == null)
            {
                result = 0;
                return(false);
            }

            return(TryParse(s.AsSpan(), NumberStyles.Float | NumberStyles.AllowThousands, NumberFormatInfo.CurrentInfo, out result));
        }
Example #19
0
        public static bool TryParse(String s, out Int16 result)
        {
            if (s == null)
            {
                result = 0;
                return(false);
            }

            return(TryParse(s.AsSpan(), NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result));
        }
Example #20
0
        public static bool TryParse(String s, NumberStyles style, IFormatProvider provider, out Int16 result)
        {
            NumberFormatInfo.ValidateParseStyleInteger(style);

            if (s == null)
            {
                result = 0;
                return(false);
            }

            return(TryParse(s.AsSpan(), style, NumberFormatInfo.GetInstance(provider), out result));
        }
Example #21
0
        public static Boolean TryParse(String s, NumberStyles style, IFormatProvider provider, out Decimal result)
        {
            NumberFormatInfo.ValidateParseStyleFloatingPoint(style);

            if (s == null)
            {
                result = 0;
                return(false);
            }

            return(Number.TryParseDecimal(s.AsSpan(), style, NumberFormatInfo.GetInstance(provider), out result));
        }
Example #22
0
        public static bool TryParse(ReadOnlySpan <char> value, out bool result)
        {
            ReadOnlySpan <char> trueSpan = TrueLiteral.AsSpan();

            if (StringSpanHelpers.Equals(trueSpan, value, StringComparison.OrdinalIgnoreCase))
            {
                result = true;
                return(true);
            }

            ReadOnlySpan <char> falseSpan = FalseLiteral.AsSpan();

            if (StringSpanHelpers.Equals(falseSpan, value, StringComparison.OrdinalIgnoreCase))
            {
                result = false;
                return(true);
            }

            // Special case: Trim whitespace as well as null characters.
            value = TrimWhiteSpaceAndNull(value);

            if (StringSpanHelpers.Equals(trueSpan, value, StringComparison.OrdinalIgnoreCase))
            {
                result = true;
                return(true);
            }

            if (StringSpanHelpers.Equals(falseSpan, value, StringComparison.OrdinalIgnoreCase))
            {
                result = false;
                return(true);
            }

            result = false;
            return(false);
        }
Example #23
0
        public virtual int BinarySearchLookup(System.String key)
        {
            // this special case is the reason that Arrays.binarySearch() isn't useful.
            if (key == null)
            {
                return(0);
            }

            int low  = 1;
            int high = lookup.Length - 1;

            byte[]      arr           = null;
            Span <byte> stringAsBytes = stackalloc byte[0]; // relax the compiler
            var         stringAsSpan  = key.AsSpan();

            var size = (ushort)Encoding.UTF8.GetByteCount(stringAsSpan);

            if (size <= 256) // allocate on the stack
            {
                stringAsBytes = stackalloc byte[size];
            }
            else
            {
                var pooledSize = BitUtil.NextHighestPowerOfTwo(size);
                arr = ArrayPool <byte> .Shared.Rent(pooledSize);

                stringAsBytes = new Span <byte>(arr, 0, size);
            }

            try
            {
                Encoding.UTF8.GetBytes(stringAsSpan, stringAsBytes);

                while (low <= high)
                {
                    int mid = Number.URShift((low + high), 1);
                    int cmp = UnmanagedStringArray.UnmanagedString.CompareOrdinal(lookup[mid], stringAsBytes);

                    if (cmp < 0)
                    {
                        low = mid + 1;
                    }
                    else if (cmp > 0)
                    {
                        high = mid - 1;
                    }
                    else
                    {
                        return(mid); // key found
                    }
                }
                return(-(low + 1)); // key not found.
            }
            finally
            {
                if (arr != null)
                {
                    ArrayPool <byte> .Shared.Return(arr);
                }
            }
        }
Example #24
0
 /// <summary>
 /// Postpends the elements onto this object, one by one.
 /// </summary>
 /// <param name="collection">This collection.</param>
 /// <param name="elements">The elements to postpend.</param>
 public static void Postpend(this IPostpend <Char> collection, String elements) => Postpend(collection, elements.AsSpan());
Example #25
0
 /// <summary>
 /// Determines whether this string and a specified <see cref="ReadOnlySpan{T}"/> of <see cref="Char"/> object have the same value. A parameter specifies the culture, case, and sort rules used in the comparison.
 /// </summary>
 /// <param name="string">The <see cref="String"/> instance.</param>
 /// <param name="span">The <see cref="ReadOnlySpan{T}"/> of <see cref="Char"/> to compare to this instance.</param>
 /// <param name="comparisonType">One of the enumeration values that specifies how the strings will be compared.</param>
 /// <returns><c>true</c> if the value of the <paramref name="other"/> parameter is the same as this string; otherwise, <c>false</c>.</returns>
 public static Boolean Equals(this String @string, ReadOnlySpan <Char> span, StringComparison comparisonType) => @string?.AsSpan().Equals(span, comparisonType) ?? false;