Rename "struct str_vector" to "struct strv"

Short names for things used often.
This commit is contained in:
Přemysl Eric Janouch 2017-01-23 23:01:20 +01:00
parent 680980632d
commit 5552ce1dbe
Signed by: p
GPG Key ID: B715679E3A361BE6
4 changed files with 80 additions and 80 deletions

View File

@ -29,7 +29,7 @@ struct irc_message
struct str_map tags; ///< IRC 3.2 message tags struct str_map tags; ///< IRC 3.2 message tags
char *prefix; ///< Message prefix char *prefix; ///< Message prefix
char *command; ///< IRC command char *command; ///< IRC command
struct str_vector params; ///< Command parameters struct strv params; ///< Command parameters
}; };
static char * static char *
@ -64,8 +64,8 @@ irc_unescape_message_tag (const char *value)
static void static void
irc_parse_message_tags (const char *tags, struct str_map *out) irc_parse_message_tags (const char *tags, struct str_map *out)
{ {
struct str_vector v; struct strv v;
str_vector_init (&v); strv_init (&v);
cstr_split (tags, ";", true, &v); cstr_split (tags, ";", true, &v);
for (size_t i = 0; i < v.len; i++) for (size_t i = 0; i < v.len; i++)
@ -80,7 +80,7 @@ irc_parse_message_tags (const char *tags, struct str_map *out)
str_map_set (out, key, xstrdup ("")); str_map_set (out, key, xstrdup (""));
} }
str_vector_free (&v); strv_free (&v);
} }
static void static void
@ -91,7 +91,7 @@ irc_parse_message (struct irc_message *msg, const char *line)
msg->prefix = NULL; msg->prefix = NULL;
msg->command = NULL; msg->command = NULL;
str_vector_init (&msg->params); strv_init (&msg->params);
// IRC 3.2 message tags // IRC 3.2 message tags
if (*line == '@') if (*line == '@')
@ -132,7 +132,7 @@ irc_parse_message (struct irc_message *msg, const char *line)
if (*line == ':') if (*line == ':')
{ {
str_vector_add (&msg->params, ++line); strv_add (&msg->params, ++line);
break; break;
} }
@ -140,7 +140,7 @@ irc_parse_message (struct irc_message *msg, const char *line)
if (!param_len) if (!param_len)
break; break;
str_vector_add_owned (&msg->params, xstrndup (line, param_len)); strv_add_owned (&msg->params, xstrndup (line, param_len));
line += param_len; line += param_len;
} }
} }
@ -151,7 +151,7 @@ irc_free_message (struct irc_message *msg)
str_map_free (&msg->tags); str_map_free (&msg->tags);
free (msg->prefix); free (msg->prefix);
free (msg->command); free (msg->command);
str_vector_free (&msg->params); strv_free (&msg->params);
} }
static void static void
@ -1360,7 +1360,7 @@ struct mpd_response
/// Task completion callback /// Task completion callback
typedef void (*mpd_client_task_cb) (const struct mpd_response *response, typedef void (*mpd_client_task_cb) (const struct mpd_response *response,
const struct str_vector *data, void *user_data); const struct strv *data, void *user_data);
struct mpd_client_task struct mpd_client_task
{ {
@ -1396,7 +1396,7 @@ struct mpd_client
struct mpd_client_task *tasks; ///< Task queue struct mpd_client_task *tasks; ///< Task queue
struct mpd_client_task *tasks_tail; ///< Tail of task queue struct mpd_client_task *tasks_tail; ///< Tail of task queue
struct str_vector data; ///< Data from last command struct strv data; ///< Data from last command
// User configuration: // User configuration:
@ -1431,7 +1431,7 @@ mpd_client_init (struct mpd_client *self, struct poller *poller)
str_init (&self->read_buffer); str_init (&self->read_buffer);
str_init (&self->write_buffer); str_init (&self->write_buffer);
str_vector_init (&self->data); strv_init (&self->data);
poller_fd_init (&self->socket_event, poller, -1); poller_fd_init (&self->socket_event, poller, -1);
poller_timer_init (&self->timeout_timer, poller); poller_timer_init (&self->timeout_timer, poller);
@ -1446,7 +1446,7 @@ mpd_client_free (struct mpd_client *self)
str_free (&self->read_buffer); str_free (&self->read_buffer);
str_free (&self->write_buffer); str_free (&self->write_buffer);
str_vector_free (&self->data); strv_free (&self->data);
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -1469,7 +1469,7 @@ mpd_client_reset (struct mpd_client *self)
str_reset (&self->read_buffer); str_reset (&self->read_buffer);
str_reset (&self->write_buffer); str_reset (&self->write_buffer);
str_vector_reset (&self->data); strv_reset (&self->data);
self->got_hello = false; self->got_hello = false;
self->idling = false; self->idling = false;
@ -1539,7 +1539,7 @@ mpd_client_dispatch (struct mpd_client *self, struct mpd_response *response)
if (task->callback) if (task->callback)
task->callback (response, &self->data, task->user_data); task->callback (response, &self->data, task->user_data);
str_vector_reset (&self->data); strv_reset (&self->data);
LIST_UNLINK_WITH_TAIL (self->tasks, self->tasks_tail, task); LIST_UNLINK_WITH_TAIL (self->tasks, self->tasks_tail, task);
free (task); free (task);
@ -1572,7 +1572,7 @@ mpd_client_parse_line (struct mpd_client *self, const char *line)
struct mpd_response response; struct mpd_response response;
memset (&response, 0, sizeof response); memset (&response, 0, sizeof response);
if (!strcmp (line, "list_OK")) if (!strcmp (line, "list_OK"))
str_vector_add_owned (&self->data, NULL); strv_add_owned (&self->data, NULL);
else if (mpd_client_parse_response (line, &response)) else if (mpd_client_parse_response (line, &response))
{ {
mpd_client_dispatch (self, &response); mpd_client_dispatch (self, &response);
@ -1580,7 +1580,7 @@ mpd_client_parse_line (struct mpd_client *self, const char *line)
free (response.message_text); free (response.message_text);
} }
else else
str_vector_add (&self->data, line); strv_add (&self->data, line);
return true; return true;
} }
@ -1735,17 +1735,17 @@ mpd_client_send_commandv (struct mpd_client *self, char **commands)
static void static void
mpd_client_send_command (struct mpd_client *self, const char *command, ...) mpd_client_send_command (struct mpd_client *self, const char *command, ...)
{ {
struct str_vector v; struct strv v;
str_vector_init (&v); strv_init (&v);
va_list ap; va_list ap;
va_start (ap, command); va_start (ap, command);
for (; command; command = va_arg (ap, const char *)) for (; command; command = va_arg (ap, const char *))
str_vector_add (&v, command); strv_add (&v, command);
va_end (ap); va_end (ap);
mpd_client_send_commandv (self, v.vector); mpd_client_send_commandv (self, v.vector);
str_vector_free (&v); strv_free (&v);
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -1793,7 +1793,7 @@ mpd_resolve_subsystem (const char *name, unsigned *output)
static void static void
mpd_client_on_idle_return (const struct mpd_response *response, mpd_client_on_idle_return (const struct mpd_response *response,
const struct str_vector *data, void *user_data) const struct strv *data, void *user_data)
{ {
(void) response; (void) response;
@ -1836,16 +1836,16 @@ mpd_client_idle (struct mpd_client *self, unsigned subsystems)
{ {
hard_assert (!self->in_list); hard_assert (!self->in_list);
struct str_vector v; struct strv v;
str_vector_init (&v); strv_init (&v);
str_vector_add (&v, "idle"); strv_add (&v, "idle");
for (size_t i = 0; i < N_ELEMENTS (mpd_subsystem_names); i++) for (size_t i = 0; i < N_ELEMENTS (mpd_subsystem_names); i++)
if (subsystems & (1 << i)) if (subsystems & (1 << i))
str_vector_add (&v, mpd_subsystem_names[i]); strv_add (&v, mpd_subsystem_names[i]);
mpd_client_send_commandv (self, v.vector); mpd_client_send_commandv (self, v.vector);
str_vector_free (&v); strv_free (&v);
self->timeout_timer.dispatcher = mpd_client_on_timeout; self->timeout_timer.dispatcher = mpd_client_on_timeout;
self->timeout_timer.user_data = self; self->timeout_timer.user_data = self;

View File

@ -62,8 +62,8 @@ struct attrs
static struct attrs static struct attrs
attrs_decode (const char *value) attrs_decode (const char *value)
{ {
struct str_vector v; struct strv v;
str_vector_init (&v); strv_init (&v);
cstr_split (value, " ", true, &v); cstr_split (value, " ", true, &v);
int colors = 0; int colors = 0;
@ -87,7 +87,7 @@ attrs_decode (const char *value)
else if (!strcmp (*it, "italic")) attrs.attrs |= A_ITALIC; else if (!strcmp (*it, "italic")) attrs.attrs |= A_ITALIC;
#endif // A_ITALIC #endif // A_ITALIC
} }
str_vector_free (&v); strv_free (&v);
return attrs; return attrs;
} }

View File

@ -377,7 +377,7 @@ xstrndup (const char *s, size_t n)
// --- Dynamically allocated string array -------------------------------------- // --- Dynamically allocated string array --------------------------------------
struct str_vector struct strv
{ {
char **vector; char **vector;
size_t len; size_t len;
@ -385,7 +385,7 @@ struct str_vector
}; };
static void static void
str_vector_init (struct str_vector *self) strv_init (struct strv *self)
{ {
self->alloc = 4; self->alloc = 4;
self->len = 0; self->len = 0;
@ -393,7 +393,7 @@ str_vector_init (struct str_vector *self)
} }
static void static void
str_vector_free (struct str_vector *self) strv_free (struct strv *self)
{ {
unsigned i; unsigned i;
for (i = 0; i < self->len; i++) for (i = 0; i < self->len; i++)
@ -404,14 +404,14 @@ str_vector_free (struct str_vector *self)
} }
static void static void
str_vector_reset (struct str_vector *self) strv_reset (struct strv *self)
{ {
str_vector_free (self); strv_free (self);
str_vector_init (self); strv_init (self);
} }
static void static void
str_vector_add_owned (struct str_vector *self, char *s) strv_add_owned (struct strv *self, char *s)
{ {
self->vector[self->len] = s; self->vector[self->len] = s;
if (++self->len >= self->alloc) if (++self->len >= self->alloc)
@ -421,38 +421,38 @@ str_vector_add_owned (struct str_vector *self, char *s)
} }
static void static void
str_vector_add (struct str_vector *self, const char *s) strv_add (struct strv *self, const char *s)
{ {
str_vector_add_owned (self, xstrdup (s)); strv_add_owned (self, xstrdup (s));
} }
static void static void
str_vector_add_args (struct str_vector *self, const char *s, ...) strv_add_args (struct strv *self, const char *s, ...)
ATTRIBUTE_SENTINEL; ATTRIBUTE_SENTINEL;
static void static void
str_vector_add_args (struct str_vector *self, const char *s, ...) strv_add_args (struct strv *self, const char *s, ...)
{ {
va_list ap; va_list ap;
va_start (ap, s); va_start (ap, s);
while (s) while (s)
{ {
str_vector_add (self, s); strv_add (self, s);
s = va_arg (ap, const char *); s = va_arg (ap, const char *);
} }
va_end (ap); va_end (ap);
} }
static void static void
str_vector_add_vector (struct str_vector *self, char **vector) strv_add_vector (struct strv *self, char **vector)
{ {
while (*vector) while (*vector)
str_vector_add (self, *vector++); strv_add (self, *vector++);
} }
static char * static char *
str_vector_steal (struct str_vector *self, size_t i) strv_steal (struct strv *self, size_t i)
{ {
hard_assert (i < self->len); hard_assert (i < self->len);
char *tmp = self->vector[i]; char *tmp = self->vector[i];
@ -462,9 +462,9 @@ str_vector_steal (struct str_vector *self, size_t i)
} }
static void static void
str_vector_remove (struct str_vector *self, size_t i) strv_remove (struct strv *self, size_t i)
{ {
free (str_vector_steal (self, i)); free (strv_steal (self, i));
} }
// --- Dynamically allocated strings ------------------------------------------- // --- Dynamically allocated strings -------------------------------------------
@ -2915,17 +2915,17 @@ base64_encode (const void *data, size_t len, struct str *output)
static void static void
cstr_split (const char *s, const char *delimiters, bool ignore_empty, cstr_split (const char *s, const char *delimiters, bool ignore_empty,
struct str_vector *out) struct strv *out)
{ {
const char *begin = s, *end; const char *begin = s, *end;
while ((end = strpbrk (begin, delimiters))) while ((end = strpbrk (begin, delimiters)))
{ {
if (!ignore_empty || begin != end) if (!ignore_empty || begin != end)
str_vector_add_owned (out, xstrndup (begin, end - begin)); strv_add_owned (out, xstrndup (begin, end - begin));
begin = ++end; begin = ++end;
} }
if (!ignore_empty || *begin) if (!ignore_empty || *begin)
str_vector_add (out, begin); strv_add (out, begin);
} }
static char * static char *
@ -2957,7 +2957,7 @@ cstr_cut_until (const char *s, const char *alphabet)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
static char * static char *
join_str_vector (const struct str_vector *v, char delimiter) join_strv (const struct strv *v, char delimiter)
{ {
if (!v->len) if (!v->len)
return xstrdup (""); return xstrdup ("");
@ -3188,7 +3188,7 @@ get_xdg_home_dir (struct str *output, const char *var, const char *def)
static char * static char *
resolve_relative_filename_generic resolve_relative_filename_generic
(struct str_vector *paths, const char *tail, const char *filename) (struct strv *paths, const char *tail, const char *filename)
{ {
for (unsigned i = 0; i < paths->len; i++) for (unsigned i = 0; i < paths->len; i++)
{ {
@ -3208,12 +3208,12 @@ resolve_relative_filename_generic
} }
static void static void
get_xdg_config_dirs (struct str_vector *out) get_xdg_config_dirs (struct strv *out)
{ {
struct str config_home; struct str config_home;
str_init (&config_home); str_init (&config_home);
get_xdg_home_dir (&config_home, "XDG_CONFIG_HOME", ".config"); get_xdg_home_dir (&config_home, "XDG_CONFIG_HOME", ".config");
str_vector_add (out, config_home.str); strv_add (out, config_home.str);
str_free (&config_home); str_free (&config_home);
const char *xdg_config_dirs; const char *xdg_config_dirs;
@ -3225,22 +3225,22 @@ get_xdg_config_dirs (struct str_vector *out)
static char * static char *
resolve_relative_config_filename (const char *filename) resolve_relative_config_filename (const char *filename)
{ {
struct str_vector paths; struct strv paths;
str_vector_init (&paths); strv_init (&paths);
get_xdg_config_dirs (&paths); get_xdg_config_dirs (&paths);
char *result = resolve_relative_filename_generic char *result = resolve_relative_filename_generic
(&paths, PROGRAM_NAME "/", filename); (&paths, PROGRAM_NAME "/", filename);
str_vector_free (&paths); strv_free (&paths);
return result; return result;
} }
static void static void
get_xdg_data_dirs (struct str_vector *out) get_xdg_data_dirs (struct strv *out)
{ {
struct str data_home; struct str data_home;
str_init (&data_home); str_init (&data_home);
get_xdg_home_dir (&data_home, "XDG_DATA_HOME", ".local/share"); get_xdg_home_dir (&data_home, "XDG_DATA_HOME", ".local/share");
str_vector_add (out, data_home.str); strv_add (out, data_home.str);
str_free (&data_home); str_free (&data_home);
const char *xdg_data_dirs; const char *xdg_data_dirs;
@ -3252,12 +3252,12 @@ get_xdg_data_dirs (struct str_vector *out)
static char * static char *
resolve_relative_data_filename (const char *filename) resolve_relative_data_filename (const char *filename)
{ {
struct str_vector paths; struct strv paths;
str_vector_init (&paths); strv_init (&paths);
get_xdg_data_dirs (&paths); get_xdg_data_dirs (&paths);
char *result = resolve_relative_filename_generic char *result = resolve_relative_filename_generic
(&paths, PROGRAM_NAME "/", filename); (&paths, PROGRAM_NAME "/", filename);
str_vector_free (&paths); strv_free (&paths);
return result; return result;
} }
@ -3406,7 +3406,7 @@ regex_free (void *regex)
// The cost of hashing a string is likely to be significantly smaller than that // The cost of hashing a string is likely to be significantly smaller than that
// of compiling the whole regular expression anew, so here is a simple cache. // of compiling the whole regular expression anew, so here is a simple cache.
// Adding basic support for subgroups is easy: check `re_nsub' and output into // Adding basic support for subgroups is easy: check `re_nsub' and output into
// a `struct str_vector' (if all we want is the substrings). // a `struct strv' (if all we want is the substrings).
static void static void
regex_cache_init (struct str_map *cache) regex_cache_init (struct str_map *cache)
@ -4630,8 +4630,8 @@ config_item_get (struct config_item *self, const char *path, struct error **e)
{ {
hard_assert (self->type == CONFIG_ITEM_OBJECT); hard_assert (self->type == CONFIG_ITEM_OBJECT);
struct str_vector v; struct strv v;
str_vector_init (&v); strv_init (&v);
cstr_split (path, ".", false, &v); cstr_split (path, ".", false, &v);
struct config_item *result = NULL; struct config_item *result = NULL;
@ -4651,7 +4651,7 @@ config_item_get (struct config_item *self, const char *path, struct error **e)
continue; continue;
break; break;
} }
str_vector_free (&v); strv_free (&v);
return result; return result;
} }

View File

@ -157,31 +157,31 @@ test_list_with_tail (void)
// --- Strings ----------------------------------------------------------------- // --- Strings -----------------------------------------------------------------
static void static void
test_str_vector (void) test_strv (void)
{ {
struct str_vector v; struct strv v;
str_vector_init (&v); strv_init (&v);
str_vector_add_owned (&v, xstrdup ("xkcd")); strv_add_owned (&v, xstrdup ("xkcd"));
str_vector_reset (&v); strv_reset (&v);
const char *a[] = const char *a[] =
{ "123", "456", "a", "bc", "def", "ghij", "klmno", "pqrstu" }; { "123", "456", "a", "bc", "def", "ghij", "klmno", "pqrstu" };
// Add the first two items via another vector // Add the first two items via another vector
struct str_vector w; struct strv w;
str_vector_init (&w); strv_init (&w);
str_vector_add_args (&w, a[0], a[1], NULL); strv_add_args (&w, a[0], a[1], NULL);
str_vector_add_vector (&v, w.vector); strv_add_vector (&v, w.vector);
str_vector_free (&w); strv_free (&w);
// Add an item and delete it right after // Add an item and delete it right after
str_vector_add (&v, "test"); strv_add (&v, "test");
str_vector_remove (&v, v.len - 1); strv_remove (&v, v.len - 1);
// Add the rest of the list properly // Add the rest of the list properly
for (int i = 2; i < (int) N_ELEMENTS (a); i++) for (int i = 2; i < (int) N_ELEMENTS (a); i++)
str_vector_add (&v, a[i]); strv_add (&v, a[i]);
// Check the contents // Check the contents
soft_assert (v.len == N_ELEMENTS (a)); soft_assert (v.len == N_ELEMENTS (a));
@ -189,7 +189,7 @@ test_str_vector (void)
soft_assert (!strcmp (v.vector[i], a[i])); soft_assert (!strcmp (v.vector[i], a[i]));
soft_assert (v.vector[v.len] == NULL); soft_assert (v.vector[v.len] == NULL);
str_vector_free (&v); strv_free (&v);
} }
static void static void
@ -629,7 +629,7 @@ main (int argc, char *argv[])
test_add_simple (&test, "/memory", NULL, test_memory); test_add_simple (&test, "/memory", NULL, test_memory);
test_add_simple (&test, "/list", NULL, test_list); test_add_simple (&test, "/list", NULL, test_list);
test_add_simple (&test, "/list-with-tail", NULL, test_list_with_tail); test_add_simple (&test, "/list-with-tail", NULL, test_list_with_tail);
test_add_simple (&test, "/str-vector", NULL, test_str_vector); test_add_simple (&test, "/strv", NULL, test_strv);
test_add_simple (&test, "/str", NULL, test_str); test_add_simple (&test, "/str", NULL, test_str);
test_add_simple (&test, "/error", NULL, test_error); test_add_simple (&test, "/error", NULL, test_error);
test_add_simple (&test, "/str-map", NULL, test_str_map); test_add_simple (&test, "/str-map", NULL, test_str_map);