Exemplo n.º 1
0
        public static ByteStringContext <ByteStringMemoryCache> .ExternalScope GetSliceFromKey <TTransaction>(
            TransactionOperationContext <TTransaction> context, string key, out Slice keySlice)
            where TTransaction : RavenTransaction
        {
            var byteCount = Utf8.GetMaxByteCount(key.Length);

            var buffer = context.GetNativeTempBuffer(
                byteCount // this buffer is allocated to also serve the GetSliceFromUnicodeKey
                + sizeof(char) * key.Length);


            if (key.Length > 512)
            {
                ThrowKeyTooBig(key);
            }


            for (var i = 0; i < key.Length; i++)
            {
                var ch = key[i];
                if (ch > 127) // not ASCII, use slower mode
                {
                    goto UnlikelyUnicode;
                }
                if ((ch >= 65) && (ch <= 90))
                {
                    buffer[i] = (byte)(ch | 0x20);
                }
                else
                {
                    buffer[i] = (byte)ch;
                }
            }

            return(Slice.External(context.Allocator, buffer, (ushort)key.Length, out keySlice));

UnlikelyUnicode:
            return(GetSliceFromUnicodeKey(context, key, out keySlice, buffer, byteCount));
        }