| 134 | } |
| 135 | |
| 136 | std::string gguf_kv_to_str(const struct gguf_context * ctx_gguf, int i) { |
| 137 | const enum gguf_type type = gguf_get_kv_type(ctx_gguf, i); |
| 138 | |
| 139 | switch (type) { |
| 140 | case GGUF_TYPE_STRING: |
| 141 | return gguf_get_val_str(ctx_gguf, i); |
| 142 | case GGUF_TYPE_ARRAY: |
| 143 | { |
| 144 | const enum gguf_type arr_type = gguf_get_arr_type(ctx_gguf, i); |
| 145 | int arr_n = gguf_get_arr_n(ctx_gguf, i); |
| 146 | const void * data = arr_type == GGUF_TYPE_STRING ? nullptr : gguf_get_arr_data(ctx_gguf, i); |
| 147 | std::stringstream ss; |
| 148 | ss << "["; |
| 149 | for (int j = 0; j < arr_n; j++) { |
| 150 | if (arr_type == GGUF_TYPE_STRING) { |
| 151 | std::string val = gguf_get_arr_str(ctx_gguf, i, j); |
| 152 | // escape quotes |
| 153 | replace_all(val, "\\", "\\\\"); |
| 154 | replace_all(val, "\"", "\\\""); |
| 155 | ss << '"' << val << '"'; |
| 156 | } else if (arr_type == GGUF_TYPE_ARRAY) { |
| 157 | ss << "???"; |
| 158 | } else { |
| 159 | ss << gguf_data_to_str(arr_type, data, j); |
| 160 | } |
| 161 | if (j < arr_n - 1) { |
| 162 | ss << ", "; |
| 163 | } |
| 164 | } |
| 165 | ss << "]"; |
| 166 | return ss.str(); |
| 167 | } |
| 168 | default: |
| 169 | return gguf_data_to_str(type, gguf_get_val_data(ctx_gguf, i), 0); |
| 170 | } |
| 171 | } |
no test coverage detected