Skip to content

Commit 5686738

Browse files
paulirwinclaude
andcommitted
UnicodeUtil: Change UTF8toUTF16 to throw DecoderFallbackException, #1024
Changed UTF8toUTF16 method to throw DecoderFallbackException instead of FormatException when invalid UTF-8 is encountered. This aligns with .NET conventions where DecoderFallbackException is the appropriate exception type for character decoding issues (equivalent to Java's CharacterCodingException). 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 36b57d5 commit 5686738

2 files changed

Lines changed: 7 additions & 6 deletions

File tree

src/Lucene.Net.Tests/Util/TestUnicodeUtil.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Lucene.Net.Attributes;
44
using NUnit.Framework;
55
using System;
6+
using System.Text;
67
using Assert = Lucene.Net.TestFramework.Assert;
78

89
namespace Lucene.Net.Util
@@ -339,7 +340,7 @@ public void TestUTF8toUTF16Exception(byte[] invalidUtf8, bool shouldThrow)
339340

340341
if (shouldThrow)
341342
{
342-
Assert.Throws<FormatException>(() => UnicodeUtil.UTF8toUTF16(invalidUtf8, scratch));
343+
Assert.Throws<DecoderFallbackException>(() => UnicodeUtil.UTF8toUTF16(invalidUtf8, scratch));
343344
}
344345
else
345346
{

src/Lucene.Net/Util/UnicodeUtil.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -975,7 +975,7 @@ public static string ToHexString(string s)
975975
/// it doesn't provide enough space to hold the worst case of each byte becoming a UTF-16 codepoint.
976976
/// <para/>
977977
/// NOTE: Full characters are read, even if this reads past the length passed (and
978-
/// can result in an <see cref="FormatException"/> if invalid UTF-8 is passed).
978+
/// can result in a <see cref="DecoderFallbackException"/> if invalid UTF-8 is passed).
979979
/// Explicit checks for valid UTF-8 are not performed.
980980
/// </summary>
981981
/// <seealso cref="UTF8toUTF16(ReadOnlySpan{byte}, CharsRef)"/>
@@ -990,7 +990,7 @@ public static void UTF8toUTF16(byte[] utf8, int offset, int length, CharsRef cha
990990
/// it doesn't provide enough space to hold the worst case of each byte becoming a UTF-16 codepoint.
991991
/// <para/>
992992
/// NOTE: Full characters are read, even if this reads past the length passed (and
993-
/// can result in an <see cref="FormatException"/> if invalid UTF-8 is passed).
993+
/// can result in a <see cref="DecoderFallbackException"/> if invalid UTF-8 is passed).
994994
/// Explicit checks for valid UTF-8 are not performed.
995995
/// </summary>
996996
/// <remarks>
@@ -1015,15 +1015,15 @@ public static void UTF8toUTF16(ReadOnlySpan<byte> utf8, CharsRef chars)
10151015
{
10161016
if (utf8.Length <= i)
10171017
{
1018-
throw new FormatException($"Invalid UTF-8 starting at [{b:x2}] at offset {i - 1}");
1018+
throw new DecoderFallbackException($"Invalid UTF-8 starting at [{b:x2}] at offset {i - 1}");
10191019
}
10201020
@out[out_offset++] = (char)(((b & 0x1f) << 6) + (utf8[i++] & 0x3f));
10211021
}
10221022
else if (b < 0xf0)
10231023
{
10241024
if (utf8.Length <= i + 1)
10251025
{
1026-
throw new FormatException($"Invalid UTF-8 starting at [{b:x2}] at offset {i - 1}");
1026+
throw new DecoderFallbackException($"Invalid UTF-8 starting at [{b:x2}] at offset {i - 1}");
10271027
}
10281028
@out[out_offset++] = (char)(((b & 0xf) << 12) + ((utf8[i] & 0x3f) << 6) + (utf8[i + 1] & 0x3f));
10291029
i += 2;
@@ -1032,7 +1032,7 @@ public static void UTF8toUTF16(ReadOnlySpan<byte> utf8, CharsRef chars)
10321032
{
10331033
if (utf8.Length <= i + 2)
10341034
{
1035-
throw new FormatException($"Invalid UTF-8 starting at [{b:x2}] at offset {i - 1}");
1035+
throw new DecoderFallbackException($"Invalid UTF-8 starting at [{b:x2}] at offset {i - 1}");
10361036
}
10371037
if (Debugging.AssertsEnabled) Debugging.Assert(b < 0xf8, "b = 0x{0:x}", b);
10381038
int ch = ((b & 0x7) << 18) + ((utf8[i] & 0x3f) << 12) + ((utf8[i + 1] & 0x3f) << 6) + (utf8[i + 2] & 0x3f);

0 commit comments

Comments
 (0)