MCPcopy Create free account
hub / github.com/antirez/llama.cpp-deepseek-v4-flash / json_brace_depth

Function json_brace_depth

common/chat-peg-parser.cpp:41–67  ·  view source on GitHub ↗

Count the number of unclosed '{' braces in a JSON-like string, properly skipping braces inside quoted strings.

Source from the content-addressed store, hash-verified

39// Count the number of unclosed '{' braces in a JSON-like string,
40// properly skipping braces inside quoted strings.
41static 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).
70static std::string escape_json_string_inner(const std::string & s) {

Callers 1

mapMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected