Skip to content

Commit

Permalink
PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
paulirwin committed Nov 19, 2024
1 parent 50b6702 commit 1a8b783
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 21 deletions.
96 changes: 88 additions & 8 deletions src/Lucene.Net.Facet/Taxonomy/WriterCache/CharBlockArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,28 @@ public virtual CharBlockArray Append(char[]? value)
return this; // No-op
}

// LUCENENET specific - use ReadOnlySpan<char> version
return Append(value.AsSpan());
int remain = value.Length;
int offset = 0;
while (remain > 0)
{
if (this.current.length == this.blockSize)
{
AddBlock();
}
int toCopy = remain;
int remainingInBlock = this.blockSize - this.current.length;
if (remainingInBlock < toCopy)
{
toCopy = remainingInBlock;
}
Arrays.Copy(value, offset, this.current.chars, this.current.length, toCopy);
offset += toCopy;
remain -= toCopy;
this.current.length += toCopy;
}

this.length += value.Length;
return this;
}

public virtual CharBlockArray Append(char[]? value, int startIndex, int length)
Expand All @@ -214,8 +234,28 @@ public virtual CharBlockArray Append(char[]? value, int startIndex, int length)
if (startIndex > value.Length - length)
throw new ArgumentOutOfRangeException(nameof(startIndex), $"Index and length must refer to a location within the string. For example {nameof(startIndex)} + {nameof(length)} <= {nameof(Length)}.");

// LUCENENET specific - use ReadOnlySpan<char> version
return Append(value.AsSpan(startIndex, length));
int offset = startIndex;
int remain = length;
while (remain > 0)
{
if (this.current.length == this.blockSize)
{
AddBlock();
}
int toCopy = remain;
int remainingInBlock = this.blockSize - this.current.length;
if (remainingInBlock < toCopy)
{
toCopy = remainingInBlock;
}
Arrays.Copy(value, offset, this.current.chars, this.current.length, toCopy);
offset += toCopy;
remain -= toCopy;
this.current.length += toCopy;
}

this.length += length;
return this;
}

public virtual CharBlockArray Append(string? value)
Expand All @@ -225,8 +265,28 @@ public virtual CharBlockArray Append(string? value)
return this; // No-op
}

// LUCENENET specific - use ReadOnlySpan<char> version
return Append(value.AsSpan());
int remain = value.Length;
int offset = 0;
while (remain > 0)
{
if (this.current.length == this.blockSize)
{
AddBlock();
}
int toCopy = remain;
int remainingInBlock = this.blockSize - this.current.length;
if (remainingInBlock < toCopy)
{
toCopy = remainingInBlock;
}
value.CopyTo(offset, this.current.chars, this.current.length, toCopy);
offset += toCopy;
remain -= toCopy;
this.current.length += toCopy;
}

this.length += value.Length;
return this;
}

public virtual CharBlockArray Append(string? value, int startIndex, int length)
Expand All @@ -248,8 +308,28 @@ public virtual CharBlockArray Append(string? value, int startIndex, int length)
if (startIndex > value.Length - length)
throw new ArgumentOutOfRangeException(nameof(startIndex), $"Index and length must refer to a location within the string. For example {nameof(startIndex)} + {nameof(length)} <= {nameof(Length)}.");

// LUCENENET specific - use ReadOnlySpan<char> version
return Append(value.AsSpan(startIndex, length));
int offset = startIndex;
int remain = length;
while (remain > 0)
{
if (this.current.length == this.blockSize)
{
AddBlock();
}
int toCopy = remain;
int remainingInBlock = this.blockSize - this.current.length;
if (remainingInBlock < toCopy)
{
toCopy = remainingInBlock;
}
value.CopyTo(offset, this.current.chars, this.current.length, toCopy);
offset += toCopy;
remain -= toCopy;
this.current.length += toCopy;
}

this.length += length;
return this;
}

public virtual CharBlockArray Append(StringBuilder? value)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using J2N.IO;
using J2N.Text;
using Lucene.Net.Attributes;
using NUnit.Framework;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -292,10 +293,6 @@ public virtual void TestAppendableInterface()

