using System.Buffers; using System.Diagnostics.Contracts; using System.Text; using Brutal.Collections.Experimental; using SystemUtf8 = System.Text.Unicode.Utf8; namespace Brutal.Strings; /// /// Combination of and for common UTF-8 operations. /// public static partial class Utf8 { /// /// Count the number of bytes in a null-terminated byte array. /// [Pure] public static unsafe int GetByteCount(Ptr bytes) { return MemoryMarshal.CreateReadOnlySpanFromNullTerminated(bytes).Length; } /// /// Convert a null-terminated byte array to a containing the bytes up to the null terminator. /// [Pure] public static unsafe String8View GetNullTerminatedSpan(Ptr bytes) { return MemoryMarshal.CreateReadOnlySpanFromNullTerminated(bytes); } /// Evaluate length from the null-terminator, if found - strlen() equivalent. [Pure] public static int EvaluateLength(String8View utf8Buffer) { var length = utf8Buffer.IndexOf((Char8)0); return length != -1 ? length : utf8Buffer.Length; } /// /// Usage: /// using (Utf8.GetChars(value, stackalloc Char16[256], out var chars)) /// { /// // ... use "chars" here /// } /// /// The value to decode /// A temporary buffer, typically stack-allocated; if too small, will rent an array. /// The decoded UTF16 characters /// Scratch handle; dispose to return any rented array to the pool. [Pure] public static ScratchSpan GetChars(String8View value, Span stackBuffer, out Span chars) { var scratch = new ScratchSpan(GetCharCount(value), stackBuffer, out chars); GetChars(value, chars); return scratch; } /// /// Usage: /// using (Utf8.GetBytes(value, stackalloc Char8[256], out var bytes)) /// { /// // ... use "bytes" here /// } /// /// The value to encode /// A temporary buffer, typically stack-allocated; if too small, will rent an array. /// The encoded UTF8 bytes /// Scratch handle; dispose to return any rented array to the pool. [Pure] public static ScratchSpan GetBytes(String16View value, Span stackBuffer, out Span bytes) { var scratch = new ScratchSpan(GetByteCount(value), stackBuffer, out bytes); GetBytes(value, bytes); return scratch; } } // System.Text.Unicode.Utf8 public static partial class Utf8 { /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static System.Buffers.OperationStatus FromUtf16( String16View source, Span destination, out int charsRead, out int bytesWritten, bool replaceInvalidSequences = true, bool isFinalBlock = true ) { return SystemUtf8.FromUtf16( source, destination, out charsRead, out bytesWritten, replaceInvalidSequences, isFinalBlock ); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static System.Buffers.OperationStatus ToUtf16( String8View source, Span destination, out int bytesRead, out int charsWritten, bool replaceInvalidSequences = true, bool isFinalBlock = true ) { return SystemUtf8.ToUtf16( source, destination, out bytesRead, out charsWritten, replaceInvalidSequences, isFinalBlock ); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool TryWrite( Span destination, [InterpolatedStringHandlerArgument(nameof(destination))] ref SystemUtf8.TryWriteInterpolatedStringHandler handler, out int bytesWritten ) { return SystemUtf8.TryWrite(destination, ref handler, out bytesWritten); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool TryWrite( Span destination, IFormatProvider? provider, [InterpolatedStringHandlerArgument(nameof(destination), nameof(provider))] ref SystemUtf8.TryWriteInterpolatedStringHandler handler, out int bytesWritten ) { return SystemUtf8.TryWrite(destination, provider, ref handler, out bytesWritten); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool IsValid(String8View value) { return SystemUtf8.IsValid(value); } } // System.Text.Encoding.UTF8 public static partial class Utf8 { /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int GetByteCount(Char16[] chars) { return Encoding.UTF8.GetByteCount(chars, 0, chars.Length); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int GetByteCount(string str) { return Encoding.UTF8.GetByteCount(str); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int GetByteCount(Char16[] chars, int index, int count) { return Encoding.UTF8.GetByteCount(chars, index, count); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int GetByteCount(string s, int index, int count) { return Encoding.UTF8.GetByteCount(s, index, count); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static unsafe int GetByteCount(Ptr chars, int count) { return Encoding.UTF8.GetByteCount(chars, count); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int GetByteCount(String16View chars) { return Encoding.UTF8.GetByteCount(chars); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Char8[] GetBytes(Char16[] chars) { return Encoding.UTF8.GetBytes(chars); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Char8[] GetBytes(Char16[] chars, int index, int count) { return Encoding.UTF8.GetBytes(chars, index, count); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int GetBytes(Char16[] chars, int charIndex, int charCount, Char8[] bytes, int byteIndex) { return Encoding.UTF8.GetBytes(chars, charIndex, charCount, bytes, byteIndex); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Char8[] GetBytes(string s) { return Encoding.UTF8.GetBytes(s); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Char8[] GetBytes(string s, int index, int count) { return Encoding.UTF8.GetBytes(s, index, count); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int GetBytes(string s, int charIndex, int charCount, Char8[] bytes, int byteIndex) { return Encoding.UTF8.GetBytes(s, charIndex, charCount, bytes, byteIndex); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static unsafe int GetBytes(Ptr chars, int charCount, Ptr bytes, int byteCount) { return Encoding.UTF8.GetBytes(chars, charCount, bytes, byteCount); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int GetBytes(String16View chars, Span bytes) { return Encoding.UTF8.GetBytes(chars, bytes); } /// /// /// Routed through , which is single-pass and alloc-free. /// Avoid using which allocates (an EncoderFallbackBuffer) /// when is too small. /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool TryGetBytes(String16View chars, Span bytes, out int bytesWritten) { if (FromUtf16(chars, bytes, out _, out bytesWritten) == OperationStatus.Done) return true; bytesWritten = 0; // return 0 on failure, matching Encoding.TryGetBytes (no partial count) return false; } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int GetCharCount(Char8[] bytes) { return Encoding.UTF8.GetCharCount(bytes, 0, bytes.Length); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int GetCharCount(Char8[] bytes, int index, int count) { return Encoding.UTF8.GetCharCount(bytes, index, count); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static unsafe int GetCharCount(Ptr bytes, int count) { return Encoding.UTF8.GetCharCount(bytes, count); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int GetCharCount(String8View bytes) { return Encoding.UTF8.GetCharCount(bytes); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Char16[] GetChars(Char8[] bytes) { return Encoding.UTF8.GetChars(bytes); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Char16[] GetChars(Char8[] bytes, int index, int count) { return Encoding.UTF8.GetChars(bytes, index, count); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int GetChars(Char8[] bytes, int byteIndex, int byteCount, Char16[] chars, int charIndex) { return Encoding.UTF8.GetChars(bytes, byteIndex, byteCount, chars, charIndex); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static unsafe int GetChars(Ptr bytes, int byteCount, Ptr chars, int charCount) { return Encoding.UTF8.GetChars(bytes, byteCount, chars, charCount); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int GetChars(String8View bytes, Span chars) { return Encoding.UTF8.GetChars(bytes, chars); } /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool TryGetChars(String8View bytes, Span chars, out int charsWritten) { if (ToUtf16(bytes, chars, out _, out charsWritten) == OperationStatus.Done) return true; charsWritten = 0; // return 0 on failure, matching Encoding.TryGetBytes (no partial count) return false; } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static unsafe string GetString(Ptr bytes, int byteCount) { return Encoding.UTF8.GetString(bytes, byteCount); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static unsafe string GetString(Ptr bytes) { return Encoding.UTF8.GetString(bytes, GetByteCount(bytes)); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static string GetString(String8View bytes) { return Encoding.UTF8.GetString(bytes); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int GetMaxByteCount(int charCount) { return Encoding.UTF8.GetMaxByteCount(charCount); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int GetMaxCharCount(int byteCount) { return Encoding.UTF8.GetMaxCharCount(byteCount); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static string GetString(Char8[] bytes) { return Encoding.UTF8.GetString(bytes); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static string GetString(Char8[] bytes, int index, int count) { return Encoding.UTF8.GetString(bytes, index, count); } }