Exemple #1
0
        public unsafe void StringEqualsConvertedToUtf16String(bool expected, string str1, string str2)
        {
            Utf8String s1 = new Utf8String(str1);
            Utf8String s2 = new Utf8String(str2);

            Assert.Equal(expected, s1.Equals(s2.ToString()));
            Assert.Equal(expected, s2.Equals(s1.ToString()));
        }
Exemple #2
0
        public unsafe void StringEqualsWithThreeArgs(bool expected, string str1, string str2)
        {   // TODO: investigate why the tests fail if we have two methods with the same name StringEquals
            Utf8String s1 = new Utf8String(str1);
            Utf8String s2 = new Utf8String(str2);

            Assert.Equal(expected, s1.Equals(s2));
            Assert.Equal(expected, s2.Equals(s1));
        }
Exemple #3
0
 public bool Equals(JsonProperty other)
 {
     if (Object != other.Object)
     {
         return(false);
     }
     if (_name.Equals(other._name))
     {
         return(true);
     }
     return(false);
 }
Exemple #4
0
        public unsafe void StringEquals(bool equal, string s1, string s2)
        {
            byte[]     b1          = Encoding.UTF8.GetBytes(s1);
            byte[]     b2          = Encoding.UTF8.GetBytes(s2);
            Utf8String s1FromBytes = new Utf8String(b1);
            Utf8String s2FromBytes = new Utf8String(b2);

            fixed(byte *b1pinned = b1)
            fixed(byte *b2pinned = b2)
            {
                Span <byte> sp1        = new Span <byte>(b1pinned, b1.Length);
                Span <byte> sp2        = new Span <byte>(b2pinned, b2.Length);
                Utf8String  s1FromSpan = new Utf8String(sp1);
                Utf8String  s2FromSpan = new Utf8String(sp2);

                Assert.Equal(equal, s1FromBytes == s2FromBytes);
                Assert.Equal(equal, s1FromBytes == s2FromSpan);
                Assert.Equal(equal, s1FromBytes == s2);

                Assert.Equal(equal, s1FromSpan == s2FromBytes);
                Assert.Equal(equal, s1FromSpan == s2FromSpan);
                Assert.Equal(equal, s1FromSpan == s2);

                Assert.Equal(equal, s1 == s2FromBytes);
                Assert.Equal(equal, s1 == s2FromSpan);


                Assert.Equal(equal, s1FromBytes.Equals(s2FromBytes));
                Assert.Equal(equal, s1FromBytes.Equals(s2FromSpan));
                Assert.Equal(equal, s1FromBytes.Equals(s2));

                Assert.Equal(equal, s1FromSpan.Equals(s2FromBytes));
                Assert.Equal(equal, s1FromSpan.Equals(s2FromSpan));
                Assert.Equal(equal, s1FromSpan.Equals(s2));

                Assert.Equal(equal, s1.EqualsUtf8String(s2FromBytes));
                Assert.Equal(equal, s1.EqualsUtf8String(s2FromSpan));
            }
        }
        public static void Equality_Ordinal(string aString, string bString, bool expected)
        {
            Utf8String a = u8(aString);
            Utf8String b = u8(bString);

            // Operators

            Assert.Equal(expected, a == b);
            Assert.NotEqual(expected, a != b);

            // Static methods

            Assert.Equal(expected, Utf8String.Equals(a, b));

            // Instance methods

            if (a != null)
            {
                Assert.Equal(expected, a.Equals(b));
                Assert.Equal(expected, a.Equals((object)b));
            }
        }
        internal HttpVersion ReadHttpVersion()
        {
            ReadOnlySpan <byte> oldBuffer = Buffer;
            Utf8String          version   = ReadHttpVersionAsUtf8String();

            if (version.Equals(s_Http1_1))
            {
                return(HttpVersion.V1_1);
            }
            else if (version.Equals(s_Http2_0))
            {
                return(HttpVersion.V2_0);
            }
            else if (version.Equals(s_Http1_0))
            {
                return(HttpVersion.V1_0);
            }
            else
            {
                Buffer = oldBuffer;
                return(HttpVersion.Unknown);
            }
        }
Exemple #7
0
        public bool TryHandle(HttpRequest request, HttpResponse response)
        {
            Utf8String requestUtf8 = request.Path.ToUtf8String(TextEncoder.Utf8);

            for (int i = 0; i < _count; i++)
            {
                // TODO: this should check the verb too
                if (requestUtf8.Equals(_uris[i]))
                {
                    _handlers[i](request, response);
                    return(true);
                }
            }
            return(false);
        }
