Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lucene.Net.Facet.Taxonomy.WriterCache.CharBlockArray: Implemented IAppendable to align with Lucene. #901

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
254 changes: 238 additions & 16 deletions src/Lucene.Net.Facet/Taxonomy/WriterCache/CharBlockArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace Lucene.Net.Facet.Taxonomy.WriterCache
// therefore it doesn't make any difference what type of serialization is used.
// To make things simpler, we are using BinaryReader and BinaryWriter since
// BinaryFormatter is not implemented in .NET Standard 1.x.
internal class CharBlockArray : ICharSequence
internal class CharBlockArray : IAppendable, ICharSequence
{
private const long serialVersionUID = 1L;

Expand Down Expand Up @@ -130,10 +130,7 @@ internal virtual int IndexInBlock(int index)
return index % blockSize;
}

public virtual CharBlockArray Append(ICharSequence chars)
{
return Append(chars, 0, chars.Length);
}
#nullable enable

public virtual CharBlockArray Append(char c)
{
Expand All @@ -147,19 +144,96 @@ public virtual CharBlockArray Append(char c)
return this;
}

public virtual CharBlockArray Append(ICharSequence chars, int start, int length)
public virtual CharBlockArray Append(ICharSequence? value)
{
int end = start + length;
for (int i = start; i < end; i++)
if (value is null) // needed for Appendable compliance
{
return this; // No-op
}

return Append(value, 0, value.Length);
}

public virtual CharBlockArray Append(ICharSequence? value, int startIndex, int length)
{
// LUCENENET: Changed semantics to be the same as the StringBuilder in .NET
if (startIndex < 0)
throw new ArgumentOutOfRangeException(nameof(startIndex), $"{nameof(startIndex)} must not be negative.");
if (length < 0)
throw new ArgumentOutOfRangeException(nameof(length), $"{nameof(length)} must not be negative.");

if (value is null)
{
Append(chars[i]);
if (startIndex == 0 && length == 0)
return this;
throw new ArgumentNullException(nameof(value));
}
if (length == 0)
return this;
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)}.");


int end = startIndex + length;
for (int i = startIndex; i < end; i++)
{
Append(value[i]);
}
return this;
}

public virtual CharBlockArray Append(char[]? value)
{
if (value is null) // needed for Appendable compliance
{
return this; // No-op
}

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[] chars, int start, int length)
public virtual CharBlockArray Append(char[]? value, int startIndex, int length)
{
int offset = start;
// LUCENENET: Changed semantics to be the same as the StringBuilder in .NET
if (startIndex < 0)
throw new ArgumentOutOfRangeException(nameof(startIndex), $"{nameof(startIndex)} must not be negative.");
if (length < 0)
throw new ArgumentOutOfRangeException(nameof(length), $"{nameof(length)} must not be negative.");

if (value is null)
{
if (startIndex == 0 && length == 0)
return this;
throw new ArgumentNullException(nameof(value));
}
if (length == 0)
return this;
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)}.");


int offset = startIndex;
int remain = length;
while (remain > 0)
{
Expand All @@ -173,7 +247,7 @@ public virtual CharBlockArray Append(char[] chars, int start, int length)
{
toCopy = remainingInBlock;
}
Arrays.Copy(chars, offset, this.current.chars, this.current.length, toCopy);
Arrays.Copy(value, offset, this.current.chars, this.current.length, toCopy);
offset += toCopy;
remain -= toCopy;
this.current.length += toCopy;
Expand All @@ -183,9 +257,14 @@ public virtual CharBlockArray Append(char[] chars, int start, int length)
return this;
}

public virtual CharBlockArray Append(string s)
public virtual CharBlockArray Append(string? value)
{
int remain = s.Length;
if (value is null) // needed for Appendable compliance
{
return this; // No-op
}

int remain = value.Length;
int offset = 0;
while (remain > 0)
{
Expand All @@ -199,16 +278,159 @@ public virtual CharBlockArray Append(string s)
{
toCopy = remainingInBlock;
}
s.CopyTo(offset, this.current.chars, this.current.length, toCopy);
value.CopyTo(offset, this.current.chars, this.current.length, toCopy);
offset += toCopy;
remain -= toCopy;
this.current.length += toCopy;
}

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

public virtual CharBlockArray Append(string? value, int startIndex, int length)
{
// LUCENENET: Changed semantics to be the same as the StringBuilder in .NET
if (startIndex < 0)
throw new ArgumentOutOfRangeException(nameof(startIndex), $"{nameof(startIndex)} must not be negative.");
if (length < 0)
throw new ArgumentOutOfRangeException(nameof(length), $"{nameof(length)} must not be negative.");

if (value is null)
{
if (startIndex == 0 && length == 0)
return this;
throw new ArgumentNullException(nameof(value));
}
if (length == 0)
return this;
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)}.");


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)
{
if (value is null) // needed for Appendable compliance
{
return this; // No-op
}

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(StringBuilder? value, int startIndex, int length)
{
// LUCENENET: Changed semantics to be the same as the StringBuilder in .NET
if (startIndex < 0)
throw new ArgumentOutOfRangeException(nameof(startIndex), $"{nameof(startIndex)} must not be negative.");
if (length < 0)
throw new ArgumentOutOfRangeException(nameof(length), $"{nameof(length)} must not be negative.");

if (value is null)
{
if (startIndex == 0 && length == 0)
return this;
throw new ArgumentNullException(nameof(value));
}
if (length == 0)
return this;
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)}.");

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;
}

#nullable restore

#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

// LUCENENET specific - replaced with this[index]
//public virtual char CharAt(int index)
//{
Expand Down
Loading