public static bool TryReadUInt64(ref ReadOnlySpan <byte> remaining, out ulong result, char standardFormat)
        {
            result = default;

            switch (GetTokenType(ref remaining))
            {
            case JsonTokenType.Number:
                if (!Utf8Reader.TryReadUInt64(ref remaining, out result, JsonSerializer.IntFormat.Symbol))
                {
                    return(false);
                }
                return(true);

            case JsonTokenType.String:
                remaining = remaining.Slice(1);
                if (!Utf8Reader.TryReadUInt64(ref remaining, out result, standardFormat))
                {
                    return(false);
                }
                if (remaining.Length == 0 || remaining[0] != '"')
                {
                    return(false);
                }
                remaining = remaining.Slice(1);
                return(true);
            }
            return(false);
        }
예제 #2
0
        public static bool TryReadUInt64(ref ReadOnlySpan <byte> remaining, out ulong result, char standardFormat)
        {
            result = default;

            if (standardFormat != '\0')
            {
                if (!TryReadUtf8Bytes(ref remaining, out var bytes))
                {
                    return(false);
                }
                return(Utf8Reader.TryReadUInt64(ref bytes, out result, standardFormat));
            }

            switch (GetTokenType(ref remaining))
            {
            case EtfTokenType.SmallInteger:
            {
                //remaining = remaining.Slice(1);
                result    = remaining[1];
                remaining = remaining.Slice(2);
                return(true);
            }

            case EtfTokenType.Integer:
            {
                remaining = remaining.Slice(1);
                int signedResult = BinaryPrimitives.ReadInt32BigEndian(remaining);
                if (signedResult < 0)
                {
                    return(false);
                }
                result    = (ulong)signedResult;
                remaining = remaining.Slice(4);
                return(true);
            }

            case EtfTokenType.SmallBig:
            {
                //remaining = remaining.Slice(1);
                byte bytes      = remaining[1];
                bool isPositive = remaining[2] == 0;
                remaining = remaining.Slice(3);
                return(TryReadUnsignedBigNumber(bytes, isPositive, ref remaining, out result));
            }

            case EtfTokenType.LargeBig:
            {
                remaining = remaining.Slice(1);
                if (!BinaryPrimitives.TryReadUInt32BigEndian(remaining, out uint bytes))
                {
                    return(false);
                }
                if (bytes > int.MaxValue)
                {
                    return(false);        // TODO: Spans dont allow uint accessors
                }
                bool isPositive = remaining[4] == 0;
                remaining = remaining.Slice(5);
                return(TryReadUnsignedBigNumber((int)bytes, isPositive, ref remaining, out result));
            }

            default:
                return(false);
            }
        }