private unsafe bool TryParseTokenUnsafe()

in Microsoft.Shared.Dna.Json/JsonParser.cs [1624:1701]


        private unsafe bool TryParseTokenUnsafe(StringSegment subsegment, out string value)
        {
            value = null;
            int endOfSegment = subsegment.Offset + subsegment.Count;
            int cursor = subsegment.Offset;
            this.decodeBuffer.Clear();
            fixed (char* payloadPointer = subsegment.String)
            {
                while (cursor < endOfSegment)
                {
                    char c = *(payloadPointer + cursor);
                    if (c == JsonConstants.StringEnclosure)
                    {
                        break;
                    }

                    if (c != JsonConstants.CharacterEscape)
                    {
                        if (this.decodeBuffer.TryAppend(c, 0))
                        {
                            cursor++;
                            continue;
                        }

                        return false;
                    }

                    if (++cursor >= endOfSegment)
                    {
                        return false;
                    }

                    c = *(payloadPointer + cursor);
                    int asIndex = c;
                    if (asIndex > JsonConstants.UnescapeSequencesLength)
                    {
                        return false;
                    }

                    if (asIndex == JsonConstants.UnescapeSequencesLength)
                    {
                        int start = cursor + 1;
                        int jump = start + 4;
                        if (jump > endOfSegment)
                        {
                            return false;
                        }

                        ushort code = 0;
                        if (JsonParser.TryParseHex(payloadPointer, start, jump, out code))
                        {
                            if (this.decodeBuffer.TryAppend((char)code, 0))
                            {
                                cursor = jump;
                                continue;
                            }

                            return false;
                        }

                        return false;
                    }

                    char u = JsonConstants.UnescapeSequences[asIndex];
                    if (this.decodeBuffer.TryAppend(u, 0))
                    {
                        cursor++;
                        continue;
                    }

                    return false;
                }

                value = this.decodeBuffer.ToString();
            }

            return true;
        }