Skip to content

Commit b135f05

Browse files
authored
Remove unnecessary string.AsSpan() calls as of C# 14, apache#1340 (apache#1374)
1 parent 3602621 commit b135f05

15 files changed

Lines changed: 47 additions & 92 deletions

File tree

src/Lucene.Net.Analysis.Common/Analysis/Compound/Hyphenation/HyphenationTree.cs

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -365,32 +365,14 @@ protected virtual void SearchPatterns(ReadOnlySpan<char> word, int index, Span<b
365365
}
366366
}
367367

368-
/// <summary>
369-
/// Hyphenate word and return a <see cref="Hyphenation"/> object.
370-
/// </summary>
371-
/// <param name="word"> the word to be hyphenated </param>
372-
/// <param name="remainCharCount"> Minimum number of characters allowed before the
373-
/// hyphenation point. </param>
374-
/// <param name="pushCharCount"> Minimum number of characters allowed after the
375-
/// hyphenation point. </param>
376-
/// <returns> a <see cref="Hyphenation"/> object representing the
377-
/// hyphenated word or null if word is not hyphenated. </returns>
378-
public virtual Hyphenation Hyphenate(string word, int remainCharCount, int pushCharCount)
379-
{
380-
// LUCENENET: use Span instead of ToCharArray
381-
return Hyphenate(word.AsSpan(), 0, word.Length, remainCharCount, pushCharCount);
382-
}
383-
384-
385-
386368
/// <summary>
387369
/// Hyphenate word and return an array of hyphenation points.
388370
/// </summary>
389371
/// <remarks>
390-
/// w = "****nnllllllnnn*****", where n is a non-letter, l is a letter, all n
391-
/// may be absent, the first n is at offset, the first l is at offset +
392-
/// iIgnoreAtBeginning; word = ".llllll.'\0'***", where all l in w are copied
393-
/// into word. In the first part of the routine len = w.length, in the second
372+
/// w = "****nnllllllnnn*****".AsSpan(offset, len), where n is a non-letter,
373+
/// l is a letter, all n may be absent, the first n is at offset, the first l is
374+
/// at offset + iIgnoreAtBeginning; word = ".llllll.'\0'***", where all l in w are
375+
/// copied into word. In the first part of the routine len = w.length, in the second
394376
/// part of the routine len = word.length. Three indices are used: index(w),
395377
/// the index in w, index(word), the index in word, letterindex(word), the
396378
/// index in the letter part of word. The following relations exist: index(w) =
@@ -399,18 +381,22 @@ public virtual Hyphenation Hyphenate(string word, int remainCharCount, int pushC
399381
/// offset - 1 + iIgnoreAtBeginning index(w) = letterindex(word) + offset +
400382
/// iIgnoreAtBeginning
401383
/// </remarks>
402-
/// <param name="w"> char array that contains the word </param>
403-
/// <param name="offset"> Offset to first character in word </param>
404-
/// <param name="len"> Length of word </param>
405-
/// <param name="remainCharCount"> Minimum number of characters allowed before the
406-
/// hyphenation point. </param>
407-
/// <param name="pushCharCount"> Minimum number of characters allowed after the
408-
/// hyphenation point. </param>
409-
/// <returns> a <see cref="Hyphenation"/> object representing the
410-
/// hyphenated word or null if word is not hyphenated. </returns>
411-
public virtual Hyphenation Hyphenate(ReadOnlySpan<char> w, int offset, int len, int remainCharCount, int pushCharCount)
384+
/// <param name="w">
385+
/// <see cref="ReadOnlySpan{T}"/> that contains the word. This should be sliced
386+
/// to the desired offset and length before passing in if necessary.
387+
/// </param>
388+
/// <param name="remainCharCount">
389+
/// Minimum number of characters allowed before the hyphenation point.
390+
/// </param>
391+
/// <param name="pushCharCount">
392+
/// Minimum number of characters allowed after the hyphenation point.
393+
/// </param>
394+
/// <returns>
395+
/// a <see cref="Hyphenation"/> object representing the hyphenated word or null if word is not hyphenated.
396+
/// </returns>
397+
public virtual Hyphenation Hyphenate(ReadOnlySpan<char> w, int remainCharCount, int pushCharCount)
412398
{
413-
int i;
399+
int i, len = w.Length;
414400
// LUCENENET: optimized method for Span and stackalloc
415401
Span<char> word = (len + 3) * sizeof(char) > Constants.MaxStackByteLimit ? new char[len + 3] : stackalloc char[len + 3];
416402

@@ -421,7 +407,7 @@ public virtual Hyphenation Hyphenate(ReadOnlySpan<char> w, int offset, int len,
421407
bool bEndOfLetters = false;
422408
for (i = 1; i <= len; i++)
423409
{
424-
c[0] = w[offset + i - 1];
410+
c[0] = w[i - 1];
425411
int nc = m_classmap.Find(c, 0);
426412
if (nc < 0) // found a non-letter character ...
427413
{

src/Lucene.Net.Analysis.Common/Analysis/Compound/Hyphenation/TernaryTree.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ protected void Init()
146146
/// same prefix is inserted. This saves a lot of space, specially for long
147147
/// keys.
148148
/// </summary>
149+
// LUCENENET: string overload retained here for public API for non-C#-14 callers
149150
public void Insert(string key, char val)
150151
=> Insert(key.AsSpan(), val);
151152

src/Lucene.Net.Analysis.Common/Analysis/Compound/HyphenationCompoundWordTokenFilter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public static HyphenationTree GetHyphenationTree(Stream hyphenationSource, Encod
188188
protected override void Decompose()
189189
{
190190
// get the hyphenation points
191-
Hyphenation.Hyphenation hyphens = hyphenator.Hyphenate(m_termAtt.Buffer, 0, m_termAtt.Length, 1, 1);
191+
Hyphenation.Hyphenation hyphens = hyphenator.Hyphenate(m_termAtt.AsSpan(), 1, 1);
192192
// No hyphen points found -> exit
193193
if (hyphens is null)
194194
{

src/Lucene.Net.Analysis.Common/Analysis/El/GreekStemmer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1004,7 +1004,7 @@ private static int Rule22(char[] s, int len) // LUCENENET: CA1822: Mark members
10041004
/// <param name="suffix"> A <see cref="string"/> object to check if the word given ends with these characters. </param>
10051005
/// <returns> True if the word ends with the suffix given , false otherwise. </returns>
10061006
private static bool EndsWith(char[] s, int len, string suffix) // LUCENENET: CA1822: Mark members as static
1007-
=> s.AsSpan(0, len).EndsWith(suffix.AsSpan()); // LUCENENET specific - optimized for ReadOnlySpan<char>
1007+
=> s.AsSpan(0, len).EndsWith(suffix); // LUCENENET specific - optimized for ReadOnlySpan<char>
10081008

10091009
/// <summary>
10101010
/// Checks if the word contained in the leading portion of <see cref="T:char[]"/> array ,

src/Lucene.Net.Analysis.Common/Analysis/Util/CharArrayMap.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3827,7 +3827,7 @@ internal static CharReturnType ConvertObjectToChars<T>(T key, out char[] chars,
38273827

38283828
#if FEATURE_SPANFORMATTABLE
38293829
else if (key is ISpanFormattable spanFormattable &&
3830-
spanFormattable.TryFormat(reuse, out int charsWritten, string.Empty.AsSpan(), invariant))
3830+
spanFormattable.TryFormat(reuse, out int charsWritten, ReadOnlySpan<char>.Empty, invariant))
38313831
{
38323832
chars = reuse.Slice(0, charsWritten).ToArray();
38333833
return CharReturnType.CharArray;

src/Lucene.Net.Analysis.Common/Analysis/Util/StemmerUtil.cs

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -38,28 +38,12 @@ public static class StemmerUtil // LUCENENET specific: CA1052 Static holder type
3838
/// <remarks>
3939
/// LUCENENET NOTE: This method has been converted to use <see cref="ReadOnlySpan{T}"/>.
4040
/// </remarks>
41+
// LUCENENET: string overload retained here for public API for non-C#-14 callers
4142
public static bool StartsWith(ReadOnlySpan<char> s, string prefix)
4243
{
4344
return StartsWith(s, prefix.AsSpan());
4445
}
4546

46-
/// <summary>
47-
/// Returns true if the character array starts with the prefix.
48-
/// </summary>
49-
/// <param name="s"> Input Buffer </param>
50-
/// <param name="len"> length of input buffer </param>
51-
/// <param name="prefix"> Prefix string to test </param>
52-
/// <returns> <c>true</c> if <paramref name="s"/> starts with <paramref name="prefix"/> </returns>
53-
/// <remarks>
54-
/// LUCENENET NOTE: This method has been converted to use <see cref="ReadOnlySpan{T}"/>.
55-
/// Callers should prefer the overload without the <paramref name="len"/> parameter and use a slice instead,
56-
/// but this overload is provided for compatibility with existing code.
57-
/// </remarks>
58-
internal static bool StartsWith(ReadOnlySpan<char> s, int len, string prefix)
59-
{
60-
return StartsWith(s.Slice(0, len), prefix.AsSpan());
61-
}
62-
6347
/// <summary>
6448
/// Returns true if the character array starts with the prefix.
6549
/// </summary>
@@ -107,28 +91,12 @@ internal static bool StartsWith(ReadOnlySpan<char> s, int len, ReadOnlySpan<char
10791
/// <remarks>
10892
/// LUCENENET NOTE: This method has been converted to use <see cref="ReadOnlySpan{T}"/>.
10993
/// </remarks>
94+
// LUCENENET: string overload retained here for public API for non-C#-14 callers
11095
public static bool EndsWith(ReadOnlySpan<char> s, string suffix)
11196
{
11297
return EndsWith(s, suffix.AsSpan());
11398
}
11499

115-
/// <summary>
116-
/// Returns true if the character array ends with the suffix.
117-
/// </summary>
118-
/// <param name="s"> Input Buffer </param>
119-
/// <param name="len"> length of input buffer </param>
120-
/// <param name="suffix"> Suffix string to test </param>
121-
/// <returns> <c>true</c> if <paramref name="s"/> ends with <paramref name="suffix"/> </returns>
122-
/// <remarks>
123-
/// LUCENENET NOTE: This method has been converted to use <see cref="ReadOnlySpan{T}"/>.
124-
/// Callers should prefer the overload without the <paramref name="len"/> parameter and use a slice instead,
125-
/// but this overload is provided for compatibility with existing code.
126-
/// </remarks>
127-
internal static bool EndsWith(ReadOnlySpan<char> s, int len, string suffix)
128-
{
129-
return EndsWith(s.Slice(0, len), suffix.AsSpan());
130-
}
131-
132100
/// <summary>
133101
/// Returns true if the character array ends with the suffix.
134102
/// </summary>

src/Lucene.Net.Analysis.Kuromoji/JapaneseReadingFormFilter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public override bool IncrementToken()
6969
else
7070
{
7171
buffer.Length = 0;
72-
ToStringUtil.GetRomanization(buffer, reading.AsSpan());
72+
ToStringUtil.GetRomanization(buffer, reading);
7373
termAttr.SetEmpty().Append(buffer);
7474
}
7575
}

src/Lucene.Net.Analysis.Kuromoji/TokenAttributes/ReadingAttributeImpl.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ public override void ReflectWith(IAttributeReflector reflector)
6767
throw new ArgumentNullException(nameof(reflector));
6868

6969
string? reading = GetReading();
70-
string? readingEN = reading is null ? null : ToStringUtil.GetRomanization(reading.AsSpan());
70+
string? readingEN = reading is null ? null : ToStringUtil.GetRomanization(reading);
7171
string? pronunciation = GetPronunciation();
72-
string? pronunciationEN = pronunciation is null ? null : ToStringUtil.GetRomanization(pronunciation.AsSpan());
72+
string? pronunciationEN = pronunciation is null ? null : ToStringUtil.GetRomanization(pronunciation);
7373
reflector.Reflect<IReadingAttribute>("reading", reading);
7474
reflector.Reflect<IReadingAttribute>("reading (en)", readingEN);
7575
reflector.Reflect<IReadingAttribute>("pronunciation", pronunciation);

src/Lucene.Net.Analysis.Phonetic/Language/Bm/Rule.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -636,41 +636,41 @@ private static IRPattern GetPattern(string regex)
636636
// exact match
637637
return new RPatternHelper(isMatchSB: (input) =>
638638
{
639-
return input.Length == 1 && Contains(bContent.AsSpan(), input[0]) == shouldMatch;
639+
return input.Length == 1 && Contains(bContent, input[0]) == shouldMatch;
640640
}, isMatchStr: (input) =>
641641
{
642-
return input.Length == 1 && Contains(bContent.AsSpan(), input[0]) == shouldMatch;
642+
return input.Length == 1 && Contains(bContent, input[0]) == shouldMatch;
643643
}, isMatchCS: (input) =>
644644
{
645-
return input.Length == 1 && Contains(bContent.AsSpan(), input[0]) == shouldMatch;
645+
return input.Length == 1 && Contains(bContent, input[0]) == shouldMatch;
646646
});
647647
}
648648
else if (startsWith)
649649
{
650650
// first char
651651
return new RPatternHelper(isMatchSB: (input) =>
652652
{
653-
return input.Length > 0 && Contains(bContent.AsSpan(), input[0]) == shouldMatch;
653+
return input.Length > 0 && Contains(bContent, input[0]) == shouldMatch;
654654
}, isMatchStr: (input) =>
655655
{
656-
return input.Length > 0 && Contains(bContent.AsSpan(), input[0]) == shouldMatch;
656+
return input.Length > 0 && Contains(bContent, input[0]) == shouldMatch;
657657
}, isMatchCS: (input) =>
658658
{
659-
return input.Length > 0 && Contains(bContent.AsSpan(), input[0]) == shouldMatch;
659+
return input.Length > 0 && Contains(bContent, input[0]) == shouldMatch;
660660
});
661661
}
662662
else if (endsWith)
663663
{
664664
// last char
665665
return new RPatternHelper(isMatchSB: (input) =>
666666
{
667-
return input.Length > 0 && Contains(bContent.AsSpan(), input[input.Length - 1]) == shouldMatch;
667+
return input.Length > 0 && Contains(bContent, input[input.Length - 1]) == shouldMatch;
668668
}, isMatchStr: (input) =>
669669
{
670-
return input.Length > 0 && Contains(bContent.AsSpan(), input[input.Length - 1]) == shouldMatch;
670+
return input.Length > 0 && Contains(bContent, input[input.Length - 1]) == shouldMatch;
671671
}, isMatchCS: (input) =>
672672
{
673-
return input.Length > 0 && Contains(bContent.AsSpan(), input[input.Length - 1]) == shouldMatch;
673+
return input.Length > 0 && Contains(bContent, input[input.Length - 1]) == shouldMatch;
674674
});
675675
}
676676
}

src/Lucene.Net.Facet/Taxonomy/WriterCache/CategoryPathUtils.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public static bool EqualsToSerialized(FacetLabel cp, CharBlockArray charBlockArr
9090
}
9191

9292
// LUCENENET specific - calculate the hash code without the allocation caused by Subsequence() and ToString()
93-
if (!charBlockArray.Equals(offset, len, cp.Components[i].AsSpan())) // LUCENENET: Corrected 2nd parameter
93+
if (!charBlockArray.Equals(offset, len, cp.Components[i])) // LUCENENET: Corrected 2nd parameter
9494
{
9595
return false;
9696
}

0 commit comments

Comments
 (0)