public static bool TryDecode(this IBytesToUtf8StringConverter converter, string text, IBufferWriter <byte> bufferWriter) { using (var recyclableMemory = BytesPool.Shared.Memory.Rent(_utf8Encoding.Value.GetMaxByteCount(text.Length))) { var length = _utf8Encoding.Value.GetBytes(text, recyclableMemory.Memory.Span); if (!converter.TryDecode(recyclableMemory.Memory.Span[..length], bufferWriter))
public static bool TryEncode(this IBytesToUtf8StringConverter converter, ReadOnlySequence <byte> sequence, out string text, bool includePrefix = false) { if (!converter.TryEncode(sequence, out var utf8string, includePrefix)) { throw new FormatException(nameof(sequence)); } text = _utf8Encoding.Value.GetString(utf8string); return(true); }
public static byte[] Utf8StringToBytes(this IBytesToUtf8StringConverter converter, ReadOnlySpan <byte> text) { using var hub = new BytesHub(); converter.TryDecode(text, hub.Writer); var result = new byte[hub.Writer.WrittenBytes]; hub.Reader.GetSequence().CopyTo(result); return(result); }
public static bool TryEncode(this IBytesToUtf8StringConverter converter, ReadOnlySequence <byte> sequence, [NotNullWhen(true)] out string?text, bool includePrefix = false) { text = null; if (!converter.TryEncode(sequence, out var utf8string, includePrefix)) { return(false); } text = _utf8Encoding.Value.GetString(utf8string); return(true); }
public static byte[] StringToBytes(this IBytesToUtf8StringConverter converter, string text) { using (var hub = new Hub()) { converter.TryDecode(text, hub.Writer); hub.Writer.Complete(); var result = new byte[hub.Writer.BytesWritten]; hub.Reader.GetSequence().CopyTo(result); hub.Reader.Complete(); return(result); } }
public static bool TryDecode(this IBytesToUtf8StringConverter converter, string text, IBufferWriter <byte> bufferWriter) { using (var recyclableMemory = BufferPool.Shared.Rent(_utf8Encoding.Value.GetMaxByteCount(text.Length))) { var length = _utf8Encoding.Value.GetBytes(text, recyclableMemory.Memory.Span); if (!converter.TryDecode(recyclableMemory.Memory.Span.Slice(0, length), bufferWriter)) { throw new FormatException(nameof(text)); } } return(true); }
public static string BytesToString(this IBytesToUtf8StringConverter converter, ReadOnlySequence <byte> sequence) { converter.TryEncode(sequence, out string text); return(text); }
public static string BytesToString(this IBytesToUtf8StringConverter converter, ReadOnlySpan <byte> span) { converter.TryEncode(span, out string text); return(text); }