Skip to content

Commit

Permalink
Use FormatException instead of ParseException
Browse files Browse the repository at this point in the history
  • Loading branch information
paulirwin committed Dec 18, 2024
1 parent 3f2dbc1 commit 76bb2da
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/Lucene.Net.Tests/Util/TestUnicodeUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ public void TestUTF8toUTF16Exception(byte[] invalidUtf8, bool shouldThrow)

if (shouldThrow)
{
Assert.Throws<ParseException>(() => UnicodeUtil.UTF8toUTF16(invalidUtf8, scratch));
Assert.Throws<FormatException>(() => UnicodeUtil.UTF8toUTF16(invalidUtf8, scratch));
}
else
{
Expand Down
11 changes: 5 additions & 6 deletions src/Lucene.Net/Util/UnicodeUtil.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using J2N;
using J2N.Text;
using Lucene.Net.Diagnostics;
using Lucene.Net.Support;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
Expand Down Expand Up @@ -887,7 +886,7 @@ public static string ToHexString(string s)
/// it doesn't provide enough space to hold the worst case of each byte becoming a UTF-16 codepoint.
/// <para/>
/// NOTE: Full characters are read, even if this reads past the length passed (and
/// can result in an <see cref="IndexOutOfRangeException"/> if invalid UTF-8 is passed).
/// can result in an <see cref="FormatException"/> if invalid UTF-8 is passed).
/// Explicit checks for valid UTF-8 are not performed.
/// </summary>
/// <seealso cref="UTF8toUTF16(ReadOnlySpan{byte}, CharsRef)"/>
Expand All @@ -902,7 +901,7 @@ public static void UTF8toUTF16(byte[] utf8, int offset, int length, CharsRef cha
/// it doesn't provide enough space to hold the worst case of each byte becoming a UTF-16 codepoint.
/// <para/>
/// NOTE: Full characters are read, even if this reads past the length passed (and
/// can result in an <see cref="IndexOutOfRangeException"/> if invalid UTF-8 is passed).
/// can result in an <see cref="FormatException"/> if invalid UTF-8 is passed).
/// Explicit checks for valid UTF-8 are not performed.
/// </summary>
/// <remarks>
Expand All @@ -927,15 +926,15 @@ public static void UTF8toUTF16(ReadOnlySpan<byte> utf8, CharsRef chars)
{
if (utf8.Length <= i)
{
throw new ParseException($"Invalid UTF-8 starting at [{b:x2}] at offset {i - 1}", i - 1);
throw new FormatException($"Invalid UTF-8 starting at [{b:x2}] at offset {i - 1}");
}
@out[out_offset++] = (char)(((b & 0x1f) << 6) + (utf8[i++] & 0x3f));
}
else if (b < 0xf0)
{
if (utf8.Length <= i + 1)
{
throw new ParseException($"Invalid UTF-8 starting at [{b:x2}] at offset {i - 1}", i - 1);
throw new FormatException($"Invalid UTF-8 starting at [{b:x2}] at offset {i - 1}");
}
@out[out_offset++] = (char)(((b & 0xf) << 12) + ((utf8[i] & 0x3f) << 6) + (utf8[i + 1] & 0x3f));
i += 2;
Expand All @@ -944,7 +943,7 @@ public static void UTF8toUTF16(ReadOnlySpan<byte> utf8, CharsRef chars)
{
if (utf8.Length <= i + 2)
{
throw new ParseException($"Invalid UTF-8 starting at [{b:x2}] at offset {i - 1}", i - 1);
throw new FormatException($"Invalid UTF-8 starting at [{b:x2}] at offset {i - 1}");
}
if (Debugging.AssertsEnabled) Debugging.Assert(b < 0xf8, "b = 0x{0:x}", b);
int ch = ((b & 0x7) << 18) + ((utf8[i] & 0x3f) << 12) + ((utf8[i + 1] & 0x3f) << 6) + (utf8[i + 2] & 0x3f);
Expand Down

0 comments on commit 76bb2da

Please sign in to comment.