Skip to content

Commit 44927d4

Browse files
committed
Make len-based overloads private; add non-len overloads
1 parent 604168c commit 44927d4

2 files changed

Lines changed: 138 additions & 21 deletions

File tree

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

Lines changed: 118 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,14 @@ public static class StemmerUtil // LUCENENET specific: CA1052 Static holder type
3232
/// Returns true if the character array starts with the prefix.
3333
/// </summary>
3434
/// <param name="s"> Input Buffer </param>
35-
/// <param name="len"> length of input buffer </param>
3635
/// <param name="prefix"> Prefix string to test </param>
3736
/// <returns> <c>true</c> if <paramref name="s"/> starts with <paramref name="prefix"/> </returns>
3837
/// <remarks>
3938
/// LUCENENET NOTE: This method has been converted to use <see cref="ReadOnlySpan{T}"/>.
4039
/// </remarks>
41-
public static bool StartsWith(ReadOnlySpan<char> s, int len, string prefix)
40+
public static bool StartsWith(ReadOnlySpan<char> s, string prefix)
4241
{
43-
return StartsWith(s, len, prefix.AsSpan());
42+
return StartsWith(s, prefix.AsSpan());
4443
}
4544

4645
/// <summary>
@@ -52,11 +51,27 @@ public static bool StartsWith(ReadOnlySpan<char> s, int len, string prefix)
5251
/// <returns> <c>true</c> if <paramref name="s"/> starts with <paramref name="prefix"/> </returns>
5352
/// <remarks>
5453
/// LUCENENET NOTE: This method has been converted to use <see cref="ReadOnlySpan{T}"/>.
54+
/// Callers should prefer the overload without the <paramref name="len"/> parameter and use a slice instead,
55+
/// but this overload is provided for compatibility with existing code.
56+
/// </remarks>
57+
internal static bool StartsWith(ReadOnlySpan<char> s, int len, string prefix)
58+
{
59+
return StartsWith(s.Slice(0, len), prefix.AsSpan());
60+
}
61+
62+
/// <summary>
63+
/// Returns true if the character array starts with the prefix.
64+
/// </summary>
65+
/// <param name="s"> Input Buffer </param>
66+
/// <param name="prefix"> Prefix string to test </param>
67+
/// <returns> <c>true</c> if <paramref name="s"/> starts with <paramref name="prefix"/> </returns>
68+
/// <remarks>
69+
/// LUCENENET NOTE: This method has been converted to use <see cref="ReadOnlySpan{T}"/>.
5570
/// </remarks>
56-
public static bool StartsWith(ReadOnlySpan<char> s, int len, ReadOnlySpan<char> prefix)
71+
public static bool StartsWith(ReadOnlySpan<char> s, ReadOnlySpan<char> prefix)
5772
{
5873
int prefixLen = prefix.Length;
59-
if (prefixLen > len)
74+
if (prefixLen > s.Length)
6075
{
6176
return false;
6277
}
@@ -66,18 +81,34 @@ public static bool StartsWith(ReadOnlySpan<char> s, int len, ReadOnlySpan<char>
6681
}
6782

6883
/// <summary>
69-
/// Returns true if the character array ends with the suffix.
84+
/// Returns true if the character array starts with the prefix.
7085
/// </summary>
7186
/// <param name="s"> Input Buffer </param>
7287
/// <param name="len"> length of input buffer </param>
88+
/// <param name="prefix"> Prefix string to test </param>
89+
/// <returns> <c>true</c> if <paramref name="s"/> starts with <paramref name="prefix"/> </returns>
90+
/// <remarks>
91+
/// LUCENENET NOTE: This method has been converted to use <see cref="ReadOnlySpan{T}"/>.
92+
/// Callers should prefer the overload without the <paramref name="len"/> parameter and use a slice instead,
93+
/// but this overload is provided for compatibility with existing code.
94+
/// </remarks>
95+
internal static bool StartsWith(ReadOnlySpan<char> s, int len, ReadOnlySpan<char> prefix)
96+
{
97+
return StartsWith(s.Slice(0, len), prefix);
98+
}
99+
100+
/// <summary>
101+
/// Returns true if the character array ends with the suffix.
102+
/// </summary>
103+
/// <param name="s"> Input Buffer </param>
73104
/// <param name="suffix"> Suffix string to test </param>
74105
/// <returns> <c>true</c> if <paramref name="s"/> ends with <paramref name="suffix"/> </returns>
75106
/// <remarks>
76107
/// LUCENENET NOTE: This method has been converted to use <see cref="ReadOnlySpan{T}"/>.
77108
/// </remarks>
78-
public static bool EndsWith(ReadOnlySpan<char> s, int len, string suffix)
109+
public static bool EndsWith(ReadOnlySpan<char> s, string suffix)
79110
{
80-
return EndsWith(s, len, suffix.AsSpan());
111+
return EndsWith(s, suffix.AsSpan());
81112
}
82113

83114
/// <summary>
@@ -89,17 +120,50 @@ public static bool EndsWith(ReadOnlySpan<char> s, int len, string suffix)
89120
/// <returns> <c>true</c> if <paramref name="s"/> ends with <paramref name="suffix"/> </returns>
90121
/// <remarks>
91122
/// LUCENENET NOTE: This method has been converted to use <see cref="ReadOnlySpan{T}"/>.
123+
/// Callers should prefer the overload without the <paramref name="len"/> parameter and use a slice instead,
124+
/// but this overload is provided for compatibility with existing code.
125+
/// </remarks>
126+
internal static bool EndsWith(ReadOnlySpan<char> s, int len, string suffix)
127+
{
128+
return EndsWith(s.Slice(0, len), suffix.AsSpan());
129+
}
130+
131+
/// <summary>
132+
/// Returns true if the character array ends with the suffix.
133+
/// </summary>
134+
/// <param name="s"> Input Buffer </param>
135+
/// <param name="suffix"> Suffix string to test </param>
136+
/// <returns> <c>true</c> if <paramref name="s"/> ends with <paramref name="suffix"/> </returns>
137+
/// <remarks>
138+
/// LUCENENET NOTE: This method has been converted to use <see cref="ReadOnlySpan{T}"/>.
92139
/// </remarks>
93-
public static bool EndsWith(ReadOnlySpan<char> s, int len, ReadOnlySpan<char> suffix)
140+
public static bool EndsWith(ReadOnlySpan<char> s, ReadOnlySpan<char> suffix)
94141
{
95142
int suffixLen = suffix.Length;
96-
if (suffixLen > len)
143+
if (suffixLen > s.Length)
97144
{
98145
return false;
99146
}
100147

101148
// LUCENENET: use more efficient implementation in MemoryExtensions
102-
return s.Slice(0, len).EndsWith(suffix, StringComparison.Ordinal);
149+
return s.EndsWith(suffix, StringComparison.Ordinal);
150+
}
151+
152+
/// <summary>
153+
/// Returns true if the character array ends with the suffix.
154+
/// </summary>
155+
/// <param name="s"> Input Buffer </param>
156+
/// <param name="len"> length of input buffer </param>
157+
/// <param name="suffix"> Suffix string to test </param>
158+
/// <returns> <c>true</c> if <paramref name="s"/> ends with <paramref name="suffix"/> </returns>
159+
/// <remarks>
160+
/// LUCENENET NOTE: This method has been converted to use <see cref="ReadOnlySpan{T}"/>.
161+
/// Callers should prefer the overload without the <paramref name="len"/> parameter and use a slice instead,
162+
/// but this overload is provided for compatibility with existing code.
163+
/// </remarks>
164+
internal static bool EndsWith(ReadOnlySpan<char> s, int len, ReadOnlySpan<char> suffix)
165+
{
166+
return EndsWith(s.Slice(0, len), suffix);
103167
}
104168

105169
// LUCENENET NOTE: char[] overload of EndsWith removed because the ReadOnlySpan<char> overload can be used instead
@@ -114,15 +178,32 @@ public static bool EndsWith(ReadOnlySpan<char> s, int len, ReadOnlySpan<char> su
114178
/// <remarks>
115179
/// LUCENENET NOTE: This method has been converted to use <see cref="Span{T}"/>.
116180
/// </remarks>
117-
public static int Delete(Span<char> s, int pos, int len)
181+
public static int Delete(Span<char> s, int pos)
118182
{
119-
if (Debugging.AssertsEnabled) Debugging.Assert(pos < len);
120-
if (pos < len - 1) // don't arraycopy if asked to delete last character
183+
if (Debugging.AssertsEnabled) Debugging.Assert(pos < s.Length);
184+
if (pos < s.Length - 1) // don't arraycopy if asked to delete last character
121185
{
122186
// Arrays.Copy(s, pos + 1, s, pos, len - pos - 1);
123-
s.Slice(pos + 1, len - pos - 1).CopyTo(s.Slice(pos, len - pos - 1));
187+
s.Slice(pos + 1, s.Length - pos - 1).CopyTo(s.Slice(pos, s.Length - pos - 1));
124188
}
125-
return len - 1;
189+
return s.Length - 1;
190+
}
191+
192+
/// <summary>
193+
/// Delete a character in-place
194+
/// </summary>
195+
/// <param name="s"> Input Buffer </param>
196+
/// <param name="pos"> Position of character to delete </param>
197+
/// <param name="len"> length of input buffer </param>
198+
/// <returns> length of input buffer after deletion </returns>
199+
/// <remarks>
200+
/// LUCENENET NOTE: This method has been converted to use <see cref="Span{T}"/>.
201+
/// Callers should prefer the overload without the <paramref name="len"/> parameter and use a slice instead,
202+
/// but this overload is provided for compatibility with existing code.
203+
/// </remarks>
204+
internal static int Delete(Span<char> s, int pos, int len)
205+
{
206+
return Delete(s.Slice(0, len), pos);
126207
}
127208

128209
/// <summary>
@@ -136,15 +217,31 @@ public static int Delete(Span<char> s, int pos, int len)
136217
/// <remarks>
137218
/// LUCENENET NOTE: This method has been converted to use <see cref="Span{T}"/>.
138219
/// </remarks>
139-
public static int DeleteN(Span<char> s, int pos, int len, int nChars)
220+
public static int DeleteN(Span<char> s, int pos, int nChars)
140221
{
141-
if (Debugging.AssertsEnabled) Debugging.Assert(pos + nChars <= len);
142-
if (pos + nChars < len) // don't arraycopy if asked to delete the last characters
222+
if (Debugging.AssertsEnabled) Debugging.Assert(pos + nChars <= s.Length);
223+
if (pos + nChars < s.Length) // don't arraycopy if asked to delete the last characters
143224
{
144225
// Arrays.Copy(s, pos + nChars, s, pos, len - pos - nChars);
145-
s.Slice(pos + nChars, len - pos - nChars).CopyTo(s.Slice(pos, len - pos - nChars));
226+
s.Slice(pos + nChars, s.Length - pos - nChars).CopyTo(s.Slice(pos, s.Length - pos - nChars));
146227
}
147-
return len - nChars;
228+
return s.Length - nChars;
229+
}
230+
231+
/// <summary>
232+
/// Delete n characters in-place
233+
/// </summary>
234+
/// <param name="s"> Input Buffer </param>
235+
/// <param name="pos"> Position of character to delete </param>
236+
/// <param name="len"> Length of input buffer </param>
237+
/// <param name="nChars"> number of characters to delete </param>
238+
/// <returns> length of input buffer after deletion </returns>
239+
/// <remarks>
240+
/// LUCENENET NOTE: This method has been converted to use <see cref="Span{T}"/>.
241+
/// </remarks>
242+
internal static int DeleteN(Span<char> s, int pos, int len, int nChars)
243+
{
244+
return DeleteN(s.Slice(0, len), pos, nChars);
148245
}
149246
}
150247
}

