Exemplo n.º 1
3
 //TODO: this should use ByteSpan
 public void Append(ReadOnlySpan<char> substring)
 {
     for (int i = 0; i < substring.Length; i++)
     {
         Append(substring[i]);
     }
 }
 public static void DangerousGetPinnableReferenceArray()
 {
     int[] a = { 91, 92, 93, 94, 95 };
     ReadOnlySpan<int> span = new ReadOnlySpan<int>(a, 1, 3);
     ref int pinnableReference = ref span.DangerousGetPinnableReference();
     Assert.True(Unsafe.AreSame<int>(ref a[1], ref pinnableReference));
 }
Exemplo n.º 3
0
        private static bool IsFalse(ReadOnlySpan<char> utf16Chars)
        {
            if (utf16Chars.Length < 5)
                return false;

            char firstChar = utf16Chars[0];
            if (firstChar != 'f' && firstChar != 'F')
                return false;

            char secondChar = utf16Chars[1];
            if (secondChar != 'a' && secondChar != 'A')
                return false;

            char thirdChar = utf16Chars[2];
            if (thirdChar != 'l' && thirdChar != 'L')
                return false;

            char fourthChar = utf16Chars[3];
            if (fourthChar != 's' && fourthChar != 'S')
                return false;

            char fifthChar = utf16Chars[4];
            if (fifthChar != 'e' && fifthChar != 'E')
                return false;

            return true;
        }
Exemplo n.º 4
0
 internal JsonObject(ReadOnlySpan<byte> values, ReadOnlySpan<byte> db, BufferPool pool = null, OwnedMemory<byte> dbMemory = null)
 {
     _db = db;
     _values = values;
     _pool = pool;
     _dbMemory = dbMemory;
 }
Exemplo n.º 5
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="source"></param>
        /// <param name="destination"></param>
        /// <returns>Number of bytes written to the destination.</returns>
        public static int Encode(ReadOnlySpan<byte> source, Span<byte> destination)
        {
            int di = 0;
            int si = 0;
            byte b0, b1, b2, b3;
            for (; si<source.Length - 2;) {
                var result = Encode(source.Slice(si));
                si += 3;
                destination.Slice(di).Write(result);
                di += 4;
            }

            if (si == source.Length - 1) {
                Encode(source[si], 0, 0, out b0, out b1, out b2, out b3);
                destination[di++] = b0;
                destination[di++] = b1;
                destination[di++] = s_encodingMap[64];
                destination[di++] = s_encodingMap[64];
            }
            else if(si == source.Length - 2) {
                Encode(source[si++], source[si], 0, out b0, out b1, out b2, out b3);
                destination[di++] = b0;
                destination[di++] = b1;
                destination[di++] = b2;
                destination[di++] = s_encodingMap[64];
            }

            return di; 
        }
Exemplo n.º 6
0
 public static void CtorArrayIntStartEqualsLength()
 {
     // Valid for start to equal the array length. This returns an empty span that starts "just past the array."
     int[] a = { 91, 92, 93 };
     ReadOnlySpan<int> span = new ReadOnlySpan<int>(a, 3);
     span.Validate<int>();
 }
Exemplo n.º 7
0
        // TODO: format should be ReadOnlySpan<T>
        public static Format.Parsed Parse(ReadOnlySpan<char> format)
        {
            if (format.Length == 0)
            {
                return default(Format.Parsed);
            }

            uint precision = NoPrecision;
            if (format.Length > 1)
            {
                var span = format.Slice(1, format.Length - 1);

                if (!PrimitiveParser.TryParseUInt32(span, out precision))
                {
                    throw new NotImplementedException("UnableToParsePrecision");
                }

                if (precision > Parsed.MaxPrecision)
                {
                    // TODO: this is a contract violation
                    throw new Exception("PrecisionValueOutOfRange");
                }
            }

            // TODO: this is duplicated from above. It needs to be refactored
            var specifier = format[0];
            return new Parsed(specifier, (byte)precision);
        }
Exemplo n.º 8
0
        // TODO: format should be ReadOnlySpan<char>
        internal static bool TryFormatInt64(long value, byte numberOfBytes, Span<byte> buffer, ReadOnlySpan<char> format, EncodingData formattingData, out int bytesWritten)
        {
            Precondition.Require(numberOfBytes <= sizeof(long));

            TextFormat parsedFormat = TextFormat.Parse(format);
            return TryFormatInt64(value, numberOfBytes, buffer, parsedFormat, formattingData, out bytesWritten);
        }
Exemplo n.º 9
0
 public static void SliceIntIntPastEnd()
 {
     int[] a = { 90, 91, 92, 93, 94, 95, 96, 97, 98, 99 };
     ReadOnlySpan<int> span = new ReadOnlySpan<int>(a).Slice(a.Length, 0);
     Assert.Equal(0, span.Length);
     Assert.True(Unsafe.AreSame<int>(ref a[a.Length - 1], ref Unsafe.Subtract<int>(ref span.DangerousGetPinnableReference(), 1)));
 }
Exemplo n.º 10
0
    public void TwoReadOnlySpansCreatedOverSameIntArrayAreEqual()
    {
        for (int i = 0; i < 2; i++)
        {
            var ints = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

            // Try out two ways of creating a slice:
            ReadOnlySpan<int> slice;
            if (i == 0)
            {
                slice = new ReadOnlySpan<int>(ints);
            }
            else
            {
                slice = ints.Slice();
            }
            Assert.Equal(ints.Length, slice.Length);
            // Now try out two ways of walking the slice's contents:
            for (int j = 0; j < ints.Length; j++)
            {
                Assert.Equal(ints[j], slice[j]);
            }
            {
                int j = 0;
                foreach (var x in slice)
                {
                    Assert.Equal(ints[j], x);
                    j++;
                }
            }
        }
    }
Exemplo n.º 11
0
 public static void SliceIntIntUpToEnd()
 {
     int[] a = { 90, 91, 92, 93, 94, 95, 96, 97, 98, 99 };
     ReadOnlySpan<int> span = new ReadOnlySpan<int>(a).Slice(4, 6);
     Assert.Equal(6, span.Length);
     Assert.True(Unsafe.AreSame<int>(ref a[4], ref span.DangerousGetPinnableReference()));
 }
Exemplo n.º 12
0
        public void CtorReadOnlySpanOverByteArrayValidCasesWithPropertiesAndBasicOperationsChecks(byte[] array)
        {
            ReadOnlySpan<byte> span = new ReadOnlySpan<byte>(array);
            Assert.Equal(array.Length, span.Length);

            Assert.NotSame(array, span.CreateArray());
            Assert.False(span.Equals(array));

            ReadOnlySpan<byte>.Enumerator it = span.GetEnumerator();
            for (int i = 0; i < span.Length; i++)
            {
                Assert.True(it.MoveNext());
                Assert.Equal(array[i], it.Current);
                Assert.Equal(array[i], span.Slice(i).Read<byte>());
                Assert.Equal(array[i], span.Slice(i).Read<MyByte>().Value);

                array[i] = unchecked((byte)(array[i] + 1));
                Assert.Equal(array[i], it.Current);
                Assert.Equal(array[i], span.Slice(i).Read<byte>());
                Assert.Equal(array[i], span.Slice(i).Read<MyByte>().Value);
            }
            Assert.False(it.MoveNext());

            it.Reset();
            for (int i = 0; i < span.Length; i++)
            {
                Assert.True(it.MoveNext());
                Assert.Equal(array[i], it.Current);
            }
            Assert.False(it.MoveNext());
        }
