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


C# Utf8String.Substring方法代码示例

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


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

示例1: ParseBool

        [InlineData(" !", false, 0, 0, 0)] // invalid character test w/ char < '0'
        public unsafe void ParseBool(string text, bool expectSuccess, int index, bool expectedValue, int expectedBytesConsumed)
        {
		    bool result;
            bool parsedValue;
            int bytesConsumed;
			var utf8String = new Utf8String(text);
			byte[] utf8Bytes = utf8String.CopyBytes();
			ReadOnlySpan<byte> utf8BytesSlice = new ReadOnlySpan<byte>(utf8Bytes);

			// System.String
			result = PrimitiveParser.TryParseBoolean(text, index, 'N', out parsedValue, out bytesConsumed);
            Assert.Equal(expectSuccess, result);
            Assert.Equal(expectedValue, parsedValue);
            Assert.Equal(expectedBytesConsumed, bytesConsumed);

			// Utf8String
			result = PrimitiveParser.TryParseBoolean(utf8String.Substring(index), 'N', out parsedValue, out bytesConsumed);
            Assert.Equal(expectSuccess, result);
            Assert.Equal(expectedValue, parsedValue);
            Assert.Equal(expectedBytesConsumed, bytesConsumed);

			// byte[]
			result = PrimitiveParser.TryParseBoolean(utf8Bytes, index, EncodingData.InvariantUtf8, 'N', out parsedValue, out bytesConsumed);
            Assert.Equal(expectSuccess, result);
            Assert.Equal(expectedValue, parsedValue);
            Assert.Equal(expectedBytesConsumed, bytesConsumed);

			// ReadOnlySpan<byte>
			result = PrimitiveParser.TryParseBoolean(utf8Bytes.Slice(index), EncodingData.InvariantUtf8, 'N', out parsedValue, out bytesConsumed);
            Assert.Equal(expectSuccess, result);
            Assert.Equal(expectedValue, parsedValue);
            Assert.Equal(expectedBytesConsumed, bytesConsumed);

			// byte*
            fixed (byte* arrayPointer = utf8Bytes)
            {
                result = PrimitiveParser.TryParseBoolean(arrayPointer, index, utf8Bytes.Length, EncodingData.InvariantUtf8, 'N',
                    out parsedValue, out bytesConsumed);

                Assert.Equal(expectSuccess, result);
                Assert.Equal(expectedValue, parsedValue);
                Assert.Equal(expectedBytesConsumed, bytesConsumed);
            }
        }
开发者ID:jkotas,项目名称:corefxlab,代码行数:45,代码来源:ParserTests.cs

示例2: TestHashesSameForEquivalentString

        private void TestHashesSameForEquivalentString(Utf8String a, Utf8String b)
        {
            // for sanity
            Assert.Equal(a.Length, b.Length);
            Assert.Equal(a, b);

            for (int i = 0; i < a.Length; i++)
            {
                Utf8String prefixOfA = a.Substring(i, a.Length - i);
                Utf8String prefixOfB = b.Substring(i, b.Length - i);
                // sanity
                Assert.Equal(prefixOfA, prefixOfB);
                Assert.Equal(prefixOfA.GetHashCode(), prefixOfB.GetHashCode());

                // for all suffixes
                Utf8String suffixOfA = a.Substring(a.Length - i, i);
                Utf8String suffixOfB = b.Substring(b.Length - i, i);
                Assert.Equal(suffixOfA, suffixOfB);
            }
        }
开发者ID:jkotas,项目名称:corefxlab,代码行数:20,代码来源:RandomTests.cs

示例3: HashesSameForTheSameSubstrings

        public void HashesSameForTheSameSubstrings()
        {
            const int len = 50;

            // two copies of the same string
            byte[] bytes = new byte[len * 2];
            for (int i = 0; i < len; i++)
            {
                // 0x20 is a spacebar, writing explicitly so
                // the value is more predictable
                bytes[i] = unchecked((byte)(0x20 + i));
                bytes[i + len] = bytes[i];
            }

            Utf8String sFromBytes = new Utf8String(bytes);
            Utf8String s1FromBytes = sFromBytes.Substring(0, len);
            Utf8String s2FromBytes = sFromBytes.Substring(len, len);

            unsafe
            {
                fixed (byte* pinnedBytes = bytes)
                {
                    Utf8String sFromSpan = new Utf8String(new Span<byte>(pinnedBytes, len * 2));
                    Utf8String s1FromSpan = sFromSpan.Substring(0, len);
                    Utf8String s2FromSpan = sFromSpan.Substring(len, len);
                    TestHashesSameForEquivalentString(s1FromBytes, s2FromBytes);
                    TestHashesSameForEquivalentString(s1FromSpan, s2FromSpan);
                    TestHashesSameForEquivalentString(s1FromSpan, s2FromBytes);
                }
            }
        }
开发者ID:jkotas,项目名称:corefxlab,代码行数:31,代码来源:RandomTests.cs

示例4: SubstringTrimOneCharacterOnEachSideConstructFromSpan

 public unsafe void SubstringTrimOneCharacterOnEachSideConstructFromSpan()
 {
     TestCase[] testCases = new TestCase[] {
         new TestCase(GetRandomString(5, 32, 126), "Short ASCII string", 50000000),
         new TestCase(GetRandomString(5, 32, 0xD7FF), "Short string", 50000000),
         new TestCase(GetRandomString(50000, 32, 126), "Long ASCII string", 50000000),
         new TestCase(GetRandomString(50000, 32, 0xD7FF), "Long string", 50000000)
     };
     foreach (TestCase testData in testCases)
     {
         string s = testData.String;
         Utf8String utf8s = new Utf8String(s);
         fixed (byte* bytes = utf8s.CopyBytes())
         {
             utf8s = new Utf8String(new Span<byte>(bytes, utf8s.Length));
             int iterations = testData.Iterations;
             _timer.Restart();
             while (iterations-- != 0)
             {
                 Utf8String result = utf8s.Substring(1, utf8s.Length - 2);
             }
             PrintTime(testData);
         }
     }
 }
开发者ID:benaadams,项目名称:corefxlab,代码行数:25,代码来源:PerformanceTests.cs

示例5: GetNumOfBackSlashesAtEndOfString

 private static int GetNumOfBackSlashesAtEndOfString(Utf8String str)
 {
     var numOfBackSlashes = 0;
     while (str.EndsWith(new Utf8String("\\")))
     {
         str = str.Substring(0, str.Length - 1);
         numOfBackSlashes++;
     }
     return numOfBackSlashes;
 }
开发者ID:Anniepoh,项目名称:corefxlab,代码行数:10,代码来源:JsonReader.cs

示例6: SubstringTrimOneCharacterOnEachSideConstructFromSpan

 public unsafe void SubstringTrimOneCharacterOnEachSideConstructFromSpan()
 {
     foreach (StringWithDescription testData in StringsWithDescription())
     {
         string s = testData.String;
         Utf8String utf8s = new Utf8String(s);
         fixed (byte* bytes = utf8s.CopyBytes())
         {
             utf8s = new Utf8String(new ByteSpan(bytes, utf8s.Length));
             int iterations = testData.Iterations;
             _timer.Restart();
             while (iterations-- != 0)
             {
                 Utf8String result = utf8s.Substring(1, utf8s.Length - 2);
             }
             PrintTime(testData);
         }
     }
 }
开发者ID:khalidabuhakmeh,项目名称:corefxlab,代码行数:19,代码来源:PerformanceTests.cs


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