src/Lucene.Net.Tests.Analysis.Common/Analysis/Util/TestStemmerUtil.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ public class TestStemmerUtil : LuceneTestCase
3737
[TestCase("foobar", 2, "foo", false)]
3838
public void TestStartsWith(string input, int len, string prefix, bool expected)
3939
{
40+
// test len overload
4041
Assert.AreEqual(expected, StemmerUtil.StartsWith(input.AsSpan(), len, prefix));
42+
43+
// test no len overload
44+
Assert.AreEqual(expected, StemmerUtil.StartsWith(input.AsSpan(0, len), prefix));
4145
}
4246

4347
[Test]
@@ -48,7 +52,11 @@ public void TestStartsWith(string input, int len, string prefix, bool expected)
4852
[TestCase("foobar", 3, "foo", true)]
4953
public void TestEndsWith(string input, int len, string prefix, bool expected)
5054
{
55+
// test len overload
5156
Assert.AreEqual(expected, StemmerUtil.EndsWith(input.AsSpan(), len, prefix));
57+
58+
// test no len overload
59+
Assert.AreEqual(expected, StemmerUtil.EndsWith(input.AsSpan(0, len), prefix));
5260
}
5361

5462
[Test]
@@ -58,9 +66,15 @@ public void TestEndsWith(string input, int len, string prefix, bool expected)
5866
[TestCase("foobar", 5, 6, "fooba", 5)]
5967
public void TestDelete(string input, int pos, int len, string expected, int expectedLen)
6068
{
69+
// test len overload
6170
char[] buffer = input.ToCharArray();
6271
Assert.AreEqual(expectedLen, StemmerUtil.Delete(buffer, pos, len));
6372
Assert.AreEqual(expected, new string(buffer, 0, expectedLen));
73+
74+
// test no len overload
75+
buffer = input.ToCharArray();
76+
Assert.AreEqual(expectedLen, StemmerUtil.Delete(buffer.AsSpan(0, len), pos));
77+
Assert.AreEqual(expected, new string(buffer, 0, expectedLen));
6478
}
6579

6680
[Test]
@@ -70,9 +84,15 @@ public void TestDelete(string input, int pos, int len, string expected, int expe
7084
[TestCase("foobar", 4, 6, 2, "foob", 4)]
7185
public void TestDeleteN(string input, int pos, int len, int nChars, string expected, int expectedLen)
7286
{
87+
// test len overload
7388
char[] buffer = input.ToCharArray();
7489
Assert.AreEqual(expectedLen, StemmerUtil.DeleteN(buffer, pos, len, nChars));
7590
Assert.AreEqual(expected, new string(buffer, 0, expectedLen));
91+
92+
// test no len overload
93+
buffer = input.ToCharArray();
94+
Assert.AreEqual(expectedLen, StemmerUtil.DeleteN(buffer.AsSpan(0, len), pos, nChars));
95+
Assert.AreEqual(expected, new string(buffer, 0, expectedLen));
7696
}
7797
}
7898
}

0 commit comments

Comments
 (0)