chiark / gitweb /
align: Add trivial manpage.
[mLib] / str.c
diff --git a/str.c b/str.c
index 711ccf789f68b6c88e917a8f1b89bbb2f7847a9f..8e3f43ab1eb5a12d29e44d27deebdf4f0d2b0f12 100644 (file)
--- a/str.c
+++ b/str.c
@@ -1,13 +1,13 @@
 /* -*-c-*-
  *
- * $Id: str.c,v 1.4 2000/10/08 09:43:34 mdw Exp $
+ * $Id: str.c,v 1.6 2004/04/08 01:36:13 mdw Exp $
  *
  * Functions for hacking with strings
  *
  * (c) 1999 Straylight/Edgeware
  */
 
-/*----- Licensing notice --------------------------------------------------* 
+/*----- Licensing notice --------------------------------------------------*
  *
  * This file is part of the mLib utilities library.
  *
  * it under the terms of the GNU Library General Public License as
  * published by the Free Software Foundation; either version 2 of the
  * License, or (at your option) any later version.
- * 
+ *
  * mLib is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU Library General Public License for more details.
- * 
+ *
  * You should have received a copy of the GNU Library General Public
  * License along with mLib; if not, write to the Free
  * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  * MA 02111-1307, USA.
  */
 
-/*----- Revision history --------------------------------------------------* 
- *
- * $Log: str.c,v $
- * Revision 1.4  2000/10/08 09:43:34  mdw
- * New quoted string handling and simple pattern matching.
- *
- * Revision 1.3  1999/12/22 15:41:14  mdw
- * Skip past trailing whitespace in str_getword.
- *
- * Revision 1.2  1999/05/26 20:52:57  mdw
- * Add new `rest' argument for `str_split'.
- *
- * Revision 1.1  1999/05/17 20:37:01  mdw
- * Some trivial string hacks.
- *
- */
-
 /*----- Header files ------------------------------------------------------*/
 
 #include <ctype.h>
@@ -68,8 +51,6 @@
  *             single and double quotes protect whitespace.
  */
 
-#define STRF_QUOTE 1u
-
 char *str_qword(char **pp, unsigned f)
 {
   char *p = *pp, *q, *qq;
@@ -184,10 +165,7 @@ size_t str_qsplit(char *p, char *v[], size_t c, char **rest, unsigned f)
  *             compatibility veneer over @str_qword@.
  */
 
-char *str_getword(char **pp)
-{
-  return (str_qword(pp, 0));
-}
+char *str_getword(char **pp) { return (str_qword(pp, 0)); }
 
 /* --- @str_split@ --- *
  *
@@ -203,14 +181,13 @@ char *str_getword(char **pp)
  */
 
 size_t str_split(char *p, char *v[], size_t c, char **rest)
-{
-  return (str_qsplit(p, v, c, rest, 0));
-}
+  { return (str_qsplit(p, v, c, rest, 0)); }
 
-/* --- @str_match@ --- *
+/* --- @str_matchx@ --- *
  *
  * Arguments:  @const char *p@ = pointer to pattern string
  *             @const char *s@ = string to compare with
+ *             @unsigned f@ = various flags
  *
  * Returns:    Nonzero if the pattern matches the string.
  *
@@ -219,12 +196,14 @@ size_t str_split(char *p, char *v[], size_t c, char **rest)
  *             '['.
  */
 
-int str_match(const char *p, const char *s)
+int str_matchx(const char *p, const char *s, unsigned f)
 {
   for (;;) {
     char pch = *p++, pche, sch;
     int sense;
 
+    if ((f & STRF_PREFIX) && !*s)
+      return (1);
     switch (pch) {
       case '?':
        if (!*s)
@@ -232,7 +211,7 @@ int str_match(const char *p, const char *s)
        s++;
        break;
       case '*':
-       if (!*p)
+       if (!*p || (f & STRF_PREFIX))
          return (1);
        while (*s) {
          if (str_match(p, s))
@@ -301,6 +280,20 @@ int str_match(const char *p, const char *s)
   }
 }
 
+/* --- @str_match@ --- *
+ *
+ * Arguments:  @const char *p@ = pointer to pattern string
+ *             @const char *s@ = string to compare with
+ *
+ * Returns:    Nonzero if the pattern matches the string.
+ *
+ * Use:                Does simple wildcard matching.  Equivalent to @str_matchx@
+ *             with zero flags word.
+ */
+
+int str_match(const char *p, const char *s)
+  { return (str_matchx(p, s, 0)); }
+
 /* --- @str_sanitize@ --- *
  *
  * Arguments:  @char *d@ = destination buffer