Coverage Report

Created: 2025-12-31 07:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/git/strvec.c
Line
Count
Source
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
0
{
9
0
  struct strvec blank = STRVEC_INIT;
10
0
  memcpy(array, &blank, sizeof(*array));
11
0
}
12
13
void strvec_push_nodup(struct strvec *array, char *value)
14
0
{
15
0
  if (array->v == empty_strvec)
16
0
    array->v = NULL;
17
18
0
  ALLOC_GROW(array->v, array->nr + 2, array->alloc);
19
0
  array->v[array->nr++] = value;
20
0
  array->v[array->nr] = NULL;
21
0
}
22
23
const char *strvec_push(struct strvec *array, const char *value)
24
0
{
25
0
  strvec_push_nodup(array, xstrdup(value));
26
0
  return array->v[array->nr - 1];
27
0
}
28
29
const char *strvec_pushf(struct strvec *array, const char *fmt, ...)
30
0
{
31
0
  va_list ap;
32
0
  struct strbuf v = STRBUF_INIT;
33
34
0
  va_start(ap, fmt);
35
0
  strbuf_vaddf(&v, fmt, ap);
36
0
  va_end(ap);
37
38
0
  strvec_push_nodup(array, strbuf_detach(&v, NULL));
39
0
  return array->v[array->nr - 1];
40
0
}
41
42
void strvec_pushl(struct strvec *array, ...)
43
0
{
44
0
  va_list ap;
45
0
  const char *arg;
46
47
0
  va_start(ap, array);
48
0
  while ((arg = va_arg(ap, const char *)))
49
0
    strvec_push(array, arg);
50
0
  va_end(ap);
51
0
}
52
53
void strvec_pushv(struct strvec *array, const char **items)
54
0
{
55
0
  for (; *items; items++)
56
0
    strvec_push(array, *items);
57
0
}
58
59
void strvec_splice(struct strvec *array, size_t idx, size_t len,
60
       const char **replacement, size_t replacement_len)
61
0
{
62
0
  if (idx + len > array->nr)
63
0
    BUG("range outside of array boundary");
64
0
  if (replacement_len > len) {
65
0
    if (array->v == empty_strvec)
66
0
      array->v = NULL;
67
0
    ALLOC_GROW(array->v, array->nr + (replacement_len - len) + 1,
68
0
         array->alloc);
69
0
    array->v[array->nr + (replacement_len - len)] = NULL;
70
0
  }
71
0
  for (size_t i = 0; i < len; i++)
72
0
    free((char *)array->v[idx + i]);
73
0
  if ((replacement_len != len) && array->nr)
74
0
    memmove(array->v + idx + replacement_len, array->v + idx + len,
75
0
      (array->nr - idx - len + 1) * sizeof(char *));
76
0
  array->nr += replacement_len - len;
77
0
  for (size_t i = 0; i < replacement_len; i++)
78
0
    array->v[idx + i] = xstrdup(replacement[i]);
79
0
}
80
81
const char *strvec_replace(struct strvec *array, size_t idx, const char *replacement)
82
0
{
83
0
  char *to_free;
84
0
  if (idx >= array->nr)
85
0
    BUG("index outside of array boundary");
86
0
  to_free = (char *) array->v[idx];
87
0
  array->v[idx] = xstrdup(replacement);
88
0
  free(to_free);
89
0
  return array->v[idx];
90
0
}
91
92
void strvec_remove(struct strvec *array, size_t idx)
93
0
{
94
0
  if (idx >= array->nr)
95
0
    BUG("index outside of array boundary");
96
0
  free((char *)array->v[idx]);
97
0
  memmove(array->v + idx, array->v + idx + 1, (array->nr - idx) * sizeof(char *));
98
0
  array->nr--;
99
0
}
100
101
void strvec_pop(struct strvec *array)
102
0
{
103
0
  if (!array->nr)
104
0
    return;
105
0
  free((char *)array->v[array->nr - 1]);
106
0
  array->v[array->nr - 1] = NULL;
107
0
  array->nr--;
108
0
}
109
110
void strvec_split(struct strvec *array, const char *to_split)
111
0
{
112
0
  while (isspace(*to_split))
113
0
    to_split++;
114
0
  for (;;) {
115
0
    const char *p = to_split;
116
117
0
    if (!*p)
118
0
      break;
119
120
0
    while (*p && !isspace(*p))
121
0
      p++;
122
0
    strvec_push_nodup(array, xstrndup(to_split, p - to_split));
123
124
0
    while (isspace(*p))
125
0
      p++;
126
0
    to_split = p;
127
0
  }
128
0
}
129
130
void strvec_clear(struct strvec *array)
131
0
{
132
0
  if (array->v != empty_strvec) {
133
0
    for (size_t i = 0; i < array->nr; i++)
134
0
      free((char *)array->v[i]);
135
0
    free(array->v);
136
0
  }
137
0
  strvec_init(array);
138
0
}
139
140
const char **strvec_detach(struct strvec *array)
141
0
{
142
0
  if (array->v == empty_strvec)
143
0
    return xcalloc(1, sizeof(const char *));
144
0
  else {
145
0
    const char **ret = array->v;
146
0
    strvec_init(array);
147
0
    return ret;
148
0
  }
149
0
}