Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 105 additions & 54 deletions src/libraries/Common/src/System/Number.Formatting.Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,40 +17,93 @@ internal static partial class Number
private const int DefaultPrecisionExponentialFormat = 6;

private const int MaxUInt32DecDigits = 10;
private const string PosNumberFormat = "#";

private static readonly string[] s_posCurrencyFormats =
[
"$#", "#$", "$ #", "# $"
];

private static readonly string[] s_negCurrencyFormats =
[
"($#)", "-$#", "$-#", "$#-",
"(#$)", "-#$", "#-$", "#$-",
"-# $", "-$ #", "# $-", "$ #-",
"$ -#", "#- $", "($ #)", "(# $)",
"$- #"
];

private static readonly string[] s_posPercentFormats =
[
"# %", "#%", "%#", "% #"
];

private static readonly string[] s_negPercentFormats =
[
"-# %", "-#%", "-%#",
"%-#", "%#-",
"#-%", "#%-",
"-% #", "# %-", "% #-",
"% -#", "#- %"
];

private static readonly string[] s_negNumberFormats =
[
"(#)", "-#", "- #", "#-", "# -",
];

private static ReadOnlySpan<byte> GetCurrencyFormat(bool isNegative, int index)
{
if (isNegative)
{
return index switch
{
0 => "($#)"u8,
1 => "-$#"u8,
2 => "$-#"u8,
3 => "$#-"u8,
4 => "(#$)"u8,
5 => "-#$"u8,
6 => "#-$"u8,
7 => "#$-"u8,
8 => "-# $"u8,
9 => "-$ #"u8,
10 => "# $-"u8,
11 => "$ #-"u8,
12 => "$ -#"u8,
13 => "#- $"u8,
14 => "($ #)"u8,
15 => "(# $)"u8,
16 => "$- #"u8,
_ => throw new UnreachableException(),
};
}

return index switch
{
0 => "$#"u8,
1 => "#$"u8,
2 => "$ #"u8,
3 => "# $"u8,
_ => throw new UnreachableException(),
};
}

private static ReadOnlySpan<byte> GetPercentFormat(bool isNegative, int index)
{
if (isNegative)
{
return index switch
{
0 => "-# %"u8,
1 => "-#%"u8,
2 => "-%#"u8,
3 => "%-#"u8,
4 => "%#-"u8,
5 => "#-%"u8,
6 => "#%-"u8,
7 => "-% #"u8,
8 => "# %-"u8,
9 => "% #-"u8,
10 => "% -#"u8,
11 => "#- %"u8,
_ => throw new UnreachableException(),
};
}

return index switch
{
0 => "# %"u8,
1 => "#%"u8,
2 => "%#"u8,
3 => "% #"u8,
_ => throw new UnreachableException(),
};
}

private static ReadOnlySpan<byte> GetNumberFormat(bool isNegative, int index)
{
if (!isNegative)
{
return "#"u8;
}

return index switch
{
0 => "(#)"u8,
1 => "-#"u8,
2 => "- #"u8,
3 => "#-"u8,
4 => "# -"u8,
_ => throw new UnreachableException(),
};
}

