Count the number of unclosed '{' braces in a JSON-like string, properly skipping braces inside quoted strings.
| 39 | // Count the number of unclosed '{' braces in a JSON-like string, |
| 40 | // properly skipping braces inside quoted strings. |
| 41 | static int json_brace_depth(const std::string & s) { |
| 42 | int depth = 0; |
| 43 | bool in_string = false; |
| 44 | bool escaped = false; |
| 45 | for (char c : s) { |
| 46 | if (escaped) { |
| 47 | escaped = false; |
| 48 | continue; |
| 49 | } |
| 50 | if (c == '\\' && in_string) { |
| 51 | escaped = true; |
| 52 | continue; |
| 53 | } |
| 54 | if (c == '"') { |
| 55 | in_string = !in_string; |
| 56 | continue; |
| 57 | } |
| 58 | if (!in_string) { |
| 59 | if (c == '{') { |
| 60 | depth++; |
| 61 | } else if (c == '}') { |
| 62 | depth--; |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | return depth; |
| 67 | } |
| 68 | |
| 69 | // JSON-escape a string and return the inner content (without surrounding quotes). |
| 70 | static std::string escape_json_string_inner(const std::string & s) { |