Skip to content

Commit b64e5df

Browse files
committed
Use Utf8ToStringWithFallback in ToString and exception/logging message building
1 parent 64c2d60 commit b64e5df

10 files changed

Lines changed: 32 additions & 21 deletions

File tree

src/Lucene.Net.Codecs/SimpleText/SimpleTextUtil.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,9 @@ public static void CheckFooter(ChecksumIndexInput input)
106106

107107
if (StringHelper.StartsWith(scratch, CHECKSUM) == false)
108108
{
109+
// LUCENENET specific - use Utf8ToStringWithFallback() to handle invalid UTF-8 bytes
109110
throw new CorruptIndexException("SimpleText failure: expected checksum line but got " +
110-
scratch.Utf8ToString() + " (resource=" + input + ")");
111+
scratch.Utf8ToStringWithFallback() + " (resource=" + input + ")");
111112
}
112113
var actualChecksum =
113114
(new BytesRef(scratch.Bytes, CHECKSUM.Length, scratch.Length - CHECKSUM.Length)).Utf8ToString();

src/Lucene.Net.Facet/SortedSet/DefaultSortedSetDocValuesReaderState.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ public DefaultSortedSetDocValuesReaderState(IndexReader reader, string field = F
7979
string[] components = FacetsConfig.StringToPath(spare.Utf8ToString());
8080
if (components.Length != 2)
8181
{
82-
throw new ArgumentException("this class can only handle 2 level hierarchy (dim/value); got: " + Arrays.ToString(components) + " " + spare.Utf8ToString());
82+
// LUCENENET specific - use Utf8ToStringWithFallback() to handle invalid UTF-8 bytes
83+
throw new ArgumentException("this class can only handle 2 level hierarchy (dim/value); got: " + Arrays.ToString(components) + " " + spare.Utf8ToStringWithFallback());
8384
}
8485
if (!components[0].Equals(lastDim, StringComparison.Ordinal))
8586
{

src/Lucene.Net.Grouping/AbstractGroupFacetCollector.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ public override int GetHashCode()
278278
public override string ToString()
279279
{
280280
return "FacetEntry{" +
281-
"value=" + value.Utf8ToString() +
281+
"value=" + value.Utf8ToStringWithFallback() + // LUCENENET specific - use Utf8ToStringWithFallback() to handle invalid UTF-8 bytes
282282
", count=" + count +
283283
'}';
284284
}

src/Lucene.Net.Join/TermsIncludingScoreQuery.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ internal Explanation Explain(int target) // LUCENENET NOTE: changed accessibilit
314314
} while (docId != DocIdSetIterator.NO_MORE_DOCS);
315315

316316
return new ComplexExplanation(true, outerInstance._scores[outerInstance._ords[_scoreUpto]],
317-
"Score based on join value " + _termsEnum.Term.Utf8ToString());
317+
"Score based on join value " + _termsEnum.Term.Utf8ToStringWithFallback()); // LUCENENET specific - use Utf8ToStringWithFallback() to handle invalid UTF-8 bytes
318318
}
319319
}
320320

src/Lucene.Net.Misc/Misc/TermStats.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ internal string GetTermText()
4545

4646
public override string ToString()
4747
{
48-
return ("TermStats: Term=" + TermText.Utf8ToString() + " DocFreq=" + DocFreq + " TotalTermFreq=" + TotalTermFreq);
48+
// LUCENENET specific - use Utf8ToStringWithFallback() to handle invalid UTF-8 bytes
49+
return "TermStats: Term=" + TermText.Utf8ToStringWithFallback() + " DocFreq=" + DocFreq + " TotalTermFreq=" + TotalTermFreq;
4950
}
5051
}
5152
}

src/Lucene.Net.Queries/TermsFilter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ public override string ToString()
319319
}
320320
first = false;
321321
builder.Append(current.field).Append(':');
322-
builder.Append(spare.Utf8ToString());
322+
builder.Append(spare.Utf8ToStringWithFallback()); // LUCENENET specific - use Utf8ToStringWithFallback() to handle invalid UTF-8 bytes
323323
}
324324
}
325325

src/Lucene.Net.Suggest/Suggest/Fst/FSTCompletion.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ internal Completion(BytesRef key, int bucket)
5959

6060
public override string ToString()
6161
{
62-
return Utf8.Utf8ToString() + "/" + Bucket.ToString("0.0", CultureInfo.InvariantCulture);
62+
// LUCENENET specific - use Utf8ToStringWithFallback() to handle invalid UTF-8 bytes
63+
return Utf8.Utf8ToStringWithFallback() + "/" + Bucket.ToString("0.0", CultureInfo.InvariantCulture);
6364
}
6465

6566
/// <seealso cref="BytesRef.CompareTo(object)"></seealso>

src/Lucene.Net/Codecs/BlockTreeTermsWriter.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,8 @@ public PendingTerm(BytesRef term, BlockTermState state)
440440

441441
public override string ToString()
442442
{
443-
return Term.Utf8ToString();
443+
// LUCENENET specific - use Utf8ToStringWithFallback() to handle invalid UTF-8 bytes
444+
return Term.Utf8ToStringWithFallback();
444445
}
445446
}
446447

