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