Ejemplo n.º 1
0
 private static unsafe bool TryParseTimeSpan(string str, out TimeSpan ts)
 {
     fixed(char *c = str)
     {
         return(LazyStringParser.TryParseTimeSpan(c, str.Length, out ts));
     }
 }
Ejemplo n.º 2
0
        internal static unsafe bool TryParseDate(string str, out DateTimeOffset dto)
        {
            fixed(char *c = str)
            {
                var result = LazyStringParser.TryParseDateTime(c, str.Length, out var dt, out dto, properlyParseThreeDigitsMilliseconds: true);

                switch (result)
                {
                case LazyStringParser.Result.DateTime:
                    dto = dt;
                    break;

                case LazyStringParser.Result.DateTimeOffset:
                    break;

                case LazyStringParser.Result.Failed:
                    return(false);

                default:
                    throw new ArgumentOutOfRangeException(nameof(LazyStringParser.Result), result, "Unknown result type");
                }

                return(true);
            }
        }
        private unsafe DateTime ReadDateTime(JsonParserState jsonParserState, IJsonParser reader, State state)
        {
            var str = CreateLazyStringValueFromParserState(jsonParserState);

            var result = LazyStringParser.TryParseDateTime(str.Buffer, str.Size, out DateTime dt, out DateTimeOffset _);
            if (result != LazyStringParser.Result.DateTime)
                ThrowInvalidLastModifiedProperty(state, str, reader);

            return dt;
        }
Ejemplo n.º 4
0
        public void TimeOnly(string date, int hh, int mm, int ss, int ms)
        {
            var bytes = date.AsSpan();

            fixed(char *buffer = bytes)
            {
                Assert.True(LazyStringParser.TryParseTimeOnly(buffer, bytes.Length, out var result));
                Assert.True(result.Equals(new TimeOnly(hh, mm, ss, ms)));
            }
        }
Ejemplo n.º 5
0
        public void DateOnly(string date, int yyyy, int mm, int dd)
        {
            var bytes = date.AsSpan();

            fixed(char *buffer = bytes)
            {
                Assert.True(LazyStringParser.TryParseDateOnly(buffer, bytes.Length, out var result));
                Assert.True(result.Equals(new DateOnly(yyyy, mm, dd)));
            }
        }
Ejemplo n.º 6
0
        public override unsafe object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if (reader.TokenType == JsonToken.Null)
            {
                return(null);
            }

            var result = new Parameters();

            do
            {
                reader.Read();
                if (reader.TokenType == JsonToken.EndObject)
                {
                    return(result);
                }
                if (reader.TokenType != JsonToken.PropertyName)
                {
                    throw new InvalidOperationException("Expected PropertyName, Got " + reader.TokenType);
                }

                var key = (string)reader.Value;
                reader.Read();// read the value

                var v = reader.Value;
                if (v is string s)
                {
                    fixed(char *pBuffer = s)
                    {
                        var r = LazyStringParser.TryParseDateTime(pBuffer, s.Length, out var dt, out var dto);

                        switch (r)
                        {
                        case LazyStringParser.Result.Failed:
                            break;

                        case LazyStringParser.Result.DateTime:
                            v = dt;
                            break;

                        case LazyStringParser.Result.DateTimeOffset:
                            v = dto;
                            break;

                        default:
                            throw new ArgumentOutOfRangeException();
                        }
                    }
                }

                result[key] = v;
            } while (true);
        }
Ejemplo n.º 7
0
        public void InvalidData(string dt)
        {
            var bytes = Encoding.UTF8.GetBytes(dt);

            fixed(byte *buffer = bytes)
            {
                DateTime       time;
                DateTimeOffset dto;

                Assert.Equal(LazyStringParser.Result.Failed,
                             LazyStringParser.TryParseDateTime(buffer, bytes.Length, out time, out dto));
            }
        }
Ejemplo n.º 8
0
        private unsafe DateTime ReadDateTime(JsonParserState jsonParserState, IJsonParser reader, State state)
        {
            var str = CreateLazyStringValueFromParserState(jsonParserState);

            var result = LazyStringParser.TryParseDateTime(str.Buffer, str.Size, out DateTime dt, out DateTimeOffset _, properlyParseThreeDigitsMilliseconds: true);

            if (result != LazyStringParser.Result.DateTime)
            {
                ThrowInvalidLastModifiedProperty(state, str, reader);
            }

            return(dt);
        }
Ejemplo n.º 9
0
        public void CanParseValidTimeSpans(string dt)
        {
            var expected = TimeSpan.ParseExact(dt, "c", CultureInfo.InvariantCulture);

            var bytes = Encoding.UTF8.GetBytes(dt);

            fixed(byte *buffer = bytes)
            {
                TimeSpan ts;

                Assert.True(LazyStringParser.TryParseTimeSpan(buffer, bytes.Length, out ts));
                Assert.Equal(expected, ts);
            }
        }
