diff options
author | Josh Stone <jistone@redhat.com> | 2010-03-11 19:19:33 -0800 |
---|---|---|
committer | Josh Stone <jistone@redhat.com> | 2010-03-11 19:19:33 -0800 |
commit | 60d985370e63cb3b4c68489fc54276d8ac2b921b (patch) | |
tree | 2731ffe47f8e619b6fabcacf451275d49a3486fd /util.h | |
parent | 0da46fcdec144f944838350f08f59a36b8709e90 (diff) | |
download | systemtap-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.h | 20 |
1 files changed, 20 insertions, 0 deletions
@@ -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 : */ |