MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / cs_clean_value

Function cs_clean_value

internal/cbm/doclink_cs.c:214–285  ·  view source on GitHub ↗

Decode the five XML entities, drop the comment prefix (`///`, ` * `) that * follows a line break inside a value, collapse whitespace, trim. */

Source from the content-addressed store, hash-verified

212/* Decode the five XML entities, drop the comment prefix (`///`, ` * `) that
213 * follows a line break inside a value, collapse whitespace, trim. */
214static const char *cs_clean_value(CBMArena *a, const char *v, size_t n) {
215 char *out;
216#if defined(CBM_ENABLE_TEST_SEAMS) && CBM_ENABLE_TEST_SEAMS
217 if (cbm_doclink_test_fail_alloc(CBM_DOCLINK_ALLOC_VALUE)) {
218 out = NULL;
219 } else
220#endif
221 {
222 out = (char *)cbm_arena_alloc(a, n + SKIP_ONE);
223 }
224#if defined(CBM_ENABLE_TEST_SEAMS) && CBM_ENABLE_TEST_SEAMS
225 if (out) {
226 cbm_doclink_test_note_doc_work(0, 0, n + SKIP_ONE);
227 }
228#endif
229 if (!out) {
230 return NULL;
231 }
232 static const struct {
233 const char *ent;
234 char ch;
235 } ents[] = {{"&lt;", '<'}, {"&gt;", '>'}, {"&amp;", '&'}, {"&quot;", '"'}, {"&apos;", '\''}};
236 size_t w = 0;
237 bool space = false;
238 size_t i = 0;
239 while (i < n) {
240 char c = v[i];
241 if (c == '\n' || c == '\r') {
242 i++;
243 while (i < n && (v[i] == ' ' || v[i] == '\t' || v[i] == '\r' || v[i] == '\n')) {
244 i++;
245 }
246 static const char line_doc[] = "///";
247 size_t ld = sizeof(line_doc) - SKIP_ONE;
248 if (i + ld <= n && memcmp(v + i, line_doc, ld) == 0) {
249 i += ld; /* the next `///` line of the same comment */
250 } else if (i < n && v[i] == '*' && !(i + SKIP_ONE < n && v[i + SKIP_ONE] == '/')) {
251 i++; /* a block comment's leading star */
252 }
253 space = true;
254 continue;
255 }
256 if (c == ' ' || c == '\t') {
257 space = true;
258 i++;
259 continue;
260 }
261 if (space && w > 0) {
262 out[w++] = ' ';
263 }
264 space = false;
265 if (c == '&') {
266 bool decoded = false;
267 for (size_t e = 0; e < sizeof(ents) / sizeof(ents[0]); e++) {
268 size_t el = strlen(ents[e].ent);
269 if (i + el <= n && memcmp(v + i, ents[e].ent, el) == 0) {
270 out[w++] = ents[e].ch;
271 i += el;

Callers 1

Calls 3

cbm_arena_allocFunction · 0.70

Tested by

no test coverage detected