Exemplo n.º 13
0
        public unsafe void ByteReadOnlySpanEqualsTestsTwoDifferentInstancesOfBuffersWithOneValueDifferent()
        {
            const int bufferLength = 128;
            byte[] buffer1 = new byte[bufferLength];
            byte[] buffer2 = new byte[bufferLength];

            for (int i = 0; i < bufferLength; i++)
            {
                buffer1[i] = (byte)(bufferLength + 1 - i);
                buffer2[i] = (byte)(bufferLength + 1 - i);
            }

            fixed (byte* buffer1pinned = buffer1)
            fixed (byte* buffer2pinned = buffer2)
            {
                ReadOnlySpan<byte> b1 = new ReadOnlySpan<byte>(buffer1pinned, bufferLength);
                ReadOnlySpan<byte> b2 = new ReadOnlySpan<byte>(buffer2pinned, bufferLength);

                for (int i = 0; i < bufferLength; i++)
                {
                    for (int diffPosition = i; diffPosition < bufferLength; diffPosition++)
                    {
                        buffer1[diffPosition] = unchecked((byte)(buffer1[diffPosition] + 1));
                        Assert.False(b1.Slice(i).SequenceEqual(b2.Slice(i)));
                    }
                }
            }
        }
Exemplo n.º 14
0
 public unsafe static bool TryParseUInt16(char* text, int length, out ushort value, out int charactersConsumed)
 {
     var span = new ReadOnlySpan<byte>(text, length * sizeof(char));
     int bytesConsumed;
     bool result = PrimitiveParser.TryParseUInt16(span, out value, out bytesConsumed, EncodingData.InvariantUtf16, 'X');
     charactersConsumed = bytesConsumed / sizeof(char);
     return result;
 }
Exemplo n.º 15
0
 public static bool TryParseBoolean(ReadOnlySpan<char> text, out bool value, out int charactersConsumed)
 {
     var byteSpan = text.Cast<char, byte>();
     int bytesConsumed;
     bool result = PrimitiveParser.TryParseBoolean(byteSpan, out value, out bytesConsumed, EncodingData.InvariantUtf16);
     charactersConsumed = bytesConsumed / 2;
     return result;
 }
Exemplo n.º 16
0
 public static int GetByteCount(ReadOnlySpan buffer)
 {
     var count = 0;
     foreach (var x in buffer)
         if ((x & 0b1100_0000) != 0b1000_0000)
             count++;
     return count;
 }
Exemplo n.º 17
0
 public static void ToArray1()
 {
     int[] a = { 91, 92, 93 };
     ReadOnlySpan<int> span = new ReadOnlySpan<int>(a);
     int[] copy = span.ToArray();
     Assert.Equal<int>(a, copy);
     Assert.NotSame(a, copy);
 }
Exemplo n.º 18
0
 // Helper methods similar to System.ArrayExtension:
 // String helper methods, offering methods like String on Slice<char>:
 // TODO(joe): culture-sensitive comparisons.
 // TODO: should these move to string/text related assembly
 public static bool Contains(this ReadOnlySpan<char> str, ReadOnlySpan<char> value)
 {
     if (value.Length > str.Length)
     {
         return false;
     }
     return str.IndexOf(value) >= 0;
 }
Exemplo n.º 19
0
 public unsafe static bool TryParseBoolean(char* text, int length, out bool value, out int charactersConsumed)
 {
     var span = new ReadOnlySpan<byte>(text, length * 2);
     int bytesConsumed;
     bool result = PrimitiveParser.TryParseBoolean(span, out value, out bytesConsumed, EncodingData.InvariantUtf16);
     charactersConsumed = bytesConsumed / 2;
     return result;
 }
Exemplo n.º 20
0
 public static bool TryParseUInt16(ReadOnlySpan<char> text, out ushort value, out int charactersConsumed)
 {
     var byteSpan = text.Cast<char, byte>();
     int bytesConsumed;
     bool result = PrimitiveParser.TryParseUInt16(byteSpan, out value, out bytesConsumed, EncodingData.InvariantUtf16, 'X');
     charactersConsumed = bytesConsumed / sizeof(char);
     return result;
 }
Exemplo n.º 21
0
        public static void EqualityReflexivity()
        {
            int[] a = { 91, 92, 93, 94, 95 };
            ReadOnlySpan<int> left = new ReadOnlySpan<int>(a, 2, 3);

            Assert.True(left == left);
            Assert.True(!(left != left));
        }
Exemplo n.º 22
0
        public static void EqualityComparesRangeNotContent()
        {
            ReadOnlySpan<int> left = new ReadOnlySpan<int>(new int[] { 0, 1, 2 }, 1, 1);
            ReadOnlySpan<int> right = new ReadOnlySpan<int>(new int[] { 0, 1, 2 }, 1, 1);

            Assert.False(left == right);
            Assert.False(!(left != right));
        }
Exemplo n.º 23
0
        public static void EmptySpansNotUnified()
        {
            ReadOnlySpan<int> left = new ReadOnlySpan<int>(new int[0]);
            ReadOnlySpan<int> right = new ReadOnlySpan<int>(new int[0]);

            Assert.False(left == right);
            Assert.False(!(left != right));
        }
Exemplo n.º 24
0
 public static void CtorPointerNull()
 {
     unsafe
     {
         ReadOnlySpan<int> span = new ReadOnlySpan<int>((void*)null, 0);
         span.Validate<int>();
         Assert.True(Unsafe.AreSame<int>(ref Unsafe.AsRef<int>((void*)null), ref span.DangerousGetPinnableReference()));
     }
 }
Exemplo n.º 25
0
        public static void EqualityIncludesBase()
        {
            int[] a = { 91, 92, 93, 94, 95 };
            ReadOnlySpan<int> left = new ReadOnlySpan<int>(a, 1, 3);
            ReadOnlySpan<int> right = new ReadOnlySpan<int>(a, 2, 3);

            Assert.False(left == right);
            Assert.False(!(left != right));
        }
Exemplo n.º 26
0
        public static void AsBytesUIntToByte()
        {
            uint[] a = { 0x44332211, 0x88776655 };
            ReadOnlySpan<uint> span = new ReadOnlySpan<uint>(a);
            ReadOnlySpan<byte> asBytes = span.AsBytes<uint>();

            Assert.True(Unsafe.AreSame<byte>(ref Unsafe.As<uint, byte>(ref span.DangerousGetPinnableReference()), ref asBytes.DangerousGetPinnableReference()));
            asBytes.Validate<byte>(0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88);
        }
Exemplo n.º 27
0
        public static void PortableCastUIntToUShort()
        {
            uint[] a = { 0x44332211, 0x88776655 };
            ReadOnlySpan<uint> span = new ReadOnlySpan<uint>(a);
            ReadOnlySpan<ushort> asUShort = span.NonPortableCast<uint, ushort>();

            Assert.True(Unsafe.AreSame<ushort>(ref Unsafe.As<uint, ushort>(ref span.DangerousGetPinnableReference()), ref asUShort.DangerousGetPinnableReference()));
            asUShort.Validate<ushort>(0x2211, 0x4433, 0x6655, 0x8877);
        }
Exemplo n.º 28
0
        public static bool TryParse(ReadOnlySpan<byte> text, FormattingData.Encoding encoding, out uint value, out int bytesConsumed)
        {
            Precondition.Require(text.Length > 0);
            Precondition.Require(encoding == FormattingData.Encoding.Utf8 || text.Length > 1);

            value = 0;
            bytesConsumed = 0;

            if (text[0] == '0')
            {
                if (encoding == FormattingData.Encoding.Utf16)
                {
                    bytesConsumed = 2;
                    return text[1] == 0;
                }
                bytesConsumed = 1;
                return true;
            }

            for (int byteIndex = 0; byteIndex < text.Length; byteIndex++)
            {
                byte nextByte = text[byteIndex];
                if (nextByte < '0' || nextByte > '9')
                {
                    if (bytesConsumed == 0)
                    {
                        value = default(uint);
                        return false;
                    }
                    else {
                        return true;
                    }
                }
                uint candidate = value * 10;
                candidate += (uint)nextByte - '0';
                if (candidate > value)
                {
                    value = candidate;
                }
                else {
                    return true;
                }
                bytesConsumed++;
                if (encoding == FormattingData.Encoding.Utf16)
                {
                    byteIndex++;
                    if (byteIndex >= text.Length || text[byteIndex] != 0)
                    {
                        return false;
                    }
                    bytesConsumed++;
                }
            }

            return true;
        }
