/// <summary> /// Decode a byte-Lease as a String, optionally specifying the encoding (UTF-8 if omitted). /// </summary> /// <param name="bytes">The bytes to decode.</param> /// <param name="encoding">The encoding to use.</param> public static string DecodeString(this Lease <byte> bytes, Encoding encoding = null) { if (bytes == null) { return(null); } if (encoding == null) { encoding = Encoding.UTF8; } if (bytes.Length == 0) { return(""); } var segment = bytes.ArraySegment; return(encoding.GetString(segment.Array, segment.Offset, segment.Count)); }
/// <summary> /// Decode a byte-Lease as a String, optionally specifying the encoding (UTF-8 if omitted). /// </summary> /// <param name="bytes">The bytes to decode.</param> /// <param name="encoding">The encoding to use.</param> public static Lease <char> DecodeLease(this Lease <byte> bytes, Encoding encoding = null) { if (bytes == null) { return(null); } encoding ??= Encoding.UTF8; if (bytes.Length == 0) { return(Lease <char> .Empty); } var bytesSegment = bytes.ArraySegment; var charCount = encoding.GetCharCount(bytesSegment.Array, bytesSegment.Offset, bytesSegment.Count); var chars = Lease <char> .Create(charCount, false); var charsSegment = chars.ArraySegment; encoding.GetChars(bytesSegment.Array, bytesSegment.Offset, bytesSegment.Count, charsSegment.Array, charsSegment.Offset); return(chars); }