As part of this, I needed to fix-up several functions that parsed strings assuming they end with '\0', which no longer holds once we switch to using spans.
Also:
- Switched various call sites to using a cast rather than AsReadOnlySpan.
- Updated string.Format / StringBuilder.AppendFormat to use the new span-based support to avoid substring allocations.
- Removed some TODOs that were left in the codebase from some of my previous changes
<value>Index (zero based) must be greater than or equal to zero and less than the size of the argument list.</value>
</data>
<data name="Format_InvalidEnumFormatSpecification" xml:space="preserve">
- <value>Format String can be only "G", "g", "X", "x", "F", "f", "D" or "d".</value>
+ <value>Format string can be only "G", "g", "X", "x", "F", "f", "D" or "d".</value>
</data>
<data name="Format_InvalidGuidFormatSpecification" xml:space="preserve">
- <value>Format String can be only "D", "d", "N", "n", "P", "p", "B", "b", "X" or "x".</value>
+ <value>Format string can be only "D", "d", "N", "n", "P", "p", "B", "b", "X" or "x".</value>
</data>
<data name="Format_InvalidString" xml:space="preserve">
<value>Input string was not in a correct format.</value>
public static byte Parse(String s)
{
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
- return Parse(s.AsReadOnlySpan(), NumberStyles.Integer, NumberFormatInfo.CurrentInfo);
+ return Parse((ReadOnlySpan<char>)s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo);
}
public static byte Parse(String s, NumberStyles style)
{
NumberFormatInfo.ValidateParseStyleInteger(style);
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
- return Parse(s.AsReadOnlySpan(), style, NumberFormatInfo.CurrentInfo);
+ return Parse((ReadOnlySpan<char>)s, style, NumberFormatInfo.CurrentInfo);
}
public static byte Parse(String s, IFormatProvider provider)
{
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
- return Parse(s.AsReadOnlySpan(), NumberStyles.Integer, NumberFormatInfo.GetInstance(provider));
+ return Parse((ReadOnlySpan<char>)s, NumberStyles.Integer, NumberFormatInfo.GetInstance(provider));
}
// Parses an unsigned byte from a String in the given style. If
{
NumberFormatInfo.ValidateParseStyleInteger(style);
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
- return Parse(s.AsReadOnlySpan(), style, NumberFormatInfo.GetInstance(provider));
+ return Parse((ReadOnlySpan<char>)s, style, NumberFormatInfo.GetInstance(provider));
}
public static byte Parse(ReadOnlySpan<char> s, NumberStyles style = NumberStyles.Integer, IFormatProvider provider = null)
return false;
}
- return TryParse(s.AsReadOnlySpan(), NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result);
+ return TryParse((ReadOnlySpan<char>)s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result);
}
public static bool TryParse(ReadOnlySpan<char> s, out byte result)
return TryParse(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result);
}
- // TODO https://github.com/dotnet/corefx/issues/23642: Remove once corefx has been updated with new overloads.
- public static bool TryParse(ReadOnlySpan<char> s, out byte result, NumberStyles style = NumberStyles.Integer, IFormatProvider provider = null) =>
- TryParse(s, style, provider, out result);
-
public static bool TryParse(String s, NumberStyles style, IFormatProvider provider, out Byte result)
{
NumberFormatInfo.ValidateParseStyleInteger(style);
return false;
}
- return TryParse(s.AsReadOnlySpan(), style, NumberFormatInfo.GetInstance(provider), out result);
+ return TryParse((ReadOnlySpan<char>)s, style, NumberFormatInfo.GetInstance(provider), out result);
}
public static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider provider, out byte result)
return Number.FormatInt32(m_value, format, NumberFormatInfo.GetInstance(provider));
}
- public bool TryFormat(Span<char> destination, out int charsWritten, string format = null, IFormatProvider provider = null)
+ // TODO https://github.com/dotnet/corefx/issues/25337: Remove this overload once corefx is updated to target the new signatures
+ public bool TryFormat(Span<char> destination, out int charsWritten, string format, IFormatProvider provider) =>
+ TryFormat(destination, out charsWritten, (ReadOnlySpan<char>)format, provider);
+
+ public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format = default, IFormatProvider provider = null)
{
return Number.TryFormatInt32(m_value, format, NumberFormatInfo.GetInstance(provider), destination, out charsWritten);
}
public static DateTime Parse(String s)
{
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
- return (DateTimeParse.Parse(s.AsReadOnlySpan(), DateTimeFormatInfo.CurrentInfo, DateTimeStyles.None));
+ return (DateTimeParse.Parse(s, DateTimeFormatInfo.CurrentInfo, DateTimeStyles.None));
}
// Constructs a DateTime from a string. The string must specify a
public static DateTime Parse(String s, IFormatProvider provider)
{
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
- return (DateTimeParse.Parse(s.AsReadOnlySpan(), DateTimeFormatInfo.GetInstance(provider), DateTimeStyles.None));
+ return (DateTimeParse.Parse(s, DateTimeFormatInfo.GetInstance(provider), DateTimeStyles.None));
}
public static DateTime Parse(String s, IFormatProvider provider, DateTimeStyles styles)
{
DateTimeFormatInfo.ValidateStyles(styles, nameof(styles));
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
- return (DateTimeParse.Parse(s.AsReadOnlySpan(), DateTimeFormatInfo.GetInstance(provider), styles));
+ return (DateTimeParse.Parse(s, DateTimeFormatInfo.GetInstance(provider), styles));
}
public static DateTime Parse(ReadOnlySpan<char> s, IFormatProvider provider = null, DateTimeStyles styles = DateTimeStyles.None)
public static DateTime ParseExact(String s, String format, IFormatProvider provider)
{
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
- return (DateTimeParse.ParseExact(s.AsReadOnlySpan(), format, DateTimeFormatInfo.GetInstance(provider), DateTimeStyles.None));
+ if (format == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.format);
+ return (DateTimeParse.ParseExact(s, format, DateTimeFormatInfo.GetInstance(provider), DateTimeStyles.None));
}
// Constructs a DateTime from a string. The string must specify a
{
DateTimeFormatInfo.ValidateStyles(style, nameof(style));
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
- return (DateTimeParse.ParseExact(s.AsReadOnlySpan(), format, DateTimeFormatInfo.GetInstance(provider), style));
+ if (format == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.format);
+ return (DateTimeParse.ParseExact(s, format, DateTimeFormatInfo.GetInstance(provider), style));
}
- public static DateTime ParseExact(ReadOnlySpan<char> s, string format, IFormatProvider provider, DateTimeStyles style = DateTimeStyles.None)
+ // TODO https://github.com/dotnet/corefx/issues/25337: Remove this overload once corefx is updated to target the new signatures
+ public static DateTime ParseExact(ReadOnlySpan<char> s, string format, IFormatProvider provider, DateTimeStyles style)
+ {
+ if (format == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.format);
+ return ParseExact(s, (ReadOnlySpan<char>)format, provider, style);
+ }
+
+ public static DateTime ParseExact(ReadOnlySpan<char> s, ReadOnlySpan<char> format, IFormatProvider provider, DateTimeStyles style = DateTimeStyles.None)
{
DateTimeFormatInfo.ValidateStyles(style, nameof(style));
return DateTimeParse.ParseExact(s, format, DateTimeFormatInfo.GetInstance(provider), style);
{
DateTimeFormatInfo.ValidateStyles(style, nameof(style));
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
- return DateTimeParse.ParseExactMultiple(s.AsReadOnlySpan(), formats, DateTimeFormatInfo.GetInstance(provider), style);
+ return DateTimeParse.ParseExactMultiple(s, formats, DateTimeFormatInfo.GetInstance(provider), style);
}
public static DateTime ParseExact(ReadOnlySpan<char> s, string[] formats, IFormatProvider provider, DateTimeStyles style = DateTimeStyles.None)
return DateTimeFormat.Format(this, format, DateTimeFormatInfo.GetInstance(provider));
}
- public bool TryFormat(Span<char> destination, out int charsWritten, string format = null, IFormatProvider provider = null) =>
+ // TODO https://github.com/dotnet/corefx/issues/25337: Remove this overload once corefx is updated to target the new signatures
+ public bool TryFormat(Span<char> destination, out int charsWritten, string format, IFormatProvider provider) =>
+ TryFormat(destination, out charsWritten, (ReadOnlySpan<char>)format, provider);
+
+ public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format = default, IFormatProvider provider = null) =>
DateTimeFormat.TryFormat(this, destination, out charsWritten, format, DateTimeFormatInfo.GetInstance(provider));
public DateTime ToUniversalTime()
{
if (s == null)
{
- result = default(DateTime);
+ result = default;
return false;
}
- return DateTimeParse.TryParse(s.AsReadOnlySpan(), DateTimeFormatInfo.CurrentInfo, DateTimeStyles.None, out result);
+ return DateTimeParse.TryParse(s, DateTimeFormatInfo.CurrentInfo, DateTimeStyles.None, out result);
}
public static bool TryParse(ReadOnlySpan<char> s, out DateTime result)
if (s == null)
{
- result = default(DateTime);
+ result = default;
return false;
}
- return DateTimeParse.TryParse(s.AsReadOnlySpan(), DateTimeFormatInfo.GetInstance(provider), styles, out result);
+ return DateTimeParse.TryParse(s, DateTimeFormatInfo.GetInstance(provider), styles, out result);
}
public static bool TryParse(ReadOnlySpan<char> s, IFormatProvider provider, DateTimeStyles styles, out DateTime result)
{
DateTimeFormatInfo.ValidateStyles(style, nameof(style));
- if (s == null)
+ if (s == null || format == null)
{
- result = default(DateTime);
+ result = default;
return false;
}
- return DateTimeParse.TryParseExact(s.AsReadOnlySpan(), format, DateTimeFormatInfo.GetInstance(provider), style, out result);
+ return DateTimeParse.TryParseExact(s, format, DateTimeFormatInfo.GetInstance(provider), style, out result);
}
+ // TODO https://github.com/dotnet/corefx/issues/25337: Remove this overload once corefx is updated to target the new signatures
public static bool TryParseExact(ReadOnlySpan<char> s, string format, IFormatProvider provider, DateTimeStyles style, out DateTime result)
{
+ if (format == null)
+ {
+ result = default;
+ return false;
+ }
+
+ return TryParseExact(s, (ReadOnlySpan<char>)format, provider, style, out result);
+ }
+
+ public static bool TryParseExact(ReadOnlySpan<char> s, ReadOnlySpan<char> format, IFormatProvider provider, DateTimeStyles style, out DateTime result)
+ {
DateTimeFormatInfo.ValidateStyles(style, nameof(style));
return DateTimeParse.TryParseExact(s, format, DateTimeFormatInfo.GetInstance(provider), style, out result);
}
if (s == null)
{
- result = default(DateTime);
+ result = default;
return false;
}
- return DateTimeParse.TryParseExactMultiple(s.AsReadOnlySpan(), formats, DateTimeFormatInfo.GetInstance(provider), style, out result);
+ return DateTimeParse.TryParseExactMultiple(s, formats, DateTimeFormatInfo.GetInstance(provider), style, out result);
}
public static bool TryParseExact(ReadOnlySpan<char> s, string[] formats, IFormatProvider provider, DateTimeStyles style, out DateTime result)
//
public static DateTimeOffset Parse(String input)
{
- if (input == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.index); // TODO: index => input
+ if (input == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.input);
TimeSpan offset;
- DateTime dateResult = DateTimeParse.Parse(input.AsReadOnlySpan(),
+ DateTime dateResult = DateTimeParse.Parse(input,
DateTimeFormatInfo.CurrentInfo,
DateTimeStyles.None,
out offset);
//
public static DateTimeOffset Parse(String input, IFormatProvider formatProvider)
{
- if (input == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.index); // TODO: index => input
+ if (input == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.input);
return Parse(input, formatProvider, DateTimeStyles.None);
}
public static DateTimeOffset Parse(String input, IFormatProvider formatProvider, DateTimeStyles styles)
{
styles = ValidateStyles(styles, nameof(styles));
- if (input == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.index); // TODO: index => input
+ if (input == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.input);
TimeSpan offset;
- DateTime dateResult = DateTimeParse.Parse(input.AsReadOnlySpan(),
+ DateTime dateResult = DateTimeParse.Parse(input,
DateTimeFormatInfo.GetInstance(formatProvider),
styles,
out offset);
//
public static DateTimeOffset ParseExact(String input, String format, IFormatProvider formatProvider)
{
- if (input == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.index); // TODO: index => input
+ if (input == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.input);
+ if (format == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.format);
return ParseExact(input, format, formatProvider, DateTimeStyles.None);
}
public static DateTimeOffset ParseExact(String input, String format, IFormatProvider formatProvider, DateTimeStyles styles)
{
styles = ValidateStyles(styles, nameof(styles));
- if (input == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.index); // TODO: index => input
+ if (input == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.input);
+ if (format == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.format);
TimeSpan offset;
- DateTime dateResult = DateTimeParse.ParseExact(input.AsReadOnlySpan(),
+ DateTime dateResult = DateTimeParse.ParseExact(input,
format,
DateTimeFormatInfo.GetInstance(formatProvider),
styles,
return new DateTimeOffset(dateResult.Ticks, offset);
}
- public static DateTimeOffset ParseExact(ReadOnlySpan<char> input, string format, IFormatProvider formatProvider, DateTimeStyles styles = DateTimeStyles.None)
+ // TODO https://github.com/dotnet/corefx/issues/25337: Remove this overload once corefx is updated to target the new signatures
+ public static DateTimeOffset ParseExact(ReadOnlySpan<char> input, string format, IFormatProvider formatProvider, DateTimeStyles styles)
+ {
+ if (format == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.format);
+ return ParseExact(input, (ReadOnlySpan<char>)format, formatProvider, styles);
+ }
+
+ public static DateTimeOffset ParseExact(ReadOnlySpan<char> input, ReadOnlySpan<char> format, IFormatProvider formatProvider, DateTimeStyles styles = DateTimeStyles.None)
{
styles = ValidateStyles(styles, nameof(styles));
DateTime dateResult = DateTimeParse.ParseExact(input, format, DateTimeFormatInfo.GetInstance(formatProvider), styles, out TimeSpan offset);
public static DateTimeOffset ParseExact(String input, String[] formats, IFormatProvider formatProvider, DateTimeStyles styles)
{
styles = ValidateStyles(styles, nameof(styles));
- if (input == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.index); // TODO: index => input
+ if (input == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.input);
TimeSpan offset;
- DateTime dateResult = DateTimeParse.ParseExactMultiple(input.AsReadOnlySpan(),
+ DateTime dateResult = DateTimeParse.ParseExactMultiple(input,
formats,
DateTimeFormatInfo.GetInstance(formatProvider),
styles,
return DateTimeFormat.Format(ClockDateTime, format, DateTimeFormatInfo.GetInstance(formatProvider), Offset);
}
- public bool TryFormat(Span<char> destination, out int charsWritten, string format = null, IFormatProvider formatProvider = null) =>
+ // TODO https://github.com/dotnet/corefx/issues/25337: Remove this overload once corefx is updated to target the new signatures
+ public bool TryFormat(Span<char> destination, out int charsWritten, string format, IFormatProvider provider) =>
+ TryFormat(destination, out charsWritten, (ReadOnlySpan<char>)format, provider);
+
+ public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format = default, IFormatProvider formatProvider = null) =>
DateTimeFormat.TryFormat(ClockDateTime, destination, out charsWritten, format, DateTimeFormatInfo.GetInstance(formatProvider), Offset);
public DateTimeOffset ToUniversalTime()
{
TimeSpan offset;
DateTime dateResult;
- Boolean parsed = DateTimeParse.TryParse(input.AsReadOnlySpan(),
+ Boolean parsed = DateTimeParse.TryParse(input,
DateTimeFormatInfo.CurrentInfo,
DateTimeStyles.None,
out dateResult,
TimeSpan offset;
DateTime dateResult;
- Boolean parsed = DateTimeParse.TryParse(input.AsReadOnlySpan(),
+ Boolean parsed = DateTimeParse.TryParse(input,
DateTimeFormatInfo.GetInstance(formatProvider),
styles,
out dateResult,
out DateTimeOffset result)
{
styles = ValidateStyles(styles, nameof(styles));
- if (input == null)
+ if (input == null || format == null)
{
result = default(DateTimeOffset);
return false;
TimeSpan offset;
DateTime dateResult;
- Boolean parsed = DateTimeParse.TryParseExact(input.AsReadOnlySpan(),
+ Boolean parsed = DateTimeParse.TryParseExact(input,
format,
DateTimeFormatInfo.GetInstance(formatProvider),
styles,
return parsed;
}
+ // TODO https://github.com/dotnet/corefx/issues/25337: Remove this overload once corefx is updated to target the new signatures
+ public static bool TryParseExact(ReadOnlySpan<char> input, string format, IFormatProvider formatProvider, DateTimeStyles styles, out DateTimeOffset result)
+ {
+ if (format == null)
+ {
+ result = default;
+ return false;
+ }
+
+ return TryParseExact(input, (ReadOnlySpan<char>)format, formatProvider, styles, out result);
+ }
+
public static bool TryParseExact(
- ReadOnlySpan<char> input, string format, IFormatProvider formatProvider, DateTimeStyles styles, out DateTimeOffset result)
+ ReadOnlySpan<char> input, ReadOnlySpan<char> format, IFormatProvider formatProvider, DateTimeStyles styles, out DateTimeOffset result)
{
styles = ValidateStyles(styles, nameof(styles));
bool parsed = DateTimeParse.TryParseExact(input, format, DateTimeFormatInfo.GetInstance(formatProvider), styles, out DateTime dateResult, out TimeSpan offset);
TimeSpan offset;
DateTime dateResult;
- Boolean parsed = DateTimeParse.TryParseExactMultiple(input.AsReadOnlySpan(),
+ Boolean parsed = DateTimeParse.TryParseExactMultiple(input,
formats,
DateTimeFormatInfo.GetInstance(formatProvider),
styles,
public static double Parse(String s)
{
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
- return Number.ParseDouble(s.AsReadOnlySpan(), NumberStyles.Float | NumberStyles.AllowThousands, NumberFormatInfo.CurrentInfo);
+ return Number.ParseDouble(s, NumberStyles.Float | NumberStyles.AllowThousands, NumberFormatInfo.CurrentInfo);
}
public static double Parse(String s, NumberStyles style)
{
NumberFormatInfo.ValidateParseStyleFloatingPoint(style);
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
- return Number.ParseDouble(s.AsReadOnlySpan(), style, NumberFormatInfo.CurrentInfo);
+ return Number.ParseDouble(s, style, NumberFormatInfo.CurrentInfo);
}
public static double Parse(String s, IFormatProvider provider)
{
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
- return Number.ParseDouble(s.AsReadOnlySpan(), NumberStyles.Float | NumberStyles.AllowThousands, NumberFormatInfo.GetInstance(provider));
+ return Number.ParseDouble(s, NumberStyles.Float | NumberStyles.AllowThousands, NumberFormatInfo.GetInstance(provider));
}
public static double Parse(String s, NumberStyles style, IFormatProvider provider)
{
NumberFormatInfo.ValidateParseStyleFloatingPoint(style);
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
- return Number.ParseDouble(s.AsReadOnlySpan(), style, NumberFormatInfo.GetInstance(provider));
+ return Number.ParseDouble(s, style, NumberFormatInfo.GetInstance(provider));
}
// Parses a double from a String in the given style. If
return false;
}
- return TryParse(s.AsReadOnlySpan(), NumberStyles.Float | NumberStyles.AllowThousands, NumberFormatInfo.CurrentInfo, out result);
+ return TryParse((ReadOnlySpan<char>)s, NumberStyles.Float | NumberStyles.AllowThousands, NumberFormatInfo.CurrentInfo, out result);
}
public static bool TryParse(ReadOnlySpan<char> s, out double result)
return false;
}
- return TryParse(s.AsReadOnlySpan(), style, NumberFormatInfo.GetInstance(provider), out result);
+ return TryParse((ReadOnlySpan<char>)s, style, NumberFormatInfo.GetInstance(provider), out result);
}
- // TODO https://github.com/dotnet/corefx/issues/23642: Remove once corefx has been updated with new overloads.
- public static bool TryParse(ReadOnlySpan<char> s, out double result, NumberStyles style = NumberStyles.Integer, IFormatProvider provider = null) =>
- TryParse(s, style, provider, out result);
-
public static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider provider, out double result)
{
NumberFormatInfo.ValidateParseStyleFloatingPoint(style);
outputBuffer.Append(HebrewNumber.ToString(digits));
}
- internal static int ParseRepeatPattern(String format, int pos, char patternChar)
+ internal static int ParseRepeatPattern(ReadOnlySpan<char> format, int pos, char patternChar)
{
int len = format.Length;
int index = pos + 1;
// The pos should point to a quote character. This method will
// append to the result StringBuilder the string encloed by the quote character.
//
- internal static int ParseQuoteString(String format, int pos, StringBuilder result)
+ internal static int ParseQuoteString(ReadOnlySpan<char> format, int pos, StringBuilder result)
{
//
// NOTE : pos will be the index of the quote character in the 'format' string.
// Return value of -1 means 'pos' is already at the end of the 'format' string.
// Otherwise, return value is the int value of the next character.
//
- internal static int ParseNextChar(String format, int pos)
+ internal static int ParseNextChar(ReadOnlySpan<char> format, int pos)
{
if (pos >= format.Length - 1)
{
// tokenLen The len of the current pattern character. This indicates how many "M" that we have.
// patternToMatch The pattern that we want to search. This generally uses "d"
//
- private static bool IsUseGenitiveForm(String format, int index, int tokenLen, char patternToMatch)
+ private static bool IsUseGenitiveForm(ReadOnlySpan<char> format, int index, int tokenLen, char patternToMatch)
{
int i;
int repeat = 0;
//
// Actions: Format the DateTime instance using the specified format.
//
- private static StringBuilder FormatCustomized(DateTime dateTime, String format, DateTimeFormatInfo dtfi, TimeSpan offset)
+ private static StringBuilder FormatCustomized(
+ DateTime dateTime, ReadOnlySpan<char> format, DateTimeFormatInfo dtfi, TimeSpan offset, StringBuilder result)
{
Calendar cal = dtfi.Calendar;
- StringBuilder result = StringBuilderCache.Acquire();
- // This is a flag to indicate if we are format the dates using Hebrew calendar.
+ bool resultBuilderIsPooled = false;
+ if (result == null)
+ {
+ resultBuilderIsPooled = true;
+ result = StringBuilderCache.Acquire();
+ }
+
+ // This is a flag to indicate if we are format the dates using Hebrew calendar.
bool isHebrewCalendar = (cal.ID == CalendarId.HEBREW);
// This is a flag to indicate if we are formating hour/minute/second only.
bool bTimeOnly = true;
}
else
{
- StringBuilderCache.Release(result);
+ if (resultBuilderIsPooled)
+ {
+ StringBuilderCache.Release(result);
+ }
throw new FormatException(SR.Format_InvalidString);
}
break;
nextChar = ParseNextChar(format, i);
// nextChar will be -1 if we already reach the end of the format string.
// Besides, we will not allow "%%" appear in the pattern.
- if (nextChar >= 0 && nextChar != (int)'%')
+ if (nextChar >= 0 && nextChar != '%')
{
- result.Append(FormatCustomized(dateTime, ((char)nextChar).ToString(), dtfi, offset));
+ char nextCharChar = (char)nextChar;
+ StringBuilder origStringBuilder = FormatCustomized(dateTime, ReadOnlySpan<char>.DangerousCreate(null, ref nextCharChar, 1), dtfi, offset, result);
+ Debug.Assert(ReferenceEquals(origStringBuilder, result));
tokenLen = 2;
}
else
// This means that '%' is at the end of the format string or
// "%%" appears in the format string.
//
- StringBuilderCache.Release(result);
+ if (resultBuilderIsPooled)
+ {
+ StringBuilderCache.Release(result);
+ }
throw new FormatException(SR.Format_InvalidString);
}
break;
//
// This means that '\' is at the end of the formatting string.
//
- StringBuilderCache.Release(result);
+ if (resultBuilderIsPooled)
+ {
+ StringBuilderCache.Release(result);
+ }
throw new FormatException(SR.Format_InvalidString);
}
break;
// output the 'z' famliy of formats, which output a the offset from UTC, e.g. "-07:30"
- private static void FormatCustomizedTimeZone(DateTime dateTime, TimeSpan offset, String format, Int32 tokenLen, Boolean timeOnly, StringBuilder result)
+ private static void FormatCustomizedTimeZone(DateTime dateTime, TimeSpan offset, ReadOnlySpan<char> format, Int32 tokenLen, Boolean timeOnly, StringBuilder result)
{
// See if the instance already has an offset
Boolean dateTimeFormat = (offset == NullOffset);
AppendNumber(result, offset.Minutes, 2);
}
-
- internal static String GetRealFormat(String format, DateTimeFormatInfo dtfi)
+ internal static String GetRealFormat(ReadOnlySpan<char> format, DateTimeFormatInfo dtfi)
{
String realFormat = null;
// This method also convert the dateTime if necessary (e.g. when the format is in Universal time),
// and change dtfi if necessary (e.g. when the format should use invariant culture).
//
- private static String ExpandPredefinedFormat(String format, ref DateTime dateTime, ref DateTimeFormatInfo dtfi, ref TimeSpan offset)
+ private static String ExpandPredefinedFormat(ReadOnlySpan<char> format, ref DateTime dateTime, ref DateTimeFormatInfo dtfi, ref TimeSpan offset)
{
switch (format[0])
{
dateTime = dateTime.ToUniversalTime();
break;
}
- format = GetRealFormat(format, dtfi);
- return (format);
+ return GetRealFormat(format, dtfi);
}
internal static String Format(DateTime dateTime, String format, DateTimeFormatInfo dtfi)
internal static string Format(DateTime dateTime, String format, DateTimeFormatInfo dtfi, TimeSpan offset) =>
StringBuilderCache.GetStringAndRelease(FormatStringBuilder(dateTime, format, dtfi, offset));
- internal static bool TryFormat(DateTime dateTime, Span<char> destination, out int charsWritten, string format, DateTimeFormatInfo dtfi) =>
+ internal static bool TryFormat(DateTime dateTime, Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, DateTimeFormatInfo dtfi) =>
TryFormat(dateTime, destination, out charsWritten, format, dtfi, NullOffset);
- internal static bool TryFormat(DateTime dateTime, Span<char> destination, out int charsWritten, string format, DateTimeFormatInfo dtfi, TimeSpan offset)
+ internal static bool TryFormat(DateTime dateTime, Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, DateTimeFormatInfo dtfi, TimeSpan offset)
{
StringBuilder sb = FormatStringBuilder(dateTime, format, dtfi, offset);
return success;
}
- internal static StringBuilder FormatStringBuilder(DateTime dateTime, String format, DateTimeFormatInfo dtfi, TimeSpan offset)
+ internal static StringBuilder FormatStringBuilder(DateTime dateTime, ReadOnlySpan<char> format, DateTimeFormatInfo dtfi, TimeSpan offset)
{
Debug.Assert(dtfi != null);
- if (format == null || format.Length == 0)
+ if (format.Length == 0)
{
Boolean timeOnlySpecialCase = false;
if (dateTime.Ticks < Calendar.TicksPerDay)
if (offset == NullOffset)
{
// Default DateTime.ToString case.
- if (timeOnlySpecialCase)
- {
- format = "s";
- }
- else
- {
- format = "G";
- }
+ format = timeOnlySpecialCase ? "s" : "G";
}
else
{
// Default DateTimeOffset.ToString case.
- if (timeOnlySpecialCase)
- {
- format = RoundtripDateTimeUnfixed;
- }
- else
- {
- format = dtfi.DateTimeOffsetPattern;
- }
+ format = timeOnlySpecialCase ? RoundtripDateTimeUnfixed : dtfi.DateTimeOffsetPattern;
}
}
format = ExpandPredefinedFormat(format, ref dateTime, ref dtfi, ref offset);
}
- return FormatCustomized(dateTime, format, dtfi, offset);
+ return FormatCustomized(dateTime, format, dtfi, offset, result: null);
}
internal static StringBuilder FastFormatRfc1123(DateTime dateTime, TimeSpan offset, DateTimeFormatInfo dtfi)
// This is a placeholder for an MDA to detect when the user is using a
// local DateTime with a format that will be interpreted as UTC.
- internal static void InvalidFormatForLocal(String format, DateTime dateTime)
+ internal static void InvalidFormatForLocal(ReadOnlySpan<char> format, DateTime dateTime)
{
}
}
internal static MatchNumberDelegate m_hebrewNumberParser = new MatchNumberDelegate(DateTimeParse.MatchHebrewDigits);
- internal static DateTime ParseExact(ReadOnlySpan<char> s, String format, DateTimeFormatInfo dtfi, DateTimeStyles style)
+ internal static DateTime ParseExact(ReadOnlySpan<char> s, ReadOnlySpan<char> format, DateTimeFormatInfo dtfi, DateTimeStyles style)
{
DateTimeResult result = new DateTimeResult(); // The buffer to store the parsing result.
result.Init();
}
}
- internal static DateTime ParseExact(ReadOnlySpan<char> s, String format, DateTimeFormatInfo dtfi, DateTimeStyles style, out TimeSpan offset)
+ internal static DateTime ParseExact(ReadOnlySpan<char> s, ReadOnlySpan<char> format, DateTimeFormatInfo dtfi, DateTimeStyles style, out TimeSpan offset)
{
DateTimeResult result = new DateTimeResult(); // The buffer to store the parsing result.
offset = TimeSpan.Zero;
}
}
- internal static bool TryParseExact(ReadOnlySpan<char> s, String format, DateTimeFormatInfo dtfi, DateTimeStyles style, out DateTime result)
+ internal static bool TryParseExact(ReadOnlySpan<char> s, ReadOnlySpan<char> format, DateTimeFormatInfo dtfi, DateTimeStyles style, out DateTime result)
{
result = DateTime.MinValue;
DateTimeResult resultData = new DateTimeResult(); // The buffer to store the parsing result.
return false;
}
- internal static bool TryParseExact(ReadOnlySpan<char> s, String format, DateTimeFormatInfo dtfi, DateTimeStyles style, out DateTime result, out TimeSpan offset)
+ internal static bool TryParseExact(ReadOnlySpan<char> s, ReadOnlySpan<char> format, DateTimeFormatInfo dtfi, DateTimeStyles style, out DateTime result, out TimeSpan offset)
{
result = DateTime.MinValue;
offset = TimeSpan.Zero;
return false;
}
- internal static bool TryParseExact(ReadOnlySpan<char> s, String format, DateTimeFormatInfo dtfi, DateTimeStyles style, ref DateTimeResult result)
+ internal static bool TryParseExact(ReadOnlySpan<char> s, ReadOnlySpan<char> format, DateTimeFormatInfo dtfi, DateTimeStyles style, ref DateTimeResult result)
{
- if (format == null)
- {
- result.SetFailure(ParseFailureKind.ArgumentNull, nameof(SR.ArgumentNull_String), null, nameof(format));
- return false;
- }
if (s.Length == 0)
{
result.SetFailure(ParseFailureKind.Format, nameof(SR.Format_BadDateTime), null);
// This method also set the dtfi according/parseInfo to some special pre-defined
// formats.
//
- private static String ExpandPredefinedFormat(String format, ref DateTimeFormatInfo dtfi, ref ParsingInfo parseInfo, ref DateTimeResult result)
+ private static String ExpandPredefinedFormat(ReadOnlySpan<char> format, ref DateTimeFormatInfo dtfi, ref ParsingInfo parseInfo, ref DateTimeResult result)
{
//
// Check the format to see if we need to override the dtfi to be InvariantInfo,
private static bool DoStrictParse(
ReadOnlySpan<char> s,
- String formatParam,
+ ReadOnlySpan<char> formatParam,
DateTimeStyles styles,
DateTimeFormatInfo dtfi,
ref DateTimeResult result)
parseInfo.fAllowInnerWhite = ((styles & DateTimeStyles.AllowInnerWhite) != 0);
parseInfo.fAllowTrailingWhite = ((styles & DateTimeStyles.AllowTrailingWhite) != 0);
- // We need the original values of the following two below.
- String originalFormat = formatParam;
-
if (formatParam.Length == 1)
{
if (((result.flags & ParseFlags.CaptureOffset) != 0) && formatParam[0] == 'U')
// if we have parsed every item twice.
result.Hour = result.Minute = result.Second = -1;
- __DTString format = new __DTString(formatParam.AsReadOnlySpan(), dtfi, false);
+ __DTString format = new __DTString(formatParam, dtfi, false);
__DTString str = new __DTString(s, dtfi, false);
if (parseInfo.fAllowTrailingWhite)
return buffer.ToString();
}
// return a string in the form: "Sun"
- private static string Hex(string str) => Hex(str.AsReadOnlySpan());
+ private static string Hex(string str) => Hex((ReadOnlySpan<char>)str);
private static string Hex(ReadOnlySpan<char> str)
{
StringBuilder buffer = new StringBuilder();
StringBuilderCache.GetStringAndRelease(FormatToBuilder(value, format, formatProvider));
/// <summary>Main method called from TimeSpan.TryFormat.</summary>
- internal static bool TryFormat(TimeSpan value, Span<char> destination, out int charsWritten, string format, IFormatProvider formatProvider)
+ internal static bool TryFormat(TimeSpan value, Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider formatProvider)
{
StringBuilder sb = FormatToBuilder(value, format, formatProvider);
if (sb.Length <= destination.Length)
}
}
- private static StringBuilder FormatToBuilder(TimeSpan value, string format, IFormatProvider formatProvider)
+ private static StringBuilder FormatToBuilder(TimeSpan value, ReadOnlySpan<char> format, IFormatProvider formatProvider)
{
- if (format == null || format.Length == 0)
+ if (format.Length == 0)
{
format = "c";
}
}
// Custom formats
- return FormatCustomized(value, format, DateTimeFormatInfo.GetInstance(formatProvider));
+ return FormatCustomized(value, format, DateTimeFormatInfo.GetInstance(formatProvider), result: null);
}
/// <summary>Format the TimeSpan instance using the specified format.</summary>
- private static StringBuilder FormatStandard(TimeSpan value, bool isInvariant, string format, Pattern pattern)
+ private static StringBuilder FormatStandard(TimeSpan value, bool isInvariant, ReadOnlySpan<char> format, Pattern pattern)
{
StringBuilder sb = StringBuilderCache.Acquire(InternalGlobalizationHelper.StringBuilderDefaultCapacity);
int day = (int)(value.Ticks / TimeSpan.TicksPerDay);
}
/// <summary>Format the TimeSpan instance using the specified format.</summary>
- private static StringBuilder FormatCustomized(TimeSpan value, string format, DateTimeFormatInfo dtfi)
+ private static StringBuilder FormatCustomized(TimeSpan value, ReadOnlySpan<char> format, DateTimeFormatInfo dtfi, StringBuilder result)
{
Debug.Assert(dtfi != null);
+ bool resultBuilderIsPooled = false;
+ if (result == null)
+ {
+ result = StringBuilderCache.Acquire(InternalGlobalizationHelper.StringBuilderDefaultCapacity);
+ resultBuilderIsPooled = true;
+ }
+
int day = (int)(value.Ticks / TimeSpan.TicksPerDay);
long time = value.Ticks % TimeSpan.TicksPerDay;
long tmp = 0;
int i = 0;
int tokenLen;
- StringBuilder result = StringBuilderCache.Acquire(InternalGlobalizationHelper.StringBuilderDefaultCapacity);
while (i < format.Length)
{
tokenLen = DateTimeFormat.ParseRepeatPattern(format, i, ch);
if (tokenLen > 2)
{
- throw new FormatException(SR.Format_InvalidString);
+ goto default; // to release the builder and throw
}
DateTimeFormat.FormatDigits(result, hours, tokenLen);
break;
tokenLen = DateTimeFormat.ParseRepeatPattern(format, i, ch);
if (tokenLen > 2)
{
- throw new FormatException(SR.Format_InvalidString);
+ goto default; // to release the builder and throw
}
DateTimeFormat.FormatDigits(result, minutes, tokenLen);
break;
tokenLen = DateTimeFormat.ParseRepeatPattern(format, i, ch);
if (tokenLen > 2)
{
- throw new FormatException(SR.Format_InvalidString);
+ goto default; // to release the builder and throw
}
DateTimeFormat.FormatDigits(result, seconds, tokenLen);
break;
tokenLen = DateTimeFormat.ParseRepeatPattern(format, i, ch);
if (tokenLen > DateTimeFormat.MaxSecondsFractionDigits)
{
- throw new FormatException(SR.Format_InvalidString);
+ goto default; // to release the builder and throw
}
tmp = fraction;
tokenLen = DateTimeFormat.ParseRepeatPattern(format, i, ch);
if (tokenLen > DateTimeFormat.MaxSecondsFractionDigits)
{
- throw new FormatException(SR.Format_InvalidString);
+ goto default; // to release the builder and throw
}
tmp = fraction;
tokenLen = DateTimeFormat.ParseRepeatPattern(format, i, ch);
if (tokenLen > 8)
{
- throw new FormatException(SR.Format_InvalidString);
+ goto default; // to release the builder and throw
}
DateTimeFormat.FormatDigits(result, day, tokenLen, true);
// Besides, we will not allow "%%" appear in the pattern.
if (nextChar >= 0 && nextChar != (int)'%')
{
- result.Append(TimeSpanFormat.FormatCustomized(value, ((char)nextChar).ToString(), dtfi));
+ char nextCharChar = (char)nextChar;
+ StringBuilder origStringBuilder = FormatCustomized(value, ReadOnlySpan<char>.DangerousCreate(null, ref nextCharChar, 1), dtfi, result);
+ Debug.Assert(ReferenceEquals(origStringBuilder, result));
tokenLen = 2;
}
else
// This means that '%' is at the end of the format string or
// "%%" appears in the format string.
//
- throw new FormatException(SR.Format_InvalidString);
+ goto default; // to release the builder and throw
}
break;
case '\\':
//
// This means that '\' is at the end of the formatting string.
//
- throw new FormatException(SR.Format_InvalidString);
+ goto default; // to release the builder and throw
}
break;
default:
+ // Invalid format string
+ if (resultBuilderIsPooled)
+ {
+ StringBuilderCache.Release(result);
+ }
throw new FormatException(SR.Format_InvalidString);
}
i += tokenLen;
// the constants guaranteed to include DHMSF ordered greatest to least significant.
// Once the data becomes more complex than this we will need to write a proper tokenizer for
// parsing and formatting
- internal void Init(string format, bool useInvariantFieldLengths)
+ internal void Init(ReadOnlySpan<char> format, bool useInvariantFieldLengths)
{
dd = hh = mm = ss = ff = 0;
_literals = new string[6];
////////////////////////////////////////////////////////////////////////////
using System.Diagnostics;
-using System.Runtime.CompilerServices;
using System.Text;
namespace System.Globalization
return false;
}
- internal static TimeSpan ParseExact(ReadOnlySpan<char> input, string format, IFormatProvider formatProvider, TimeSpanStyles styles)
+ internal static TimeSpan ParseExact(ReadOnlySpan<char> input, ReadOnlySpan<char> format, IFormatProvider formatProvider, TimeSpanStyles styles)
{
var parseResult = new TimeSpanResult(throwOnFailure: true);
bool success = TryParseExactTimeSpan(input, format, formatProvider, styles, ref parseResult);
return parseResult.parsedTimeSpan;
}
- internal static bool TryParseExact(ReadOnlySpan<char> input, string format, IFormatProvider formatProvider, TimeSpanStyles styles, out TimeSpan result)
+ internal static bool TryParseExact(ReadOnlySpan<char> input, ReadOnlySpan<char> format, IFormatProvider formatProvider, TimeSpanStyles styles, out TimeSpan result)
{
var parseResult = new TimeSpanResult(throwOnFailure: false);
}
/// <summary>Common private ParseExact method called by both ParseExact and TryParseExact.</summary>
- private static bool TryParseExactTimeSpan(ReadOnlySpan<char> input, string format, IFormatProvider formatProvider, TimeSpanStyles styles, ref TimeSpanResult result)
+ private static bool TryParseExactTimeSpan(ReadOnlySpan<char> input, ReadOnlySpan<char> format, IFormatProvider formatProvider, TimeSpanStyles styles, ref TimeSpanResult result)
{
- if (format == null)
- {
- return result.SetFailure(ParseFailureKind.ArgumentNull, nameof(SR.ArgumentNull_String), argumentName: nameof(format));
- }
-
if (format.Length == 0)
{
return result.SetFailure(ParseFailureKind.Format, nameof(SR.Format_BadFormatSpecifier));
}
/// <summary>Parse the TimeSpan instance using the specified format. Used by TryParseExactTimeSpan.</summary>
- private static bool TryParseByFormat(ReadOnlySpan<char> input, string format, TimeSpanStyles styles, ref TimeSpanResult result)
+ private static bool TryParseByFormat(ReadOnlySpan<char> input, ReadOnlySpan<char> format, TimeSpanStyles styles, ref TimeSpanResult result)
{
- Debug.Assert(format != null, "format != null");
-
bool seenDD = false; // already processed days?
bool seenHH = false; // already processed hours?
bool seenMM = false; // already processed minutes?
case '\'':
case '\"':
StringBuilder enquotedString = StringBuilderCache.Acquire();
- if (!DateTimeParse.TryParseQuoteString(format.AsReadOnlySpan(), i, enquotedString, out tokenLen))
+ if (!DateTimeParse.TryParseQuoteString(format, i, enquotedString, out tokenLen))
{
StringBuilderCache.Release(enquotedString);
return result.SetFailure(ParseFailureKind.FormatWithParameter, nameof(SR.Format_BadQuote), ch);
GuidResult result = new GuidResult();
result.Init(GuidParseThrowStyle.All);
- if (TryParseGuid(g.AsReadOnlySpan(), GuidStyles.Any, ref result))
+ if (TryParseGuid(g, GuidStyles.Any, ref result))
{
this = result._parsedGuid;
}
}
public static Guid Parse(string input) =>
- Parse(input != null ? input.AsReadOnlySpan() : throw new ArgumentNullException(nameof(input)));
+ Parse(input != null ? (ReadOnlySpan<char>)input : throw new ArgumentNullException(nameof(input)));
public static Guid Parse(ReadOnlySpan<char> input)
{
return false;
}
- return TryParse(input.AsReadOnlySpan(), out result);
+ return TryParse((ReadOnlySpan<char>)input, out result);
}
public static bool TryParse(ReadOnlySpan<char> input, out Guid result)
}
public static Guid ParseExact(string input, string format) =>
- ParseExact(input != null ? input.AsReadOnlySpan() : throw new ArgumentNullException(nameof(input)), format);
+ ParseExact(
+ input != null ? (ReadOnlySpan<char>)input : throw new ArgumentNullException(nameof(input)),
+ format != null ? (ReadOnlySpan<char>)format : throw new ArgumentNullException(nameof(format)));
- public static Guid ParseExact(ReadOnlySpan<char> input, string format)
+ // TODO https://github.com/dotnet/corefx/issues/23642: Remove once corefx has been updated with new overloads.
+ public static Guid ParseExact(ReadOnlySpan<char> input, string format) =>
+ ParseExact(
+ input,
+ format != null ? (ReadOnlySpan<char>)format : throw new ArgumentNullException(nameof(format)));
+
+ public static Guid ParseExact(ReadOnlySpan<char> input, ReadOnlySpan<char> format)
{
- if (format == null)
- {
- throw new ArgumentNullException(nameof(format));
- }
if (format.Length != 1)
{
// all acceptable format strings are of length 1
return false;
}
- return TryParseExact(input.AsReadOnlySpan(), format, out result);
+ return TryParseExact((ReadOnlySpan<char>)input, format, out result);
}
- public static bool TryParseExact(ReadOnlySpan<char> input, string format, out Guid result)
+ // TODO https://github.com/dotnet/corefx/issues/23642: Remove once corefx has been updated with new overloads.
+ public static bool TryParseExact(ReadOnlySpan<char> input, string format, out Guid result) =>
+ TryParseExact(input, (ReadOnlySpan<char>)format, out result);
+
+ public static bool TryParseExact(ReadOnlySpan<char> input, ReadOnlySpan<char> format, out Guid result)
{
- if (format == null || format.Length != 1)
+ if (format.Length != 1)
{
result = default(Guid);
return false;
return guidString;
}
+ // TODO https://github.com/dotnet/corefx/issues/23642: Remove once corefx has been updated with new overloads.
+ public bool TryFormat(Span<char> destination, out int charsWritten, string format) =>
+ TryFormat(destination, out charsWritten, (ReadOnlySpan<char>)format);
+
// Returns whether the guid is successfully formatted as a span.
- public bool TryFormat(Span<char> destination, out int charsWritten, string format = null)
+ public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format = default)
{
- if (format == null || format.Length == 0)
+ if (format.Length == 0)
format = "D";
// all acceptable format strings are of length 1
return true;
}
- bool ISpanFormattable.TryFormat(Span<char> destination, out int charsWritten, string format, IFormatProvider provider)
+ bool ISpanFormattable.TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider provider)
{
// Like with the IFormattable implementation, provider is ignored.
return TryFormat(destination, out charsWritten, format);
{
internal interface ISpanFormattable
{
- bool TryFormat(Span<char> destination, out int charsWritten, string format = null, IFormatProvider provider = null);
+ bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider provider);
}
}
return ToString(format, NumberFormatInfo.GetInstance(provider));
}
- public bool TryFormat(Span<char> destination, out int charsWritten, string format = null, IFormatProvider provider = null)
+ // TODO https://github.com/dotnet/corefx/issues/23642: Remove once corefx has been updated with new overloads.
+ public bool TryFormat(Span<char> destination, out int charsWritten, string format, IFormatProvider provider) =>
+ TryFormat(destination, out charsWritten, (ReadOnlySpan<char>)format, provider);
+
+ public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format = default, IFormatProvider provider = null)
{
NumberFormatInfo info = NumberFormatInfo.GetInstance(provider);
- if (m_value < 0 && format != null && format.Length > 0 && (format[0] == 'X' || format[0] == 'x'))
+ if (m_value < 0 && format.Length > 0 && (format[0] == 'X' || format[0] == 'x'))
{
uint temp = (uint)(m_value & 0x0000FFFF);
return Number.TryFormatUInt32(temp, format, info, destination, out charsWritten);
public static short Parse(String s)
{
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
- return Parse(s.AsReadOnlySpan(), NumberStyles.Integer, NumberFormatInfo.CurrentInfo);
+ return Parse((ReadOnlySpan<char>)s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo);
}
public static short Parse(String s, NumberStyles style)
{
NumberFormatInfo.ValidateParseStyleInteger(style);
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
- return Parse(s.AsReadOnlySpan(), style, NumberFormatInfo.CurrentInfo);
+ return Parse((ReadOnlySpan<char>)s, style, NumberFormatInfo.CurrentInfo);
}
public static short Parse(String s, IFormatProvider provider)
{
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
- return Parse(s.AsReadOnlySpan(), NumberStyles.Integer, NumberFormatInfo.GetInstance(provider));
+ return Parse((ReadOnlySpan<char>)s, NumberStyles.Integer, NumberFormatInfo.GetInstance(provider));
}
public static short Parse(String s, NumberStyles style, IFormatProvider provider)
{
NumberFormatInfo.ValidateParseStyleInteger(style);
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
- return Parse(s.AsReadOnlySpan(), style, NumberFormatInfo.GetInstance(provider));
+ return Parse((ReadOnlySpan<char>)s, style, NumberFormatInfo.GetInstance(provider));
}
public static short Parse(ReadOnlySpan<char> s, NumberStyles style = NumberStyles.Integer, IFormatProvider provider = null)
return false;
}
- return TryParse(s.AsReadOnlySpan(), NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result);
+ return TryParse((ReadOnlySpan<char>)s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result);
}
public static bool TryParse(ReadOnlySpan<char> s, out short result)
return false;
}
- return TryParse(s.AsReadOnlySpan(), style, NumberFormatInfo.GetInstance(provider), out result);
+ return TryParse((ReadOnlySpan<char>)s, style, NumberFormatInfo.GetInstance(provider), out result);
}
- // TODO https://github.com/dotnet/corefx/issues/23642: Remove once corefx has been updated with new overloads.
- public static bool TryParse(ReadOnlySpan<char> s, out Int16 result, NumberStyles style = NumberStyles.Integer, IFormatProvider provider = null) =>
- TryParse(s, style, provider, out result);
-
public static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider provider, out short result)
{
NumberFormatInfo.ValidateParseStyleInteger(style);
return Number.FormatInt32(m_value, format, NumberFormatInfo.GetInstance(provider));
}
- public bool TryFormat(Span<char> destination, out int charsWritten, string format = null, IFormatProvider provider = null)
+ // TODO https://github.com/dotnet/corefx/issues/23642: Remove once corefx has been updated with new overloads.
+ public bool TryFormat(Span<char> destination, out int charsWritten, string format, IFormatProvider provider) =>
+ TryFormat(destination, out charsWritten, (ReadOnlySpan<char>)format, provider);
+
+ public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format = default, IFormatProvider provider = null)
{
return Number.TryFormatInt32(m_value, format, NumberFormatInfo.GetInstance(provider), destination, out charsWritten);
}
public static int Parse(String s)
{
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
- return Number.ParseInt32(s.AsReadOnlySpan(), NumberStyles.Integer, NumberFormatInfo.CurrentInfo);
+ return Number.ParseInt32(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo);
}
public static int Parse(String s, NumberStyles style)
{
NumberFormatInfo.ValidateParseStyleInteger(style);
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
- return Number.ParseInt32(s.AsReadOnlySpan(), style, NumberFormatInfo.CurrentInfo);
+ return Number.ParseInt32(s, style, NumberFormatInfo.CurrentInfo);
}
// Parses an integer from a String in the given style. If
public static int Parse(String s, IFormatProvider provider)
{
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
- return Number.ParseInt32(s.AsReadOnlySpan(), NumberStyles.Integer, NumberFormatInfo.GetInstance(provider));
+ return Number.ParseInt32(s, NumberStyles.Integer, NumberFormatInfo.GetInstance(provider));
}
// Parses an integer from a String in the given style. If
{
NumberFormatInfo.ValidateParseStyleInteger(style);
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
- return Number.ParseInt32(s.AsReadOnlySpan(), style, NumberFormatInfo.GetInstance(provider));
+ return Number.ParseInt32(s, style, NumberFormatInfo.GetInstance(provider));
}
public static int Parse(ReadOnlySpan<char> s, NumberStyles style = NumberStyles.Integer, IFormatProvider provider = null)
return false;
}
- return Number.TryParseInt32(s.AsReadOnlySpan(), NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result);
+ return Number.TryParseInt32(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result);
}
public static bool TryParse(ReadOnlySpan<char> s, out int result)
return false;
}
- return Number.TryParseInt32(s.AsReadOnlySpan(), style, NumberFormatInfo.GetInstance(provider), out result);
+ return Number.TryParseInt32(s, style, NumberFormatInfo.GetInstance(provider), out result);
}
- // TODO https://github.com/dotnet/corefx/issues/23642: Remove once corefx has been updated with new overloads.
- public static bool TryParse(ReadOnlySpan<char> s, out int result, NumberStyles style = NumberStyles.Integer, IFormatProvider provider = null) =>
- TryParse(s, style, provider, out result);
-
public static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider provider, out int result)
{
NumberFormatInfo.ValidateParseStyleInteger(style);
return Number.FormatInt64(m_value, format, NumberFormatInfo.GetInstance(provider));
}
- public bool TryFormat(Span<char> destination, out int charsWritten, string format = null, IFormatProvider provider = null)
+ // TODO https://github.com/dotnet/corefx/issues/23642: Remove once corefx has been updated with new overloads.
+ public bool TryFormat(Span<char> destination, out int charsWritten, string format, IFormatProvider provider) =>
+ TryFormat(destination, out charsWritten, (ReadOnlySpan<char>)format, provider);
+
+ public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format = default, IFormatProvider provider = null)
{
return Number.TryFormatInt64(m_value, format, NumberFormatInfo.GetInstance(provider), destination, out charsWritten);
}
public static long Parse(String s)
{
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
- return Number.ParseInt64(s.AsReadOnlySpan(), NumberStyles.Integer, NumberFormatInfo.CurrentInfo);
+ return Number.ParseInt64(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo);
}
public static long Parse(String s, NumberStyles style)
{
NumberFormatInfo.ValidateParseStyleInteger(style);
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
- return Number.ParseInt64(s.AsReadOnlySpan(), style, NumberFormatInfo.CurrentInfo);
+ return Number.ParseInt64(s, style, NumberFormatInfo.CurrentInfo);
}
public static long Parse(String s, IFormatProvider provider)
{
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
- return Number.ParseInt64(s.AsReadOnlySpan(), NumberStyles.Integer, NumberFormatInfo.GetInstance(provider));
+ return Number.ParseInt64(s, NumberStyles.Integer, NumberFormatInfo.GetInstance(provider));
}
{
NumberFormatInfo.ValidateParseStyleInteger(style);
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
- return Number.ParseInt64(s.AsReadOnlySpan(), style, NumberFormatInfo.GetInstance(provider));
+ return Number.ParseInt64(s, style, NumberFormatInfo.GetInstance(provider));
}
public static long Parse(ReadOnlySpan<char> s, NumberStyles style = NumberStyles.Integer, IFormatProvider provider = null)
return false;
}
- return Number.TryParseInt64(s.AsReadOnlySpan(), NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result);
+ return Number.TryParseInt64(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result);
}
public static bool TryParse(ReadOnlySpan<char> s, out long result)
return false;
}
- return Number.TryParseInt64(s.AsReadOnlySpan(), style, NumberFormatInfo.GetInstance(provider), out result);
+ return Number.TryParseInt64(s, style, NumberFormatInfo.GetInstance(provider), out result);
}
- // TODO https://github.com/dotnet/corefx/issues/23642: Remove once corefx has been updated with new overloads.
- public static bool TryParse(ReadOnlySpan<char> s, out long result, NumberStyles style = NumberStyles.Integer, IFormatProvider provider = null) =>
- TryParse(s, style, provider, out result);
-
public static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider provider, out long result)
{
NumberFormatInfo.ValidateParseStyleInteger(style);
"(#)", "-#", "- #", "#-", "# -",
};
- public static string FormatDecimal(decimal value, string format, NumberFormatInfo info)
+ public static string FormatDecimal(decimal value, ReadOnlySpan<char> format, NumberFormatInfo info)
{
char fmt = ParseFormatSpecifier(format, out int digits);
return sb.GetString();
}
- public static bool TryFormatDecimal(decimal value, string format, NumberFormatInfo info, Span<char> destination, out int charsWritten)
+ public static bool TryFormatDecimal(decimal value, ReadOnlySpan<char> format, NumberFormatInfo info, Span<char> destination, out int charsWritten)
{
char fmt = ParseFormatSpecifier(format, out int digits);
*dst = '\0';
}
- public static string FormatInt32(int value, string format, NumberFormatInfo info)
+ public static string FormatInt32(int value, ReadOnlySpan<char> format, NumberFormatInfo info)
{
int digits;
char fmt = ParseFormatSpecifier(format, out digits);
}
}
- public static bool TryFormatInt32(int value, string format, NumberFormatInfo info, Span<char> destination, out int charsWritten)
+ public static bool TryFormatInt32(int value, ReadOnlySpan<char> format, NumberFormatInfo info, Span<char> destination, out int charsWritten)
{
int digits;
char fmt = ParseFormatSpecifier(format, out digits);
}
}
- public static string FormatUInt32(uint value, string format, NumberFormatInfo info)
+ public static string FormatUInt32(uint value, ReadOnlySpan<char> format, NumberFormatInfo info)
{
int digits;
char fmt = ParseFormatSpecifier(format, out digits);
}
}
- public static bool TryFormatUInt32(uint value, string format, NumberFormatInfo info, Span<char> destination, out int charsWritten)
+ public static bool TryFormatUInt32(uint value, ReadOnlySpan<char> format, NumberFormatInfo info, Span<char> destination, out int charsWritten)
{
int digits;
char fmt = ParseFormatSpecifier(format, out digits);
}
}
- public static string FormatInt64(long value, string format, NumberFormatInfo info)
+ public static string FormatInt64(long value, ReadOnlySpan<char> format, NumberFormatInfo info)
{
int digits;
char fmt = ParseFormatSpecifier(format, out digits);
}
}
- public static bool TryFormatInt64(long value, string format, NumberFormatInfo info, Span<char> destination, out int charsWritten)
+ public static bool TryFormatInt64(long value, ReadOnlySpan<char> format, NumberFormatInfo info, Span<char> destination, out int charsWritten)
{
int digits;
char fmt = ParseFormatSpecifier(format, out digits);
}
}
- public static string FormatUInt64(ulong value, string format, NumberFormatInfo info)
+ public static string FormatUInt64(ulong value, ReadOnlySpan<char> format, NumberFormatInfo info)
{
int digits;
char fmt = ParseFormatSpecifier(format, out digits);
}
}
- public static bool TryFormatUInt64(ulong value, string format, NumberFormatInfo info, Span<char> destination, out int charsWritten)
+ public static bool TryFormatUInt64(ulong value, ReadOnlySpan<char> format, NumberFormatInfo info, Span<char> destination, out int charsWritten)
{
int digits;
char fmt = ParseFormatSpecifier(format, out digits);
return TryCopyTo(p, (int)(buffer + bufferSize - p), destination, out charsWritten);
}
- internal static unsafe char ParseFormatSpecifier(string format, out int digits)
+ internal static unsafe char ParseFormatSpecifier(ReadOnlySpan<char> format, out int digits)
{
- if (format != null)
- {
- fixed (char* pFormat = format)
+ char c = default;
+ if (format.Length > 0)
+ {
+ // If the format begins with a symbol, see if it's a standard format
+ // with or without a specified number of digits.
+ c = format[0];
+ if ((uint)(c - 'A') <= 'Z' - 'A' ||
+ (uint)(c - 'a') <= 'z' - 'a')
{
- char ch = *pFormat;
- if (ch != 0)
+ // Fast path for sole symbol, e.g. "D"
+ if (format.Length == 1)
{
- if ((uint)(ch - 'A') <= 'Z' - 'A' ||
- (uint)(ch - 'a') <= 'z' - 'a')
+ digits = -1;
+ return c;
+ }
+
+ if (format.Length == 2)
+ {
+ // Fast path for symbol and single digit, e.g. "X4"
+ int d = format[1] - '0';
+ if ((uint)d < 10)
{
- int i = 1;
- int n = -1;
- if ((uint)(pFormat[i] - '0') <= '9' - '0')
- {
- n = pFormat[i++] - '0';
- while ((uint)(pFormat[i] - '0') <= '9' - '0')
- {
- n = (n * 10) + pFormat[i++] - '0';
- if (n >= 10)
- break;
- }
- }
- if (pFormat[i] == 0)
- {
- digits = n;
- return ch;
- }
+ digits = d;
+ return c;
}
+ }
+ else if (format.Length == 3)
+ {
+ // Fast path for symbol and double digit, e.g. "F12"
+ int d1 = format[1] - '0', d2 = format[2] - '0';
+ if ((uint)d1 < 10 && (uint)d2 < 10)
+ {
+ digits = d1 * 10 + d2;
+ return c;
+ }
+ }
- digits = -1;
- return '\0';
+ // Fallback for symbol and any length digits. The digits value must be >= 0 && <= 99,
+ // but it can begin with any number of 0s, and thus we may need to check more than two
+ // digits. Further, for compat, we need to stop when we hit a null char.
+ int n = 0;
+ int i = 1;
+ while (i < format.Length && (((uint)format[i] - '0') < 10) && n < 10)
+ {
+ n = (n * 10) + format[i++] - '0';
+ }
+
+ // If we're at the end of the digits rather than having stopped because we hit something
+ // other than a digit or overflowed, return the standard format info.
+ if (i == format.Length || format[i] == '\0')
+ {
+ digits = n;
+ return c;
}
}
}
+ // Default empty format to be "G"; custom format is signified with '\0'.
digits = -1;
- return 'G';
+ return format.Length == 0 || c == '\0' ? // For compat, treat '\0' as the end of the specifier, even if the specifer extends beyond it.
+ 'G' :
+ '\0';
}
internal static unsafe void NumberToString(ref ValueStringBuilder sb, ref NumberBuffer number, char format, int nMaxDigits, NumberFormatInfo info, bool isDecimal)
}
}
- internal static unsafe void NumberToStringFormat(ref ValueStringBuilder sb, ref NumberBuffer number, string format, NumberFormatInfo info)
+ internal static unsafe void NumberToStringFormat(ref ValueStringBuilder sb, ref NumberBuffer number, ReadOnlySpan<char> format, NumberFormatInfo info)
{
int digitCount;
int decimalPos;
scaleAdjust = 0;
src = section;
- fixed (char* pFormat = format)
+ fixed (char* pFormat = &format.DangerousGetPinnableReference())
{
- while ((ch = pFormat[src++]) != 0 && ch != ';')
+ while (src < format.Length && (ch = pFormat[src++]) != 0 && ch != ';')
{
switch (ch)
{
break;
case '\'':
case '"':
- while (pFormat[src] != 0 && pFormat[src++] != ch)
+ while (src < format.Length && pFormat[src] != 0 && pFormat[src++] != ch)
;
break;
case '\\':
- if (pFormat[src] != 0)
+ if (src < format.Length && pFormat[src] != 0)
src++;
break;
case 'E':
case 'e':
- if (pFormat[src] == '0' || ((pFormat[src] == '+' || pFormat[src] == '-') && pFormat[src + 1] == '0'))
+ if ((src < format.Length && pFormat[src] == '0') ||
+ (src + 1 < format.Length && (pFormat[src] == '+' || pFormat[src] == '-') && pFormat[src + 1] == '0'))
{
- while (pFormat[++src] == '0')
- ;
+ while (++src < format.Length && pFormat[src] == '0');
scientific = true;
}
break;
bool decimalWritten = false;
- fixed (char* pFormat = format)
+ fixed (char* pFormat = &format.DangerousGetPinnableReference())
{
char* cur = dig;
- while ((ch = pFormat[src++]) != 0 && ch != ';')
+ while (src < format.Length && (ch = pFormat[src++]) != 0 && ch != ';')
{
if (adjust > 0)
{
break;
case '\'':
case '"':
- while (pFormat[src] != 0 && pFormat[src] != ch)
+ while (src < format.Length && pFormat[src] != 0 && pFormat[src] != ch)
sb.Append(pFormat[src++]);
- if (pFormat[src] != 0)
+ if (src < format.Length && pFormat[src] != 0)
src++;
break;
case '\\':
- if (pFormat[src] != 0)
+ if (src < format.Length && pFormat[src] != 0)
sb.Append(pFormat[src++]);
break;
case 'E':
int i = 0;
if (scientific)
{
- if (pFormat[src] == '0')
+ if (src < format.Length && pFormat[src] == '0')
{
// Handles E0, which should format the same as E-0
i++;
}
- else if (pFormat[src] == '+' && pFormat[src + 1] == '0')
+ else if (src+1 < format.Length && pFormat[src] == '+' && pFormat[src + 1] == '0')
{
// Handles E+0
positiveSign = true;
}
- else if (pFormat[src] == '-' && pFormat[src + 1] == '0')
+ else if (src+1 < format.Length && pFormat[src] == '-' && pFormat[src + 1] == '0')
{
// Handles E-0
// Do nothing, this is just a place holder s.t. we don't break out of the loop.
break;
}
- while (pFormat[++src] == '0')
+ while (++src < format.Length && pFormat[src] == '0')
i++;
if (i > 10)
i = 10;
else
{
sb.Append(ch); // Copy E or e to output
- if (pFormat[src] == '+' || pFormat[src] == '-')
- sb.Append(pFormat[src++]);
- while (pFormat[src] == '0')
- sb.Append(pFormat[src++]);
+ if (src < format.Length)
+ {
+ if (pFormat[src] == '+' || pFormat[src] == '-')
+ sb.Append(pFormat[src++]);
+ while (src < format.Length && pFormat[src] == '0')
+ sb.Append(pFormat[src++]);
+ }
}
break;
}
dig[i] = '\0';
}
- private static unsafe int FindSection(string format, int section)
+ private static unsafe int FindSection(ReadOnlySpan<char> format, int section)
{
int src;
char ch;
if (section == 0)
return 0;
- fixed (char* pFormat = format)
+ fixed (char* pFormat = &format.DangerousGetPinnableReference())
{
src = 0;
for (;;)
{
+ if (src >= format.Length)
+ {
+ return 0;
+ }
+
switch (ch = pFormat[src++])
{
case '\'':
case '"':
- while (pFormat[src] != 0 && pFormat[src++] != ch)
+ while (src < format.Length && pFormat[src] != 0 && pFormat[src++] != ch)
;
break;
case '\\':
- if (pFormat[src] != 0)
+ if (src < format.Length && pFormat[src] != 0)
src++;
break;
case ';':
if (--section != 0)
break;
- if (pFormat[src] != 0 && pFormat[src] != ';')
+ if (src < format.Length && pFormat[src] != 0 && pFormat[src] != ';')
return src;
goto case '\0';
case '\0':
return ToString(format, NumberFormatInfo.GetInstance(provider));
}
- public bool TryFormat(Span<char> destination, out int charsWritten, string format = null, IFormatProvider provider = null)
+ // TODO https://github.com/dotnet/corefx/issues/23642: Remove once corefx has been updated with new overloads.
+ public bool TryFormat(Span<char> destination, out int charsWritten, string format, IFormatProvider provider) =>
+ TryFormat(destination, out charsWritten, (ReadOnlySpan<char>)format, provider);
+
+ public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format = default, IFormatProvider provider = null)
{
NumberFormatInfo info = NumberFormatInfo.GetInstance(provider);
- if (m_value < 0 && format != null && format.Length > 0 && (format[0] == 'X' || format[0] == 'x'))
+ if (m_value < 0 && format.Length > 0 && (format[0] == 'X' || format[0] == 'x'))
{
uint temp = (uint)(m_value & 0x000000FF);
return Number.TryFormatUInt32(temp, format, info, destination, out charsWritten);
public static sbyte Parse(String s)
{
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
- return Parse(s.AsReadOnlySpan(), NumberStyles.Integer, NumberFormatInfo.CurrentInfo);
+ return Parse((ReadOnlySpan<char>)s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo);
}
[CLSCompliant(false)]
{
NumberFormatInfo.ValidateParseStyleInteger(style);
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
- return Parse(s.AsReadOnlySpan(), style, NumberFormatInfo.CurrentInfo);
+ return Parse((ReadOnlySpan<char>)s, style, NumberFormatInfo.CurrentInfo);
}
[CLSCompliant(false)]
public static sbyte Parse(String s, IFormatProvider provider)
{
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
- return Parse(s.AsReadOnlySpan(), NumberStyles.Integer, NumberFormatInfo.GetInstance(provider));
+ return Parse((ReadOnlySpan<char>)s, NumberStyles.Integer, NumberFormatInfo.GetInstance(provider));
}
// Parses a signed byte from a String in the given style. If
{
NumberFormatInfo.ValidateParseStyleInteger(style);
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
- return Parse(s.AsReadOnlySpan(), style, NumberFormatInfo.GetInstance(provider));
+ return Parse((ReadOnlySpan<char>)s, style, NumberFormatInfo.GetInstance(provider));
}
[CLSCompliant(false)]
private static sbyte Parse(String s, NumberStyles style, NumberFormatInfo info)
{
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
- return Parse(s.AsReadOnlySpan(), style, info);
+ return Parse((ReadOnlySpan<char>)s, style, info);
}
private static sbyte Parse(ReadOnlySpan<char> s, NumberStyles style, NumberFormatInfo info)
return false;
}
- return TryParse(s.AsReadOnlySpan(), NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result);
+ return TryParse((ReadOnlySpan<char>)s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result);
}
[CLSCompliant(false)]
return false;
}
- return TryParse(s.AsReadOnlySpan(), style, NumberFormatInfo.GetInstance(provider), out result);
+ return TryParse((ReadOnlySpan<char>)s, style, NumberFormatInfo.GetInstance(provider), out result);
}
- // TODO https://github.com/dotnet/corefx/issues/23642: Remove once corefx has been updated with new overloads.
- [CLSCompliant(false)]
- public static bool TryParse(ReadOnlySpan<char> s, out sbyte result, NumberStyles style = NumberStyles.Integer, IFormatProvider provider = null) =>
- TryParse(s, style, provider, out result);
-
[CLSCompliant(false)]
public static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider provider, out sbyte result)
{
public static float Parse(String s)
{
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
- return Number.ParseSingle(s.AsReadOnlySpan(), NumberStyles.Float | NumberStyles.AllowThousands, NumberFormatInfo.CurrentInfo);
+ return Number.ParseSingle(s, NumberStyles.Float | NumberStyles.AllowThousands, NumberFormatInfo.CurrentInfo);
}
public static float Parse(String s, NumberStyles style)
{
NumberFormatInfo.ValidateParseStyleFloatingPoint(style);
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
- return Number.ParseSingle(s.AsReadOnlySpan(), style, NumberFormatInfo.CurrentInfo);
+ return Number.ParseSingle(s, style, NumberFormatInfo.CurrentInfo);
}
public static float Parse(String s, IFormatProvider provider)
{
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
- return Number.ParseSingle(s.AsReadOnlySpan(), NumberStyles.Float | NumberStyles.AllowThousands, NumberFormatInfo.GetInstance(provider));
+ return Number.ParseSingle(s, NumberStyles.Float | NumberStyles.AllowThousands, NumberFormatInfo.GetInstance(provider));
}
public static float Parse(String s, NumberStyles style, IFormatProvider provider)
{
NumberFormatInfo.ValidateParseStyleFloatingPoint(style);
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
- return Number.ParseSingle(s.AsReadOnlySpan(), style, NumberFormatInfo.GetInstance(provider));
+ return Number.ParseSingle(s, style, NumberFormatInfo.GetInstance(provider));
}
public static float Parse(ReadOnlySpan<char> s, NumberStyles style = NumberStyles.Integer, IFormatProvider provider = null)
return false;
}
- return TryParse(s.AsReadOnlySpan(), NumberStyles.Float | NumberStyles.AllowThousands, NumberFormatInfo.CurrentInfo, out result);
+ return TryParse((ReadOnlySpan<char>)s, NumberStyles.Float | NumberStyles.AllowThousands, NumberFormatInfo.CurrentInfo, out result);
}
public static bool TryParse(ReadOnlySpan<char> s, out float result)
return false;
}
- return TryParse(s.AsReadOnlySpan(), style, NumberFormatInfo.GetInstance(provider), out result);
+ return TryParse((ReadOnlySpan<char>)s, style, NumberFormatInfo.GetInstance(provider), out result);
}
- // TODO https://github.com/dotnet/corefx/issues/23642: Remove once corefx has been updated with new overloads.
- public static Boolean TryParse(ReadOnlySpan<char> s, out Single result, NumberStyles style = NumberStyles.Integer, IFormatProvider provider = null) =>
- TryParse(s, style, provider, out result);
-
public static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider provider, out float result)
{
NumberFormatInfo.ValidateParseStyleFloatingPoint(style);
//
Object arg = args[index];
String itemFormat = null;
+ ReadOnlySpan<char> itemFormatSpan = default; // used if itemFormat is null
// Is current character a colon? which indicates start of formatting parameter.
if (ch == ':')
{
if (startPos != pos)
{
// There was no brace escaping, extract the item format as a single string
- itemFormat = format.Substring(startPos, pos - startPos);
+ itemFormatSpan = format.AsReadOnlySpan().Slice(startPos, pos - startPos);
}
}
else
{
unescapedItemFormat.Append(format, startPos, pos - startPos);
- itemFormat = unescapedItemFormat.ToString();
+ itemFormatSpan = itemFormat = unescapedItemFormat.ToString();
unescapedItemFormat.Clear();
}
}
String s = null;
if (cf != null)
{
+ if (itemFormatSpan.Length != 0 && itemFormat == null)
+ {
+ itemFormat = new string(itemFormatSpan);
+ }
s = cf.Format(itemFormat, arg, provider);
}
// try formatting it into the remaining current chunk.
if (arg is ISpanFormattable spanFormattableArg &&
(leftJustify || width == 0) &&
- spanFormattableArg.TryFormat(RemainingCurrentChunk, out int charsWritten, itemFormat, provider))
+ spanFormattableArg.TryFormat(RemainingCurrentChunk, out int charsWritten, itemFormatSpan, provider))
{
m_ChunkLength += charsWritten;
// Otherwise, fallback to trying IFormattable or calling ToString.
if (arg is IFormattable formattableArg)
{
+ if (itemFormatSpan.Length != 0 && itemFormat == null)
+ {
+ itemFormat = new string(itemFormatSpan);
+ }
s = formattableArg.ToString(itemFormat, provider);
}
else if (arg != null)
{
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.input);
/* Constructs a TimeSpan from a string. Leading and trailing white space characters are allowed. */
- return TimeSpanParse.Parse(s.AsReadOnlySpan(), null);
+ return TimeSpanParse.Parse(s, null);
}
public static TimeSpan Parse(String input, IFormatProvider formatProvider)
{
if (input == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.input);
- return TimeSpanParse.Parse(input.AsReadOnlySpan(), formatProvider);
+ return TimeSpanParse.Parse(input, formatProvider);
}
public static TimeSpan Parse(ReadOnlySpan<char> input, IFormatProvider formatProvider = null)
{
public static TimeSpan ParseExact(String input, String format, IFormatProvider formatProvider)
{
if (input == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.input);
- return TimeSpanParse.ParseExact(input.AsReadOnlySpan(), format, formatProvider, TimeSpanStyles.None);
+ if (format == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.format);
+ return TimeSpanParse.ParseExact(input, format, formatProvider, TimeSpanStyles.None);
}
public static TimeSpan ParseExact(String input, String[] formats, IFormatProvider formatProvider)
{
if (input == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.input);
- return TimeSpanParse.ParseExactMultiple(input.AsReadOnlySpan(), formats, formatProvider, TimeSpanStyles.None);
+ return TimeSpanParse.ParseExactMultiple(input, formats, formatProvider, TimeSpanStyles.None);
}
public static TimeSpan ParseExact(String input, String format, IFormatProvider formatProvider, TimeSpanStyles styles)
{
ValidateStyles(styles, nameof(styles));
if (input == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.input);
- return TimeSpanParse.ParseExact(input.AsReadOnlySpan(), format, formatProvider, styles);
+ if (format == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.format);
+ return TimeSpanParse.ParseExact(input, format, formatProvider, styles);
+ }
+
+ // TODO https://github.com/dotnet/corefx/issues/23642: Remove once corefx has been updated with new overloads.
+ public static TimeSpan ParseExact(ReadOnlySpan<char> input, string format, IFormatProvider formatProvider, TimeSpanStyles styles)
+ {
+ if (format == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.format);
+ return TimeSpanParse.ParseExact(input, format, formatProvider, styles);
}
- public static TimeSpan ParseExact(ReadOnlySpan<char> input, string format, IFormatProvider formatProvider, TimeSpanStyles styles = TimeSpanStyles.None)
+
+ public static TimeSpan ParseExact(ReadOnlySpan<char> input, ReadOnlySpan<char> format, IFormatProvider formatProvider, TimeSpanStyles styles = TimeSpanStyles.None)
{
ValidateStyles(styles, nameof(styles));
return TimeSpanParse.ParseExact(input, format, formatProvider, styles);
{
ValidateStyles(styles, nameof(styles));
if (input == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.input);
- return TimeSpanParse.ParseExactMultiple(input.AsReadOnlySpan(), formats, formatProvider, styles);
+ return TimeSpanParse.ParseExactMultiple(input, formats, formatProvider, styles);
}
public static TimeSpan ParseExact(ReadOnlySpan<char> input, string[] formats, IFormatProvider formatProvider, TimeSpanStyles styles = TimeSpanStyles.None)
{
result = default(TimeSpan);
return false;
}
- return TimeSpanParse.TryParse(s.AsReadOnlySpan(), null, out result);
+ return TimeSpanParse.TryParse(s, null, out result);
}
public static bool TryParse(ReadOnlySpan<char> s, out TimeSpan result)
{
return TimeSpanParse.TryParse(s, null, out result);
}
- // TODO https://github.com/dotnet/corefx/issues/23642: Remove once corefx has been updated with new overloads.
- public static bool TryParse(ReadOnlySpan<char> input, out TimeSpan result, IFormatProvider formatProvider = null) =>
- TryParse(input, formatProvider, out result);
-
public static Boolean TryParse(String input, IFormatProvider formatProvider, out TimeSpan result)
{
if (input == null)
result = default(TimeSpan);
return false;
}
- return TimeSpanParse.TryParse(input.AsReadOnlySpan(), formatProvider, out result);
+ return TimeSpanParse.TryParse(input, formatProvider, out result);
}
public static bool TryParse(ReadOnlySpan<char> input, IFormatProvider formatProvider, out TimeSpan result)
{
}
public static Boolean TryParseExact(String input, String format, IFormatProvider formatProvider, out TimeSpan result)
{
- if (input == null)
+ if (input == null || format == null)
{
- result = default(TimeSpan);
+ result = default;
return false;
}
- return TimeSpanParse.TryParseExact(input.AsReadOnlySpan(), format, formatProvider, TimeSpanStyles.None, out result);
+ return TimeSpanParse.TryParseExact(input, format, formatProvider, TimeSpanStyles.None, out result);
}
+
+ // TODO https://github.com/dotnet/corefx/issues/23642: Remove once corefx has been updated with new overloads.
public static bool TryParseExact(ReadOnlySpan<char> input, string format, IFormatProvider formatProvider, out TimeSpan result)
{
+ if (format == null)
+ {
+ result = default;
+ return false;
+ }
+
+ return TryParseExact(input, (ReadOnlySpan<char>)format, formatProvider, out result);
+ }
+
+ public static bool TryParseExact(ReadOnlySpan<char> input, ReadOnlySpan<char> format, IFormatProvider formatProvider, out TimeSpan result)
+ {
return TimeSpanParse.TryParseExact(input, format, formatProvider, TimeSpanStyles.None, out result);
}
public static Boolean TryParseExact(String input, String[] formats, IFormatProvider formatProvider, out TimeSpan result)
result = default(TimeSpan);
return false;
}
- return TimeSpanParse.TryParseExactMultiple(input.AsReadOnlySpan(), formats, formatProvider, TimeSpanStyles.None, out result);
+ return TimeSpanParse.TryParseExactMultiple(input, formats, formatProvider, TimeSpanStyles.None, out result);
}
public static bool TryParseExact(ReadOnlySpan<char> input, string[] formats, IFormatProvider formatProvider, out TimeSpan result)
{
return TimeSpanParse.TryParseExactMultiple(input, formats, formatProvider, TimeSpanStyles.None, out result);
}
- // TODO https://github.com/dotnet/corefx/issues/23642: Remove once corefx has been updated with new overloads.
- public static bool TryParseExact(ReadOnlySpan<char> input, string format, IFormatProvider formatProvider, out TimeSpan result, TimeSpanStyles styles = TimeSpanStyles.None) =>
- TryParseExact(input, format, formatProvider, styles, out result);
-
public static Boolean TryParseExact(String input, String format, IFormatProvider formatProvider, TimeSpanStyles styles, out TimeSpan result)
{
ValidateStyles(styles, nameof(styles));
- if (input == null)
+ if (input == null || format == null)
{
- result = default(TimeSpan);
+ result = default;
return false;
}
- return TimeSpanParse.TryParseExact(input.AsReadOnlySpan(), format, formatProvider, styles, out result);
+
+ return TimeSpanParse.TryParseExact(input, format, formatProvider, styles, out result);
}
+
+ // TODO https://github.com/dotnet/corefx/issues/23642: Remove once corefx has been updated with new overloads.
public static bool TryParseExact(ReadOnlySpan<char> input, string format, IFormatProvider formatProvider, TimeSpanStyles styles, out TimeSpan result)
{
+ if (format == null)
+ {
+ result = default;
+ return false;
+ }
+
+ return TimeSpanParse.TryParseExact(input, format, formatProvider, styles, out result);
+ }
+
+ public static bool TryParseExact(ReadOnlySpan<char> input, ReadOnlySpan<char> format, IFormatProvider formatProvider, TimeSpanStyles styles, out TimeSpan result)
+ {
ValidateStyles(styles, nameof(styles));
return TimeSpanParse.TryParseExact(input, format, formatProvider, styles, out result);
}
result = default(TimeSpan);
return false;
}
- return TimeSpanParse.TryParseExactMultiple(input.AsReadOnlySpan(), formats, formatProvider, styles, out result);
+ return TimeSpanParse.TryParseExactMultiple(input, formats, formatProvider, styles, out result);
}
- // TODO https://github.com/dotnet/corefx/issues/23642: Remove once corefx has been updated with new overloads.
- public static bool TryParseExact(ReadOnlySpan<char> input, string[] formats, IFormatProvider formatProvider, out TimeSpan result, TimeSpanStyles styles = TimeSpanStyles.None) =>
- TryParseExact(input, formats, formatProvider, styles, out result);
-
public static bool TryParseExact(ReadOnlySpan<char> input, string[] formats, IFormatProvider formatProvider, TimeSpanStyles styles, out TimeSpan result)
{
ValidateStyles(styles, nameof(styles));
{
return TimeSpanFormat.Format(this, format, formatProvider);
}
- public bool TryFormat(Span<char> destination, out int charsWritten, string format = null, IFormatProvider formatProvider = null)
+
+ // TODO https://github.com/dotnet/corefx/issues/23642: Remove once corefx has been updated with new overloads.
+ public bool TryFormat(Span<char> destination, out int charsWritten, string format, IFormatProvider formatProvider) =>
+ TryFormat(destination, out charsWritten, (ReadOnlySpan<char>)format, formatProvider);
+
+ public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format = default, IFormatProvider formatProvider = null)
{
return TimeSpanFormat.TryFormat(this, destination, out charsWritten, format, formatProvider);
}
return Number.FormatUInt32(m_value, format, NumberFormatInfo.GetInstance(provider));
}
- public bool TryFormat(Span<char> destination, out int charsWritten, string format = null, IFormatProvider provider = null)
+ // TODO https://github.com/dotnet/corefx/issues/23642: Remove once corefx has been updated with new overloads.
+ public bool TryFormat(Span<char> destination, out int charsWritten, string format, IFormatProvider provider) =>
+ TryFormat(destination, out charsWritten, (ReadOnlySpan<char>)format, provider);
+
+ public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format = default, IFormatProvider provider = null)
{
return Number.TryFormatUInt32(m_value, format, NumberFormatInfo.GetInstance(provider), destination, out charsWritten);
}
public static ushort Parse(String s)
{
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
- return Parse(s.AsReadOnlySpan(), NumberStyles.Integer, NumberFormatInfo.CurrentInfo);
+ return Parse((ReadOnlySpan<char>)s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo);
}
[CLSCompliant(false)]
{
NumberFormatInfo.ValidateParseStyleInteger(style);
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
- return Parse(s.AsReadOnlySpan(), style, NumberFormatInfo.CurrentInfo);
+ return Parse((ReadOnlySpan<char>)s, style, NumberFormatInfo.CurrentInfo);
}
public static ushort Parse(String s, IFormatProvider provider)
{
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
- return Parse(s.AsReadOnlySpan(), NumberStyles.Integer, NumberFormatInfo.GetInstance(provider));
+ return Parse((ReadOnlySpan<char>)s, NumberStyles.Integer, NumberFormatInfo.GetInstance(provider));
}
[CLSCompliant(false)]
{
NumberFormatInfo.ValidateParseStyleInteger(style);
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
- return Parse(s.AsReadOnlySpan(), style, NumberFormatInfo.GetInstance(provider));
+ return Parse((ReadOnlySpan<char>)s, style, NumberFormatInfo.GetInstance(provider));
}
[CLSCompliant(false)]
return false;
}
- return TryParse(s.AsReadOnlySpan(), NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result);
+ return TryParse((ReadOnlySpan<char>)s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result);
}
[CLSCompliant(false)]
return false;
}
- return TryParse(s.AsReadOnlySpan(), style, NumberFormatInfo.GetInstance(provider), out result);
+ return TryParse((ReadOnlySpan<char>)s, style, NumberFormatInfo.GetInstance(provider), out result);
}
- // TODO https://github.com/dotnet/corefx/issues/23642: Remove once corefx has been updated with new overloads.
- [CLSCompliant(false)]
- public static bool TryParse(ReadOnlySpan<char> s, out ushort result, NumberStyles style = NumberStyles.Integer, IFormatProvider provider = null) =>
- TryParse(s, style, provider, out result);
-
[CLSCompliant(false)]
public static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider provider, out ushort result)
{
return Number.FormatUInt32(m_value, format, NumberFormatInfo.GetInstance(provider));
}
- public bool TryFormat(Span<char> destination, out int charsWritten, string format = null, IFormatProvider provider = null)
+ // TODO https://github.com/dotnet/corefx/issues/23642: Remove once corefx has been updated with new overloads.
+ public bool TryFormat(Span<char> destination, out int charsWritten, string format, IFormatProvider provider) =>
+ TryFormat(destination, out charsWritten, (ReadOnlySpan<char>)format, provider);
+
+ public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format = default, IFormatProvider provider = null)
{
return Number.TryFormatUInt32(m_value, format, NumberFormatInfo.GetInstance(provider), destination, out charsWritten);
}
public static uint Parse(String s)
{
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
- return Number.ParseUInt32(s.AsReadOnlySpan(), NumberStyles.Integer, NumberFormatInfo.CurrentInfo);
+ return Number.ParseUInt32(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo);
}
[CLSCompliant(false)]
{
NumberFormatInfo.ValidateParseStyleInteger(style);
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
- return Number.ParseUInt32(s.AsReadOnlySpan(), style, NumberFormatInfo.CurrentInfo);
+ return Number.ParseUInt32(s, style, NumberFormatInfo.CurrentInfo);
}
public static uint Parse(String s, IFormatProvider provider)
{
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
- return Number.ParseUInt32(s.AsReadOnlySpan(), NumberStyles.Integer, NumberFormatInfo.GetInstance(provider));
+ return Number.ParseUInt32(s, NumberStyles.Integer, NumberFormatInfo.GetInstance(provider));
}
[CLSCompliant(false)]
{
NumberFormatInfo.ValidateParseStyleInteger(style);
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
- return Number.ParseUInt32(s.AsReadOnlySpan(), style, NumberFormatInfo.GetInstance(provider));
+ return Number.ParseUInt32(s, style, NumberFormatInfo.GetInstance(provider));
}
[CLSCompliant(false)]
return false;
}
- return Number.TryParseUInt32(s.AsReadOnlySpan(), NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result);
+ return Number.TryParseUInt32(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result);
}
[CLSCompliant(false)]
return false;
}
- return Number.TryParseUInt32(s.AsReadOnlySpan(), style, NumberFormatInfo.GetInstance(provider), out result);
+ return Number.TryParseUInt32(s, style, NumberFormatInfo.GetInstance(provider), out result);
}
- // TODO https://github.com/dotnet/corefx/issues/23642: Remove once corefx has been updated with new overloads.
- [CLSCompliant(false)]
- public static bool TryParse(ReadOnlySpan<char> s, out UInt32 result, NumberStyles style = NumberStyles.Integer, IFormatProvider provider = null) =>
- TryParse(s, style, provider, out result);
-
[CLSCompliant(false)]
public static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider provider, out uint result)
{
return Number.FormatUInt64(m_value, format, NumberFormatInfo.GetInstance(provider));
}
- public bool TryFormat(Span<char> destination, out int charsWritten, string format = null, IFormatProvider provider = null)
+ // TODO https://github.com/dotnet/corefx/issues/23642: Remove once corefx has been updated with new overloads.
+ public bool TryFormat(Span<char> destination, out int charsWritten, string format, IFormatProvider provider) =>
+ TryFormat(destination, out charsWritten, (ReadOnlySpan<char>)format, provider);
+
+ public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format = default, IFormatProvider provider = null)
{
return Number.TryFormatUInt64(m_value, format, NumberFormatInfo.GetInstance(provider), destination, out charsWritten);
}
public static ulong Parse(String s)
{
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
- return Number.ParseUInt64(s.AsReadOnlySpan(), NumberStyles.Integer, NumberFormatInfo.CurrentInfo);
+ return Number.ParseUInt64(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo);
}
[CLSCompliant(false)]
{
NumberFormatInfo.ValidateParseStyleInteger(style);
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
- return Number.ParseUInt64(s.AsReadOnlySpan(), style, NumberFormatInfo.CurrentInfo);
+ return Number.ParseUInt64(s, style, NumberFormatInfo.CurrentInfo);
}
[CLSCompliant(false)]
public static ulong Parse(string s, IFormatProvider provider)
{
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
- return Number.ParseUInt64(s.AsReadOnlySpan(), NumberStyles.Integer, NumberFormatInfo.GetInstance(provider));
+ return Number.ParseUInt64(s, NumberStyles.Integer, NumberFormatInfo.GetInstance(provider));
}
[CLSCompliant(false)]
{
NumberFormatInfo.ValidateParseStyleInteger(style);
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
- return Number.ParseUInt64(s.AsReadOnlySpan(), style, NumberFormatInfo.GetInstance(provider));
+ return Number.ParseUInt64(s, style, NumberFormatInfo.GetInstance(provider));
}
[CLSCompliant(false)]
return false;
}
- return Number.TryParseUInt64(s.AsReadOnlySpan(), NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result);
+ return Number.TryParseUInt64(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result);
}
[CLSCompliant(false)]
return false;
}
- return Number.TryParseUInt64(s.AsReadOnlySpan(), style, NumberFormatInfo.GetInstance(provider), out result);
+ return Number.TryParseUInt64(s, style, NumberFormatInfo.GetInstance(provider), out result);
}
- // TODO https://github.com/dotnet/corefx/issues/23642: Remove once corefx has been updated with new overloads.
- [CLSCompliant(false)]
- public static Boolean TryParse(ReadOnlySpan<char> s, out UInt64 result, NumberStyles style = NumberStyles.Integer, IFormatProvider provider = null) =>
- TryParse(s, style, provider, out result);
-
[CLSCompliant(false)]
public static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider provider, out ulong result)
{
return false;
}
- bool ISpanFormattable.TryFormat(Span<char> destination, out int charsWritten, string format, IFormatProvider provider)
+ bool ISpanFormattable.TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider provider)
{
// format and provider are ignored.
return TryFormat(destination, out charsWritten);
return Number.FormatDecimal(this, format, NumberFormatInfo.GetInstance(provider));
}
- public bool TryFormat(Span<char> destination, out int charsWritten, string format = null, IFormatProvider provider = null)
+ // TODO https://github.com/dotnet/corefx/issues/23642: Remove once corefx has been updated with new overloads.
+ public bool TryFormat(Span<char> destination, out int charsWritten, string format, IFormatProvider provider) =>
+ TryFormat(destination, out charsWritten, (ReadOnlySpan<char>)format, provider);
+
+ public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format = default, IFormatProvider provider = null)
{
return Number.TryFormatDecimal(this, format, NumberFormatInfo.GetInstance(provider), destination, out charsWritten);
}
public static Decimal Parse(String s)
{
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
- return Number.ParseDecimal(s.AsReadOnlySpan(), NumberStyles.Number, NumberFormatInfo.CurrentInfo);
+ return Number.ParseDecimal(s, NumberStyles.Number, NumberFormatInfo.CurrentInfo);
}
public static Decimal Parse(String s, NumberStyles style)
{
NumberFormatInfo.ValidateParseStyleFloatingPoint(style);
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
- return Number.ParseDecimal(s.AsReadOnlySpan(), style, NumberFormatInfo.CurrentInfo);
+ return Number.ParseDecimal(s, style, NumberFormatInfo.CurrentInfo);
}
public static Decimal Parse(String s, IFormatProvider provider)
{
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
- return Number.ParseDecimal(s.AsReadOnlySpan(), NumberStyles.Number, NumberFormatInfo.GetInstance(provider));
+ return Number.ParseDecimal(s, NumberStyles.Number, NumberFormatInfo.GetInstance(provider));
}
public static Decimal Parse(String s, NumberStyles style, IFormatProvider provider)
{
NumberFormatInfo.ValidateParseStyleFloatingPoint(style);
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
- return Number.ParseDecimal(s.AsReadOnlySpan(), style, NumberFormatInfo.GetInstance(provider));
+ return Number.ParseDecimal(s, style, NumberFormatInfo.GetInstance(provider));
}
public static decimal Parse(ReadOnlySpan<char> s, NumberStyles style = NumberStyles.Integer, IFormatProvider provider = null)
return false;
}
- return Number.TryParseDecimal(s.AsReadOnlySpan(), NumberStyles.Number, NumberFormatInfo.CurrentInfo, out result);
+ return Number.TryParseDecimal(s, NumberStyles.Number, NumberFormatInfo.CurrentInfo, out result);
}
public static bool TryParse(ReadOnlySpan<char> s, out decimal result)
return false;
}
- return Number.TryParseDecimal(s.AsReadOnlySpan(), style, NumberFormatInfo.GetInstance(provider), out result);
+ return Number.TryParseDecimal(s, style, NumberFormatInfo.GetInstance(provider), out result);
}
- // TODO https://github.com/dotnet/corefx/issues/23642: Remove once corefx has been updated with new overloads.
- public static bool TryParse(ReadOnlySpan<char> s, out decimal result, NumberStyles style = NumberStyles.Integer, IFormatProvider provider = null) =>
- TryParse(s, style, provider, out result);
-
public static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider provider, out decimal result)
{
NumberFormatInfo.ValidateParseStyleFloatingPoint(style);
input,
ownedMemory,
pointer,
- start
+ start,
+ format
}
//