Created
January 2, 2019 16:51
-
-
Save kevingosse/d1f68b58555dd2ec6996f7dba431ae1a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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