Exemple #1
0
        private unsafe bool TryParseDateTime(ReadOnlySpan <byte> input, out DateTime value)
        {
#if NET5_0_OR_GREATER
            Unsafe.SkipInit(out value);
#else
            value = default;
#endif
            bool success = true;
            if (input.Length == 19 | input.Length == 20)
            {
                return(Utf8_19ToDate(in AsciiInterface.StringTo <byte, Vector256 <sbyte> >(input), out value));
            }
            if (input.Length == 10 | input.Length == 11)
            {
                var v128 = AsciiInterface.StringTo <byte, Vector128 <sbyte> >(input);
                if (IsNumber(v128, input.Length))
                {
                    //纯数字时表示时间戳
                    System.Buffers.Text.Utf8Parser.TryParse(input, out long lv, out _);
                    value = new DateTime(1970, 1, 1).AddTicks(TimeSpan.TicksPerSecond * lv);
                    return(true);
                }
#if NET6_0_OR_GREATER
                var r = DateOnlyF.Utf8_10ToDate(v128, out var v0);
                value = v0.ToDateTime(default);
Exemple #2
0
        public unsafe bool TryParse(string str, out Guid value)
        {
            var vector = AsciiInterface.UnicodeToAscii_32(ref AsciiInterface.StringTo <char, Vector256 <short> >(str)).AsInt16();
            var r      = TryParseGuid(in vector, out value);

            return(r);
        }
Exemple #3
0
        public unsafe string GuidToString(ref Guid value)
        {
            var str    = AsciiInterface.FastAllocateString(32);
            var vector = GuidToUtf8_32(in Unsafe.As <Guid, Vector128 <byte> >(ref value)).AsByte();

            AsciiInterface.AsciiToUnicode(vector, ref AsciiInterface.StringTo <char, Vector256 <short> >(str));
            return(str);
        }
        public unsafe string LongToString(long value)
        {
            var str    = AsciiInterface.FastAllocateString(16);
            var vector = Avx2.ConvertToVector256Int16(LongToUtf8_16(value));

            Unsafe.As <char, Vector256 <short> >(ref Unsafe.AsRef(in str.GetPinnableReference())) = vector;
            return(str);
        }
Exemple #5
0
        public unsafe string DateTimeToString(DateTime value)
        {
            var str = AsciiInterface.FastAllocateString(19);
            var r   = DateTimeToUtf8_19(value);

            Set38(Avx2.ConvertToVector256Int16(Avx2.ExtractVector128(r, 0)), Avx2.ConvertToVector256Int16(Avx2.ExtractVector128(r, 1)), ref Unsafe.AsRef(in str.GetPinnableReference()));
            return(str);
        }
        public unsafe string IntToString(int value)
        {
            var str    = AsciiInterface.FastAllocateString(8);
            var vector = IntToUtf8_8(value);

            Unsafe.As <char, Vector128 <short> >(ref Unsafe.AsRef(in str.GetPinnableReference())) = Sse41.ConvertToVector128Int16((byte *)&vector);
            return(str);
        }
        public unsafe string BytesToString(byte[] value, bool prefix = false)
        {
#if NET6_0_OR_GREATER
            ArgumentNullException.ThrowIfNull(value);
#else
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }
#endif
            var      str     = AsciiInterface.FastAllocateString(value.Length * 2 + (prefix ? 2 : 0));
            ref char strSite = ref Unsafe.AsRef(in str.GetPinnableReference());
Exemple #8
0
        public override unsafe Guid Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            var str = reader.ValueSpan;

            if (str.Length == 32)
            {
                if (TryParseGuid(in AsciiInterface.StringTo <byte, Vector256 <short> >(str), out var v))
                {
                    return(v);
                }
            }
            else
            {
                return(reader.GetGuid());
            }
            throw new InvalidCastException();
        }
        public override unsafe long Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            if (reader.TokenType == JsonTokenType.Number)
            {
                return(reader.GetInt64());
            }
            var str = reader.ValueSpan;

            if (str.Length != 16)
            {
                throw new InvalidCastException();
            }
            if (TryParseLong(in AsciiInterface.StringTo <byte, Vector128 <short> >(str), out var v))
            {
                return(v);
            }
            throw new InvalidCastException();
        }
Exemple #10
0
        public unsafe bool TryParse(ReadOnlySpan <char> s, out DateTime result)
        {
            var vector = AsciiInterface.UnicodeToAscii_32(ref AsciiInterface.StringTo <char, Vector256 <short> >(s));

            return(TryParseDateTime(new ReadOnlySpan <byte>(&vector, s.Length), out result));
        }