Skip to content

Instantly share code, notes, and snippets.

@antiduh
Created February 1, 2022 19:15
Show Gist options
  • Save antiduh/23984a4f622147a1d46b8daba9e379af to your computer and use it in GitHub Desktop.
Save antiduh/23984a4f622147a1d46b8daba9e379af to your computer and use it in GitHub Desktop.
namespace Demo
{
public class CursedBuffer
{
private readonly object[] data;
private readonly Pointer pointer;
public CursedBuffer(int size)
{
data = new object[size];
pointer = new Pointer(size);
}
private sealed class Pointer
{
private readonly int limit;
public Pointer(int limit) => this.limit = limit;
private int _pointer = 0;
public void Set(int value) => _pointer = value;
public int Advance()
{
var value = _pointer;
_pointer++;
return value;
}
public bool AtEnd() => _pointer >= limit;
}
public object _
{
get
{
if (pointer.AtEnd())
{
return null;
}
return data[pointer.Advance()];
}
set
{
if (value is int intValue)
{
pointer.Set(intValue);
return;
}
if (pointer.AtEnd())
{
return;
}
data[pointer.Advance()] = value;
}
}
public static implicit operator bool(CursedBuffer buffer)
{
return buffer.pointer.AtEnd() == false;
}
internal sealed class CursedBufferTest : CursedBuffer
{
public CursedBufferTest() : base(13)
{
_ = 'H'; _ = 'e'; _ = 'l'; _ = 'l'; _ = 'o'; _ = ','; _ = ' ';
_ = 'w'; _ = 'o'; _ = 'r'; _ = 'l'; _ = 'd'; _ = '!'; _ = 0;
var sb = new StringBuilder();
for (var value = _; this; value = _) sb.Append(value);
var result = sb.ToString();
Debug.WriteLine(result); /* Output: Hello, world! */
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment