Skip to content

Instantly share code, notes, and snippets.

@kevingosse
Created January 2, 2019 16:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevingosse/d1f68b58555dd2ec6996f7dba431ae1a to your computer and use it in GitHub Desktop.
Save kevingosse/d1f68b58555dd2ec6996f7dba431ae1a to your computer and use it in GitHub Desktop.
public class StringBuffer
{
private Queue<string> _buffer = new Queue<string>();
private int _offset;
public int Length;
public void Append(string content)
{
_buffer.Enqueue(content);
Length += content.Length;
}
public char this[int index]
{
get
{
var i = index - _offset;
foreach (var element in _buffer)
{
if (i >= element.Length)
{
i -= element.Length;
continue;
}
return element[i];
}
throw new IndexOutOfRangeException();
}
}
public void Clear()
{
_buffer.Clear();
Length = 0;
_offset = 0;
}
public void Trim(int index)
{
index = index - _offset - 1;
while (index > 0)
{
var str = _buffer.Peek();
index -= str.Length;
if (index > 0)
{
_offset += str.Length;
_buffer.Dequeue();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment