]> diplodocus.org Git - nmh/commitdiff
Added find_str() and rfind_str() to sbr/utils.c
authorDavid Levine <levinedl@acm.org>
Mon, 25 Feb 2013 03:55:28 +0000 (21:55 -0600)
committerDavid Levine <levinedl@acm.org>
Mon, 25 Feb 2013 03:55:28 +0000 (21:55 -0600)
h/utils.h
sbr/utils.c

index 25bbeee96373ee3e05a7e271d0b324396b0da2c5..7a096a18fbe45c4d4c8521078fe9709bda1f4ecf 100644 (file)
--- a/h/utils.h
+++ b/h/utils.h
@@ -19,3 +19,5 @@ struct msgs_array {
 
 void app_msgarg(struct msgs_array *, char *);
 int open_form(char **, char *);
+char *find_str (const char [], size_t, const char *);
+char *rfind_str (const char [], size_t, const char *);
index 5bfc649e31d7ab1999ca4e46181e68f9f3e84ad2..87fedd584946bad98974f1b0cbdeecc204421b90 100644 (file)
@@ -241,3 +241,41 @@ open_form(char **form, char *def)
        }
        return in;
 }
+
+
+/*
+ * Finds first occurrence of str in buf.  buf is not a C string but a
+ * byte array of length buflen.  str is a null-terminated C string.
+ * find_str() does not modify buf but passes back a non-const char *
+ * pointer so that the caller can modify it.
+ */
+char *
+find_str (const char buf[], size_t buflen, const char *str) {
+    const size_t len = strlen (str);
+    size_t i;
+
+    for (i = 0; i + len <= buflen; ++i, ++buf) {
+        if (! memcmp (buf, str, len)) return (char *) buf;
+    }
+
+    return NULL;
+}
+
+
+/*
+ * Finds last occurrence of str in buf.  buf is not a C string but a
+ * byte array of length buflen.  str is a null-terminated C string.
+ * find_str() does not modify buf but passes back a non-const char *
+ * pointer so that the caller can modify it.
+ */
+char *
+rfind_str (const char buf[], size_t buflen, const char *str) {
+    const size_t len = strlen (str);
+    size_t i;
+
+    for (i = 0, buf += buflen - len; i + len <= buflen; ++i, --buf) {
+        if (! memcmp (buf, str, len)) return (char *) buf;
+    }
+
+    return NULL;
+}