mirror of
https://git.proxmox.com/git/libgit2
synced 2026-01-06 02:29:21 +00:00
Add string utility functions for prefix and suffix compares
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
This commit is contained in:
parent
8ed341c55e
commit
9eb7976448
20
src/util.c
20
src/util.c
@ -24,3 +24,23 @@ char *git__strdup(const char *s)
|
||||
return git_ptr_error(GIT_ENOMEM);
|
||||
return r;
|
||||
}
|
||||
|
||||
int git__prefixcmp(const char *str, const char *prefix)
|
||||
{
|
||||
for (;;) {
|
||||
char p = *(prefix++), s;
|
||||
if (!p)
|
||||
return 0;
|
||||
if ((s = *(str++)) != p)
|
||||
return s - p;
|
||||
}
|
||||
}
|
||||
|
||||
int git__suffixcmp(const char *str, const char *suffix)
|
||||
{
|
||||
size_t a = strlen(str);
|
||||
size_t b = strlen(suffix);
|
||||
if (a < b)
|
||||
return -1;
|
||||
return strcmp(str + (a - b), suffix);
|
||||
}
|
||||
|
||||
@ -26,6 +26,9 @@ extern char *git__strdup(const char *);
|
||||
# define strdup(a) GIT__FORBID_MALLOC
|
||||
#endif
|
||||
|
||||
extern int git__prefixcmp(const char *str, const char *prefix);
|
||||
extern int git__suffixcmp(const char *str, const char *suffix);
|
||||
|
||||
/*
|
||||
* Realloc the buffer pointed at by variable 'x' so that it can hold
|
||||
* at least 'nr' entries; the number of entries currently allocated
|
||||
|
||||
67
tests/t0003-strutil.c
Normal file
67
tests/t0003-strutil.c
Normal file
@ -0,0 +1,67 @@
|
||||
#include "test_lib.h"
|
||||
#include "common.h"
|
||||
|
||||
BEGIN_TEST(prefixcmp_empty_empty)
|
||||
must_be_true(git__prefixcmp("", "") == 0);
|
||||
END_TEST
|
||||
|
||||
BEGIN_TEST(prefixcmp_a_empty)
|
||||
must_be_true(git__prefixcmp("a", "") == 0);
|
||||
END_TEST
|
||||
|
||||
BEGIN_TEST(prefixcmp_empty_a)
|
||||
must_be_true(git__prefixcmp("", "a") < 0);
|
||||
END_TEST
|
||||
|
||||
BEGIN_TEST(prefixcmp_a_b)
|
||||
must_be_true(git__prefixcmp("a", "b") < 0);
|
||||
END_TEST
|
||||
|
||||
BEGIN_TEST(prefixcmp_b_a)
|
||||
must_be_true(git__prefixcmp("b", "a") > 0);
|
||||
END_TEST
|
||||
|
||||
BEGIN_TEST(prefixcmp_ab_a)
|
||||
must_be_true(git__prefixcmp("ab", "a") == 0);
|
||||
END_TEST
|
||||
|
||||
BEGIN_TEST(prefixcmp_ab_ac)
|
||||
must_be_true(git__prefixcmp("ab", "ac") < 0);
|
||||
END_TEST
|
||||
|
||||
BEGIN_TEST(prefixcmp_ab_aa)
|
||||
must_be_true(git__prefixcmp("ab", "aa") > 0);
|
||||
END_TEST
|
||||
|
||||
|
||||
BEGIN_TEST(suffixcmp_empty_empty)
|
||||
must_be_true(git__suffixcmp("", "") == 0);
|
||||
END_TEST
|
||||
|
||||
BEGIN_TEST(suffixcmp_a_empty)
|
||||
must_be_true(git__suffixcmp("a", "") == 0);
|
||||
END_TEST
|
||||
|
||||
BEGIN_TEST(suffixcmp_empty_a)
|
||||
must_be_true(git__suffixcmp("", "a") < 0);
|
||||
END_TEST
|
||||
|
||||
BEGIN_TEST(suffixcmp_a_b)
|
||||
must_be_true(git__suffixcmp("a", "b") < 0);
|
||||
END_TEST
|
||||
|
||||
BEGIN_TEST(suffixcmp_b_a)
|
||||
must_be_true(git__suffixcmp("b", "a") > 0);
|
||||
END_TEST
|
||||
|
||||
BEGIN_TEST(suffixcmp_ba_a)
|
||||
must_be_true(git__suffixcmp("ba", "a") == 0);
|
||||
END_TEST
|
||||
|
||||
BEGIN_TEST(suffixcmp_zaa_ac)
|
||||
must_be_true(git__suffixcmp("zaa", "ac") < 0);
|
||||
END_TEST
|
||||
|
||||
BEGIN_TEST(suffixcmp_zaz_ac)
|
||||
must_be_true(git__suffixcmp("zaz", "ac") > 0);
|
||||
END_TEST
|
||||
Loading…
Reference in New Issue
Block a user