servicepoint/crates/servicepoint_binding_cs/ServicePoint/Cp437Grid.cs
2024-10-20 13:31:41 +02:00

57 lines
1.2 KiB
C#

using System.Text;
namespace ServicePoint;
public sealed partial class Cp437Grid
{
public static Cp437Grid Load(nuint width, nuint height, ReadOnlySpan<byte> bytes)
{
unsafe
{
fixed (byte* bytesPtr = bytes)
{
return Load(width, height, bytesPtr, (nuint)bytes.Length);
}
}
}
public byte this[nuint x, nuint y]
{
get => Get(x, y);
set => Set(x, y, value);
}
public string this[nuint y]
{
set
{
var width = Width();
ArgumentOutOfRangeException.ThrowIfGreaterThan((nuint)value.Length, width);
nuint x = 0;
for (; x < (nuint)value.Length; x++)
this[x, y] = (byte)value[(int)x];
for (; x < width; x++)
this[x, y] = 0;
}
get
{
var sb = new StringBuilder();
var width = Width();
for (nuint x = 0; x < width; x++)
{
var val = this[x, y];
if (val == 0)
break;
sb.Append((char)val);
}
return sb.ToString();
}
}
public Span<byte> Data => UnsafeDataRef().AsSpan();
}