diff options
author | Edward Thomson <ethomson@edwardthomson.com> | 2023-04-22 14:04:09 +0100 |
---|---|---|
committer | Edward Thomson <ethomson@edwardthomson.com> | 2023-05-17 11:01:15 +0100 |
commit | 46c3bdc6f065848977b592396ad730056a2c2cf8 (patch) | |
tree | 40d83613a3c1348b394e5aa718cf8b6185593f33 | |
parent | f041a94e2c358e84adb5a0fe108288fcb3802970 (diff) | |
download | libgit2-46c3bdc6f065848977b592396ad730056a2c2cf8.tar.gz |
Introduce `git_strlist` to manage string lists
-rw-r--r-- | src/util/strlist.c | 79 | ||||
-rw-r--r-- | src/util/strlist.h | 32 |
2 files changed, 111 insertions, 0 deletions
diff --git a/src/util/strlist.c b/src/util/strlist.c new file mode 100644 index 000000000..8ba476812 --- /dev/null +++ b/src/util/strlist.c @@ -0,0 +1,79 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include <stdio.h> + +#include "git2_util.h" +#include "vector.h" +#include "strlist.h" + +int git_strlist_copy_with_null(char ***out, const char **in, size_t len) +{ + char **dup; + size_t new_len, i; + + GIT_ERROR_CHECK_ALLOC_ADD(&new_len, len, 1); + + dup = git__calloc(new_len, sizeof(char *)); + GIT_ERROR_CHECK_ALLOC(dup); + + for (i = 0; i < len; i++) { + dup[i] = git__strdup(in[i]); + GIT_ERROR_CHECK_ALLOC(dup[i]); + } + + *out = dup; + return 0; +} + +bool git_strlist_contains_prefix( + const char **strings, + size_t len, + const char *str, + size_t n) +{ + size_t i; + + for (i = 0; i < len; i++) { + if (strncmp(strings[i], str, n) == 0) + return true; + } + + return false; +} + +bool git_strlist_contains_key( + const char **strings, + size_t len, + const char *key, + char delimiter) +{ + const char *c; + + for (c = key; *c; c++) { + if (*c == delimiter) + break; + } + + return *c ? + git_strlist_contains_prefix(strings, len, key, (c - key)) : + false; +} + +void git_strlist_free_with_null(char **strings) +{ + char **s; + + if (!strings) + return; + + for (s = strings; *s; s++) + git__free(*s); + + git__free(strings); +} + diff --git a/src/util/strlist.h b/src/util/strlist.h new file mode 100644 index 000000000..c37afb4b6 --- /dev/null +++ b/src/util/strlist.h @@ -0,0 +1,32 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifndef INCLUDE_runtime_h__ +#define INCLUDE_runtime_h__ + +#include "git2_util.h" + +extern int git_strlist_copy_with_null( + char ***out, + const char **in, + size_t len); + +extern bool git_strlist_contains_prefix( + const char **strings, + size_t len, + const char *str, + size_t n); + +extern bool git_strlist_contains_key( + const char **strings, + size_t len, + const char *key, + char delimiter); + +extern void git_strlist_free_with_null(char **strings); + +#endif |