当前位置: 首页>>代码示例>>C#>>正文


C# Utf8String.Equals方法代码示例

本文整理汇总了C#中Utf8String.Equals方法的典型用法代码示例。如果您正苦于以下问题:C# Utf8String.Equals方法的具体用法?C# Utf8String.Equals怎么用?C# Utf8String.Equals使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Utf8String的用法示例。


在下文中一共展示了Utf8String.Equals方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CreateFormattingData

        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);
        }
开发者ID:Applied-Duality,项目名称:corefxlab,代码行数:71,代码来源:FormattingDataProvider.cs

示例2: StringEqualsConvertedToUtf16String

 public unsafe void StringEqualsConvertedToUtf16String(bool expected, Utf8String s1, Utf8String s2)
 {
     Assert.Equal(expected, s1.Equals(s2.ToString()));
     Assert.Equal(expected, s2.Equals(s1.ToString()));
 }
开发者ID:jkotas,项目名称:corefxlab,代码行数:5,代码来源:RandomTests.cs

示例3: StringEquals

 public unsafe void StringEquals(bool expected, Utf8String s1, Utf8String s2)
 {
     Assert.Equal(expected, s1.Equals(s2));
     Assert.Equal(expected, s2.Equals(s1));
 }
开发者ID:jkotas,项目名称:corefxlab,代码行数:5,代码来源:RandomTests.cs

示例4: StringEquals

        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));
            }
        }
开发者ID:Applied-Duality,项目名称:corefxlab,代码行数:37,代码来源:RandomTests.cs

示例5: CreateEncoding

        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);
        }
开发者ID:jkotas,项目名称:corefxlab,代码行数:64,代码来源:FormattingDataProvider.cs

示例6: EqualsUtf8String

 // 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);
 }
开发者ID:GrimDerp,项目名称:corefxlab,代码行数:5,代码来源:Utf16LittleEndianStringExtensions.cs


注:本文中的Utf8String.Equals方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。