Exemple #8
0
        public bool TryHandle(HttpRequest request, TcpConnectionFormatter response)
        {
            // TODO: this should not allocate new string
            Utf8String requestUtf8 = request.Path.ToUtf8String(TextEncoder.Utf8);

            for (int i = 0; i < _count; i++)
            {
                // TODO: this should check the verb too
                if (requestUtf8.Equals(new Utf8String(_uris[i])))
                {
                    _handlers[i](request, response);
                    return(true);
                }
            }
            return(false);
        }
Exemple #9
0
 // The assumption here is that the name of the blob is unique.
 // We can't emit two blobs with the same name and different contents.
 // The name is part of the symbolic name and we don't do any mangling on it.
 public bool Equals(ReadOnlyDataBlobKey other) => Name.Equals(other.Name);
Exemple #10
0
        public static FormattingData CreateFormattingData(string localeId)
        {
            const int maxIdLength = 15;
            const int recordSize  = 20;

            var resourceName   = "System.Text.Formatting.locales.bin";
            var resourceStream = typeof(FormattingDataProvider).GetTypeInfo().Assembly.GetManifestResourceStream(resourceName);

            if (resourceStream == null)
            {
                throw new Exception("resource missing");
            }

            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 (!localeId.TryFormat(idBytes, default(Format.Parsed), FormattingData.InvariantUtf8, out idByteCount))
            {
                throw new Exception("bad locale id");
            }
            var id = new Utf8String(idBytes.Slice(0, idByteCount).CreateArray());

            int recordStart = -1;

            for (int record = 0; record < numberOfIDs; record++)
            {
                var indexId = index.Slice(record * recordSize, idByteCount);
                if (id.Equals(new Utf8String(indexId.CreateArray()))) // found record
                {
                    var indexData = index.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)
            {
                return(FormattingData.InvariantUtf16);
            }

            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(new FormattingData(utf16digitsAndSymbols, FormattingData.Encoding.Utf16));
        }
Exemple #11
0
 // TODO: Naming it Equals causes picking up wrong overload when compiling (Equals(object))
 public static bool EqualsUtf8String(this string left, Utf8String right)
 {
     return(right.Equals(left));
 }
Exemple #12
0
 public unsafe void StringEqualsConvertedToUtf16String(bool expected, Utf8String s1, Utf8String s2)
 {
     Assert.Equal(expected, s1.Equals(s2.ToString()));
     Assert.Equal(expected, s2.Equals(s1.ToString()));
 }
Exemple #13
0
 public unsafe void StringEquals(bool expected, Utf8String s1, Utf8String s2)
 {
     Assert.Equal(expected, s1.Equals(s2));
     Assert.Equal(expected, s2.Equals(s1));
 }
        public unsafe void StringEquals(bool equal, string s1, string s2)
        {
            byte[] b1 = Encoding.UTF8.GetBytes(s1);
            byte[] b2 = Encoding.UTF8.GetBytes(s2);
            Utf8String s1FromBytes = new Utf8String(b1);
            Utf8String s2FromBytes = new Utf8String(b2);
            fixed (byte* b1pinned = b1)
            fixed (byte* b2pinned = b2)
            {
                ByteSpan sp1 = new ByteSpan(b1pinned, b1.Length);
                ByteSpan sp2 = new ByteSpan(b2pinned, b2.Length);
                Utf8String s1FromSpan = new Utf8String(sp1);
                Utf8String s2FromSpan = new Utf8String(sp2);

                Assert.Equal(equal, s1FromBytes == s2FromBytes);
                Assert.Equal(equal, s1FromBytes == s2FromSpan);
                Assert.Equal(equal, s1FromBytes == s2);

                Assert.Equal(equal, s1FromSpan == s2FromBytes);
                Assert.Equal(equal, s1FromSpan == s2FromSpan);
                Assert.Equal(equal, s1FromSpan == s2);

                Assert.Equal(equal, s1 == s2FromBytes);
                Assert.Equal(equal, s1 == s2FromSpan);

                Assert.Equal(equal, s1FromBytes.Equals(s2FromBytes));
                Assert.Equal(equal, s1FromBytes.Equals(s2FromSpan));
                Assert.Equal(equal, s1FromBytes.Equals(s2));

                Assert.Equal(equal, s1FromSpan.Equals(s2FromBytes));
                Assert.Equal(equal, s1FromSpan.Equals(s2FromSpan));
                Assert.Equal(equal, s1FromSpan.Equals(s2));

                Assert.Equal(equal, s1.EqualsUtf8String(s2FromBytes));
                Assert.Equal(equal, s1.EqualsUtf8String(s2FromSpan));
            }
        }
 // TODO: Naming it Equals causes picking up wrong overload when compiling (Equals(object))
 public static bool EqualsUtf8String(this string left, Utf8String right)
 {
     return right.Equals(left);
 }
        public static FormattingData CreateFormattingData(string localeId)
        {
            const int maxIdLength = 15;
            const int recordSize = 20;

            var resourceName = "System.Text.Formatting.locales.bin";
            var resourceStream = typeof(FormattingDataProvider).GetTypeInfo().Assembly.GetManifestResourceStream(resourceName);
            if (resourceStream == null)
            {
                throw new Exception("resource missing");
            }

            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 (!localeId.TryFormat(idBytes, default(Format.Parsed), FormattingData.InvariantUtf8, out idByteCount))
            {
                throw new Exception("bad locale id");
            }
            var id = new Utf8String(idBytes.Slice(0, idByteCount).CreateArray());

            int recordStart = -1;
            for (int record = 0; record < numberOfIDs; record++)
            {
                var indexId = index.Slice(record * recordSize, idByteCount);
                if (id.Equals(new Utf8String(indexId.CreateArray()))) // found record
                {
                    var indexData = index.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 new FormattingData(utf16digitsAndSymbols, FormattingData.Encoding.Utf16);
        }
Exemple #17
0
        private static SymbolTable CreateSymbolTable(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];
            var    status  = Encoders.Utf16.ToUtf8(localeId.AsReadOnlySpan().AsBytes(), idBytes, out int consumed, out int idByteCount);

            if (status != System.Buffers.TransformationStatus.Done)
            {
                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(new CultureUtf16SymbolTable(utf16digitsAndSymbols));
        }
Exemple #18
0
 public unsafe void StringEqualsConvertedToUtf16String(bool expected, Utf8String s1, Utf8String s2)
 {
     Assert.Equal(expected, s1.Equals(s2.ToString()));
     Assert.Equal(expected, s2.Equals(s1.ToString()));
 }
Exemple #19
0
 public unsafe void StringEquals(bool expected, Utf8String s1, Utf8String s2)
 {
     Assert.Equal(expected, s1.Equals(s2));
     Assert.Equal(expected, s2.Equals(s1));
 }
        private static EncodingData CreateEncoding(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 (!localeId.TryFormat(new Span<byte>(idBytes), default(Format.Parsed), EncodingData.InvariantUtf8, out idByteCount))
            {
                throw new Exception("bad locale id");
            }
            var id = new Utf8String(idBytes.Slice(0, idByteCount));

            int recordStart = -1;
            for (int record = 0; record < numberOfIDs; record++)
            {
                var indexId = index.Slice(record * recordSize, idByteCount);
                if (id.Equals(new Utf8String(indexId))) // found record
                {
                    var indexData = index.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 new EncodingData(utf16digitsAndSymbols, EncodingData.Encoding.Utf16);
        }
        private static EncodingData CreateEncoding(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 (!EncodingData.InvariantUtf8.TextEncoder.TryEncode(localeId, new Span <byte>(idBytes), out idByteCount))
            {
                throw new Exception("bad locale id");
            }
            var id = new Utf8String(idBytes.Slice(0, idByteCount));

            int recordStart = -1;

            for (int record = 0; record < numberOfIDs; record++)
            {
                var indexId = index.Slice(record * recordSize, idByteCount);
                if (id.Equals(new Utf8String(indexId))) // found record
                {
                    var indexData = index.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(new EncodingData(utf16digitsAndSymbols, TextEncoder.Utf16));
        }
Exemple #22
0
 public override bool Equals(Utf8String?x, Utf8String?y) => Utf8String.Equals(x, y);
Exemple #23
0
 // The assumption here is that the name of the blob is unique.
 // We can't emit two blobs with the same name and different contents.
 // The name is part of the symbolic name and we don't do any mangling on it.
 public bool Equals(UninitializedWritableDataBlobKey other) => Name.Equals(other.Name);