Coverage Report

Created: 2024-09-08 06:24

/src/git/strvec.c
Line
Count
Source (jump to first uncovered line)
1
#include "git-compat-util.h"
2
#include "strvec.h"
3
#include "strbuf.h"
4
5
const char *empty_strvec[] = { NULL };
6
7
void strvec_init(struct strvec *array)
8
244k
{
9
244k
  struct strvec blank = STRVEC_INIT;
10
244k
  memcpy(array, &blank, sizeof(*array));
11
244k
}
12
13
void strvec_push_nodup(struct strvec *array, char *value)
14
258k
{
15
258k
  if (array->v == empty_strvec)
16
99.3k
    array->v = NULL;
17
18
258k
  ALLOC_GROW(array->v, array->nr + 2, array->alloc);
19
258k
  array->v[array->nr++] = value;
20
258k
  array->v[array->nr] = NULL;
21
258k
}
22
23
const char *strvec_push(struct strvec *array, const char *value)
24
221k
{
25
221k
  strvec_push_nodup(array, xstrdup(value));
26
221k
  return array->v[array->nr - 1];
27
221k
}
28
29
const char *strvec_pushf(struct strvec *array, const char *fmt, ...)
30
36.4k
{
31
36.4k
  va_list ap;
32
36.4k
  struct strbuf v = STRBUF_INIT;
33
34
36.4k
  va_start(ap, fmt);
35
36.4k
  strbuf_vaddf(&v, fmt, ap);
36
36.4k
  va_end(ap);
37
38
36.4k
  strvec_push_nodup(array, strbuf_detach(&v, NULL));
39
36.4k
  return array->v[array->nr - 1];
40
36.4k
}
41
42
void strvec_pushl(struct strvec *array, ...)
43
9.08k
{
44
9.08k
  va_list ap;
45
9.08k
  const char *arg;
46
47
9.08k
  va_start(ap, array);
48
36.3k
  while ((arg = va_arg(ap, const char *)))
49
27.2k
    strvec_push(array, arg);
50
9.08k
  va_end(ap);
51
9.08k
}
52
53
void strvec_pushv(struct strvec *array, const char **items)
54
17.3k
{
55
75.2k
  for (; *items; items++)
56
57.8k
    strvec_push(array, *items);
57
17.3k
}
58
59
const char *strvec_replace(struct strvec *array, size_t idx, const char *replacement)
60
0
{
61
0
  char *to_free;
62
0
  if (idx >= array->nr)
63
0
    BUG("index outside of array boundary");
64
0
  to_free = (char *) array->v[idx];
65
0
  array->v[idx] = xstrdup(replacement);
66
0
  free(to_free);
67
0
  return array->v[idx];
68
0
}
69
70
void strvec_remove(struct strvec *array, size_t idx)
71
0
{
72
0
  if (idx >= array->nr)
73
0
    BUG("index outside of array boundary");
74
0
  free((char *)array->v[idx]);
75
0
  memmove(array->v + idx, array->v + idx + 1, (array->nr - idx) * sizeof(char *));
76
0
  array->nr--;
77
0
}
78
79
void strvec_pop(struct strvec *array)
80
0
{
81
0
  if (!array->nr)
82
0
    return;
83
0
  free((char *)array->v[array->nr - 1]);
84
0
  array->v[array->nr - 1] = NULL;
85
0
  array->nr--;
86
0
}
87
88
void strvec_split(struct strvec *array, const char *to_split)
89
0
{
90
0
  while (isspace(*to_split))
91
0
    to_split++;
92
0
  for (;;) {
93
0
    const char *p = to_split;
94
95
0
    if (!*p)
96
0
      break;
97
98
0
    while (*p && !isspace(*p))
99
0
      p++;
100
0
    strvec_push_nodup(array, xstrndup(to_split, p - to_split));
101
102
0
    while (isspace(*p))
103
0
      p++;
104
0
    to_split = p;
105
0
  }
106
0
}
107
108
void strvec_clear(struct strvec *array)
109
244k
{
110
244k
  if (array->v != empty_strvec) {
111
99.3k
    int i;
112
357k
    for (i = 0; i < array->nr; i++)
113
258k
      free((char *)array->v[i]);
114
99.3k
    free(array->v);
115
99.3k
  }
116
244k
  strvec_init(array);
117
244k
}
118
119
const char **strvec_detach(struct strvec *array)
120
0
{
121
0
  if (array->v == empty_strvec)
122
0
    return xcalloc(1, sizeof(const char *));
123
0
  else {
124
0
    const char **ret = array->v;
125
0
    strvec_init(array);
126
0
    return ret;
127
0
  }
128
0
}