Skip to content

Commit

Permalink
Lucene.Net.Facet.Taxonomy.WriterCache.CharBlockArray: Implement IAppe…
Browse files Browse the repository at this point in the history
…ndable to align with Lucene. Added tests to check the implementations, since we have many additional overloads in .NET.
  • Loading branch information
NightOwl888 committed Jan 19, 2024
1 parent 11eca1f commit 3e23da2
Show file tree
Hide file tree
Showing 2 changed files with 437 additions and 16 deletions.
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 @@ -38,7 +38,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 @@ -133,10 +133,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 @@ -150,19 +147,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)
{
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)
{
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)
{
int end = start + length;
for (int i = start; i < end; i++)
if (value is null) // needed for Appendable compliance
{
Append(chars[i]);
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 @@ -176,7 +250,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 @@ -186,9 +260,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 @@ -202,16 +281,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

0 comments on commit 3e23da2

Please sign in to comment.