Skip to content

Instantly share code, notes, and snippets.

@kevingosse
Last active November 6, 2018 08:32
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/7e56b2fc8aa0ba785c86472286f8dc7f to your computer and use it in GitHub Desktop.
Save kevingosse/7e56b2fc8aa0ba785c86472286f8dc7f to your computer and use it in GitHub Desktop.
/*
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, with no condition.
*/
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