Esempio n. 1
0
        // Sets up cultures with digits represented by 1 or 5 'A's (0) through 1 or 5 'J's (9) and the minus sigh represented by an underscore followed by a question mark
        static CustomCultureTests()
        {
            byte[][] utf16digitsAndSymbols = new byte[17][];
            for (ushort digit = 0; digit < 10; digit++)
            {
                char digitChar   = (char)(digit + 'A');
                var  digitString = new string(digitChar, 5);
                utf16digitsAndSymbols[digit] = GetBytesUtf16(digitString);
            }
            utf16digitsAndSymbols[(ushort)TextEncoder.Symbol.DecimalSeparator] = GetBytesUtf16(".");
            utf16digitsAndSymbols[(ushort)TextEncoder.Symbol.GroupSeparator]   = GetBytesUtf16(",");
            utf16digitsAndSymbols[(ushort)TextEncoder.Symbol.MinusSign]        = GetBytesUtf16("_?");
            Culture5 = TextEncoder.CreateUtf16Encoder(utf16digitsAndSymbols);

            utf16digitsAndSymbols = new byte[17][];
            for (ushort digit = 0; digit < 10; digit++)
            {
                char digitChar   = (char)(digit + 'A');
                var  digitString = new string(digitChar, 1);
                utf16digitsAndSymbols[digit] = GetBytesUtf16(digitString);
            }
            utf16digitsAndSymbols[(ushort)TextEncoder.Symbol.DecimalSeparator] = GetBytesUtf16(".");
            utf16digitsAndSymbols[(ushort)TextEncoder.Symbol.GroupSeparator]   = GetBytesUtf16(",");
            utf16digitsAndSymbols[(ushort)TextEncoder.Symbol.MinusSign]        = GetBytesUtf16("_?");
            Culture1 = TextEncoder.CreateUtf16Encoder(utf16digitsAndSymbols);
        }
Esempio n. 2
0
        static TextEncoder GetTargetEncoder(EncoderTarget target)
        {
            switch (target)
            {
            case EncoderTarget.InvariantUtf8:
                return(TextEncoder.Utf8);

            case EncoderTarget.InvariantUtf16:
                return(TextEncoder.Utf16);

            case EncoderTarget.SlowUtf8:
                return(TextEncoder.CreateUtf8Encoder(Utf8DigitsAndSymbols));

            case EncoderTarget.SlowUtf16:
                return(TextEncoder.CreateUtf16Encoder(Utf16DigitsAndSymbols));

            default:
                Assert.True(false, "Invalid encoder targetted in test");
                return(null);
            }
        }
Esempio n. 3
0
        private static TextEncoder CreateEncoder(string localeId, Stream resourceStream)
        {
            const int maxIdLength = 15;
            const int recordSize  = 20;

            var b1          = resourceStream.ReadByte();
            var b2          = resourceStream.ReadByte();
            var numberOfIDs = b1 * 256 + b2;

            var indexSize = numberOfIDs * 20;
            var index     = new byte[indexSize];

            resourceStream.Read(index, 0, indexSize);

            byte[] idBytes = new byte[maxIdLength];
            int    idByteCount;

            if (!TextEncoder.Utf8.TryEncode(localeId, new Span <byte>(idBytes), out idByteCount))
            {
                throw new Exception("bad locale id");
            }
            var id = new Utf8String(idBytes.AsSpan().Slice(0, idByteCount));

            int recordStart = -1;

            for (int record = 0; record < numberOfIDs; record++)
            {
                var indexId = index.AsSpan().Slice(record * recordSize, idByteCount);
                if (id.Equals(new Utf8String(indexId))) // found record
                {
                    var indexData = index.AsSpan().Slice(record * recordSize + maxIdLength);
                    recordStart  = 0;
                    recordStart += indexData[3] * 256 * 256 * 256;
                    recordStart += indexData[2] * 256 * 256;
                    recordStart += indexData[1] * 256;
                    recordStart += indexData[0];
                    break;
                }
            }

            if (recordStart == -1)
            {
                throw new Exception("local not found");
            }

            resourceStream.Position = recordStart;

            const int bufferSize = 512;
            var       data       = new byte[bufferSize];
            var       bytesRead  = resourceStream.Read(data, 0, bufferSize);
            // TODO: maybe we should store length in the index

            var numberOfStrings = ReadUInt16At(data, 0);

            Debug.Assert(numberOfStrings == 17);

            var utf16digitsAndSymbols = new byte[numberOfStrings][];

            for (int stringIndex = 0; stringIndex < numberOfStrings; stringIndex++)
            {
                var stringStart  = ReadUInt16At(data, stringIndex * 2 + 1);
                var stringLength = ReadUInt16At(data, stringIndex * 2 + 2);
                utf16digitsAndSymbols[stringIndex] = new byte[stringLength];
                Array.Copy(data, stringStart, utf16digitsAndSymbols[stringIndex], 0, stringLength);
            }

            return(TextEncoder.CreateUtf16Encoder(utf16digitsAndSymbols));
        }