From 76bb2da03527ffe86b2b40b12a42bbdc5c60601d Mon Sep 17 00:00:00 2001 From: Paul Irwin Date: Wed, 18 Dec 2024 08:41:46 -0700 Subject: [PATCH] Use FormatException instead of ParseException --- src/Lucene.Net.Tests/Util/TestUnicodeUtil.cs | 2 +- src/Lucene.Net/Util/UnicodeUtil.cs | 11 +++++------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/Lucene.Net.Tests/Util/TestUnicodeUtil.cs b/src/Lucene.Net.Tests/Util/TestUnicodeUtil.cs index 6cad0a4e4e..a35c0a4d62 100644 --- a/src/Lucene.Net.Tests/Util/TestUnicodeUtil.cs +++ b/src/Lucene.Net.Tests/Util/TestUnicodeUtil.cs @@ -340,7 +340,7 @@ public void TestUTF8toUTF16Exception(byte[] invalidUtf8, bool shouldThrow) if (shouldThrow) { - Assert.Throws(() => UnicodeUtil.UTF8toUTF16(invalidUtf8, scratch)); + Assert.Throws(() => UnicodeUtil.UTF8toUTF16(invalidUtf8, scratch)); } else { diff --git a/src/Lucene.Net/Util/UnicodeUtil.cs b/src/Lucene.Net/Util/UnicodeUtil.cs index 434ca2d265..9a08d8bb4c 100644 --- a/src/Lucene.Net/Util/UnicodeUtil.cs +++ b/src/Lucene.Net/Util/UnicodeUtil.cs @@ -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; @@ -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. /// /// NOTE: Full characters are read, even if this reads past the length passed (and - /// can result in an if invalid UTF-8 is passed). + /// can result in an if invalid UTF-8 is passed). /// Explicit checks for valid UTF-8 are not performed. /// /// @@ -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. /// /// NOTE: Full characters are read, even if this reads past the length passed (and - /// can result in an if invalid UTF-8 is passed). + /// can result in an if invalid UTF-8 is passed). /// Explicit checks for valid UTF-8 are not performed. /// /// @@ -927,7 +926,7 @@ public static void UTF8toUTF16(ReadOnlySpan 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)); } @@ -935,7 +934,7 @@ public static void UTF8toUTF16(ReadOnlySpan utf8, CharsRef chars) { 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; @@ -944,7 +943,7 @@ public static void UTF8toUTF16(ReadOnlySpan 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);