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
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Unicode;

namespace System
{
Expand Down Expand Up @@ -1377,7 +1378,27 @@ internal static void ThrowOverflowOrFormatException<TChar, TInteger>(ParsingStat
internal static void ThrowFormatException<TChar>(ReadOnlySpan<TChar> value)
where TChar : unmanaged, IUtfChar<TChar>
{
throw new FormatException(SR.Format(SR.Format_InvalidStringWithValue, value.ToString()));
string errorMessage;

if (typeof(TChar) == typeof(byte))
{
// Decode the UTF8 value into a string we can include in the error message. We're here
// because we failed to parse, which also means the bytes might not be valid UTF8,
// so fallback to a message that doesn't include the value if the bytes are invalid.
// It's possible after we check the bytes for validity that they could be concurrently
// mutated, but if that's happening, all bets are off, anyway, and it simply impacts
// which exception is thrown.
ReadOnlySpan<byte> bytes = MemoryMarshal.Cast<TChar, byte>(value);
errorMessage = Utf8.IsValid(bytes) ?
SR.Format(SR.Format_InvalidStringWithValue, Encoding.UTF8.GetString(bytes)) :
SR.Format_InvalidString;
Comment thread
stephentoub marked this conversation as resolved.
}
else
{
errorMessage = SR.Format(SR.Format_InvalidStringWithValue, value.ToString());
}

throw new FormatException(errorMessage);
}

[DoesNotReturn]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,13 +405,26 @@ public static void Parse_Utf8Span_Invalid(string value, NumberStyles style, IFor
Assert.Equal(0u, result);
}

Assert.Throws(exceptionType, () => byte.Parse(Encoding.UTF8.GetBytes(value), style, provider));
Exception e = Assert.Throws(exceptionType, () => byte.Parse(Encoding.UTF8.GetBytes(value), style, provider));
if (e is FormatException fe)
{
Assert.Contains(value, fe.Message);
}

Assert.False(byte.TryParse(valueUtf8, style, provider, out result));
Assert.Equal(0u, result);
}
}

[Fact]
public static void Parse_Utf8Span_InvalidUtf8()
{
FormatException fe = Assert.Throws<FormatException>(() => byte.Parse([0xA0]));
Assert.DoesNotContain("A0", fe.Message, StringComparison.Ordinal);
Assert.DoesNotContain("ReadOnlySpan", fe.Message, StringComparison.Ordinal);
Assert.DoesNotContain("\uFFFD", fe.Message, StringComparison.Ordinal);
}

[Theory]
[MemberData(nameof(ToString_TestData))]
public static void TryFormat(byte i, string format, IFormatProvider provider, string expected) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1037,13 +1037,26 @@ public static void Parse_Utf8Span_Invalid(string value, NumberStyles style, IFor
if (value != null)
{
ReadOnlySpan<byte> valueUtf8 = Encoding.UTF8.GetBytes(value);
Assert.Throws(exceptionType, () => decimal.Parse(Encoding.UTF8.GetBytes(value), style, provider));
Exception e = Assert.Throws(exceptionType, () => decimal.Parse(Encoding.UTF8.GetBytes(value), style, provider));
if (e is FormatException fe)
{
Assert.Contains(value, fe.Message);
}

Assert.False(decimal.TryParse(valueUtf8, style, provider, out decimal result));
Assert.Equal(0, result);
}
}

[Fact]
public static void Parse_Utf8Span_InvalidUtf8()
{
FormatException fe = Assert.Throws<FormatException>(() => decimal.Parse([0xA0]));
Assert.DoesNotContain("A0", fe.Message, StringComparison.Ordinal);
Assert.DoesNotContain("ReadOnlySpan", fe.Message, StringComparison.Ordinal);
Assert.DoesNotContain("\uFFFD", fe.Message, StringComparison.Ordinal);
}

