quote: turn 'nodq' parameter into a set of flags
quote_c_style() and its friend quote_two_c_style() both take an optional "please omit the double quotes around the quoted body" parameter. Turn it into a flag word, assign one bit out of it, and call it CQUOTE_NODQ bit. No behaviour change intended. Signed-off-by: Junio C Hamano <gitster@pobox.com>
Junio C Hamano committed
Sep 10, 2020 at 10:01 UTC
7c37c9750a0b402a68c73d0e4644f538a026d121
3 files changed
+20
-13
diff.c
+4
-4
@@ -482,14 +482,14 @@ int git_diff_basic_config(const char *var, const char *value, void *cb)
482
483
static char *quote_two(const char *one, const char *two)
484
{
485
- int need_one = quote_c_style(one, NULL, NULL, 1);
486
- int need_two = quote_c_style(two, NULL, NULL, 1);
485
+ int need_one = quote_c_style(one, NULL, NULL, CQUOTE_NODQ);
486
+ int need_two = quote_c_style(two, NULL, NULL, CQUOTE_NODQ);
487
struct strbuf res = STRBUF_INIT;
488
489
if (need_one + need_two) {
490
strbuf_addch(&res, '"');
491
- quote_c_style(one, &res, NULL, 1);
492
- quote_c_style(two, &res, NULL, 1);
491
+ quote_c_style(one, &res, NULL, CQUOTE_NODQ);
492
+ quote_c_style(two, &res, NULL, CQUOTE_NODQ);
493
strbuf_addch(&res, '"');
494
} else {
495
strbuf_addstr(&res, one);
quote.c
+11
-7
@@ -256,7 +256,7 @@ static size_t next_quote_pos(const char *s, ssize_t maxlen)
256
* Return value is the same as in (1).
257
*/
258
static size_t quote_c_style_counted(const char *name, ssize_t maxlen,
259
- struct strbuf *sb, FILE *fp, int no_dq)
259
+ struct strbuf *sb, FILE *fp, unsigned flags)
260
{
261
#undef EMIT
262
#define EMIT(c) \
@@ -272,6 +272,7 @@ static size_t quote_c_style_counted(const char *name, ssize_t maxlen,
272
count += (l); \
273
} while (0)
274
275
+ int no_dq = !!(flags & CQUOTE_NODQ);
276
size_t len, count = 0;
277
const char *p = name;
278
@@ -309,19 +310,21 @@ static size_t quote_c_style_counted(const char *name, ssize_t maxlen,
310
return count;
311
}
312
312
-size_t quote_c_style(const char *name, struct strbuf *sb, FILE *fp, int nodq)
313
+size_t quote_c_style(const char *name, struct strbuf *sb, FILE *fp, unsigned flags)
314
{
314
- return quote_c_style_counted(name, -1, sb, fp, nodq);
315
+ return quote_c_style_counted(name, -1, sb, fp, flags);
316
}
317
317
-void quote_two_c_style(struct strbuf *sb, const char *prefix, const char *path, int nodq)
318
+void quote_two_c_style(struct strbuf *sb, const char *prefix, const char *path,
319
+ unsigned flags)
320
{
321
+ int nodq = !!(flags & CQUOTE_NODQ);
322
if (quote_c_style(prefix, NULL, NULL, 0) ||
323
quote_c_style(path, NULL, NULL, 0)) {
324
if (!nodq)
325
strbuf_addch(sb, '"');
323
- quote_c_style(prefix, sb, NULL, 1);
324
- quote_c_style(path, sb, NULL, 1);
326
+ quote_c_style(prefix, sb, NULL, CQUOTE_NODQ);
327
+ quote_c_style(path, sb, NULL, CQUOTE_NODQ);
328
if (!nodq)
329
strbuf_addch(sb, '"');
330
} else {
@@ -367,7 +370,8 @@ char *quote_path(const char *in, const char *prefix, struct strbuf *out, unsigne
370
*/
371
if (force_dq)
372
strbuf_addch(out, '"');
370
- quote_c_style_counted(rel, strlen(rel), out, NULL, force_dq);
373
+ quote_c_style_counted(rel, strlen(rel), out, NULL,
374
+ force_dq ? CQUOTE_NODQ : 0);
375
if (force_dq)
376
strbuf_addch(out, '"');
377
strbuf_release(&sb);
quote.h
+5
-2
@@ -64,8 +64,11 @@ struct strvec;
64
int sq_dequote_to_strvec(char *arg, struct strvec *);
65
66
int unquote_c_style(struct strbuf *, const char *quoted, const char **endp);
67
-size_t quote_c_style(const char *name, struct strbuf *, FILE *, int no_dq);
68
-void quote_two_c_style(struct strbuf *, const char *, const char *, int);
67
+
68
+/* Bits in the flags parameter to quote_c_style() */
69
+#define CQUOTE_NODQ 01
70
+size_t quote_c_style(const char *name, struct strbuf *, FILE *, unsigned);
71
+void quote_two_c_style(struct strbuf *, const char *, const char *, unsigned);
72
73
void write_name_quoted(const char *name, FILE *, int terminator);
74
void write_name_quoted_relative(const char *name, const char *prefix,