internal static char ParseFormatSpecifier(ReadOnlySpan<char> format, out int digits)
{
Expand Down Expand Up @@ -723,23 +776,23 @@ private static unsafe void FormatCurrency<TChar>(ref ValueListBuilder<TChar> vlb
{
Debug.Assert(sizeof(TChar) is sizeof(char) or sizeof(byte));

string fmt = number.IsNegative ?
s_negCurrencyFormats[info.CurrencyNegativePattern] :
s_posCurrencyFormats[info.CurrencyPositivePattern];
ReadOnlySpan<byte> fmt = GetCurrencyFormat(
number.IsNegative,
number.IsNegative ? info.CurrencyNegativePattern : info.CurrencyPositivePattern);

foreach (char ch in fmt)
foreach (byte ch in fmt)
{
switch (ch)
{
case '#':
case (byte)'#':
FormatFixed(ref vlb, ref number, nMaxDigits, info.CurrencyGroupSizes(), info.CurrencyDecimalSeparatorTChar<TChar>(), info.CurrencyGroupSeparatorTChar<TChar>());
break;

case '-':
case (byte)'-':
vlb.Append(info.NegativeSignTChar<TChar>());
break;

case '$':
case (byte)'$':
vlb.Append(info.CurrencySymbolTChar<TChar>());
break;

Expand Down Expand Up @@ -893,19 +946,17 @@ private static unsafe void FormatNumber<TChar>(ref ValueListBuilder<TChar> vlb,
{
Debug.Assert(sizeof(TChar) is sizeof(char) or sizeof(byte));

string fmt = number.IsNegative ?
s_negNumberFormats[info.NumberNegativePattern] :
PosNumberFormat;
ReadOnlySpan<byte> fmt = GetNumberFormat(number.IsNegative, info.NumberNegativePattern);

foreach (char ch in fmt)
foreach (byte ch in fmt)
{
switch (ch)
{
case '#':
case (byte)'#':
FormatFixed(ref vlb, ref number, nMaxDigits, info.NumberGroupSizes(), info.NumberDecimalSeparatorTChar<TChar>(), info.NumberGroupSeparatorTChar<TChar>());
break;

case '-':
case (byte)'-':
vlb.Append(info.NegativeSignTChar<TChar>());
break;

Expand Down Expand Up @@ -1020,23 +1071,23 @@ private static unsafe void FormatPercent<TChar>(ref ValueListBuilder<TChar> vlb,
{
Debug.Assert(sizeof(TChar) is sizeof(char) or sizeof(byte));

string fmt = number.IsNegative ?
s_negPercentFormats[info.PercentNegativePattern] :
s_posPercentFormats[info.PercentPositivePattern];
ReadOnlySpan<byte> fmt = GetPercentFormat(
number.IsNegative,
number.IsNegative ? info.PercentNegativePattern : info.PercentPositivePattern);

foreach (char ch in fmt)
foreach (byte ch in fmt)
{
switch (ch)
{
case '#':
case (byte)'#':
FormatFixed(ref vlb, ref number, nMaxDigits, info.PercentGroupSizes(), info.PercentDecimalSeparatorTChar<TChar>(), info.PercentGroupSeparatorTChar<TChar>());
break;

case '-':
case (byte)'-':
vlb.Append(info.NegativeSignTChar<TChar>());
break;

case '%':
case (byte)'%':
vlb.Append(info.PercentSymbolTChar<TChar>());
break;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,28 @@ namespace System.Globalization
{
public partial class CompareInfo
{
// Characters which require special handling are those in [0x00, 0x1F] and [0x7F, 0xFFFF] except \t\v\f
// Matches HighCharTable below.
private static readonly SearchValues<char> s_nonSpecialAsciiChars =
SearchValues.Create("\t\v\f !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~");
private static class IcuSearchValues
{
// Characters which do not require special handling
internal static readonly SearchValues<char> s_nonSpecialAsciiChars = CreateNonSpecialAsciiChars();

private static SearchValues<char> CreateNonSpecialAsciiChars()
{
ReadOnlySpan<bool> highCharTable = HighCharTable;
Span<char> values = stackalloc char[highCharTable.Length];
int valueIndex = 0;

for (int i = 0; i < highCharTable.Length; i++)
{
if (!highCharTable[i])
{
values[valueIndex++] = (char)i;
}
}

return SearchValues.Create(values.Slice(0, valueIndex));
}
}

[NonSerialized]
private bool _isAsciiEqualityOrdinal;
Expand Down Expand Up @@ -114,14 +132,14 @@ private unsafe int IndexOfOrdinalIgnoreCaseHelper(ReadOnlySpan<char> source, Rea
char* a = ap;
char* b = bp;

if (target.ContainsAnyExcept(s_nonSpecialAsciiChars))
if (target.ContainsAnyExcept(IcuSearchValues.s_nonSpecialAsciiChars))
{
goto InteropCall;
}

if (target.Length > source.Length)
{
if (source.ContainsAnyExcept(s_nonSpecialAsciiChars))
if (source.ContainsAnyExcept(IcuSearchValues.s_nonSpecialAsciiChars))
{
goto InteropCall;
}
Expand Down Expand Up @@ -197,7 +215,7 @@ private unsafe int IndexOfOrdinalIgnoreCaseHelper(ReadOnlySpan<char> source, Rea
? source.Slice(endIndex)
: source.Slice(0, startIndex);

if (remainingSource.ContainsAnyExcept(s_nonSpecialAsciiChars))
if (remainingSource.ContainsAnyExcept(IcuSearchValues.s_nonSpecialAsciiChars))
{
goto InteropCall;
}
Expand Down Expand Up @@ -229,14 +247,14 @@ private unsafe int IndexOfOrdinalHelper(ReadOnlySpan<char> source, ReadOnlySpan<
char* a = ap;
char* b = bp;

if (target.ContainsAnyExcept(s_nonSpecialAsciiChars))
if (target.ContainsAnyExcept(IcuSearchValues.s_nonSpecialAsciiChars))
{
goto InteropCall;
}

if (target.Length > source.Length)
{
if (source.ContainsAnyExcept(s_nonSpecialAsciiChars))
if (source.ContainsAnyExcept(IcuSearchValues.s_nonSpecialAsciiChars))
{
goto InteropCall;
}
Expand Down
Loading
Loading