summaryrefslogtreecommitdiffstats
path: root/util.h
diff options
context:
space:
mode:
authorJosh Stone <jistone@redhat.com>2010-03-11 19:19:33 -0800
committerJosh Stone <jistone@redhat.com>2010-03-11 19:19:33 -0800
commit60d985370e63cb3b4c68489fc54276d8ac2b921b (patch)
tree2731ffe47f8e619b6fabcacf451275d49a3486fd /util.h
parent0da46fcdec144f944838350f08f59a36b8709e90 (diff)
downloadsystemtap-steved-60d985370e63cb3b4c68489fc54276d8ac2b921b.tar.gz
systemtap-steved-60d985370e63cb3b4c68489fc54276d8ac2b921b.tar.xz
systemtap-steved-60d985370e63cb3b4c68489fc54276d8ac2b921b.zip
Add startswith/endswith helpers
Inspired by the Python equivalents, these new utility functions just make it a little cleaner to match at the beginning or end of a string.
Diffstat (limited to 'util.h')
-rw-r--r--util.h20
1 files changed, 20 insertions, 0 deletions
diff --git a/util.h b/util.h
index 75e198ca..c6cb4554 100644
--- a/util.h
+++ b/util.h
@@ -1,3 +1,4 @@
+#include <cstring>
#include <string>
#include <vector>
#include <iostream>
@@ -112,4 +113,23 @@ void delete_map(T& t)
}
+// Returns whether a string starts with the given prefix
+inline bool
+startswith(const std::string & s, const char * prefix)
+{
+ return (s.compare(0, std::strlen(prefix), prefix) == 0);
+}
+
+
+// Returns whether a string ends with the given suffix
+inline bool
+endswith(const std::string & s, const char * suffix)
+{
+ size_t s_len = s.size(), suffix_len = std::strlen(suffix);
+ if (suffix_len > s_len)
+ return false;
+ return (s.compare(s_len - suffix_len, suffix_len, suffix) == 0);
+}
+
+
/* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */