Пример #1
0
    private static string ParseString(char[] json, ref int index, ref bool success)
    {
        StringBuilder stringBuilder = GetStringBuilder(); //new StringBuilder(2000);

        SimpleJsonTool.EatWhitespace(json, ref index);
        char   c    = json[index++];
        bool   flag = false;
        string result;

        while (!flag)
        {
            if (index == json.Length)
            {
                break;
            }
            c = json[index++];
            if (c == '"')
            {
                flag = true;
                break;
            }
            if (c == '\\')
            {
                if (index == json.Length)
                {
                    break;
                }
                c = json[index++];
                if (c == '"')
                {
                    stringBuilder.Append('"');
                }
                else if (c == '\\')
                {
                    stringBuilder.Append('\\');
                }
                else if (c == '/')
                {
                    stringBuilder.Append('/');
                }
                else if (c == 'b')
                {
                    stringBuilder.Append('\b');
                }
                else if (c == 'f')
                {
                    stringBuilder.Append('\f');
                }
                else if (c == 'n')
                {
                    stringBuilder.Append('\n');
                }
                else if (c == 'r')
                {
                    stringBuilder.Append('\r');
                }
                else if (c == 't')
                {
                    stringBuilder.Append('\t');
                }
                else if (c == 'u')
                {
                    int num = json.Length - index;
                    if (num >= 4)
                    {
                        uint num2;
                        if (!(success = uint.TryParse(new string(json, index, 4), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out num2)))
                        {
                            result = "";
                        }
                        else
                        {
                            if (55296u > num2 || num2 > 56319u)
                            {
                                stringBuilder.Append(SimpleJsonTool.ConvertFromUtf32((int)num2));
                                index += 4;
                                continue;
                            }
                            index += 4;
                            num    = json.Length - index;
                            if (num >= 6)
                            {
                                uint num3;
                                if (new string(json, index, 2) == "\\u" && uint.TryParse(new string(json, index + 2, 4), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out num3))
                                {
                                    if (56320u <= num3 && num3 <= 57343u)
                                    {
                                        stringBuilder.Append((char)num2);
                                        stringBuilder.Append((char)num3);
                                        index += 6;
                                        continue;
                                    }
                                }
                            }
                            success = false;
                            result  = "";
                        }
                        return(result);
                    }
                    break;
                }
            }
            else
            {
                stringBuilder.Append(c);
            }
        }
        if (!flag)
        {
            success = false;
            result  = null;
            return(result);
        }
        result = stringBuilder.ToString();

        RecycleStringBuilder(stringBuilder);

        return(result);
    }