]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/commitdiff
strstarts: helper function for !strncmp(str, prefix, strlen(prefix))
authorRusty Russell <rusty@rustcorp.com.au>
Tue, 31 Mar 2009 19:05:36 +0000 (13:05 -0600)
committerRusty Russell <rusty@rustcorp.com.au>
Tue, 31 Mar 2009 02:35:36 +0000 (13:05 +1030)
Impact: minor new API

ksplice added a "starts_with" function, which seems like a common need.
When people open-code it they seem to use fixed numbers rather than strlen,
so it's quite a readability win (also, strncmp() almost always wants != 0
on it).

So here's strstarts().

Cc: Anders Kaseorg <andersk@mit.edu>
Cc: Jeff Arnold <jbarnold@mit.edu>
Cc: Tim Abbott <tabbott@mit.edu>
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
include/linux/string.h

index d18fc198aa2fd3c337e821e78eedc97e5ea8459b..76ec218bb30f090781ee9b74d27ac290d5c2b32e 100644 (file)
@@ -114,5 +114,14 @@ extern bool sysfs_streq(const char *s1, const char *s2);
 extern ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos,
                        const void *from, size_t available);
 
+/**
+ * strstarts - does @str start with @prefix?
+ * @str: string to examine
+ * @prefix: prefix to look for.
+ */
+static inline bool strstarts(const char *str, const char *prefix)
+{
+       return strncmp(str, prefix, strlen(prefix)) == 0;
+}
 #endif
 #endif /* _LINUX_STRING_H_ */