1
1
// Lucene version compatibility level 4.8.1
2
2
using Lucene . Net . Diagnostics ;
3
- using Lucene . Net . Support ;
4
3
using System ;
5
- using System . Diagnostics ;
6
4
7
5
namespace Lucene . Net . Analysis . Util
8
6
{
@@ -25,7 +23,7 @@ namespace Lucene.Net.Analysis.Util
25
23
26
24
/// <summary>
27
25
/// Some commonly-used stemming functions
28
- ///
26
+ ///
29
27
/// @lucene.internal
30
28
/// </summary>
31
29
public static class StemmerUtil // LUCENENET specific: CA1052 Static holder types should be Static or NotInheritable
@@ -37,21 +35,34 @@ public static class StemmerUtil // LUCENENET specific: CA1052 Static holder type
37
35
/// <param name="len"> length of input buffer </param>
38
36
/// <param name="prefix"> Prefix string to test </param>
39
37
/// <returns> <c>true</c> if <paramref name="s"/> starts with <paramref name="prefix"/> </returns>
40
- public static bool StartsWith ( char [ ] s , int len , string prefix )
38
+ /// <remarks>
39
+ /// LUCENENET NOTE: This method has been converted to use <see cref="ReadOnlySpan{T}"/>.
40
+ /// </remarks>
41
+ public static bool StartsWith ( ReadOnlySpan < char > s , int len , string prefix )
42
+ {
43
+ return StartsWith ( s , len , prefix . AsSpan ( ) ) ;
44
+ }
45
+
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
+ /// </remarks>
56
+ public static bool StartsWith ( ReadOnlySpan < char > s , int len , ReadOnlySpan < char > prefix )
41
57
{
42
58
int prefixLen = prefix . Length ;
43
59
if ( prefixLen > len )
44
60
{
45
61
return false ;
46
62
}
47
- for ( int i = 0 ; i < prefixLen ; i ++ )
48
- {
49
- if ( s [ i ] != prefix [ i ] )
50
- {
51
- return false ;
52
- }
53
- }
54
- return true ;
63
+
64
+ // LUCENENET: use more efficient implementation in MemoryExtensions
65
+ return s . StartsWith ( prefix ) ;
55
66
}
56
67
57
68
/// <summary>
@@ -61,22 +72,12 @@ public static bool StartsWith(char[] s, int len, string prefix)
61
72
/// <param name="len"> length of input buffer </param>
62
73
/// <param name="suffix"> Suffix string to test </param>
63
74
/// <returns> <c>true</c> if <paramref name="s"/> ends with <paramref name="suffix"/> </returns>
64
- public static bool EndsWith ( char [ ] s , int len , string suffix )
75
+ /// <remarks>
76
+ /// LUCENENET NOTE: This method has been converted to use <see cref="ReadOnlySpan{T}"/>.
77
+ /// </remarks>
78
+ public static bool EndsWith ( ReadOnlySpan < char > s , int len , string suffix )
65
79
{
66
- int suffixLen = suffix . Length ;
67
- if ( suffixLen > len )
68
- {
69
- return false ;
70
- }
71
- for ( int i = suffixLen - 1 ; i >= 0 ; i -- )
72
- {
73
- if ( s [ len - ( suffixLen - i ) ] != suffix [ i ] )
74
- {
75
- return false ;
76
- }
77
- }
78
-
79
- return true ;
80
+ return EndsWith ( s , len , suffix . AsSpan ( ) ) ;
80
81
}
81
82
82
83
/// <summary>
@@ -86,37 +87,40 @@ public static bool EndsWith(char[] s, int len, string suffix)
86
87
/// <param name="len"> length of input buffer </param>
87
88
/// <param name="suffix"> Suffix string to test </param>
88
89
/// <returns> <c>true</c> if <paramref name="s"/> ends with <paramref name="suffix"/> </returns>
89
- public static bool EndsWith ( char [ ] s , int len , char [ ] suffix )
90
+ /// <remarks>
91
+ /// LUCENENET NOTE: This method has been converted to use <see cref="ReadOnlySpan{T}"/>.
92
+ /// </remarks>
93
+ public static bool EndsWith ( ReadOnlySpan < char > s , int len , ReadOnlySpan < char > suffix )
90
94
{
91
95
int suffixLen = suffix . Length ;
92
96
if ( suffixLen > len )
93
97
{
94
98
return false ;
95
99
}
96
- for ( int i = suffixLen - 1 ; i >= 0 ; i -- )
97
- {
98
- if ( s [ len - ( suffixLen - i ) ] != suffix [ i ] )
99
- {
100
- return false ;
101
- }
102
- }
103
100
104
- return true ;
101
+ // LUCENENET: use more efficient implementation in MemoryExtensions
102
+ return s . Slice ( 0 , len ) . EndsWith ( suffix ) ;
105
103
}
106
104
105
+ // LUCENENET NOTE: char[] overload of EndsWith removed because the ReadOnlySpan<char> overload can be used instead
106
+
107
107
/// <summary>
108
108
/// Delete a character in-place
109
109
/// </summary>
110
110
/// <param name="s"> Input Buffer </param>
111
111
/// <param name="pos"> Position of character to delete </param>
112
112
/// <param name="len"> length of input buffer </param>
113
113
/// <returns> length of input buffer after deletion </returns>
114
- public static int Delete ( char [ ] s , int pos , int len )
114
+ /// <remarks>
115
+ /// LUCENENET NOTE: This method has been converted to use <see cref="Span{T}"/>.
116
+ /// </remarks>
117
+ public static int Delete ( Span < char > s , int pos , int len )
115
118
{
116
119
if ( Debugging . AssertsEnabled ) Debugging . Assert ( pos < len ) ;
117
120
if ( pos < len - 1 ) // don't arraycopy if asked to delete last character
118
121
{
119
- Arrays . Copy ( s , pos + 1 , s , pos , len - pos - 1 ) ;
122
+ // 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 ) ) ;
120
124
}
121
125
return len - 1 ;
122
126
}
@@ -129,14 +133,18 @@ public static int Delete(char[] s, int pos, int len)
129
133
/// <param name="len"> Length of input buffer </param>
130
134
/// <param name="nChars"> number of characters to delete </param>
131
135
/// <returns> length of input buffer after deletion </returns>
132
- public static int DeleteN ( char [ ] s , int pos , int len , int nChars )
136
+ /// <remarks>
137
+ /// LUCENENET NOTE: This method has been converted to use <see cref="Span{T}"/>.
138
+ /// </remarks>
139
+ public static int DeleteN ( Span < char > s , int pos , int len , int nChars )
133
140
{
134
141
if ( Debugging . AssertsEnabled ) Debugging . Assert ( pos + nChars <= len ) ;
135
142
if ( pos + nChars < len ) // don't arraycopy if asked to delete the last characters
136
143
{
137
- Arrays . Copy ( s , pos + nChars , s , pos , len - pos - nChars ) ;
144
+ // 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 ) ) ;
138
146
}
139
147
return len - nChars ;
140
148
}
141
149
}
142
- }
150
+ }
0 commit comments