summaryrefslogtreecommitdiff
path: root/string-list.c
diff options
context:
space:
mode:
authorMichael Haggerty <mhagger@alum.mit.edu>2012-09-12 14:04:46 (GMT)
committerJunio C Hamano <gitster@pobox.com>2012-09-12 18:43:25 (GMT)
commitf103f95b11d087f07c0c48bf784cd9197e18f203 (patch)
tree97c404425b942675ffedece855f58b5132cd55e5 /string-list.c
parent31d5451eed2677531c80177ff9dc8f5285f5a187 (diff)
downloadgit-f103f95b11d087f07c0c48bf784cd9197e18f203.zip
git-f103f95b11d087f07c0c48bf784cd9197e18f203.tar.gz
git-f103f95b11d087f07c0c48bf784cd9197e18f203.tar.bz2
string_list: add a function string_list_longest_prefix()
Add a function that finds the longest string from a string_list that is a prefix of a given string. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'string-list.c')
-rw-r--r--string-list.c20
1 files changed, 20 insertions, 0 deletions
diff --git a/string-list.c b/string-list.c
index decfa74..c54b816 100644
--- a/string-list.c
+++ b/string-list.c
@@ -136,6 +136,26 @@ void filter_string_list(struct string_list *list, int free_util,
list->nr = dst;
}
+char *string_list_longest_prefix(const struct string_list *prefixes,
+ const char *string)
+{
+ int i, max_len = -1;
+ char *retval = NULL;
+
+ for (i = 0; i < prefixes->nr; i++) {
+ char *prefix = prefixes->items[i].string;
+ if (!prefixcmp(string, prefix)) {
+ int len = strlen(prefix);
+ if (len > max_len) {
+ retval = prefix;
+ max_len = len;
+ }
+ }
+ }
+
+ return retval;
+}
+
void string_list_clear(struct string_list *list, int free_util)
{
if (list->items) {