From: Martin Mares Date: Wed, 25 Jun 2008 18:42:36 +0000 (+0200) Subject: Moved matching and splitting functions to lib/str-*.c. X-Git-Tag: holmes-import~422 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=90bcd26d254db8411ddbd079ce089e022c8fe062;p=libucw.git Moved matching and splitting functions to lib/str-*.c. --- diff --git a/lib/Makefile b/lib/Makefile index 6df66820..13999541 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -20,7 +20,7 @@ LIBUCW_MODS= \ fastbuf ff-binary ff-string ff-printf ff-unicode \ fb-file carefulio fb-mem fb-temp fb-mmap fb-limfd fb-buffer fb-grow fb-pool fb-atomic fb-param fb-socket \ str_ctype str_upper str_lower unicode stkstring \ - wildmatch wordsplit ctmatch patimatch patmatch regex \ + wildmatch ctmatch regex \ prime primetable random timer randomkey \ bit-ffs bit-fls \ db \ @@ -31,7 +31,7 @@ LIBUCW_MODS= \ base64 base224 \ sync \ qache \ - string str-esc \ + string str-esc str-split str-match str-imatch \ bbuf \ getopt diff --git a/lib/patimatch.c b/lib/patimatch.c deleted file mode 100644 index ea5a4be9..00000000 --- a/lib/patimatch.c +++ /dev/null @@ -1,16 +0,0 @@ -/* - * UCW Library -- Shell-Like Case-Insensitive Pattern Matching (currently only '?' and '*') - * - * (c) 1997 Martin Mares - * - * This software may be freely distributed and used according to the terms - * of the GNU Lesser General Public License. - */ - -#include "lib/lib.h" -#include "lib/chartype.h" - -#define Convert(x) Cupcase(x) -#define MATCH_FUNC_NAME str_match_pattern_nocase - -#include "lib/patmatch.h" diff --git a/lib/patmatch.c b/lib/patmatch.c deleted file mode 100644 index 373054ad..00000000 --- a/lib/patmatch.c +++ /dev/null @@ -1,15 +0,0 @@ -/* - * UCW Library -- Shell-Like Pattern Matching (currently only '?' and '*') - * - * (c) 1997 Martin Mares - * - * This software may be freely distributed and used according to the terms - * of the GNU Lesser General Public License. - */ - -#include "lib/lib.h" - -#define Convert(x) (x) -#define MATCH_FUNC_NAME str_match_pattern - -#include "lib/patmatch.h" diff --git a/lib/patmatch.h b/lib/patmatch.h deleted file mode 100644 index ef8508a1..00000000 --- a/lib/patmatch.h +++ /dev/null @@ -1,48 +0,0 @@ -/* - * UCW Library -- Generic Shell-Like Pattern Matching (currently only '?' and '*') - * - * (c) 1997 Martin Mares - * - * This software may be freely distributed and used according to the terms - * of the GNU Lesser General Public License. - */ - -#include "lib/string.h" - -int -MATCH_FUNC_NAME(const char *p, const char *s) -{ - while (*p) - { - if (*p == '?' && *s) - p++, s++; - else if (*p == '*') - { - int z = p[1]; - - if (!z) - return 1; - if (z == '\\' && p[2]) - z = p[2]; - z = Convert(z); - for(;;) - { - while (*s && Convert(*s) != z) - s++; - if (!*s) - return 0; - if (MATCH_FUNC_NAME(p+1, s)) - return 1; - s++; - } - } - else - { - if (*p == '\\' && p[1]) - p++; - if (Convert(*p++) != Convert(*s++)) - return 0; - } - } - return !*s; -} diff --git a/lib/str-imatch.c b/lib/str-imatch.c new file mode 100644 index 00000000..9d739efd --- /dev/null +++ b/lib/str-imatch.c @@ -0,0 +1,16 @@ +/* + * UCW Library -- Shell-Like Case-Insensitive Pattern Matching (currently only '?' and '*') + * + * (c) 1997 Martin Mares + * + * This software may be freely distributed and used according to the terms + * of the GNU Lesser General Public License. + */ + +#include "lib/lib.h" +#include "lib/chartype.h" + +#define Convert(x) Cupcase(x) +#define MATCH_FUNC_NAME str_match_pattern_nocase + +#include "lib/str-match.h" diff --git a/lib/str-match.c b/lib/str-match.c new file mode 100644 index 00000000..839da6ca --- /dev/null +++ b/lib/str-match.c @@ -0,0 +1,15 @@ +/* + * UCW Library -- Shell-Like Pattern Matching (currently only '?' and '*') + * + * (c) 1997 Martin Mares + * + * This software may be freely distributed and used according to the terms + * of the GNU Lesser General Public License. + */ + +#include "lib/lib.h" + +#define Convert(x) (x) +#define MATCH_FUNC_NAME str_match_pattern + +#include "lib/str-match.h" diff --git a/lib/str-match.h b/lib/str-match.h new file mode 100644 index 00000000..ef8508a1 --- /dev/null +++ b/lib/str-match.h @@ -0,0 +1,48 @@ +/* + * UCW Library -- Generic Shell-Like Pattern Matching (currently only '?' and '*') + * + * (c) 1997 Martin Mares + * + * This software may be freely distributed and used according to the terms + * of the GNU Lesser General Public License. + */ + +#include "lib/string.h" + +int +MATCH_FUNC_NAME(const char *p, const char *s) +{ + while (*p) + { + if (*p == '?' && *s) + p++, s++; + else if (*p == '*') + { + int z = p[1]; + + if (!z) + return 1; + if (z == '\\' && p[2]) + z = p[2]; + z = Convert(z); + for(;;) + { + while (*s && Convert(*s) != z) + s++; + if (!*s) + return 0; + if (MATCH_FUNC_NAME(p+1, s)) + return 1; + s++; + } + } + else + { + if (*p == '\\' && p[1]) + p++; + if (Convert(*p++) != Convert(*s++)) + return 0; + } + } + return !*s; +} diff --git a/lib/str-split.c b/lib/str-split.c new file mode 100644 index 00000000..97ce242a --- /dev/null +++ b/lib/str-split.c @@ -0,0 +1,63 @@ +/* + * UCW Library -- Word Splitting + * + * (c) 1997 Martin Mares + * (c) 2004 Robert Spalek + * + * This software may be freely distributed and used according to the terms + * of the GNU Lesser General Public License. + */ + +#include "lib/lib.h" +#include "lib/chartype.h" +#include "lib/string.h" + +#include + +int +str_sepsplit(char *str, uns sep, char **rec, uns max) +{ + uns cnt = 0; + while (1) + { + rec[cnt++] = str; + str = strchr(str, sep); + if (!str) + return cnt; + if (cnt >= max) + return -1; + *str++ = 0; + } +} + +int +str_wordsplit(char *src, char **dst, uns max) +{ + uns cnt = 0; + + for(;;) + { + while (Cspace(*src)) + *src++ = 0; + if (!*src) + break; + if (cnt >= max) + return -1; + if (*src == '"') + { + src++; + dst[cnt++] = src; + while (*src && *src != '"') + src++; + if (*src) + *src++ = 0; + } + else + { + dst[cnt++] = src; + while (*src && !Cspace(*src)) + src++; + } + } + return cnt; +} diff --git a/lib/string.h b/lib/string.h index a9de082f..50c4dc47 100644 --- a/lib/string.h +++ b/lib/string.h @@ -11,15 +11,20 @@ #ifndef _UCW_STRING_H #define _UCW_STRING_H -char *str_unesc(char *dest, const char *src); +/* string.c */ + char *str_format_flags(char *dest, const char *fmt, uns flags); -/* wordsplit.c */ +/* str-esc.c */ + +char *str_unesc(char *dest, const char *src); + +/* str-split.c */ int str_sepsplit(char *str, uns sep, char **rec, uns max); int str_wordsplit(char *str, char **rec, uns max); -/* pat(i)match.c: Matching of shell patterns */ +/* str-(i)match.c: Matching of shell patterns */ int str_match_pattern(const char *patt, const char *str); int str_match_pattern_nocase(const char *patt, const char *str); diff --git a/lib/wordsplit.c b/lib/wordsplit.c deleted file mode 100644 index 97ce242a..00000000 --- a/lib/wordsplit.c +++ /dev/null @@ -1,63 +0,0 @@ -/* - * UCW Library -- Word Splitting - * - * (c) 1997 Martin Mares - * (c) 2004 Robert Spalek - * - * This software may be freely distributed and used according to the terms - * of the GNU Lesser General Public License. - */ - -#include "lib/lib.h" -#include "lib/chartype.h" -#include "lib/string.h" - -#include - -int -str_sepsplit(char *str, uns sep, char **rec, uns max) -{ - uns cnt = 0; - while (1) - { - rec[cnt++] = str; - str = strchr(str, sep); - if (!str) - return cnt; - if (cnt >= max) - return -1; - *str++ = 0; - } -} - -int -str_wordsplit(char *src, char **dst, uns max) -{ - uns cnt = 0; - - for(;;) - { - while (Cspace(*src)) - *src++ = 0; - if (!*src) - break; - if (cnt >= max) - return -1; - if (*src == '"') - { - src++; - dst[cnt++] = src; - while (*src && *src != '"') - src++; - if (*src) - *src++ = 0; - } - else - { - dst[cnt++] = src; - while (*src && !Cspace(*src)) - src++; - } - } - return cnt; -}