MCPcopy Create free account
hub / github.com/F-Stack/f-stack / csv_quote_flags

Function csv_quote_flags

tools/libxo/encoder/csv/enc_csv.c:270–297  ·  view source on GitHub ↗

* Determine how much quote processing is needed. The details of the * quoting rules are given at the top of this file. We return a set * of flags, indicating what's needed. */

Source from the content-addressed store, hash-verified

268 * of flags, indicating what's needed.
269 */
270static uint32_t
271csv_quote_flags (xo_handle_t *xop UNUSED, csv_private_t *csv UNUSED,
272 const char *value)
273{
274 static const char quoted[] = "\n\r\",";
275 static const char escaped[] = "\"";
276
277 if (csv->c_flags & CF_NO_QUOTES) /* User doesn't want quotes */
278 return 0;
279
280 size_t len = strlen(value);
281 uint32_t rc = 0;
282
283 if (strcspn(value, quoted) != len)
284 rc |= QF_NEEDS_QUOTES;
285 else if (isspace((int) value[0])) /* Leading whitespace */
286 rc |= QF_NEEDS_QUOTES;
287 else if (isspace((int) value[len - 1])) /* Trailing whitespace */
288 rc |= QF_NEEDS_QUOTES;
289
290 if (strcspn(value, escaped) != len)
291 rc |= QF_NEEDS_ESCAPE;
292
293 csv_dbg(xop, csv, "csv: quote flags [%s] -> %x (%zu/%zu)\n",
294 value, rc, len, strcspn(value, quoted));
295
296 return rc;
297}
298
299/*
300 * Escape the string, following the rules in RFC4180

Callers 1

csv_emit_recordFunction · 0.85

Calls 3

strcspnFunction · 0.85
isspaceFunction · 0.85
csv_dbgFunction · 0.85

Tested by

no test coverage detected