Add two string git_buf_join and tweak input error checking. This commit addresses two of the comments: * renamed existing n-input git_buf_join to git_buf_join_n * added new git_buf_join that always takes two inputs * moved some parameter error checking to asserts * extended unit tests to cover new version of git_buf_join
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
diff --git a/src/buffer.c b/src/buffer.c
index 3fd0421..50014fc 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -121,14 +121,14 @@ const char *git_buf_cstr(git_buf *buf)
void git_buf_free(git_buf *buf)
{
- if (buf) {
- if (buf->ptr) {
- git__free(buf->ptr);
- buf->ptr = NULL;
- }
- buf->asize = 0;
- buf->size = 0;
+ assert(buf);
+
+ if (buf->ptr) {
+ git__free(buf->ptr);
+ buf->ptr = NULL;
}
+ buf->asize = 0;
+ buf->size = 0;
}
void git_buf_clear(git_buf *buf)
@@ -173,7 +173,7 @@ char *git_buf_take_cstr(git_buf *buf)
return data;
}
-void git_buf_join(git_buf *buf, char separator, int nbuf, ...)
+void git_buf_join_n(git_buf *buf, char separator, int nbuf, ...)
{
/* Make two passes to avoid multiple reallocation */
@@ -239,3 +239,63 @@ void git_buf_join(git_buf *buf, char separator, int nbuf, ...)
buf->size = out - buf->ptr;
}
+void git_buf_join(
+ git_buf *buf,
+ char separator,
+ const char *str_a,
+ const char *str_b)
+{
+ int add_size = 0;
+ int sep_a = 0;
+ int strlen_a = 0;
+ int sep_b = 0;
+ int strlen_b = 0;
+ char *ptr;
+
+ /* calculate string lengths and need for added separators */
+ if (str_a) {
+ while (*str_a == separator) { sep_a = 1; str_a++; }
+ strlen_a = strlen(str_a);
+ }
+ if (str_b) {
+ while (*str_b == separator) { sep_b = 1; str_b++; }
+ strlen_b = strlen(str_b);
+ }
+ if (buf->size > 0) {
+ if (buf->ptr[buf->size - 1] == separator) {
+ sep_a = 0;
+ if (!strlen_a) sep_b = 0;
+ } else if (!strlen_a)
+ sep_b = (str_b != NULL);
+ }
+ if (strlen_a > 0) {
+ if (str_a[strlen_a - 1] == separator)
+ sep_b = 0;
+ else if (str_b)
+ sep_b = 1;
+ }
+
+ add_size = sep_a + strlen_a + sep_b + strlen_b;
+
+ if (!add_size) return;
+
+ ENSURE_SIZE(buf, buf->size + add_size);
+
+ /* concatenate strings */
+ ptr = buf->ptr + buf->size;
+ if (sep_a)
+ *ptr++ = separator;
+ if (strlen_a) {
+ memmove(ptr, str_a, strlen_a);
+ ptr += strlen_a;
+ }
+ if (sep_b)
+ *ptr++ = separator;
+ if (strlen_b) {
+ memmove(ptr, str_b, strlen_b);
+ ptr += strlen_b;
+ }
+
+ /* set size based on num characters actually written */
+ buf->size = ptr - buf->ptr;
+}
diff --git a/src/buffer.h b/src/buffer.h
index baa8f4f..2ed9047 100644
--- a/src/buffer.h
+++ b/src/buffer.h
@@ -37,7 +37,8 @@ void git_buf_puts(git_buf *buf, const char *string);
void git_buf_printf(git_buf *buf, const char *format, ...) GIT_FORMAT_PRINTF(2, 3);
void git_buf_clear(git_buf *buf);
void git_buf_consume(git_buf *buf, const char *end);
-void git_buf_join(git_buf *buf, char separator, int nbuf, ...);
+void git_buf_join_n(git_buf *buf, char separator, int nbuf, ...);
+void git_buf_join(git_buf *buf, char separator, const char *str_a, const char *str_b);
const char *git_buf_cstr(git_buf *buf);
char *git_buf_take_cstr(git_buf *buf);
diff --git a/tests-clay/core/buffer.c b/tests-clay/core/buffer.c
index 411c5e8..5adc106 100644
--- a/tests-clay/core/buffer.c
+++ b/tests-clay/core/buffer.c
@@ -352,7 +352,32 @@ void test_core_buffer__7(void)
static void
-check_joinbuf(
+check_joinbuf_2(
+ const char *a,
+ const char *b,
+ const char *expected)
+{
+ char sep = '/';
+ git_buf buf = GIT_BUF_INIT;
+
+ /* first validate join from nothing */
+ git_buf_join(&buf, sep, a, b);
+ cl_assert(git_buf_oom(&buf) == 0);
+ cl_assert_strequal(expected, git_buf_cstr(&buf));
+ git_buf_free(&buf);
+
+ /* next validate join-append */
+ git_buf_sets(&buf, a);
+ cl_assert(git_buf_oom(&buf) == 0);
+ git_buf_join(&buf, sep, NULL, b);
+ cl_assert(git_buf_oom(&buf) == 0);
+ cl_assert_strequal(expected, git_buf_cstr(&buf));
+
+ git_buf_free(&buf);
+}
+
+static void
+check_joinbuf_n_2(
const char *a,
const char *b,
const char *expected)
@@ -363,7 +388,7 @@ check_joinbuf(
git_buf_sets(&buf, a);
cl_assert(git_buf_oom(&buf) == 0);
- git_buf_join(&buf, sep, 1, b);
+ git_buf_join_n(&buf, sep, 1, b);
cl_assert(git_buf_oom(&buf) == 0);
cl_assert_strequal(expected, git_buf_cstr(&buf));
@@ -371,7 +396,7 @@ check_joinbuf(
}
static void
-check_joinbuf_n(
+check_joinbuf_n_4(
const char *a,
const char *b,
const char *c,
@@ -380,7 +405,7 @@ check_joinbuf_n(
{
char sep = ';';
git_buf buf = GIT_BUF_INIT;
- git_buf_join(&buf, sep, 4, a, b, c, d);
+ git_buf_join_n(&buf, sep, 4, a, b, c, d);
cl_assert(git_buf_oom(&buf) == 0);
cl_assert_strequal(expected, git_buf_cstr(&buf));
git_buf_free(&buf);
@@ -391,45 +416,63 @@ void test_core_buffer__8(void)
{
git_buf a = GIT_BUF_INIT;
- git_buf_join(&a, '/', 1, "foo");
+ git_buf_join_n(&a, '/', 1, "foo");
cl_assert(git_buf_oom(&a) == 0);
cl_assert_strequal("foo", git_buf_cstr(&a));
- git_buf_join(&a, '/', 1, "bar");
+ git_buf_join_n(&a, '/', 1, "bar");
cl_assert(git_buf_oom(&a) == 0);
cl_assert_strequal("foo/bar", git_buf_cstr(&a));
- git_buf_join(&a, '/', 1, "baz");
+ git_buf_join_n(&a, '/', 1, "baz");
cl_assert(git_buf_oom(&a) == 0);
cl_assert_strequal("foo/bar/baz", git_buf_cstr(&a));
git_buf_free(&a);
- check_joinbuf("", "", "");
- check_joinbuf("", "a", "a");
- check_joinbuf("", "/a", "/a");
- check_joinbuf("a", "", "a/");
- check_joinbuf("a", "/", "a/");
- check_joinbuf("a", "b", "a/b");
- check_joinbuf("/", "a", "/a");
- check_joinbuf("/", "", "/");
- check_joinbuf("/a", "/b", "/a/b");
- check_joinbuf("/a", "/b/", "/a/b/");
- check_joinbuf("/a/", "b/", "/a/b/");
- check_joinbuf("/a/", "/b/", "/a/b/");
- check_joinbuf("/abcd", "/defg", "/abcd/defg");
- check_joinbuf("/abcd", "/defg/", "/abcd/defg/");
- check_joinbuf("/abcd/", "defg/", "/abcd/defg/");
- check_joinbuf("/abcd/", "/defg/", "/abcd/defg/");
-
- check_joinbuf_n("", "", "", "", "");
- check_joinbuf_n("", "a", "", "", "a;");
- check_joinbuf_n("a", "", "", "", "a;");
- check_joinbuf_n("", "", "", "a", "a");
- check_joinbuf_n("a", "b", "", ";c;d;", "a;b;c;d;");
- check_joinbuf_n("a", "b", "", ";c;d", "a;b;c;d");
- check_joinbuf_n("abcd", "efgh", "ijkl", "mnop", "abcd;efgh;ijkl;mnop");
- check_joinbuf_n("abcd;", "efgh;", "ijkl;", "mnop;", "abcd;efgh;ijkl;mnop;");
- check_joinbuf_n(";abcd;", ";efgh;", ";ijkl;", ";mnop;", ";abcd;efgh;ijkl;mnop;");
+ check_joinbuf_2("", "", "");
+ check_joinbuf_2("", "a", "a");
+ check_joinbuf_2("", "/a", "/a");
+ check_joinbuf_2("a", "", "a/");
+ check_joinbuf_2("a", "/", "a/");
+ check_joinbuf_2("a", "b", "a/b");
+ check_joinbuf_2("/", "a", "/a");
+ check_joinbuf_2("/", "", "/");
+ check_joinbuf_2("/a", "/b", "/a/b");
+ check_joinbuf_2("/a", "/b/", "/a/b/");
+ check_joinbuf_2("/a/", "b/", "/a/b/");
+ check_joinbuf_2("/a/", "/b/", "/a/b/");
+ check_joinbuf_2("/a/", "//b/", "/a/b/");
+ check_joinbuf_2("/abcd", "/defg", "/abcd/defg");
+ check_joinbuf_2("/abcd", "/defg/", "/abcd/defg/");
+ check_joinbuf_2("/abcd/", "defg/", "/abcd/defg/");
+ check_joinbuf_2("/abcd/", "/defg/", "/abcd/defg/");
+
+ check_joinbuf_n_2("", "", "");
+ check_joinbuf_n_2("", "a", "a");
+ check_joinbuf_n_2("", "/a", "/a");
+ check_joinbuf_n_2("a", "", "a/");
+ check_joinbuf_n_2("a", "/", "a/");
+ check_joinbuf_n_2("a", "b", "a/b");
+ check_joinbuf_n_2("/", "a", "/a");
+ check_joinbuf_n_2("/", "", "/");
+ check_joinbuf_n_2("/a", "/b", "/a/b");
+ check_joinbuf_n_2("/a", "/b/", "/a/b/");
+ check_joinbuf_n_2("/a/", "b/", "/a/b/");
+ check_joinbuf_n_2("/a/", "/b/", "/a/b/");
+ check_joinbuf_n_2("/abcd", "/defg", "/abcd/defg");
+ check_joinbuf_n_2("/abcd", "/defg/", "/abcd/defg/");
+ check_joinbuf_n_2("/abcd/", "defg/", "/abcd/defg/");
+ check_joinbuf_n_2("/abcd/", "/defg/", "/abcd/defg/");
+
+ check_joinbuf_n_4("", "", "", "", "");
+ check_joinbuf_n_4("", "a", "", "", "a;");
+ check_joinbuf_n_4("a", "", "", "", "a;");
+ check_joinbuf_n_4("", "", "", "a", "a");
+ check_joinbuf_n_4("a", "b", "", ";c;d;", "a;b;c;d;");
+ check_joinbuf_n_4("a", "b", "", ";c;d", "a;b;c;d");
+ check_joinbuf_n_4("abcd", "efgh", "ijkl", "mnop", "abcd;efgh;ijkl;mnop");
+ check_joinbuf_n_4("abcd;", "efgh;", "ijkl;", "mnop;", "abcd;efgh;ijkl;mnop;");
+ check_joinbuf_n_4(";abcd;", ";efgh;", ";ijkl;", ";mnop;", ";abcd;efgh;ijkl;mnop;");
}