String<T>
StringEncoding<T>.Write(memory, srcEnumr, bytesWritten, atIndex?) writes from UTF8 encoded 'srcEnumr' into 'memory'. 'srcEnumr' can exceed 2GB char count.
byte[] data = new byte[5];
var memory = new ListMemory<byte[], byte>(data);
StringEncoding.Write(memory, srcEnumr: "Hello", out long bytesWritten);
WriteLine(bytesWritten); // 5
StringEncoding<T>.Write(memory, srcSpan, bytesWritten, atIndex?) writes from 'srcSpan' into 'memory'.
byte[] data = new byte[5];
var memory = new ListMemory<byte[], byte>(data);
StringEncoding.Write(memory, srcSpan: "Hello".AsSpan(), out long bytesWritten);
WriteLine(bytesWritten); // 5
StringEncoding<T>.TryWrite(memory, srcEnumr, bytesWritten, atIndex?) tries to write from 'srcEnumr' into 'memory'.
byte[] data = new byte[5];
var memory = new ListMemory<byte[], byte>(data);
bool ok = StringEncoding.TryWrite(memory, srcEnumr: "Hello", out long bytesWritten);
WriteLine(bytesWritten); // 5
StringEncoding<T>.TryWrite(memory, srcSpan, bytesWritten, atIndex?) tries to write from 'srcSpan' into 'memory'.
byte[] data = new byte[5];
var memory = new ListMemory<byte[], byte>(data);
bool ok = StringEncoding.TryWrite(memory, srcSpan: "Hello".AsSpan(), out long bytesWritten);
WriteLine(bytesWritten); // 5
StringEncoding<T>.Read(memory, bytesRead, atIndex?) reads from 'memory' into string.
byte[] data = new byte[] { 72, 101, 108, 108, 111 };
var memory = new ListMemory<byte[], byte>(data);
string value = StringEncoding.Read(memory, out long bytesRead);
WriteLine(value); // "Hello"
StringEncoding<T>.Read(memory, dstSpan, int charsWritten, atIndex?) reads from 'memory' into 'dstSpan'.
byte[] data = new byte[] { 72, 101, 108, 108, 111 };
var memory = new ListMemory<byte[], byte>(data);
Span<char> dstSpan = new char[5];
StringEncoding.Read(memory, dstSpan, out int charsWritten);
WriteLine(charsWritten); // 5
StringEncoding<T>.Read(memory, dstMemory, atIndex?) reads from 'memory' into 'dstSpan'. 'dstMemory' can exceed 2GB char count.
byte[] data = new byte[] { 72, 101, 108, 108, 111 };
var memory = new ListMemory<byte[], byte>(data);
IMemory<char> dstMemory = new BlockMemory<char>();
dstMemory.Count = StringEncoding.CodePoints(memory).Utf16;
StringEncoding.Read(memory, dstMemory);
WriteLine(dstMemory.Count); // 5
StringEncoding<T>.TryRead(memory, out value, bytesRead, atIndex?) tries to read from 'memory' into string.
byte[] data = new byte[] { 72, 101, 108, 108, 111 };
var memory = new ListMemory<byte[], byte>(data);
bool ok = StringEncoding.TryRead(memory, out string value, out long bytesRead);
WriteLine(value); // "Hello"
StringEncoding<T>.TryRead(memory, dstSpan, int charsWritten, atIndex?) tries to read from 'memory' into 'dstSpan'.
byte[] data = new byte[] { 72, 101, 108, 108, 111 };
var memory = new ListMemory<byte[], byte>(data);
Span<char> dstSpan = new char[5];
bool ok = StringEncoding.TryRead(memory, dstSpan, out int charsWritten);
WriteLine(charsWritten); // 5
StringEncoding<T>.TryRead(memory, dstMemory, atIndex?) tries to read from 'memory' into 'dstSpan'. 'dstMemory' can exceed 2GB char count.
byte[] data = new byte[] { 72, 101, 108, 108, 111 };
var memory = new ListMemory<byte[], byte>(data);
IMemory<char> dstMemory = new BlockMemory<char>();
dstMemory.Count = StringEncoding.CodePoints(memory).Utf16;
bool ok = StringEncoding.TryRead(memory, dstMemory);
WriteLine(dstMemory.Count); // 5
StringEncoding<T>.Insert(memory, srcEnumr, bytesWritten, atIndex?) inserts from 'srcEnumr' into 'memory'. 'srcEnumr' can exceed 2GB char count.
byte[] data = new byte[5];
var memory = new Slice<ListMemory<byte[], byte>, byte>(new ListMemory<byte[], byte>(data), 0, 0, data.Length);
StringEncoding.Insert(ref memory, srcEnumr: "Hello", out long bytesWritten);
WriteLine(bytesWritten); // 5
StringEncoding<T>.Insert(memory, srcSpan, bytesWritten, atIndex?) inserts from 'srcEnumr' into 'srcSpan'.
byte[] data = new byte[5];
var memory = new Slice<ListMemory<byte[], byte>, byte>(new ListMemory<byte[], byte>(data), 0, 0, data.Length);
StringEncoding.Insert(ref memory, srcSpan: "Hello".AsSpan(), out long bytesWritten);
WriteLine(bytesWritten); // 5
StringEncoding<T>.TryInsert(memory, srcEnumr, bytesWritten, atIndex?) tries to insert from 'srcEnumr' into 'memory'. 'srcEnumr' can exceed 2GB char count.
byte[] data = new byte[5];
var memory = new Slice<ListMemory<byte[], byte>, byte>(new ListMemory<byte[], byte>(data), 0, 0, data.Length);
StringEncoding.TryInsert(ref memory, srcEnumr: "Hello", out long bytesWritten);
WriteLine(bytesWritten); // 5
StringEncoding<T>.TryInsert(memory, srcSpan, bytesWritten, atIndex?) tries to insert from 'srcEnumr' into 'srcSpan'.
byte[] data = new byte[5];
var memory = new Slice<ListMemory<byte[], byte>, byte>(new ListMemory<byte[], byte>(data), 0, 0, data.Length);
StringEncoding.TryInsert(ref memory, srcSpan: "Hello".AsSpan(), out long bytesWritten);
WriteLine(bytesWritten); // 5
StringEncoding.CodePoints(charEnumr) calculates utf8 and utf16 code points from IEnumerable<char>.
(long utf8Count, long utf16Count) = StringEncoding.CodePoints(charEnumr: "Hello world. Ö🏰Ö");
WriteLine(utf16Count); // 17
WriteLine(utf8Count); // 21
StringEncoding.CodePoints(charSpan) calculates from ReadOnlySpan<char>.
(long utf8Count, long utf16Count) = StringEncoding.CodePoints(charSpan: "Hello world. Ö🏰Ö".AsSpan());
WriteLine(utf16Count); // 17
WriteLine(utf8Count); // 21
StringEncoding.CodePoints(byteEnumr) calculates from IEnumerable<byte>.
byte[] data = new byte[] { 72, 101, 108, 108, 111 };
var memory = new ListMemory<byte[], byte>(data);
(long Utf8Count, long Utf16Count) = StringEncoding.CodePoints(byteEnumr: memory);
WriteLine(Utf16Count); // 5
WriteLine(Utf8Count); // 5
StringEncoding.CodePoints(byteSpan) calculates from ReadOnlySpan<byte>.
byte[] data = new byte[] { 72, 101, 108, 108, 111 };
(long Utf8Count, long Utf16Count) = StringEncoding.CodePoints(byteSpan: data.AsSpan());
WriteLine(Utf16Count); // 5
WriteLine(Utf8Count); // 5
Properties
Key | Type | Description |
---|---|---|
Encoding | string "Utf8", "Utf16", "Utf32" | String encoding. |
Pattern | string | Regular expression pattern for valid values. |
Full Example
Full example
using Avalanche.Memory;
using static System.Console;
public class @string
{
public static void Run()
{
{
// <01>
byte[] data = new byte[5];
var memory = new ListMemory<byte[], byte>(data);
StringEncoding.Write(memory, srcEnumr: "Hello", out long bytesWritten);
WriteLine(bytesWritten); // 5
// </01>
}
{
// <01B>
byte[] data = new byte[5];
var memory = new ListMemory<byte[], byte>(data);
StringEncoding.Write(memory, srcSpan: "Hello".AsSpan(), out long bytesWritten);
WriteLine(bytesWritten); // 5
// </01B>
}
{
// <02>
byte[] data = new byte[5];
var memory = new ListMemory<byte[], byte>(data);
bool ok = StringEncoding.TryWrite(memory, srcEnumr: "Hello", out long bytesWritten);
WriteLine(bytesWritten); // 5
// </02>
}
{
// <02B>
byte[] data = new byte[5];
var memory = new ListMemory<byte[], byte>(data);
bool ok = StringEncoding.TryWrite(memory, srcSpan: "Hello".AsSpan(), out long bytesWritten);
WriteLine(bytesWritten); // 5
// </02B>
}
{
// <03>
byte[] data = new byte[] { 72, 101, 108, 108, 111 };
var memory = new ListMemory<byte[], byte>(data);
string value = StringEncoding.Read(memory, out long bytesRead);
WriteLine(value); // "Hello"
// </03>
}
{
// <03B>
byte[] data = new byte[] { 72, 101, 108, 108, 111 };
var memory = new ListMemory<byte[], byte>(data);
Span<char> dstSpan = new char[5];
StringEncoding.Read(memory, dstSpan, out int charsWritten);
WriteLine(charsWritten); // 5
// </03B>
}
{
// <03C>
byte[] data = new byte[] { 72, 101, 108, 108, 111 };
var memory = new ListMemory<byte[], byte>(data);
IMemory<char> dstMemory = new BlockMemory<char>();
dstMemory.Count = StringEncoding.CodePoints(memory).Utf16;
StringEncoding.Read(memory, dstMemory);
WriteLine(dstMemory.Count); // 5
// </03C>
}
{
// <04>
byte[] data = new byte[] { 72, 101, 108, 108, 111 };
var memory = new ListMemory<byte[], byte>(data);
bool ok = StringEncoding.TryRead(memory, out string value, out long bytesRead);
WriteLine(value); // "Hello"
// </04>
}
{
// <04B>
byte[] data = new byte[] { 72, 101, 108, 108, 111 };
var memory = new ListMemory<byte[], byte>(data);
Span<char> dstSpan = new char[5];
bool ok = StringEncoding.TryRead(memory, dstSpan, out int charsWritten);
WriteLine(charsWritten); // 5
// </04B>
}
{
// <04C>
byte[] data = new byte[] { 72, 101, 108, 108, 111 };
var memory = new ListMemory<byte[], byte>(data);
IMemory<char> dstMemory = new BlockMemory<char>();
dstMemory.Count = StringEncoding.CodePoints(memory).Utf16;
bool ok = StringEncoding.TryRead(memory, dstMemory);
WriteLine(dstMemory.Count); // 5
// </04C>
}
{
// <05>
byte[] data = new byte[5];
var memory = new Slice<ListMemory<byte[], byte>, byte>(new ListMemory<byte[], byte>(data), 0, 0, data.Length);
StringEncoding.Insert(ref memory, srcEnumr: "Hello", out long bytesWritten);
WriteLine(bytesWritten); // 5
// </05>
}
{
// <05B>
byte[] data = new byte[5];
var memory = new Slice<ListMemory<byte[], byte>, byte>(new ListMemory<byte[], byte>(data), 0, 0, data.Length);
StringEncoding.Insert(ref memory, srcSpan: "Hello".AsSpan(), out long bytesWritten);
WriteLine(bytesWritten); // 5
// </05B>
}
{
// <06>
byte[] data = new byte[5];
var memory = new Slice<ListMemory<byte[], byte>, byte>(new ListMemory<byte[], byte>(data), 0, 0, data.Length);
StringEncoding.TryInsert(ref memory, srcEnumr: "Hello", out long bytesWritten);
WriteLine(bytesWritten); // 5
// </06>
}
{
// <06B>
byte[] data = new byte[5];
var memory = new Slice<ListMemory<byte[], byte>, byte>(new ListMemory<byte[], byte>(data), 0, 0, data.Length);
StringEncoding.TryInsert(ref memory, srcSpan: "Hello".AsSpan(), out long bytesWritten);
WriteLine(bytesWritten); // 5
// </06B>
}
{
// <11>
(long utf8Count, long utf16Count) = StringEncoding.CodePoints(charEnumr: "Hello world. Ö🏰Ö");
WriteLine(utf16Count); // 17
WriteLine(utf8Count); // 21
// </11>
}
{
// <12>
(long utf8Count, long utf16Count) = StringEncoding.CodePoints(charSpan: "Hello world. Ö🏰Ö".AsSpan());
WriteLine(utf16Count); // 17
WriteLine(utf8Count); // 21
// </12>
}
{
// <13>
byte[] data = new byte[] { 72, 101, 108, 108, 111 };
var memory = new ListMemory<byte[], byte>(data);
(long Utf8Count, long Utf16Count) = StringEncoding.CodePoints(byteEnumr: memory);
WriteLine(Utf16Count); // 5
WriteLine(Utf8Count); // 5
// </13>
}
{
// <14>
byte[] data = new byte[] { 72, 101, 108, 108, 111 };
(long Utf8Count, long Utf16Count) = StringEncoding.CodePoints(byteSpan: data.AsSpan());
WriteLine(Utf16Count); // 5
WriteLine(Utf8Count); // 5
// </14>
}
}
}