chiark / gitweb /
buf: Fix two embarassing bugs found while writing Lisp bindings.
[mLib] / str.c
diff --git a/str.c b/str.c
index 6423a4f79ce570abfce8556fc671b2e685b4df54..e30682d57b639e418217b15c19a44a3ca7b09703 100644 (file)
--- a/str.c
+++ b/str.c
@@ -165,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@ --- *
  *
@@ -184,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.
  *
@@ -200,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)
@@ -213,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))
@@ -282,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