t.Append((char[])null); // No-op
Assert.AreEqual("4teste", t.ToString());

// LUCENENET specific - test ReadOnlySpan<char> overload
t.SetEmpty().Append("12345678".AsSpan());
Assert.AreEqual("12345678", t.ToString());
}

[Test]
Expand Down Expand Up @@ -330,10 +327,29 @@ public virtual void TestAppendableInterfaceWithLongSequences()
const string longTestString = "012345678901234567890123456789";
t.Append(new CharSequenceAnonymousClass(longTestString));
Assert.AreEqual("4567890123456" + longTestString, t.ToString());
}

[Test]
[LuceneNetSpecific]
public virtual void TestSpanAppendableInterface()
{
CharTermAttribute t = new CharTermAttribute();

// Test with a span
t.Append("12345678".AsSpan());
Assert.AreEqual("12345678", t.ToString());

// LUCENENET specific - test ReadOnlySpan<char> overload
// test with a span slice
t.Append("0123456789".AsSpan(3, 5 - 3));
Assert.AreEqual("1234567834", t.ToString());

// test with a long span
t.SetEmpty().Append("01234567890123456789012345678901234567890123456789".AsSpan());
Assert.AreEqual("01234567890123456789012345678901234567890123456789", t.ToString());

// test with a long span slice
t.Append("01234567890123456789012345678901234567890123456789".AsSpan(3, 50 - 3));
Assert.AreEqual("0123456789012345678901234567890123456789012345678934567890123456789012345678901234567890123456789", t.ToString());
}

private sealed class CharSequenceAnonymousClass : ICharSequence
Expand Down
3 changes: 1 addition & 2 deletions src/Lucene.Net/Analysis/TokenAttributes/CharTermAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ namespace Lucene.Net.Analysis.TokenAttributes
/// <summary>
/// The term text of a <see cref="Token"/>.
/// </summary>
public interface ICharTermAttribute : IAttribute, ICharSequence, IAppendable,
ISpanAppendable /* LUCENENET specific */
public interface ICharTermAttribute : IAttribute, ICharSequence, IAppendable
{
/// <summary>
/// Copies the contents of buffer, starting at offset for
Expand Down
19 changes: 13 additions & 6 deletions src/Lucene.Net/Analysis/TokenAttributes/CharTermAttributeImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,10 @@ public CharTermAttribute Append(string value, int startIndex, int charCount)
if (startIndex > value.Length - charCount)
throw new ArgumentOutOfRangeException(nameof(startIndex), $"Index and length must refer to a location within the string. For example {nameof(startIndex)} + {nameof(charCount)} <= {nameof(Length)}.");

// LUCENENET specific - use ReadOnlySpan<char> version for better performance
return Append(value.AsSpan(startIndex, charCount));
value.CopyTo(startIndex, InternalResizeBuffer(termLength + charCount), termLength, charCount);
Length += charCount;

return this;
}

public CharTermAttribute Append(char value)
Expand All @@ -232,8 +234,11 @@ public CharTermAttribute Append(char[] value)
//return AppendNull();
return this; // No-op

// LUCENENET specific - use ReadOnlySpan<char> version for better performance
return Append(value.AsSpan());
int len = value.Length;
value.CopyTo(InternalResizeBuffer(termLength + len), termLength);
Length += len;

return this;
}

public CharTermAttribute Append(char[] value, int startIndex, int charCount)
Expand All @@ -255,8 +260,10 @@ public CharTermAttribute Append(char[] value, int startIndex, int charCount)
if (startIndex > value.Length - charCount)
throw new ArgumentOutOfRangeException(nameof(startIndex), $"Index and length must refer to a location within the string. For example {nameof(startIndex)} + {nameof(charCount)} <= {nameof(Length)}.");

// LUCENENET specific - use ReadOnlySpan<char> version for better performance
return Append(value.AsSpan(startIndex, charCount));
Arrays.Copy(value, startIndex, InternalResizeBuffer(termLength + charCount), termLength, charCount);
Length += charCount;

return this;
}

public CharTermAttribute Append(string value)
Expand Down

0 comments on commit 1a8b783

Please sign in to comment.