public static IEnumerable<object[]> Remainder_Valid_TestData()
{
decimal NegativeZero = new decimal(0, 0, 0, true, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -658,13 +658,26 @@ public static void Parse_Utf8Span_Invalid(string value, NumberStyles style, IFor
if (value != null)
{
ReadOnlySpan<byte> valueUtf8 = Encoding.UTF8.GetBytes(value);
Assert.Throws(exceptionType, () => double.Parse(Encoding.UTF8.GetBytes(value), style, provider));
Exception e = Assert.Throws(exceptionType, () => double.Parse(Encoding.UTF8.GetBytes(value), style, provider));
if (e is FormatException fe)
{
Assert.Contains(value, fe.Message);
}

Assert.False(double.TryParse(valueUtf8, style, provider, out double result));
Assert.Equal(0, result);
}
}

[Fact]
public static void Parse_Utf8Span_InvalidUtf8()
{
FormatException fe = Assert.Throws<FormatException>(() => double.Parse([0xA0]));
Assert.DoesNotContain("A0", fe.Message, StringComparison.Ordinal);
Assert.DoesNotContain("ReadOnlySpan", fe.Message, StringComparison.Ordinal);
Assert.DoesNotContain("\uFFFD", fe.Message, StringComparison.Ordinal);
}

[Fact]
public static void PositiveInfinity()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -921,13 +921,26 @@ public static void Parse_Utf8Span_Invalid(string value, NumberStyles style, IFor
if (value != null)
{
ReadOnlySpan<byte> valueUtf8 = Encoding.UTF8.GetBytes(value);
Assert.Throws(exceptionType, () => float.Parse(Encoding.UTF8.GetBytes(value), style, provider));
Exception e = Assert.Throws(exceptionType, () => Half.Parse(Encoding.UTF8.GetBytes(value), style, provider));
Comment thread
stephentoub marked this conversation as resolved.
if (e is FormatException fe)
{
Assert.Contains(value, fe.Message);
}

Assert.False(float.TryParse(valueUtf8, style, provider, out float result));
Assert.Equal(0, result);
}
}

[Fact]
public static void Parse_Utf8Span_InvalidUtf8()
{
FormatException fe = Assert.Throws<FormatException>(() => Half.Parse([0xA0]));
Assert.DoesNotContain("A0", fe.Message, StringComparison.Ordinal);
Assert.DoesNotContain("ReadOnlySpan", fe.Message, StringComparison.Ordinal);
Assert.DoesNotContain("\uFFFD", fe.Message, StringComparison.Ordinal);
}

public static IEnumerable<object[]> ToString_TestData()
{
yield return new object[] { -4570.0f, "G", null, "-4570" };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,13 +476,26 @@ public static void Parse_Utf8Span_Invalid(string value, NumberStyles style, IFor
Assert.Equal(0, result);
}

Assert.Throws(exceptionType, () => Int128.Parse(Encoding.UTF8.GetBytes(value), style, provider));
Exception e = Assert.Throws(exceptionType, () => Int128.Parse(Encoding.UTF8.GetBytes(value), style, provider));
if (e is FormatException fe)
{
Assert.Contains(value, fe.Message);
}

Assert.False(Int128.TryParse(valueUtf8, style, provider, out result));
Assert.Equal(0, result);
}
}

[Fact]
public static void Parse_Utf8Span_InvalidUtf8()
{
FormatException fe = Assert.Throws<FormatException>(() => Int128.Parse([0xA0]));
Assert.DoesNotContain("A0", fe.Message, StringComparison.Ordinal);
Assert.DoesNotContain("ReadOnlySpan", fe.Message, StringComparison.Ordinal);
Assert.DoesNotContain("\uFFFD", fe.Message, StringComparison.Ordinal);
}

[Theory]
[MemberData(nameof(ToString_TestData))]
public static void TryFormat(Int128 i, string format, IFormatProvider provider, string expected) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -430,13 +430,26 @@ public static void Parse_Utf8Span_Invalid(string value, NumberStyles style, IFor
Assert.Equal(0, result);
}

Assert.Throws(exceptionType, () => short.Parse(Encoding.UTF8.GetBytes(value), style, provider));
Exception e = Assert.Throws(exceptionType, () => short.Parse(Encoding.UTF8.GetBytes(value), style, provider));
if (e is FormatException fe)
{
Assert.Contains(value, fe.Message);
}

Assert.False(short.TryParse(valueUtf8, style, provider, out result));
Assert.Equal(0, result);
}
}