Exemplo n.º 29
0
        public static void TryCopyTo()
        {
            int[] src = { 1, 2, 3 };
            int[] dst = { 99, 100, 101 };

            ReadOnlySpan<int> srcSpan = new ReadOnlySpan<int>(src);
            bool success = srcSpan.TryCopyTo(dst);
            Assert.True(success);
            Assert.Equal<int>(src, dst);
        }
Exemplo n.º 30
0
        public static void CopyToShorter()
        {
            int[] src = { 1, 2, 3 };
            int[] dst = { 99, 100 };

            ReadOnlySpan<int> srcSpan = new ReadOnlySpan<int>(src);
            AssertThrows<ArgumentException, int>(srcSpan, (_srcSpan) => _srcSpan.CopyTo(dst));
            int[] expected = { 99, 100 };
            Assert.Equal<int>(expected, dst);  // CopyTo() checks for sufficient space before doing any copying.
        }
Exemplo n.º 31
0
 public new bool TryHashData(ReadOnlySpan <byte> source, Span <byte> destination, HashAlgorithmName hashAlgorithm, out int bytesWritten) =>
 base.TryHashData(source, destination, hashAlgorithm, out bytesWritten);
Exemplo n.º 32
0
        private bool TryPinAsTensor <T>(
            out MemoryHandle pinnedMemoryHandle,
            out IntPtr dataBufferPointer,
            out int dataBufferLength,
            out ReadOnlySpan <int> shape,
            out int rank,
            out TensorElementType nativeElementType
            )
        {
            nativeElementType = TensorElementType.DataTypeMax; //invalid
            dataBufferPointer = IntPtr.Zero;
            dataBufferLength  = 0;
            shape             = null;
            rank = 0;
            pinnedMemoryHandle = default;

            Debug.Assert(typeof(T) != typeof(string), "NamedOnnxValue.TryPinAsTensor() must not be called with a string Tensor value");

            if (_value is Tensor <T> )
            {
                Tensor <T> t = _value as Tensor <T>;
                if (t.IsReversedStride)
                {
                    //TODO: not sure how to support reverse stride. may be able to calculate the shape differently
                    throw new NotSupportedException(nameof(Tensor <T>) + " of reverseStride is not supported");
                }

                DenseTensor <T> dt = null;
                if (_value is DenseTensor <T> )
                {
                    dt = _value as DenseTensor <T>;
                }
                else
                {
                    dt = t.ToDenseTensor();
                }

                shape = dt.Dimensions;  // does not work for reverse stride
                rank  = dt.Rank;
                pinnedMemoryHandle = dt.Buffer.Pin();
                unsafe
                {
                    dataBufferPointer = (IntPtr)pinnedMemoryHandle.Pointer;
                }

                // find the native type
                if (typeof(T) == typeof(float))
                {
                    nativeElementType = TensorElementType.Float;
                    dataBufferLength  = dt.Buffer.Length * sizeof(float);
                }
                else if (typeof(T) == typeof(double))
                {
                    nativeElementType = TensorElementType.Double;
                    dataBufferLength  = dt.Buffer.Length * sizeof(double);
                }
                else if (typeof(T) == typeof(int))
                {
                    nativeElementType = TensorElementType.Int32;
                    dataBufferLength  = dt.Buffer.Length * sizeof(int);
                }
                else if (typeof(T) == typeof(uint))
                {
                    nativeElementType = TensorElementType.UInt32;
                    dataBufferLength  = dt.Buffer.Length * sizeof(uint);
                }
                else if (typeof(T) == typeof(long))
                {
                    nativeElementType = TensorElementType.Int64;
                    dataBufferLength  = dt.Buffer.Length * sizeof(long);
                }
                else if (typeof(T) == typeof(ulong))
                {
                    nativeElementType = TensorElementType.UInt64;
                    dataBufferLength  = dt.Buffer.Length * sizeof(ulong);
                }
                else if (typeof(T) == typeof(short))
                {
                    nativeElementType = TensorElementType.Int16;
                    dataBufferLength  = dt.Buffer.Length * sizeof(short);
                }
                else if (typeof(T) == typeof(ushort))
                {
                    nativeElementType = TensorElementType.UInt16;
                    dataBufferLength  = dt.Buffer.Length * sizeof(ushort);
                }
                else if (typeof(T) == typeof(byte))
                {
                    nativeElementType = TensorElementType.UInt8;
                    dataBufferLength  = dt.Buffer.Length * sizeof(byte);
                }
                else if (typeof(T) == typeof(sbyte))
                {
                    nativeElementType = TensorElementType.Int8;
                    dataBufferLength  = dt.Buffer.Length * sizeof(sbyte);
                }
                else if (typeof(T) == typeof(string))
                {
                    nativeElementType = TensorElementType.String;
                    dataBufferLength  = dt.Buffer.Length * IntPtr.Size;
                }
                else if (typeof(T) == typeof(bool))
                {
                    nativeElementType = TensorElementType.Bool;
                    dataBufferLength  = dt.Buffer.Length * sizeof(bool); // Assumes sizeof(BOOL) is always 1 byte in native
                }
                else
                {
                    //TODO: may extend the supported types
                    // do not throw exception, rather assign the sentinel value
                    nativeElementType = TensorElementType.DataTypeMax;
                }
                return(true);
            }

            return(false);
        }
Exemplo n.º 33
0
 public static bool IsPathRooted(ReadOnlySpan <char> path)
 {
     return(path.Length > 0 && path[0] == PathInternal.DirectorySeparatorChar);
 }
Exemplo n.º 34
0
 /// <inheritdoc />
 public ReadOnlySpan <byte> TransformBlock(ReadOnlySpan <byte> block)
 {
     return(block);
 }
