コード例 #1
0
        internal Lease <byte> AsLease()
        {
            if (IsNull)
            {
                return(null);
            }
            switch (Type)
            {
            case ResultType.SimpleString:
            case ResultType.BulkString:
                var payload = Payload;
                var lease   = Lease <byte> .Create(checked ((int)payload.Length), false);

                payload.CopyTo(lease.Span);
                return(lease);
            }
            throw new InvalidCastException("Cannot convert to Lease: " + Type);
        }
コード例 #2
0
        /// <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);
        }