@@ -468,7 +469,8 @@ public PendingBlock(BytesRef prefix, long fp, bool hasTerms, bool isFloor, int f
468469

469470
public override string ToString()
470471
{
471-
return $"BLOCK: {Prefix.Utf8ToString()}";
472+
// LUCENENET specific - use Utf8ToStringWithFallback() to handle invalid UTF-8 bytes
473+
return $"BLOCK: {Prefix.Utf8ToStringWithFallback()}";
472474
}
473475

474476
#nullable enable

src/Lucene.Net/Codecs/Lucene3x/Lucene3xFields.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,8 @@ private bool SeekToNonBMP(SegmentTermEnum te, BytesRef term, int pos)
343343

344344
if (DEBUG_SURROGATES)
345345
{
346-
Console.WriteLine(" try seek term=" + UnicodeUtil.ToHexString(term.Utf8ToString()));
346+
// LUCENENET specific - use Utf8ToStringWithFallback() to handle invalid UTF-8 bytes
347+
Console.WriteLine(" try seek term=" + UnicodeUtil.ToHexString(term.Utf8ToStringWithFallback()));
347348
}
348349

349350
// Seek "back":
@@ -487,7 +488,8 @@ private bool DoPop()
487488

488489
if (DEBUG_SURROGATES)
489490
{
490-
Console.WriteLine(" seek to term=" + UnicodeUtil.ToHexString(scratchTerm.Utf8ToString()) + " " + scratchTerm.ToString());
491+
// LUCENENET specific - use Utf8ToStringWithFallback() to handle invalid UTF-8 bytes
492+
Console.WriteLine(" seek to term=" + UnicodeUtil.ToHexString(scratchTerm.Utf8ToStringWithFallback()) + " " + scratchTerm.ToString());
491493
}
492494

493495
// TODO: more efficient seek? can we simply swap
@@ -598,10 +600,11 @@ private void SurrogateDance()
598600

599601
if (DEBUG_SURROGATES)
600602
{
603+
// LUCENENET specific - use Utf8ToStringWithFallback() to handle invalid UTF-8 bytes
601604
Console.WriteLine(" dance");
602-
Console.WriteLine(" prev=" + UnicodeUtil.ToHexString(prevTerm.Utf8ToString()));
605+
Console.WriteLine(" prev=" + UnicodeUtil.ToHexString(prevTerm.Utf8ToStringWithFallback()));
603606
Console.WriteLine(" " + prevTerm.ToString());
604-
Console.WriteLine(" term=" + UnicodeUtil.ToHexString(scratchTerm.Utf8ToString()));
607+
Console.WriteLine(" term=" + UnicodeUtil.ToHexString(scratchTerm.Utf8ToStringWithFallback()));
605608
Console.WriteLine(" " + scratchTerm.ToString());
606609
}
607610

@@ -678,7 +681,8 @@ private void DoPushes()
678681

679682
if (DEBUG_SURROGATES)
680683
{
681-
Console.WriteLine(" try seek 1 pos=" + upTo + " term=" + UnicodeUtil.ToHexString(scratchTerm.Utf8ToString()) + " " + scratchTerm.ToString() + " len=" + scratchTerm.Length);
684+
// LUCENENET specific - use Utf8ToStringWithFallback() to handle invalid UTF-8 bytes
685+
Console.WriteLine(" try seek 1 pos=" + upTo + " term=" + UnicodeUtil.ToHexString(scratchTerm.Utf8ToStringWithFallback()) + " " + scratchTerm.ToString() + " len=" + scratchTerm.Length);
682686
}
683687

684688
// Seek "forward":
@@ -831,7 +835,8 @@ public override SeekStatus SeekCeil(BytesRef term)
831835
{
832836
if (DEBUG_SURROGATES)
833837
{
834-
Console.WriteLine("TE.seek target=" + UnicodeUtil.ToHexString(term.Utf8ToString()));
838+
// LUCENENET specific - use Utf8ToStringWithFallback() to handle invalid UTF-8 bytes
839+
Console.WriteLine("TE.seek target=" + UnicodeUtil.ToHexString(term.Utf8ToStringWithFallback()));
835840
}
836841
skipNext = false;
837842
TermInfosReader tis = outerInstance.TermsDict;

src/Lucene.Net/Util/BytesRef.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,9 @@ public string Utf8ToString()
248248
/// resulting <see cref="string"/>.
249249
/// </summary>
250250
/// <remarks>
251-
/// LUCENENET specific version that does not throw exceptions,
252-
/// primarily for use in ToString() and other methods that
253-
/// should not throw exceptions.
251+
/// LUCENENET specific version that does not throw exceptions on invalid UTF-8,
252+
/// primarily for use in ToString() and other cases that should not throw exceptions,
253+
/// such as when building a message for another exception.
254254
/// </remarks>
255255
[MethodImpl(MethodImplOptions.AggressiveInlining)]
256256
public string Utf8ToStringWithFallback()
@@ -604,11 +604,11 @@ public override string ToString()
604604
switch (format)
605605
{
606606
case BytesRefFormat.UTF8:
607-
try
607+
if (bytesRef.TryUtf8ToString(out var utf8String))
608608
{
609-
return bytesRef.Utf8ToString();
609+
return utf8String;
610610
}
611-
catch (Exception e) when (e.IsIndexOutOfBoundsException())
611+
else
612612
{
613613
return bytesRef.ToString();
614614
}

0 commit comments

Comments
 (0)