Exemplo n.º 35
0
        /// <summary>
        /// Attempts to Pin the buffer, and create a native OnnxValue out of it. the pinned MemoryHandle is passed to output.
        /// In this case, the pinnedHandle should be kept alive till the native OnnxValue is used, then dispose it.
        /// If it is not possible to Pin the buffer, then creates OnnxValue from the copy of the data. The output pinnedMemoryHandle
        /// contains a default value in that case.
        /// Attempts to infer the type of the value while creating the OnnxValue
        /// </summary>
        /// <param name="onnxValue"></param>
        /// <param name="pinnedMemoryHandle"></param>
        unsafe internal void ToNativeOnnxValue(out IntPtr onnxValue, out MemoryHandle pinnedMemoryHandle)
        {
            //try to cast _value to Tensor<T>
            TensorElementType nativeElementType = TensorElementType.DataTypeMax; //invalid
            IntPtr            dataBufferPointer = IntPtr.Zero;
            int dataBufferLength     = 0;
            ReadOnlySpan <int> shape = null;
            int rank = 0;

            onnxValue = IntPtr.Zero;

            if (TryPinAsTensor <float>(out pinnedMemoryHandle,
                                       out dataBufferPointer,
                                       out dataBufferLength,
                                       out shape,
                                       out rank,
                                       out nativeElementType
                                       ))
            {
            }
            else if (TryPinAsTensor <double>(out pinnedMemoryHandle,
                                             out dataBufferPointer,
                                             out dataBufferLength,
                                             out shape,
                                             out rank,
                                             out nativeElementType
                                             ))
            {
            }
            else if (TryPinAsTensor <int>(out pinnedMemoryHandle,
                                          out dataBufferPointer,
                                          out dataBufferLength,
                                          out shape,
                                          out rank,
                                          out nativeElementType
                                          ))
            {
            }
            else if (TryPinAsTensor <uint>(out pinnedMemoryHandle,
                                           out dataBufferPointer,
                                           out dataBufferLength,
                                           out shape,
                                           out rank,
                                           out nativeElementType
                                           ))
            {
            }
            else if (TryPinAsTensor <long>(out pinnedMemoryHandle,
                                           out dataBufferPointer,
                                           out dataBufferLength,
                                           out shape,
                                           out rank,
                                           out nativeElementType
                                           ))
            {
            }
            else if (TryPinAsTensor <ulong>(out pinnedMemoryHandle,
                                            out dataBufferPointer,
                                            out dataBufferLength,
                                            out shape,
                                            out rank,
                                            out nativeElementType
                                            ))
            {
            }
            else if (TryPinAsTensor <short>(out pinnedMemoryHandle,
                                            out dataBufferPointer,
                                            out dataBufferLength,
                                            out shape,
                                            out rank,
                                            out nativeElementType
                                            ))
            {
            }
            else if (TryPinAsTensor <ushort>(out pinnedMemoryHandle,
                                             out dataBufferPointer,
                                             out dataBufferLength,
                                             out shape,
                                             out rank,
                                             out nativeElementType
                                             ))
            {
            }
            else if (TryPinAsTensor <byte>(out pinnedMemoryHandle,
                                           out dataBufferPointer,
                                           out dataBufferLength,
                                           out shape,
                                           out rank,
                                           out nativeElementType
                                           ))
            {
            }
            else if (TryPinAsTensor <sbyte>(out pinnedMemoryHandle,
                                            out dataBufferPointer,
                                            out dataBufferLength,
                                            out shape,
                                            out rank,
                                            out nativeElementType
                                            ))
            {
            }
            else if (TryPinAsTensor <bool>(out pinnedMemoryHandle,
                                           out dataBufferPointer,
                                           out dataBufferLength,
                                           out shape,
                                           out rank,
                                           out nativeElementType
                                           ))
            {
            }
            //TODO: add other types
            // special case for string Tensor, data needs to be copied to the native buffer
            else if (!(_value is Tensor <string>))
            {
                // nothing to cleanup here, since no memory has been pinned
                throw new NotSupportedException("The inference value " + nameof(_value) + " is not of a supported type");
            }


            if (_value is Tensor <string> )
            {
                // calculate native tensor length (sum of string lengths in utf-8)
                var tensorValue = _value as Tensor <string>;
                int totalLength = 0;
                for (int i = 0; i < tensorValue.Length; i++)
                {
                    totalLength += Encoding.UTF8.GetByteCount(tensorValue.GetValue(i));
                }

                long[] longShape = new long[tensorValue.Dimensions.Length];
                for (int i = 0; i < tensorValue.Dimensions.Length; i++)
                {
                    longShape[i] = tensorValue.Dimensions[i];
                }

                // allocate the native tensor
                IntPtr nativeTensor = IntPtr.Zero;
                try
                {
                    NativeApiStatus.VerifySuccess(NativeMethods.OrtCreateTensorAsOrtValue(
                                                      NativeMemoryAllocator.DefaultInstance.Handle,
                                                      longShape,
                                                      (UIntPtr)(longShape.Length),
                                                      TensorElementType.String,
                                                      out nativeTensor
                                                      ));

                    // fill the native tensor, using GetValue(index) from the Tensor<string>
                    var len             = tensorValue.Length;
                    var stringsInTensor = new IntPtr[len];
                    var pinnedHandles   = new GCHandle[len + 1];
                    pinnedHandles[len] = GCHandle.Alloc(stringsInTensor, GCHandleType.Pinned);
                    try
                    {
                        for (int i = 0; i < len; i++)
                        {
                            var utf8str = UTF8Encoding.UTF8.GetBytes(tensorValue.GetValue(i) + "\0");
                            pinnedHandles[i]   = GCHandle.Alloc(utf8str, GCHandleType.Pinned);
                            stringsInTensor[i] = pinnedHandles[i].AddrOfPinnedObject();
                        }

                        NativeApiStatus.VerifySuccess(NativeMethods.OrtFillStringTensor(nativeTensor, stringsInTensor, (UIntPtr)len));
                    }
                    finally
                    {
                        foreach (var handle in pinnedHandles)
                        {
                            if (handle.IsAllocated)
                            {
                                handle.Free();
                            }
                        }
                    }
                }
                catch (OnnxRuntimeException e)
                {
                    if (nativeTensor != IntPtr.Zero)
                    {
                        NativeMethods.OrtReleaseValue(nativeTensor);
                        throw e;
                    }
                }

                onnxValue          = nativeTensor; // set the output
                pinnedMemoryHandle = default;      // dummy value for the output
            }
            else
            {
                Debug.Assert(dataBufferPointer != IntPtr.Zero, "dataBufferPointer must be non-null after obtaining the pinned buffer");

                // copy to an ulong[] shape to match size_t[]
                var longShape = stackalloc long[rank];
                for (int i = 0; i < rank; i++)
                {
                    longShape[i] = shape[i];
                }

                IntPtr status = NativeMethods.OrtCreateTensorWithDataAsOrtValue(
                    NativeMemoryInfo.DefaultInstance.Handle,
                    dataBufferPointer,
                    (UIntPtr)(dataBufferLength),
                    longShape,
                    (UIntPtr)rank,
                    nativeElementType,
                    out onnxValue
                    );
                try
                {
                    NativeApiStatus.VerifySuccess(status);
                }
                catch (OnnxRuntimeException e)
                {
                    pinnedMemoryHandle.Dispose();
                    throw e;
                }
            }
        }
