예제 #1
0
        public static bool TryReadBoolean(ref ReadOnlySpan <byte> remaining, out bool result, char standardFormat)
        {
            result = default;

            switch (GetTokenType(ref remaining))
            {
            case JsonTokenType.True:
                if (remaining.Length < 4)
                {
                    return(false);
                }
                result    = true;
                remaining = remaining.Slice(4);
                return(true);

            case JsonTokenType.False:
                if (remaining.Length < 5)
                {
                    return(false);
                }
                result    = false;
                remaining = remaining.Slice(5);
                return(true);

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

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

            switch (GetTokenType(ref remaining))
            {
            case EtfTokenType.SmallAtom:
            case EtfTokenType.SmallAtomUtf8:
            {
                if (remaining.Length < 2)
                {
                    return(false);
                }
                //remaining = remaining.Slice(1);
                byte length = remaining[1];
                //remaining = remaining.Slice(1);
                switch (length)
                {
                case 4:
                    remaining = remaining.Slice(6);
                    result    = true;
                    return(true);

                case 5:
                    remaining = remaining.Slice(7);
                    result    = false;
                    return(true);

                default:
                    return(false);
                }
            }

            case EtfTokenType.Atom:
            case EtfTokenType.AtomUtf8:
            {
                if (remaining.Length < 3)
                {
                    return(false);
                }
                remaining = remaining.Slice(1);
                ushort length = BinaryPrimitives.ReadUInt16BigEndian(remaining);
                //remaining = remaining.Slice(2);
                switch (length)
                {
                case 4:
                    remaining = remaining.Slice(6);
                    result    = true;
                    return(true);

                case 5:
                    remaining = remaining.Slice(7);
                    result    = false;
                    return(true);

                default:
                    return(false);
                }
            }

            default:
                return(false);
            }
        }