Skip to content

Commit a14d540

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 0a8d39c commit a14d540

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
@@ -886,7 +886,7 @@ public static string ToHexString(string s)
886886
/// it doesn't provide enough space to hold the worst case of each byte becoming a UTF-16 codepoint.
887887
/// <para/>
888888
/// NOTE: Full characters are read, even if this reads past the length passed (and
889-
/// can result in an <see cref="FormatException"/> if invalid UTF-8 is passed).
889+
/// can result in a <see cref="DecoderFallbackException"/> if invalid UTF-8 is passed).
890890
/// Explicit checks for valid UTF-8 are not performed.
891891
/// </summary>
892892
/// <seealso cref="UTF8toUTF16(ReadOnlySpan{byte}, CharsRef)"/>
@@ -901,7 +901,7 @@ public static void UTF8toUTF16(byte[] utf8, int offset, int length, CharsRef cha
901901
/// it doesn't provide enough space to hold the worst case of each byte becoming a UTF-16 codepoint.
902902
/// <para/>
903903
/// NOTE: Full characters are read, even if this reads past the length passed (and
904-
/// can result in an <see cref="FormatException"/> if invalid UTF-8 is passed).
904+
/// can result in a <see cref="DecoderFallbackException"/> if invalid UTF-8 is passed).
905905
/// Explicit checks for valid UTF-8 are not performed.
906906
/// </summary>
907907
/// <remarks>
@@ -926,15 +926,15 @@ public static void UTF8toUTF16(ReadOnlySpan<byte> utf8, CharsRef chars)
926926
{
927927
if (utf8.Length <= i)
928928
{
929-
throw new FormatException($"Invalid UTF-8 starting at [{b:x2}] at offset {i - 1}");
929+
throw new DecoderFallbackException($"Invalid UTF-8 starting at [{b:x2}] at offset {i - 1}");
930930
}
931931
@out[out_offset++] = (char)(((b & 0x1f) << 6) + (utf8[i++] & 0x3f));
932932
}
933933
else if (b < 0xf0)
934934
{
935935
if (utf8.Length <= i + 1)
936936
{
937-
throw new FormatException($"Invalid UTF-8 starting at [{b:x2}] at offset {i - 1}");
937+
throw new DecoderFallbackException($"Invalid UTF-8 starting at [{b:x2}] at offset {i - 1}");
938938
}
939939
@out[out_offset++] = (char)(((b & 0xf) << 12) + ((utf8[i] & 0x3f) << 6) + (utf8[i + 1] & 0x3f));
940940
i += 2;
@@ -943,7 +943,7 @@ public static void UTF8toUTF16(ReadOnlySpan<byte> utf8, CharsRef chars)
943943
{
944944
if (utf8.Length <= i + 2)
945945
{
946-
throw new FormatException($"Invalid UTF-8 starting at [{b:x2}] at offset {i - 1}");
946+
throw new DecoderFallbackException($"Invalid UTF-8 starting at [{b:x2}] at offset {i - 1}");
947947
}
948948
if (Debugging.AssertsEnabled) Debugging.Assert(b < 0xf8, "b = 0x{0:x}", b);
949949
int ch = ((b & 0x7) << 18) + ((utf8[i] & 0x3f) << 12) + ((utf8[i + 1] & 0x3f) << 6) + (utf8[i + 2] & 0x3f);

0 commit comments

Comments
 (0)