Exemplo n.º 36
0
        // 4-pattern, lower/upper and '-' or no
        public GuidBits(ReadOnlySpan <byte> utf8string)
        {
            this = default(GuidBits);

            // 32
            if (utf8string.Length == 32)
            {
                if (BitConverter.IsLittleEndian)
                {
                    this.Byte0 = Parse(utf8string, 6);
                    this.Byte1 = Parse(utf8string, 4);
                    this.Byte2 = Parse(utf8string, 2);
                    this.Byte3 = Parse(utf8string, 0);

                    this.Byte4 = Parse(utf8string, 10);
                    this.Byte5 = Parse(utf8string, 8);

                    this.Byte6 = Parse(utf8string, 14);
                    this.Byte7 = Parse(utf8string, 12);
                }
                else
                {
                    this.Byte0 = Parse(utf8string, 0);
                    this.Byte1 = Parse(utf8string, 2);
                    this.Byte2 = Parse(utf8string, 4);
                    this.Byte3 = Parse(utf8string, 6);

                    this.Byte4 = Parse(utf8string, 8);
                    this.Byte5 = Parse(utf8string, 10);

                    this.Byte6 = Parse(utf8string, 12);
                    this.Byte7 = Parse(utf8string, 14);
                }

                this.Byte8 = Parse(utf8string, 16);
                this.Byte9 = Parse(utf8string, 18);

                this.Byte10 = Parse(utf8string, 20);
                this.Byte11 = Parse(utf8string, 22);
                this.Byte12 = Parse(utf8string, 24);
                this.Byte13 = Parse(utf8string, 26);
                this.Byte14 = Parse(utf8string, 28);
                this.Byte15 = Parse(utf8string, 30);
                return;
            }
            else if (utf8string.Length == 36)
            {
                // '-' => 45
                if (BitConverter.IsLittleEndian)
                {
                    this.Byte0 = Parse(utf8string, 6);
                    this.Byte1 = Parse(utf8string, 4);
                    this.Byte2 = Parse(utf8string, 2);
                    this.Byte3 = Parse(utf8string, 0);

                    if (utf8string[8] != '-')
                    {
                        goto ERROR;
                    }

                    this.Byte4 = Parse(utf8string, 11);
                    this.Byte5 = Parse(utf8string, 9);

                    if (utf8string[13] != '-')
                    {
                        goto ERROR;
                    }

                    this.Byte6 = Parse(utf8string, 16);
                    this.Byte7 = Parse(utf8string, 14);
                }
                else
                {
                    this.Byte0 = Parse(utf8string, 0);
                    this.Byte1 = Parse(utf8string, 2);
                    this.Byte2 = Parse(utf8string, 4);
                    this.Byte3 = Parse(utf8string, 6);

                    if (utf8string[8] != '-')
                    {
                        goto ERROR;
                    }

                    this.Byte4 = Parse(utf8string, 9);
                    this.Byte5 = Parse(utf8string, 11);

                    if (utf8string[13] != '-')
                    {
                        goto ERROR;
                    }

                    this.Byte6 = Parse(utf8string, 14);
                    this.Byte7 = Parse(utf8string, 16);
                }

                if (utf8string[18] != '-')
                {
                    goto ERROR;
                }

                this.Byte8 = Parse(utf8string, 19);
                this.Byte9 = Parse(utf8string, 21);

                if (utf8string[23] != '-')
                {
                    goto ERROR;
                }

                this.Byte10 = Parse(utf8string, 24);
                this.Byte11 = Parse(utf8string, 26);
                this.Byte12 = Parse(utf8string, 28);
                this.Byte13 = Parse(utf8string, 30);
                this.Byte14 = Parse(utf8string, 32);
                this.Byte15 = Parse(utf8string, 34);
                return;
            }

ERROR:
            throw new MessagePackSerializationException("Invalid Guid Pattern.");
        }
 private byte[] ExportEncryptedPkcs8(ReadOnlySpan<char> pkcs8Password, int kdfCount)
 {
     return Key.ExportPkcs8KeyBlob(pkcs8Password, kdfCount);
 }