Ejemplo n.º 10
0
        public static unsafe DateTime ParseDate(string dateStr, string name)
        {
            fixed(char *c = dateStr)
            {
                var result = LazyStringParser.TryParseDateTime(c, dateStr.Length, out var dt, out _);

                if (result != LazyStringParser.Result.DateTime)
                {
                    ThrowInvalidDateTime(name, dateStr);
                }

                return(dt);
            }
        }
Ejemplo n.º 11
0
        public void CanParseValidDatesTimeOffset(string dt)
        {
            var expected = DateTimeOffset.ParseExact(dt, DefaultFormat.DateTimeOffsetFormatsToWrite, CultureInfo.InvariantCulture,
                                                     DateTimeStyles.RoundtripKind);

            var bytes = Encoding.UTF8.GetBytes(dt);

            fixed(byte *buffer = bytes)
            {
                DateTime       time;
                DateTimeOffset dto;

                Assert.Equal(LazyStringParser.Result.DateTimeOffset,
                             LazyStringParser.TryParseDateTime(buffer, bytes.Length, out time, out dto));
                Assert.Equal(expected, dto);
            }
        }
Ejemplo n.º 12
0
        public void WillNotParseAsTimeSpan(string dt)
        {
            TimeSpan expected;
            var      result = TimeSpan.TryParseExact(dt, "c", CultureInfo.InvariantCulture, out expected);

            Assert.False(result);

            var bytes = Encoding.UTF8.GetBytes(dt);

            fixed(byte *buffer = bytes)
            {
                TimeSpan ts;

                Assert.False(LazyStringParser.TryParseTimeSpan(buffer, bytes.Length, out ts));
                Assert.Equal(expected, ts);
            }
        }
Ejemplo n.º 13
0
        public void CanParseValidDates(string dt)
        {
            var expected = DateTime.ParseExact(dt, DefaultFormat.OnlyDateTimeFormat, CultureInfo.InvariantCulture,
                                               DateTimeStyles.RoundtripKind);

            var bytes = Encoding.UTF8.GetBytes(dt);

            fixed(byte *buffer = bytes)
            {
                DateTime       time;
                DateTimeOffset dto;

                Assert.Equal(LazyStringParser.Result.DateTime,
                             LazyStringParser.TryParseDateTime(buffer, bytes.Length, out time, out dto, properlyParseThreeDigitsMilliseconds: true));
                Assert.Equal(expected, time);
            }
        }
Ejemplo n.º 14
0
        public static unsafe string GetTermValue(string value, LuceneTermType type, bool exact)
        {
            switch (type)
            {
            case LuceneTermType.Double:
            case LuceneTermType.Long:
                return(value);

            default:
            {
                if (value == null)
                {
                    return(Constants.Documents.Indexing.Fields.NullValue);
                }

                if (value == string.Empty)
                {
                    return(Constants.Documents.Indexing.Fields.EmptyString);
                }

                if (exact)
                    return(value);

                fixed(char *pValue = value)
                {
                    var result = LazyStringParser.TryParseDateTime(pValue, value.Length, out DateTime _, out DateTimeOffset _);

                    switch (result)
                    {
                    case LazyStringParser.Result.DateTime:
                    case LazyStringParser.Result.DateTimeOffset:
                        return(value);

                    default:
                        return(value.ToLowerInvariant());
                    }
                }
            }
            }
        }
Ejemplo n.º 15
0
        public override unsafe object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if (reader.TokenType == JsonToken.Null)
            {
                return(null);
            }

            var result = new Parameters();

            do
            {
                reader.Read();
                if (reader.TokenType == JsonToken.EndObject)
                {
                    return(result);
                }
                if (reader.TokenType != JsonToken.PropertyName)
                {
                    throw new InvalidOperationException("Expected PropertyName, Got " + reader.TokenType);
                }

                var key = (string)reader.Value;
                reader.Read();// read the value

                var v = reader.Value;
                if (v is string s)
                {
                    fixed(char *pBuffer = s)
                    {
                        var r = LazyStringParser.TryParseTimeForQuery(pBuffer, s.Length, out var dt, out var dto,
#if FEATURE_DATEONLY_TIMEONLY_SUPPORT
                                                                      out var @do,
                                                                      out var to,
#endif
                                                                      properlyParseThreeDigitsMilliseconds: true);

                        switch (r)
                        {
                        case LazyStringParser.Result.Failed:
                            break;

                        case LazyStringParser.Result.DateTime:
                            v = dt;
                            break;

                        case LazyStringParser.Result.DateTimeOffset:
                            v = dto;
                            break;

#if FEATURE_DATEONLY_TIMEONLY_SUPPORT
                        case LazyStringParser.Result.DateOnly:
                            v = @do;
                            break;

                        case LazyStringParser.Result.TimeOnly:
                            v = to;
                            break;
#endif
                        default:
                            throw new ArgumentOutOfRangeException();
                        }
                    }
                }

                result[key] = v;
            } while (true);
        }