TODO: extract to common helper (copied from test-grammar-integration.cpp)
| 120 | |
| 121 | // TODO: extract to common helper (copied from test-grammar-integration.cpp) |
| 122 | static bool match_string(const std::string & input, llama_grammar * grammar) { |
| 123 | const auto cpts = unicode_cpts_from_utf8(input); |
| 124 | |
| 125 | auto & stacks_cur = llama_grammar_get_stacks(grammar); |
| 126 | |
| 127 | for (const auto & cpt : cpts) { |
| 128 | llama_grammar_accept(grammar, cpt); |
| 129 | |
| 130 | if (stacks_cur.empty()) { |
| 131 | // no stacks means that the grammar failed to match at this point |
| 132 | return false; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | if (std::any_of(stacks_cur.begin(), stacks_cur.end(), [](const auto & stack) { return stack.empty(); })) { |
| 137 | // An empty stack means that the grammar has been completed |
| 138 | return true; |
| 139 | } |
| 140 | |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | static std::string renormalize_json(const std::string & json_str) { |
| 145 | try { |
no test coverage detected