[Fact]
public static void Parse_Utf8Span_InvalidUtf8()
{
FormatException fe = Assert.Throws<FormatException>(() => short.Parse([0xA0]));
Assert.DoesNotContain("A0", fe.Message, StringComparison.Ordinal);
Assert.DoesNotContain("ReadOnlySpan", fe.Message, StringComparison.Ordinal);
Assert.DoesNotContain("\uFFFD", fe.Message, StringComparison.Ordinal);
}

[Theory]
[MemberData(nameof(ToString_TestData))]
public static void TryFormat(short i, string format, IFormatProvider provider, string expected) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -864,13 +864,26 @@ public static void Parse_Utf8Span_Invalid(string value, NumberStyles style, IFor
Assert.Equal(0, result);
}

Assert.Throws(exceptionType, () => int.Parse(Encoding.UTF8.GetBytes(value), style, provider));
Exception e = Assert.Throws(exceptionType, () => int.Parse(Encoding.UTF8.GetBytes(value), style, provider));
if (e is FormatException fe)
{
Assert.Contains(value, fe.Message);
}

Assert.False(int.TryParse(valueUtf8, style, provider, out result));
Assert.Equal(0, result);
}
}

[Fact]
public static void Parse_Utf8Span_InvalidUtf8()
{
FormatException fe = Assert.Throws<FormatException>(() => int.Parse([0xA0]));
Assert.DoesNotContain("A0", fe.Message, StringComparison.Ordinal);
Assert.DoesNotContain("ReadOnlySpan", fe.Message, StringComparison.Ordinal);
Assert.DoesNotContain("\uFFFD", fe.Message, StringComparison.Ordinal);
}

[Theory]
[InlineData("N")]
[InlineData("F")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -458,13 +458,26 @@ public static void Parse_Utf8Span_Invalid(string value, NumberStyles style, IFor
Assert.Equal(0, result);
}

Assert.Throws(exceptionType, () => long.Parse(Encoding.UTF8.GetBytes(value), style, provider));
Exception e = Assert.Throws(exceptionType, () => long.Parse(Encoding.UTF8.GetBytes(value), style, provider));
if (e is FormatException fe)
{
Assert.Contains(value, fe.Message);
}

Assert.False(long.TryParse(valueUtf8, style, provider, out result));
Assert.Equal(0, result);
}
}

[Fact]
public static void Parse_Utf8Span_InvalidUtf8()
{
FormatException fe = Assert.Throws<FormatException>(() => long.Parse([0xA0]));
Assert.DoesNotContain("A0", fe.Message, StringComparison.Ordinal);
Assert.DoesNotContain("ReadOnlySpan", fe.Message, StringComparison.Ordinal);
Assert.DoesNotContain("\uFFFD", fe.Message, StringComparison.Ordinal);
}

[Theory]
[MemberData(nameof(ToString_TestData))]
public static void TryFormat(long i, string format, IFormatProvider provider, string expected) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1021,13 +1021,26 @@ public static void Parse_Utf8Span_Invalid(string value, NumberStyles style, IFor
Assert.Equal(default, result);
}

Assert.Throws(exceptionType, () => int.Parse(Encoding.UTF8.GetBytes(value), style, provider));
Exception e = Assert.Throws(exceptionType, () => nint.Parse(Encoding.UTF8.GetBytes(value), style, provider));
Comment thread
stephentoub marked this conversation as resolved.
if (e is FormatException fe)
{
Assert.Contains(value, fe.Message);
}

Assert.False(nint.TryParse(valueUtf8, style, provider, out result));
Assert.Equal(default, result);
}
}

[Fact]
public static void Parse_Utf8Span_InvalidUtf8()
{
FormatException fe = Assert.Throws<FormatException>(() => nint.Parse([0xA0]));
Assert.DoesNotContain("A0", fe.Message, StringComparison.Ordinal);
Assert.DoesNotContain("ReadOnlySpan", fe.Message, StringComparison.Ordinal);
Assert.DoesNotContain("\uFFFD", fe.Message, StringComparison.Ordinal);
}

[Theory]
[InlineData("N")]
[InlineData("F")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,13 +424,26 @@ public static void Parse_Utf8Span_Invalid(string value, NumberStyles style, IFor
Assert.Equal(0, result);
}

Assert.Throws(exceptionType, () => sbyte.Parse(Encoding.UTF8.GetBytes(value), style, provider));
Exception e = Assert.Throws(exceptionType, () => sbyte.Parse(Encoding.UTF8.GetBytes(value), style, provider));
if (e is FormatException fe)
{
Assert.Contains(value, fe.Message);
}

Assert.False(sbyte.TryParse(valueUtf8, style, provider, out result));
Assert.Equal(0, result);
}
}

[Fact]
public static void Parse_Utf8Span_InvalidUtf8()
{
FormatException fe = Assert.Throws<FormatException>(() => sbyte.Parse([0xA0]));
Assert.DoesNotContain("A0", fe.Message, StringComparison.Ordinal);
Assert.DoesNotContain("ReadOnlySpan", fe.Message, StringComparison.Ordinal);
Assert.DoesNotContain("\uFFFD", fe.Message, StringComparison.Ordinal);
}

[Theory]
[MemberData(nameof(ToString_TestData))]
public static void TryFormat(sbyte i, string format, IFormatProvider provider, string expected) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -597,13 +597,26 @@ public static void Parse_Utf8Span_Invalid(string value, NumberStyles style, IFor
if (value != null)
{
ReadOnlySpan<byte> valueUtf8 = Encoding.UTF8.GetBytes(value);
Assert.Throws(exceptionType, () => float.Parse(Encoding.UTF8.GetBytes(value), style, provider));
Exception e = Assert.Throws(exceptionType, () => float.Parse(Encoding.UTF8.GetBytes(value), style, provider));
if (e is FormatException fe)
{
Assert.Contains(value, fe.Message);
}

Assert.False(float.TryParse(valueUtf8, style, provider, out float result));
Assert.Equal(0, result);
}
}

[Fact]
public static void Parse_Utf8Span_InvalidUtf8()
{
FormatException fe = Assert.Throws<FormatException>(() => float.Parse([0xA0]));
Assert.DoesNotContain("A0", fe.Message, StringComparison.Ordinal);
Assert.DoesNotContain("ReadOnlySpan", fe.Message, StringComparison.Ordinal);
Assert.DoesNotContain("\uFFFD", fe.Message, StringComparison.Ordinal);
}

[Fact]
public static void PositiveInfinity()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -463,13 +463,26 @@ public static void Parse_Utf8Span_Invalid(string value, NumberStyles style, IFor
Assert.Equal(0u, result);
}

Assert.Throws(exceptionType, () => UInt128.Parse(Encoding.UTF8.GetBytes(value), style, provider));
Exception e = Assert.Throws(exceptionType, () => UInt128.Parse(Encoding.UTF8.GetBytes(value), style, provider));
if (e is FormatException fe)
{
Assert.Contains(value, fe.Message);
}

Assert.False(UInt128.TryParse(valueUtf8, style, provider, out result));
Assert.Equal(0u, result);
}
}

[Fact]
public static void Parse_Utf8Span_InvalidUtf8()
{
FormatException fe = Assert.Throws<FormatException>(() => UInt128.Parse([0xA0]));
Assert.DoesNotContain("A0", fe.Message, StringComparison.Ordinal);
Assert.DoesNotContain("ReadOnlySpan", fe.Message, StringComparison.Ordinal);
Assert.DoesNotContain("\uFFFD", fe.Message, StringComparison.Ordinal);
}

[Theory]
[MemberData(nameof(ToString_TestData))]
public static void TryFormat(UInt128 i, string format, IFormatProvider provider, string expected) =>
Expand Down
Loading