-
Notifications
You must be signed in to change notification settings - Fork 624
/
CharTermAttribute.cs
496 lines (396 loc) · 17.3 KB
/
CharTermAttribute.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
using J2N.Text;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Text;
using WritableArrayAttribute = Lucene.Net.Support.WritableArrayAttribute;
namespace Lucene.Net.Analysis.TokenAttributes
{
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using ArrayUtil = Lucene.Net.Util.ArrayUtil;
using Attribute = Lucene.Net.Util.Attribute;
using BytesRef = Lucene.Net.Util.BytesRef;
using IAttribute = Lucene.Net.Util.IAttribute;
using IAttributeReflector = Lucene.Net.Util.IAttributeReflector;
using RamUsageEstimator = Lucene.Net.Util.RamUsageEstimator;
using UnicodeUtil = Lucene.Net.Util.UnicodeUtil;
/// <summary>
/// Default implementation of <see cref="ICharTermAttribute"/>. </summary>
public class CharTermAttribute : Attribute, ICharTermAttribute, ITermToBytesRefAttribute, IAppendable // LUCENENET specific: Not implementing ICloneable per Microsoft's recommendation
{
private const int MIN_BUFFER_SIZE = 10;
private char[] termBuffer = CreateBuffer(MIN_BUFFER_SIZE);
private int termLength = 0;
/// <summary>
/// Initialize this attribute with empty term text </summary>
public CharTermAttribute()
{
}
// LUCENENET specific - ICharSequence member from J2N
bool ICharSequence.HasValue => termBuffer != null;
public void CopyBuffer(char[] buffer, int offset, int length)
{
GrowTermBuffer(length);
Array.Copy(buffer, offset, termBuffer, 0, length);
termLength = length;
}
char[] ICharTermAttribute.Buffer => termBuffer;
[WritableArray]
[SuppressMessage("Microsoft.Performance", "CA1819", Justification = "Lucene's design requires some writable array properties")]
public char[] Buffer => termBuffer;
public char[] ResizeBuffer(int newSize)
{
if (termBuffer.Length < newSize)
{
// Not big enough; create a new array with slight
// over allocation and preserve content
// LUCENENET: Resize rather than copy
Array.Resize(ref termBuffer, ArrayUtil.Oversize(newSize, RamUsageEstimator.NUM_BYTES_CHAR));
}
return termBuffer;
}
private void GrowTermBuffer(int newSize)
{
if (termBuffer.Length < newSize)
{
// Not big enough; create a new array with slight
// over allocation:
termBuffer = new char[ArrayUtil.Oversize(newSize, RamUsageEstimator.NUM_BYTES_CHAR)];
}
}
int ICharTermAttribute.Length { get => Length; set => SetLength(value); }
int ICharSequence.Length => Length;
public int Length
{
get => termLength;
set => SetLength(value);
}
public CharTermAttribute SetLength(int length)
{
if (length > termBuffer.Length)
{
throw new ArgumentException("length " + length + " exceeds the size of the termBuffer (" + termBuffer.Length + ")");
}
termLength = length;
return this;
}
public CharTermAttribute SetEmpty()
{
termLength = 0;
return this;
}
// *** TermToBytesRefAttribute interface ***
private BytesRef bytes = new BytesRef(MIN_BUFFER_SIZE);
public virtual void FillBytesRef()
{
UnicodeUtil.UTF16toUTF8(termBuffer, 0, termLength, bytes);
}
public virtual BytesRef BytesRef => bytes;
// *** CharSequence interface ***
// LUCENENET specific: Replaced CharAt(int) with this[int] to .NETify
char ICharSequence.this[int index] => this[index];
char ICharTermAttribute.this[int index] { get => this[index]; set => this[index] = value; }
// LUCENENET specific indexer to make CharTermAttribute act more like a .NET type
public char this[int index]
{
get
{
if (index < 0 || index >= termLength) // LUCENENET: Added better bounds checking
{
throw new ArgumentOutOfRangeException(nameof(index));
}
return termBuffer[index];
}
set
{
if (index < 0 || index >= termLength)
{
throw new ArgumentOutOfRangeException(nameof(index)); // LUCENENET: Added better bounds checking
}
termBuffer[index] = value;
}
}
public ICharSequence Subsequence(int startIndex, int length)
{
// From Apache Harmony String class
if (termBuffer == null || (startIndex == 0 && length == termBuffer.Length))
{
return new CharArrayCharSequence(termBuffer);
}
if (startIndex < 0)
throw new ArgumentOutOfRangeException(nameof(startIndex));
if (length < 0)
throw new ArgumentOutOfRangeException(nameof(length));
if (startIndex + length > Length)
throw new ArgumentOutOfRangeException("", $"{nameof(startIndex)} + {nameof(length)} > {nameof(Length)}");
char[] result = new char[length];
for (int i = 0, j = startIndex; i < length; i++, j++)
result[i] = termBuffer[j];
return new CharArrayCharSequence(result);
}
// *** Appendable interface ***
public CharTermAttribute Append(string value, int startIndex, int charCount)
{
// LUCENENET: Changed semantics to be the same as the StringBuilder in .NET
if (startIndex < 0)
throw new ArgumentOutOfRangeException(nameof(startIndex));
if (charCount < 0)
throw new ArgumentOutOfRangeException(nameof(charCount));
if (value == null)
{
if (startIndex == 0 && charCount == 0)
return this;
throw new ArgumentNullException(nameof(value));
}
if (charCount == 0)
return this;
if (startIndex > value.Length - charCount)
throw new ArgumentOutOfRangeException(nameof(startIndex));
value.CopyTo(startIndex, InternalResizeBuffer(termLength + charCount), termLength, charCount);
Length += charCount;
return this;
}
public CharTermAttribute Append(char value)
{
ResizeBuffer(termLength + 1)[termLength++] = value;
return this;
}
public CharTermAttribute Append(char[] value)
{
if (value == null)
//return AppendNull();
return this; // No-op
int len = value.Length;
value.CopyTo(InternalResizeBuffer(termLength + len), termLength);
Length += len;
return this;
}
public CharTermAttribute Append(char[] value, int startIndex, int charCount)
{
// LUCENENET: Changed semantics to be the same as the StringBuilder in .NET
if (startIndex < 0)
throw new ArgumentOutOfRangeException(nameof(startIndex));
if (charCount < 0)
throw new ArgumentOutOfRangeException(nameof(charCount));
if (value == null)
{
if (startIndex == 0 && charCount == 0)
return this;
throw new ArgumentNullException(nameof(value));
}
if (charCount == 0)
return this;
if (startIndex > value.Length - charCount)
throw new ArgumentOutOfRangeException(nameof(startIndex));
Array.Copy(value, startIndex, InternalResizeBuffer(termLength + charCount), termLength, charCount);
Length += charCount;
return this;
}
public CharTermAttribute Append(string value)
{
return Append(value, 0, value == null ? 0 : value.Length);
}
public CharTermAttribute Append(StringBuilder value)
{
if (value == null) // needed for Appendable compliance
{
//return AppendNull();
return this; // No-op
}
return Append(value.ToString());
}
public CharTermAttribute Append(StringBuilder value, int startIndex, int charCount)
{
// LUCENENET: Changed semantics to be the same as the StringBuilder in .NET
if (startIndex < 0)
throw new ArgumentOutOfRangeException(nameof(startIndex));
if (charCount < 0)
throw new ArgumentOutOfRangeException(nameof(charCount));
if (value == null)
{
if (startIndex == 0 && charCount == 0)
return this;
throw new ArgumentNullException(nameof(value));
}
if (charCount == 0)
return this;
if (startIndex > value.Length - charCount)
throw new ArgumentOutOfRangeException(nameof(startIndex));
return Append(value.ToString(startIndex, charCount));
}
public CharTermAttribute Append(ICharTermAttribute value)
{
if (value == null) // needed for Appendable compliance
{
//return AppendNull();
return this; // No-op
}
int len = value.Length;
Array.Copy(value.Buffer, 0, ResizeBuffer(termLength + len), termLength, len);
termLength += len;
return this;
}
public CharTermAttribute Append(ICharSequence value)
{
if (value == null)
//return AppendNull();
return this; // No-op
return Append(value, 0, value.Length);
}
public CharTermAttribute Append(ICharSequence value, int startIndex, int charCount)
{
// LUCENENET: Changed semantics to be the same as the StringBuilder in .NET
if (startIndex < 0)
throw new ArgumentOutOfRangeException(nameof(startIndex));
if (charCount < 0)
throw new ArgumentOutOfRangeException(nameof(charCount));
if (value == null)
{
if (startIndex == 0 && charCount == 0)
return this;
throw new ArgumentNullException(nameof(value));
}
if (charCount == 0)
return this;
if (startIndex > value.Length - charCount)
throw new ArgumentOutOfRangeException(nameof(startIndex));
ResizeBuffer(termLength + charCount);
for (int i = 0; i < charCount; i++)
termBuffer[termLength++] = value[startIndex + i];
return this;
}
private char[] InternalResizeBuffer(int length)
{
if (termBuffer.Length < length)
{
char[] newBuffer = CreateBuffer(length);
Array.Copy(termBuffer, 0, newBuffer, 0, termBuffer.Length);
this.termBuffer = newBuffer;
}
return termBuffer;
}
private static char[] CreateBuffer(int length)
{
return new char[ArrayUtil.Oversize(length, RamUsageEstimator.NUM_BYTES_CHAR)];
}
// LUCENENET: Not used - we are doing a no-op when the value is null
//private CharTermAttribute AppendNull()
//{
// ResizeBuffer(termLength + 4);
// termBuffer[termLength++] = 'n';
// termBuffer[termLength++] = 'u';
// termBuffer[termLength++] = 'l';
// termBuffer[termLength++] = 'l';
// return this;
//}
// *** Attribute ***
public override int GetHashCode()
{
int code = termLength;
code = code * 31 + ArrayUtil.GetHashCode(termBuffer, 0, termLength);
return code;
}
public override void Clear()
{
termLength = 0;
}
public override object Clone()
{
CharTermAttribute t = (CharTermAttribute)base.Clone();
// Do a deep clone
t.termBuffer = new char[this.termLength];
Array.Copy(this.termBuffer, 0, t.termBuffer, 0, this.termLength);
t.bytes = BytesRef.DeepCopyOf(bytes);
return t;
}
public override bool Equals(object other)
{
if (other == this)
{
return true;
}
if (other is CharTermAttribute o)
{
if (termLength != o.termLength)
{
return false;
}
for (int i = 0; i < termLength; i++)
{
if (termBuffer[i] != o.termBuffer[i])
{
return false;
}
}
return true;
}
return false;
}
/// <summary>
/// Returns solely the term text as specified by the
/// <see cref="ICharSequence"/> interface.
/// <para/>
/// this method changed the behavior with Lucene 3.1,
/// before it returned a String representation of the whole
/// term with all attributes.
/// this affects especially the
/// <see cref="Lucene.Net.Analysis.Token"/> subclass.
/// </summary>
public override string ToString()
{
return new string(termBuffer, 0, termLength);
}
public override void ReflectWith(IAttributeReflector reflector)
{
reflector.Reflect(typeof(ICharTermAttribute), "term", ToString());
FillBytesRef();
reflector.Reflect(typeof(ITermToBytesRefAttribute), "bytes", BytesRef.DeepCopyOf(bytes));
}
public override void CopyTo(IAttribute target)
{
CharTermAttribute t = (CharTermAttribute)target;
t.CopyBuffer(termBuffer, 0, termLength);
}
#region ICharTermAttribute Members
void ICharTermAttribute.CopyBuffer(char[] buffer, int offset, int length) => CopyBuffer(buffer, offset, length);
char[] ICharTermAttribute.ResizeBuffer(int newSize) => ResizeBuffer(newSize);
ICharTermAttribute ICharTermAttribute.SetLength(int length) => SetLength(length);
ICharTermAttribute ICharTermAttribute.SetEmpty() => SetEmpty();
ICharTermAttribute ICharTermAttribute.Append(ICharSequence value) => Append(value);
ICharTermAttribute ICharTermAttribute.Append(ICharSequence value, int startIndex, int count) => Append(value, startIndex, count);
ICharTermAttribute ICharTermAttribute.Append(char value) => Append(value);
ICharTermAttribute ICharTermAttribute.Append(char[] value) => Append(value);
ICharTermAttribute ICharTermAttribute.Append(char[] value, int startIndex, int count) => Append(value, startIndex, count);
ICharTermAttribute ICharTermAttribute.Append(string value) => Append(value);
ICharTermAttribute ICharTermAttribute.Append(string value, int startIndex, int count) => Append(value, startIndex, count);
ICharTermAttribute ICharTermAttribute.Append(StringBuilder value) => Append(value);
ICharTermAttribute ICharTermAttribute.Append(StringBuilder value, int startIndex, int count) => Append(value, startIndex, count);
ICharTermAttribute ICharTermAttribute.Append(ICharTermAttribute value) => Append(value);
#endregion
#region IAppendable Members
IAppendable IAppendable.Append(char value) => Append(value);
IAppendable IAppendable.Append(string value) => Append(value);
IAppendable IAppendable.Append(string value, int startIndex, int count) => Append(value, startIndex, count);
IAppendable IAppendable.Append(StringBuilder value) => Append(value);
IAppendable IAppendable.Append(StringBuilder value, int startIndex, int count) => Append(value, startIndex, count);
IAppendable IAppendable.Append(char[] value) => Append(value);
IAppendable IAppendable.Append(char[] value, int startIndex, int count) => Append(value, startIndex, count);
IAppendable IAppendable.Append(ICharSequence value) => Append(value);
IAppendable IAppendable.Append(ICharSequence value, int startIndex, int count) => Append(value, startIndex, count);
#endregion
}
}