diff options
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 : */ |