Exemplo n.º 38
0
 internal override void WriteSpan(AsnWriter writer, Asn1Tag tag, ReadOnlySpan <char> s) =>
 writer.WriteCharacterString(UniversalTagNumber.BMPString, s, tag);
        public bool MoveNext()
        {
            var status = Rune.DecodeFromUtf8(_utf8, out _current, out int consumed);

            _utf8 = _utf8[consumed..];
Exemplo n.º 40
0
 /// <summary>
 /// Concatenates 2 byte strings.
 /// </summary>
 /// <param name="dest"></param>
 /// <param name="source"></param>
 /// <returns>The length of the resulting string.</returns>
 /// <remarks>This function appends the source string to the end of the null-terminated destination string.
 /// If the destination buffer is not large enough to contain the resulting string,
 /// bytes from the source string will be appended to the destination string util the buffer is full.
 /// If the length of the final string is the same length of the destination buffer,
 /// no null terminating byte will be written to the end of the string.</remarks>
 public static int Concat(Span <byte> dest, ReadOnlySpan <byte> source)
 {
     return(Concat(dest, GetLength(dest), source));
 }
        internal static bool TryParseStatusFile(string statusFilePath, out ParsedStatus result, ReusableTextReader reusableReader)
        {
            if (!TryReadFile(statusFilePath, reusableReader, out string?fileContents))
            {
                // Between the time that we get an ID and the time that we try to read the associated stat
                // file(s), the process could be gone.
                result = default(ParsedStatus);
                return(false);
            }

            ParsedStatus        results            = default(ParsedStatus);
            ReadOnlySpan <char> statusFileContents = fileContents.AsSpan();
            int unitSliceLength = -1;

#if DEBUG
            int nonUnitSliceLength = -1;
#endif
            while (!statusFileContents.IsEmpty)
            {
                int startIndex = statusFileContents.IndexOf(':');
                if (startIndex == -1)
                {
                    // Reached end of file
                    break;
                }

                ReadOnlySpan <char> title = statusFileContents.Slice(0, startIndex);
                statusFileContents = statusFileContents.Slice(startIndex + 1);
                int endIndex = statusFileContents.IndexOf('\n');
                if (endIndex == -1)
                {
                    endIndex        = statusFileContents.Length - 1;
                    unitSliceLength = statusFileContents.Length - 3;
#if DEBUG
                    nonUnitSliceLength = statusFileContents.Length;
#endif
                }
                else
                {
                    unitSliceLength = endIndex - 3;
#if DEBUG
                    nonUnitSliceLength = endIndex;
#endif
                }

                ReadOnlySpan <char> value = default;
                bool valueParsed          = true;
#if DEBUG
                if (title.SequenceEqual("Pid".AsSpan()))
                {
                    value       = statusFileContents.Slice(0, nonUnitSliceLength);
                    valueParsed = int.TryParse(value, out results.Pid);
                }
#endif
                if (title.SequenceEqual("VmHWM".AsSpan()))
                {
                    value       = statusFileContents.Slice(0, unitSliceLength);
                    valueParsed = ulong.TryParse(value, out results.VmHWM);
                }
                else if (title.SequenceEqual("VmRSS".AsSpan()))
                {
                    value       = statusFileContents.Slice(0, unitSliceLength);
                    valueParsed = ulong.TryParse(value, out results.VmRSS);
                }
                else if (title.SequenceEqual("VmData".AsSpan()))
                {
                    value           = statusFileContents.Slice(0, unitSliceLength);
                    valueParsed     = ulong.TryParse(value, out ulong vmData);
                    results.VmData += vmData;
                }
                else if (title.SequenceEqual("VmSwap".AsSpan()))
                {
                    value       = statusFileContents.Slice(0, unitSliceLength);
                    valueParsed = ulong.TryParse(value, out results.VmSwap);
                }
                else if (title.SequenceEqual("VmSize".AsSpan()))
                {
                    value       = statusFileContents.Slice(0, unitSliceLength);
                    valueParsed = ulong.TryParse(value, out results.VmSize);
                }
                else if (title.SequenceEqual("VmPeak".AsSpan()))
                {
                    value       = statusFileContents.Slice(0, unitSliceLength);
                    valueParsed = ulong.TryParse(value, out results.VmPeak);
                }
                else if (title.SequenceEqual("VmStk".AsSpan()))
                {
                    value           = statusFileContents.Slice(0, unitSliceLength);
                    valueParsed     = ulong.TryParse(value, out ulong vmStack);
                    results.VmData += vmStack;
                }

                Debug.Assert(valueParsed);
                statusFileContents = statusFileContents.Slice(endIndex + 1);
            }

            results.VmData *= 1024;
            results.VmPeak *= 1024;
            results.VmSize *= 1024;
            results.VmSwap *= 1024;
            results.VmRSS  *= 1024;
            results.VmHWM  *= 1024;
            result          = results;
            return(true);
        }
Exemplo n.º 42
0
 public static string Utf8ToString(ReadOnlySpan <byte> value)
 {
     return(Encoding.UTF8.GetString(value));
 }
Exemplo n.º 43
0
 public static string Utf8ZToString(ReadOnlySpan <byte> value)
 {
     return(Utf8ToString(value.Slice(0, GetLength(value))));
 }
Exemplo n.º 44
0
        /// <summary>
        /// Verifies that the signature is valid for the message's content using the specified key.
        /// </summary>
        /// <param name="key">The private key used to sign the content.</param>
        /// <param name="detachedContent">The content that was previously signed.</param>
        /// <param name="associatedData">The extra data associated with the signature, which must match the value provided during signing.</param>
        /// <returns><see langword="true"/> if the signature is valid; otherwise, <see langword="false"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="key"/> is <see langword="null"/>.</exception>
        /// <exception cref="ArgumentException"><paramref name="key"/> is of an unsupported type.</exception>
        /// <exception cref="InvalidOperationException">The content is embedded on the associated message, use an overload that uses embedded content.</exception>
        /// <exception cref="CryptographicException">
        ///   <para>
        ///     <see cref="CoseMessage.ProtectedHeaders"/> does not have a value for the <see cref="CoseHeaderLabel.Algorithm"/> header.
        ///   </para>
        ///   <para>-or-</para>
        ///   <para>
        ///     The algorithm protected header was incorrectly formatted.
        ///   </para>
        ///   <para>-or-</para>
        ///   <para>
        ///     The algorithm protected header was not one of the values supported by this implementation.
        ///   </para>
        ///   <para>-or-</para>
        ///   <para>
        ///     The algorithm protected header doesn't match with the algorithms supported by the specified <paramref name="key"/>.
        ///   </para>
        /// </exception>
        /// <seealso cref="VerifyEmbedded(AsymmetricAlgorithm, ReadOnlySpan{byte})"/>
        /// <seealso cref="CoseMessage.Content"/>
        public bool VerifyDetached(AsymmetricAlgorithm key, ReadOnlySpan <byte> detachedContent, ReadOnlySpan <byte> associatedData = default)
        {
            if (key is null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            if (!Message.IsDetached)
            {
                throw new InvalidOperationException(SR.ContentWasEmbedded);
            }

            return(VerifyCore(key, detachedContent, null, associatedData, CoseHelpers.GetKeyType(key)));
        }
Exemplo n.º 45
0
        private void AnalysisAction(RequestData requestData)
        {
            var line                   = _line.AsSpan();
            int len                    = requestData.Data.Count;
            var receiveData            = requestData.GetSpan();
            ReadOnlySpan <byte> http   = line;
            ReadOnlySpan <byte> method = line;
            ReadOnlySpan <byte> url    = line;
            int offset2                = 0;
            int count                  = 0;

            for (int i = 0; i < len; i++)
            {
                if (receiveData[i] == line[0])
                {
                    http = receiveData.Slice(offset2, i - offset2);
                    break;
                }
                else
                {
                    if (receiveData[i] == _Space)
                    {
                        if (count != 0)
                        {
                            url     = receiveData.Slice(offset2, i - offset2);
                            offset2 = i + 1;
                        }
                        else
                        {
                            method  = receiveData.Slice(offset2, i - offset2);
                            offset2 = i + 1;
                            count++;
                        }
                    }
                }
            }
            int queryIndex = AnalysisUrl(url);
            ReadOnlySpan <byte> baseUrl     = default;
            ReadOnlySpan <byte> queryString = default;

            if (queryIndex > 0)
            {
                baseUrl                 = url.Slice(0, queryIndex);
                queryString             = url.Slice(queryIndex + 1, url.Length - queryIndex - 1);
                requestData.QueryString = Encoding.ASCII.GetString(queryString);
            }
            else
            {
                baseUrl = url;
            }
            if (baseUrl.Length == _path_Plaintext.Length && baseUrl.StartsWith(_path_Plaintext))
            {
                requestData.Action = ActionType.Plaintext;
            }
            else if (baseUrl.Length == _path_Json.Length && baseUrl.StartsWith(_path_Json))
            {
                requestData.Action = ActionType.Json;
            }
            else if (baseUrl.Length == _path_Db.Length && baseUrl.StartsWith(_path_Db))
            {
                requestData.Action = ActionType.Db;
            }
            else if (baseUrl.Length == _path_Queries.Length && baseUrl.StartsWith(_path_Queries))
            {
                requestData.Action = ActionType.Queries;
            }

            else if (baseUrl.Length == _cached_worlds.Length && baseUrl.StartsWith(_cached_worlds))
            {
                requestData.Action = ActionType.Caching;
            }

            else if (baseUrl.Length == _path_Updates.Length && baseUrl.StartsWith(_path_Updates))
            {
                requestData.Action = ActionType.Updates;
            }
            else if (baseUrl.Length == _path_Fortunes.Length && baseUrl.StartsWith(_path_Fortunes))
            {
                requestData.Action = ActionType.Fortunes;
            }
            else
            {
                requestData.Action = ActionType.Other;
            }
        }
 public Utf8RuneEnumerator(ReadOnlySpan <byte> utf8)
 {
     _utf8    = utf8;
     _current = default;
 }
Exemplo n.º 47
0
        static List <MiniYamlNode> FromLines(IEnumerable <ReadOnlyMemory <char> > lines, string filename, bool discardCommentsAndWhitespace, Dictionary <string, string> stringPool)
        {
            if (stringPool == null)
            {
                stringPool = new Dictionary <string, string>();
            }

            var levels = new List <List <MiniYamlNode> >();

            levels.Add(new List <MiniYamlNode>());

            var lineNo = 0;

            foreach (var ll in lines)
            {
                var line = ll.Span;
                ++lineNo;

                var keyStart  = 0;
                var level     = 0;
                var spaces    = 0;
                var textStart = false;

                ReadOnlySpan <char> key     = default;
                ReadOnlySpan <char> value   = default;
                ReadOnlySpan <char> comment = default;
                var location = new MiniYamlNode.SourceLocation {
                    Filename = filename, Line = lineNo
                };

                if (line.Length > 0)
                {
                    var currChar = line[keyStart];

                    while (!(currChar == '\n' || currChar == '\r') && keyStart < line.Length && !textStart)
                    {
                        currChar = line[keyStart];
                        switch (currChar)
                        {
                        case ' ':
                            spaces++;
                            if (spaces >= SpacesPerLevel)
                            {
                                spaces = 0;
                                level++;
                            }

                            keyStart++;
                            break;

                        case '\t':
                            level++;
                            keyStart++;
                            break;

                        default:
                            textStart = true;
                            break;
                        }
                    }

                    if (levels.Count <= level)
                    {
                        throw new YamlException($"Bad indent in miniyaml at {location}");
                    }

                    while (levels.Count > level + 1)
                    {
                        levels[levels.Count - 1].TrimExcess();
                        levels.RemoveAt(levels.Count - 1);
                    }

                    // Extract key, value, comment from line as `<key>: <value>#<comment>`
                    // The # character is allowed in the value if escaped (\#).
                    // Leading and trailing whitespace is always trimmed from keys.
                    // Leading and trailing whitespace is trimmed from values unless they
                    // are marked with leading or trailing backslashes
                    var keyLength    = line.Length - keyStart;
                    var valueStart   = -1;
                    var valueLength  = 0;
                    var commentStart = -1;
                    for (var i = 0; i < line.Length; i++)
                    {
                        if (valueStart < 0 && line[i] == ':')
                        {
                            valueStart  = i + 1;
                            keyLength   = i - keyStart;
                            valueLength = line.Length - i - 1;
                        }

                        if (commentStart < 0 && line[i] == '#' && (i == 0 || line[i - 1] != '\\'))
                        {
                            commentStart = i + 1;
                            if (commentStart <= keyLength)
                            {
                                keyLength = i - keyStart;
                            }
                            else
                            {
                                valueLength = i - valueStart;
                            }

                            break;
                        }
                    }

                    if (keyLength > 0)
                    {
                        key = line.Slice(keyStart, keyLength).Trim();
                    }

                    if (valueStart >= 0)
                    {
                        var trimmed = line.Slice(valueStart, valueLength).Trim();
                        if (trimmed.Length > 0)
                        {
                            value = trimmed;
                        }
                    }

                    if (commentStart >= 0 && !discardCommentsAndWhitespace)
                    {
                        comment = line.Slice(commentStart);
                    }

                    if (value.Length > 1)
                    {
                        // Remove leading/trailing whitespace guards
                        var trimLeading  = value[0] == '\\' && (value[1] == ' ' || value[1] == '\t') ? 1 : 0;
                        var trimTrailing = value[value.Length - 1] == '\\' && (value[value.Length - 2] == ' ' || value[value.Length - 2] == '\t') ? 1 : 0;
                        if (trimLeading + trimTrailing > 0)
                        {
                            value = value.Slice(trimLeading, value.Length - trimLeading - trimTrailing);
                        }

                        // Remove escape characters from #
                        if (value.Contains("\\#", StringComparison.Ordinal))
                        {
                            value = value.ToString().Replace("\\#", "#");
                        }
                    }
                }

                if (!key.IsEmpty || !discardCommentsAndWhitespace)
                {
                    var keyString   = key.IsEmpty ? null : key.ToString();
                    var valueString = value.IsEmpty ? null : value.ToString();

                    // Note: We need to support empty comments here to ensure that empty comments
                    // (i.e. a lone # at the end of a line) can be correctly re-serialized
                    var commentString = comment == default ? null : comment.ToString();

                    keyString     = keyString == null ? null : stringPool.GetOrAdd(keyString, keyString);
                    valueString   = valueString == null ? null : stringPool.GetOrAdd(valueString, valueString);
                    commentString = commentString == null ? null : stringPool.GetOrAdd(commentString, commentString);

                    var nodes = new List <MiniYamlNode>();
                    levels[level].Add(new MiniYamlNode(keyString, valueString, commentString, nodes, location));

                    levels.Add(nodes);
                }
            }

            foreach (var nodes in levels)
            {
                nodes.TrimExcess();
            }

            return(levels[0]);
        }
Exemplo n.º 48
0
        private static ProcessInfo[] GetProcessInfos(PerformanceCounterLib library, int processIndex, int threadIndex, ReadOnlySpan <byte> data)
        {
            Dictionary <int, ProcessInfo> processInfos = new Dictionary <int, ProcessInfo>();
            List <ThreadInfo>             threadInfos  = new List <ThreadInfo>();

            ref readonly PERF_DATA_BLOCK dataBlock = ref MemoryMarshal.AsRef <PERF_DATA_BLOCK>(data);
Exemplo n.º 49
0
 private static byte Parse(ReadOnlySpan <byte> bytes, int highOffset)
 {
     return(unchecked ((byte)((SwitchParse(bytes[highOffset]) * 16) + SwitchParse(bytes[highOffset + 1]))));
 }
Exemplo n.º 50
0
        // Fast parsing for single span in ReadOnlySequence
        private void ParseFormValuesFast(ReadOnlySpan<byte> span,
            ref KeyValueAccumulator accumulator,
            bool isFinalBlock,
            out int consumed)
        {
            ReadOnlySpan<byte> key;
            ReadOnlySpan<byte> value;
            consumed = 0;
            var equalsDelimiter = GetEqualsForEncoding();
            var andDelimiter = GetAndForEncoding();

            while (span.Length > 0)
            {
                // Find the end of the key=value pair.
                var ampersand = span.IndexOf(andDelimiter);
                ReadOnlySpan<byte> keyValuePair;
                int equals;
                var foundAmpersand = ampersand != -1;

                if (foundAmpersand)
                {
                    keyValuePair = span.Slice(0, ampersand);
                    span = span.Slice(keyValuePair.Length + andDelimiter.Length);
                    consumed += keyValuePair.Length + andDelimiter.Length;
                }
                else
                {
                    // We can't know that what is currently read is the end of the form value, that's only the case if this is the final block
                    // If we're not in the final block, then consume nothing
                    if (!isFinalBlock)
                    {
                        // Don't buffer indefinitely
                        if ((uint)span.Length > (uint)KeyLengthLimit + (uint)ValueLengthLimit)
                        {
                            ThrowKeyOrValueTooLargeException();
                        }
                        return;
                    }

                    keyValuePair = span;
                    span = default;
                    consumed += keyValuePair.Length;
                }

                equals = keyValuePair.IndexOf(equalsDelimiter);

                if (equals == -1)
                {
                    // Too long for the whole segment to be a key.
                    if (keyValuePair.Length > KeyLengthLimit)
                    {
                        ThrowKeyTooLargeException();
                    }

                    // There is no more data, this segment must be "key" with no equals or value.
                    key = keyValuePair;
                    value = default;
                }
                else
                {
                    key = keyValuePair.Slice(0, equals);
                    if (key.Length > KeyLengthLimit)
                    {
                        ThrowKeyTooLargeException();
                    }

                    value = keyValuePair.Slice(equals + equalsDelimiter.Length);
                    if (value.Length > ValueLengthLimit)
                    {
                        ThrowValueTooLargeException();
                    }
                }

                var decodedKey = GetDecodedString(key);
                var decodedValue = GetDecodedString(value);

                AppendAndVerify(ref accumulator, decodedKey, decodedValue);
            }
        }
Exemplo n.º 51
0
 /// <summary>
 /// Returns the <see cref="EllipticalCurve"/> corresponding to the <paramref name="crv"/>.
 /// </summary>
 /// <param name="crv"></param>
 /// <returns></returns>
 internal static EllipticalCurve FromSpan(ReadOnlySpan<byte> crv)
 {
     ref byte crvRef = ref MemoryMarshal.GetReference(crv);
Exemplo n.º 52
0
 internal ReadOnlySpanWhereRefEnumerable(ReadOnlySpan <TSource> source, Predicate <TSource> predicate)
 {
     this.source    = source;
     this.predicate = predicate;
 }
Exemplo n.º 53
0
        public override void WriteSurrogateCharEntity(char lowCh, char highCh)
        {
            ReadOnlySpan <char> entity = stackalloc char[] { highCh, lowCh };

            AddString(new string(entity));
        }
Exemplo n.º 54
0
 public static ReadOnlySpan <char> GetPathRoot(ReadOnlySpan <char> path)
 {
     return(IsPathRooted(path) ? PathInternal.DirectorySeparatorCharAsString.AsSpan() : ReadOnlySpan <char> .Empty);
 }
Exemplo n.º 55
0
 public static int NeedsEscaping(ReadOnlySpan <byte> value, JavaScriptEncoder?encoder)
 {
     return((encoder ?? JavaScriptEncoder.Default).FindFirstCharacterToEncodeUtf8(value));
 }
 private static int ParseQueries(ReadOnlySpan <byte> parameter)
 {
     if (!Utf8Parser.TryParse(parameter, out int queries, out _) || queries < 1)
     {
         queries = 1;
     }
Exemplo n.º 57
0
 public static ReadOnlySpanWhereAtRefEnumerable<TSource, TPredicate> WhereAtRef<TSource, TPredicate>(this ReadOnlySpan<TSource> source, TPredicate predicate = default)
     where TPredicate : struct, IFunctionIn<TSource, int, bool>
     => new(source, predicate);
Exemplo n.º 58
0
 /// <summary>
 /// Creates a new Tensor of a different type with the specified dimensions and the same layout as this tensor with elements initialized to their default value.
 /// </summary>
 /// <typeparam name="TResult">Type contained in the returned Tensor.</typeparam>
 /// <param name="dimensions">An span of integers that represent the size of each dimension of the DenseTensor to create.</param>
 /// <returns>A new tensor with the same layout as this tensor but different type and dimensions.</returns>
 public override Tensor <TResult> CloneEmpty <TResult>(ReadOnlySpan <int> dimensions)
 {
     return(new DenseTensor <TResult>(dimensions, IsReversedStride));
 }
Exemplo n.º 59
0
 private static ulong ApplyRound(ulong acc, ReadOnlySpan <byte> lane)
 {
     return(ApplyRound(acc, BinaryPrimitives.ReadUInt64LittleEndian(lane)));
 }
Exemplo n.º 60
0
        public void StringExtensionsTest()
        {
            bool   boolVal;
            string strVal;

            char[] charArray;
            ReadOnlyMemory <char> charRom;
            ReadOnlySpan <char>   charRos;

            boolVal   = StringExtensions.TryToBoolean(string.Empty, out boolVal);
            boolVal   = StringExtensions.ToBoolean("true");
            strVal    = StringExtensions.Abridge(string.Empty, 4);
            strVal    = StringExtensions.RemoveControlChars(string.Empty);
            strVal    = StringExtensions.RemoveControlChars(string.Empty, true);
            strVal    = StringExtensions.RemoveControlChars(string.Empty, ControlCharsReplaceMode.RemoveAll);
            strVal    = StringExtensions.RemoveControlChars(string.Empty, ControlCharsReplaceMode.RemoveAll, false);
            strVal    = StringExtensions.RemoveControlChars(string.Empty, ControlCharsReplaceMode.RemoveAll, false, true);
            strVal    = StringExtensions.RemoveControlChars(string.Empty, ControlCharsReplaceMode.RemoveAll, false, true, false);
            charArray = StringExtensions.RemoveControlChars(Array.Empty <char>(), ControlCharsReplaceMode.RemoveAll);
            charArray = StringExtensions.RemoveControlChars(Array.Empty <char>(), ControlCharsReplaceMode.RemoveAll, false);
            charArray = StringExtensions.RemoveControlChars(Array.Empty <char>(), ControlCharsReplaceMode.RemoveAll, false, true);
            charArray = StringExtensions.RemoveControlChars(Array.Empty <char>(), ControlCharsReplaceMode.RemoveAll, false, true, false);
            charRom   = StringExtensions.RemoveControlChars(new Memory <char>(), ControlCharsReplaceMode.RemoveAll);
            charRom   = StringExtensions.RemoveControlChars(new Memory <char>(), ControlCharsReplaceMode.RemoveAll, false);
            charRom   = StringExtensions.RemoveControlChars(new Memory <char>(), ControlCharsReplaceMode.RemoveAll, false, true);
            charRom   = StringExtensions.RemoveControlChars(new Memory <char>(), ControlCharsReplaceMode.RemoveAll, false, true, false);
            charRom   = StringExtensions.RemoveControlChars(new ReadOnlyMemory <char>(), ControlCharsReplaceMode.RemoveAll);
            charRom   = StringExtensions.RemoveControlChars(new ReadOnlyMemory <char>(), ControlCharsReplaceMode.RemoveAll, false);
            charRom   = StringExtensions.RemoveControlChars(new ReadOnlyMemory <char>(), ControlCharsReplaceMode.RemoveAll, false, true);
            charRom   = StringExtensions.RemoveControlChars(new ReadOnlyMemory <char>(), ControlCharsReplaceMode.RemoveAll, false, true, false);
            charRos   = StringExtensions.RemoveControlChars(new ReadOnlySpan <char>(), ControlCharsReplaceMode.RemoveAll);
            charRos   = StringExtensions.RemoveControlChars(new ReadOnlySpan <char>(), ControlCharsReplaceMode.RemoveAll, false);
            charRos   = StringExtensions.RemoveControlChars(new ReadOnlySpan <char>(), ControlCharsReplaceMode.RemoveAll, false, true);
            charRos   = StringExtensions.RemoveControlChars(new ReadOnlySpan <char>(), ControlCharsReplaceMode.RemoveAll, false, true, false);
            charRos   = StringExtensions.RemoveControlChars(new Span <char>(), ControlCharsReplaceMode.RemoveAll);
            charRos   = StringExtensions.RemoveControlChars(new Span <char>(), ControlCharsReplaceMode.RemoveAll, false);
            charRos   = StringExtensions.RemoveControlChars(new Span <char>(), ControlCharsReplaceMode.RemoveAll, false, true);
            charRos   = StringExtensions.RemoveControlChars(new Span <char>(), ControlCharsReplaceMode.RemoveAll, false, true, false);

            boolVal   = string.Empty.TryToBoolean(out boolVal);
            boolVal   = "true".ToBoolean();
            strVal    = string.Empty.Abridge(4);
            strVal    = string.Empty.RemoveControlChars();
            strVal    = string.Empty.RemoveControlChars(true);
            strVal    = string.Empty.RemoveControlChars(ControlCharsReplaceMode.RemoveAll);
            strVal    = string.Empty.RemoveControlChars(ControlCharsReplaceMode.RemoveAll, false);
            strVal    = string.Empty.RemoveControlChars(ControlCharsReplaceMode.RemoveAll, false, true);
            strVal    = string.Empty.RemoveControlChars(ControlCharsReplaceMode.RemoveAll, false, true, false);
            charArray = Array.Empty <char>().RemoveControlChars(ControlCharsReplaceMode.RemoveAll);
            charArray = Array.Empty <char>().RemoveControlChars(ControlCharsReplaceMode.RemoveAll, false);
            charArray = Array.Empty <char>().RemoveControlChars(ControlCharsReplaceMode.RemoveAll, false, true);
            charArray = Array.Empty <char>().RemoveControlChars(ControlCharsReplaceMode.RemoveAll, false, true, false);
            charRom   = new Memory <char>().RemoveControlChars(ControlCharsReplaceMode.RemoveAll);
            charRom   = new Memory <char>().RemoveControlChars(ControlCharsReplaceMode.RemoveAll, false);
            charRom   = new Memory <char>().RemoveControlChars(ControlCharsReplaceMode.RemoveAll, false, true);
            charRom   = new Memory <char>().RemoveControlChars(ControlCharsReplaceMode.RemoveAll, false, true, false);
            charRom   = new ReadOnlyMemory <char>().RemoveControlChars(ControlCharsReplaceMode.RemoveAll);
            charRom   = new ReadOnlyMemory <char>().RemoveControlChars(ControlCharsReplaceMode.RemoveAll, false);
            charRom   = new ReadOnlyMemory <char>().RemoveControlChars(ControlCharsReplaceMode.RemoveAll, false, true);
            charRom   = new ReadOnlyMemory <char>().RemoveControlChars(ControlCharsReplaceMode.RemoveAll, false, true, false);
            charRos   = new ReadOnlySpan <char>().RemoveControlChars(ControlCharsReplaceMode.RemoveAll);
            charRos   = new ReadOnlySpan <char>().RemoveControlChars(ControlCharsReplaceMode.RemoveAll, false);
            charRos   = new ReadOnlySpan <char>().RemoveControlChars(ControlCharsReplaceMode.RemoveAll, false, true);
            charRos   = new ReadOnlySpan <char>().RemoveControlChars(ControlCharsReplaceMode.RemoveAll, false, true, false);
            charRos   = new Span <char>().RemoveControlChars(ControlCharsReplaceMode.RemoveAll);
            charRos   = new Span <char>().RemoveControlChars(ControlCharsReplaceMode.RemoveAll, false);
            charRos   = new Span <char>().RemoveControlChars(ControlCharsReplaceMode.RemoveAll, false, true);
            charRos   = new Span <char>().RemoveControlChars(ControlCharsReplaceMode.RemoveAll, false, true, false);
        }