Git fork

trace2: do not use strbuf_split*()

tr2_cfg_load_patterns() and tr2_load_env_vars() functions are
functions with very similar structure that each reads an environment
variable, splits its value at the ',' boundaries, and trims the
resulting string pieces into an array of strbufs.

But the code paths that later use these strbufs take no advantage of
the strbuf-ness of the result (they do not benefit from <ptr,len>
representation to avoid having to run strlen(<ptr>), for example).

Simplify the code by teaching these functions to split into a string
list instead; even the trimming comes for free ;-).

Signed-off-by: Junio C Hamano <gitster@pobox.com>

+27 -51
+27 -51
trace2/tr2_cfg.c
··· 8 8 #include "trace2/tr2_sysenv.h" 9 9 #include "wildmatch.h" 10 10 11 - static struct strbuf **tr2_cfg_patterns; 12 - static int tr2_cfg_count_patterns; 11 + static struct string_list tr2_cfg_patterns = STRING_LIST_INIT_DUP; 13 12 static int tr2_cfg_loaded; 14 13 15 - static struct strbuf **tr2_cfg_env_vars; 16 - static int tr2_cfg_env_vars_count; 14 + static struct string_list tr2_cfg_env_vars = STRING_LIST_INIT_DUP; 17 15 static int tr2_cfg_env_vars_loaded; 18 16 19 17 /* 20 18 * Parse a string containing a comma-delimited list of config keys 21 - * or wildcard patterns into a list of strbufs. 19 + * or wildcard patterns into a string list. 22 20 */ 23 - static int tr2_cfg_load_patterns(void) 21 + static size_t tr2_cfg_load_patterns(void) 24 22 { 25 - struct strbuf **s; 26 23 const char *envvar; 27 24 28 25 if (tr2_cfg_loaded) 29 - return tr2_cfg_count_patterns; 26 + return tr2_cfg_patterns.nr; 30 27 tr2_cfg_loaded = 1; 31 28 32 29 envvar = tr2_sysenv_get(TR2_SYSENV_CFG_PARAM); 33 30 if (!envvar || !*envvar) 34 - return tr2_cfg_count_patterns; 31 + return tr2_cfg_patterns.nr; 35 32 36 - tr2_cfg_patterns = strbuf_split_buf(envvar, strlen(envvar), ',', -1); 37 - for (s = tr2_cfg_patterns; *s; s++) { 38 - struct strbuf *buf = *s; 39 - 40 - if (buf->len && buf->buf[buf->len - 1] == ',') 41 - strbuf_setlen(buf, buf->len - 1); 42 - strbuf_trim(*s); 43 - } 44 - 45 - tr2_cfg_count_patterns = s - tr2_cfg_patterns; 46 - return tr2_cfg_count_patterns; 33 + string_list_split_f(&tr2_cfg_patterns, envvar, ",", -1, 34 + STRING_LIST_SPLIT_TRIM); 35 + return tr2_cfg_patterns.nr; 47 36 } 48 37 49 38 void tr2_cfg_free_patterns(void) 50 39 { 51 - if (tr2_cfg_patterns) 52 - strbuf_list_free(tr2_cfg_patterns); 53 - tr2_cfg_count_patterns = 0; 40 + if (tr2_cfg_patterns.nr) 41 + string_list_clear(&tr2_cfg_patterns, 0); 54 42 tr2_cfg_loaded = 0; 55 43 } 56 44 57 45 /* 58 46 * Parse a string containing a comma-delimited list of environment variable 59 - * names into a list of strbufs. 47 + * names into a string list. 60 48 */ 61 - static int tr2_load_env_vars(void) 49 + static size_t tr2_load_env_vars(void) 62 50 { 63 - struct strbuf **s; 64 51 const char *varlist; 65 52 66 53 if (tr2_cfg_env_vars_loaded) 67 - return tr2_cfg_env_vars_count; 54 + return tr2_cfg_env_vars.nr; 68 55 tr2_cfg_env_vars_loaded = 1; 69 56 70 57 varlist = tr2_sysenv_get(TR2_SYSENV_ENV_VARS); 71 58 if (!varlist || !*varlist) 72 - return tr2_cfg_env_vars_count; 73 - 74 - tr2_cfg_env_vars = strbuf_split_buf(varlist, strlen(varlist), ',', -1); 75 - for (s = tr2_cfg_env_vars; *s; s++) { 76 - struct strbuf *buf = *s; 77 - 78 - if (buf->len && buf->buf[buf->len - 1] == ',') 79 - strbuf_setlen(buf, buf->len - 1); 80 - strbuf_trim(*s); 81 - } 59 + return tr2_cfg_env_vars.nr; 82 60 83 - tr2_cfg_env_vars_count = s - tr2_cfg_env_vars; 84 - return tr2_cfg_env_vars_count; 61 + string_list_split_f(&tr2_cfg_env_vars, varlist, ",", -1, 62 + STRING_LIST_SPLIT_TRIM); 63 + return tr2_cfg_env_vars.nr; 85 64 } 86 65 87 66 void tr2_cfg_free_env_vars(void) 88 67 { 89 - if (tr2_cfg_env_vars) 90 - strbuf_list_free(tr2_cfg_env_vars); 91 - tr2_cfg_env_vars_count = 0; 68 + if (tr2_cfg_env_vars.nr) 69 + string_list_clear(&tr2_cfg_env_vars, 0); 92 70 tr2_cfg_env_vars_loaded = 0; 93 71 } 94 72 ··· 103 81 static int tr2_cfg_cb(const char *key, const char *value, 104 82 const struct config_context *ctx, void *d) 105 83 { 106 - struct strbuf **s; 84 + struct string_list_item *item; 107 85 struct tr2_cfg_data *data = (struct tr2_cfg_data *)d; 108 86 109 - for (s = tr2_cfg_patterns; *s; s++) { 110 - struct strbuf *buf = *s; 111 - int wm = wildmatch(buf->buf, key, WM_CASEFOLD); 87 + for_each_string_list_item(item, &tr2_cfg_patterns) { 88 + int wm = wildmatch(item->string, key, WM_CASEFOLD); 112 89 if (wm == WM_MATCH) { 113 90 trace2_def_param_fl(data->file, data->line, key, value, 114 91 ctx->kvi); ··· 130 107 void tr2_list_env_vars_fl(const char *file, int line) 131 108 { 132 109 struct key_value_info kvi = KVI_INIT; 133 - struct strbuf **s; 110 + struct string_list_item *item; 134 111 135 112 kvi_from_param(&kvi); 136 113 if (tr2_load_env_vars() <= 0) 137 114 return; 138 115 139 - for (s = tr2_cfg_env_vars; *s; s++) { 140 - struct strbuf *buf = *s; 141 - const char *val = getenv(buf->buf); 116 + for_each_string_list_item(item, &tr2_cfg_env_vars) { 117 + const char *val = getenv(item->string); 142 118 if (val && *val) 143 - trace2_def_param_fl(file, line, buf->buf, val, &kvi); 119 + trace2_def_param_fl(file, line, item->string, val, &kvi); 144 120 } 145 121 } 146 122