lib/strutils: move *swith() functions to private library

Avoid code dublication in libmount and time-util.

Proposed-by: Karel Zak <kzak@redhat.com>
Reference: http://markmail.org/message/h7zexvqsieqngtmx
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
This commit is contained in:
Sami Kerola 2013-08-29 15:50:17 +01:00
parent 2659a49ef0
commit 199e939d88
7 changed files with 73 additions and 94 deletions

View file

@ -102,4 +102,8 @@ extern int parse_range(const char *str, int *lower, int *upper, int def);
extern int streq_except_trailing_slash(const char *s1, const char *s2); extern int streq_except_trailing_slash(const char *s1, const char *s2);
extern char *startswith(const char *s, const char *prefix);
extern char *startswith_no_case(const char *s, const char *prefix);
extern char *endswith(const char *s, const char *postfix);
#endif #endif

View file

@ -10,6 +10,7 @@
#include <errno.h> #include <errno.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <string.h> #include <string.h>
#include <assert.h>
#include "c.h" #include "c.h"
#include "nls.h" #include "nls.h"
@ -685,6 +686,72 @@ int streq_except_trailing_slash(const char *s1, const char *s2)
return equal; return equal;
} }
/*
* Match string beginning.
*/
char *startswith(const char *s, const char *prefix)
{
const char *a, *b;
assert(s);
assert(prefix);
a = s, b = prefix;
for (;;) {
if (*b == 0)
return (char *)a;
if (*a != *b)
return NULL;
a++, b++;
}
}
/*
* Case insensitive match string beginning.
*/
char *startswith_no_case(const char *s, const char *prefix)
{
const char *a, *b;
assert(s);
assert(prefix);
a = s, b = prefix;
for (;;) {
if (*b == 0)
return (char *)a;
if (tolower(*a) != tolower(*b))
return NULL;
a++, b++;
}
}
/*
* Match string ending.
*/
char *endswith(const char *s, const char *postfix)
{
size_t sl, pl;
assert(s);
assert(postfix);
sl = strlen(s);
pl = strlen(postfix);
if (pl == 0)
return (char *)s + sl;
if (sl < pl)
return NULL;
if (memcmp(s + sl - pl, postfix, pl) != 0)
return NULL;
return (char *)s + sl - pl;
}
#ifdef TEST_PROGRAM #ifdef TEST_PROGRAM

View file

@ -30,64 +30,6 @@
#define streq(a,b) (strcmp((a),(b)) == 0) #define streq(a,b) (strcmp((a),(b)) == 0)
static char *startswith(const char *s, const char *prefix)
{
const char *a, *b;
assert(s);
assert(prefix);
a = s, b = prefix;
for (;;) {
if (*b == 0)
return (char *)a;
if (*a != *b)
return NULL;
a++, b++;
}
}
static char *startswith_no_case(const char *s, const char *prefix)
{
const char *a, *b;
assert(s);
assert(prefix);
a = s, b = prefix;
for (;;) {
if (*b == 0)
return (char *)a;
if (tolower(*a) != tolower(*b))
return NULL;
a++, b++;
}
}
static char *endswith(const char *s, const char *postfix)
{
size_t sl, pl;
assert(s);
assert(postfix);
sl = strlen(s);
pl = strlen(postfix);
if (pl == 0)
return (char *)s + sl;
if (sl < pl)
return NULL;
if (memcmp(s + sl - pl, postfix, pl) != 0)
return NULL;
return (char *)s + sl - pl;
}
static int parse_sec(const char *t, usec_t *usec) static int parse_sec(const char *t, usec_t *usec)
{ {
static const struct { static const struct {

View file

@ -21,6 +21,7 @@
#include "linux_version.h" #include "linux_version.h"
#include "mountP.h" #include "mountP.h"
#include "strutils.h"
/* /*
* Kernel supports only one MS_PROPAGATION flag change by one mount(2) syscall, * Kernel supports only one MS_PROPAGATION flag change by one mount(2) syscall,

View file

@ -137,11 +137,6 @@ extern int mnt_run_test(struct libmnt_test *tests, int argc, char *argv[]);
#endif #endif
/* utils.c */ /* utils.c */
extern int endswith(const char *s, const char *sx)
__attribute__((nonnull));
extern int startswith(const char *s, const char *sx)
__attribute__((nonnull));
extern char *stripoff_last_component(char *path); extern char *stripoff_last_component(char *path);
extern int mnt_valid_tagname(const char *tagname); extern int mnt_valid_tagname(const char *tagname);

View file

@ -58,6 +58,7 @@
* mount/mount.h. * mount/mount.h.
*/ */
#include "mountP.h" #include "mountP.h"
#include "strutils.h"
/* /*
* fs-independent mount flags (built-in MNT_LINUX_MAP) * fs-independent mount flags (built-in MNT_LINUX_MAP)

View file

@ -23,37 +23,6 @@
#include "env.h" #include "env.h"
#include "match.h" #include "match.h"
int endswith(const char *s, const char *sx)
{
ssize_t off;
assert(s);
assert(sx);
off = strlen(s);
if (!off)
return 0;
off -= strlen(sx);
if (off < 0)
return 0;
return !strcmp(s + off, sx);
}
int startswith(const char *s, const char *sx)
{
size_t off;
assert(s);
assert(sx);
off = strlen(sx);
if (!off)
return 0;
return !strncmp(s, sx, off);
}
int append_string(char **a, const char *b) int append_string(char **a, const char *b)
{ {
size_t al, bl; size_t al, bl;