示例#1
0
        private static bool parseString(JSONParser.Context ctx)
        {
            if (ctx.pos >= ctx.src.Length || (int)ctx.src[ctx.pos] != 34)
            {
                throw new JSONParser.InvalidStringException(ctx);
            }
            ++ctx.pos;
            ctx.sb.Length = 0;
            for (; ctx.pos < ctx.src.Length; ++ctx.pos)
            {
                if (char.IsControl(ctx.src[ctx.pos]))
                {
                    throw new JSONParser.InvalidCharacterException(ctx);
                }
                if ((int)ctx.src[ctx.pos] == 92)
                {
                    ++ctx.pos;
                    if (ctx.pos >= ctx.src.Length)
                    {
                        throw new JSONParser.InvalidStringException(ctx);
                    }
                    char ch = ctx.src[ctx.pos];
                    switch (ch)
                    {
                    case 'n':
                        ctx.sb.Append('\n');
                        continue;

                    case 'r':
                        ctx.sb.Append('\r');
                        continue;

                    case 't':
                        ctx.sb.Append('\t');
                        continue;

                    case 'u':
                        ++ctx.pos;
                        uint num = (uint)((int)JSONParser.parseHex(ctx) << 12 | (int)JSONParser.parseHex(ctx) << 8 | (int)JSONParser.parseHex(ctx) << 4) | JSONParser.parseHex(ctx);
                        ctx.sb.Append((char)num);
                        --ctx.pos;
                        continue;

                    default:
                        switch (ch)
                        {
                        case '"':
                            ctx.sb.Append('"');
                            continue;

                        case '/':
                            ctx.sb.Append('/');
                            continue;

                        case '\\':
                            ctx.sb.Append('\\');
                            continue;

                        case 'b':
                            ctx.sb.Append('\b');
                            continue;

                        case 'f':
                            ctx.sb.Append('\f');
                            continue;

                        default:
                            throw new JSONParser.InvalidStringException(ctx);
                        }
                    }
                }
                else
                {
                    if ((int)ctx.src[ctx.pos] == 34)
                    {
                        ++ctx.pos;
                        break;
                    }
                    ctx.sb.Append(ctx.src[ctx.pos]);
                }
            }
            return(true);
        }