* 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. */
| 268 | * of flags, indicating what's needed. |
| 269 | */ |
| 270 | static uint32_t |
| 271 | csv_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 |
no test coverage detected