diff options
author | Edward Thomson <ethomson@edwardthomson.com> | 2020-05-19 01:29:52 +0100 |
---|---|---|
committer | Edward Thomson <ethomson@edwardthomson.com> | 2020-06-05 07:14:25 +0100 |
commit | 7ac09cf5b63cbc3591d26b79fb71de43d0103469 (patch) | |
tree | 59cead364d2dff474cf5d6243f3057291b7b2a79 | |
parent | 5da8ab9d81713163e8f93c160d0007311a1d1973 (diff) | |
download | libgit2-7ac09cf5b63cbc3591d26b79fb71de43d0103469.tar.gz |
util: provide helper function to search string arrays
Provide functions to search through string arrays.
-rw-r--r-- | src/util/util.c | 32 | ||||
-rw-r--r-- | src/util/util.h | 25 |
2 files changed, 57 insertions, 0 deletions
diff --git a/src/util/util.c b/src/util/util.c index f4a6709e5..9a8810ccd 100644 --- a/src/util/util.c +++ b/src/util/util.c @@ -58,6 +58,38 @@ int git_strings_copy_deep(char **tgt, char *const *const src, size_t n) return 0; } +bool git_strings_contains_prefix( + const char **strings, + size_t strings_len, + const char *str, + size_t n) +{ + size_t i; + + for (i = 0; i < strings_len; i++) { + if (strncmp(strings[i], str, n) == 0) + return true; + } + + return false; +} + +bool git_strings_contains_key( + const char **strings, + size_t strings_len, + const char *key, + char delimiter) +{ + const char *c; + + for (c = key; *c; c++) { + if (*c == delimiter) + break; + } + + return *c ? git_strings_contains_prefix(strings, strings_len, key, (c - key)) : false; +} + int git__strntol64(int64_t *result, const char *nptr, size_t nptr_len, const char **endptr, int base) { const char *p; diff --git a/src/util/util.h b/src/util/util.h index aa4123bf2..c2bbea61e 100644 --- a/src/util/util.h +++ b/src/util/util.h @@ -356,6 +356,31 @@ extern void git_strings_free_deep(char **strings, size_t n); extern int git_strings_copy_deep( char **tgt, char *const *const src, size_t n); +/** + * Examines the strings in the given strarray for a particular prefix. + * + * @param strings The array to search + * @param strings_len The length of the array + * @param str The string prefix to search for + * @param n The size of the string prefix + * @return true if found, false otherwise + */ +extern bool git_strings_contains_prefix( + const char **strings, size_t strings_len, const char *str, size_t n); + +/** + * Examines the strings in the given strarray for a particular key, + * ending in the given delimiter. + * + * @param strings The array to search + * @param strings_len The length of the array + * @param key The key to search for + * @param delimiter The key/value delimiter character in strings + * @return true if found, false otherwise + */ +extern bool git_strings_contains_key( + const char **strings, size_t strings_len, const char *key, char delimiter); + /* * Safely zero-out memory, making sure that the compiler * doesn't optimize away the operation. |