str: add an overloaded startswith() utility method
[gem5.git] / src / base / str.hh
index 08953caf3e41943bb576172b9a0eba18f23d93e9..b3f3153ec1713f8b55f919cabd5fcdb68c117ab4 100644 (file)
 #ifndef __STR_HH__
 #define __STR_HH__
 
+#include <cctype>
+#include <cstring>
 #include <sstream>
 #include <string>
 #include <vector>
 
-#include <ctype.h>
-
 template<class> class Hash;
 template<>
 class Hash<std::string> {
@@ -141,4 +141,35 @@ quote(const std::string &s)
     return ret;
 }
 
+
+/**
+ * Return true if 's' starts with the prefix string 'prefix'.
+ */
+inline bool
+startswith(const char *s, const char *prefix)
+{
+    return (strncmp(s, prefix, strlen(prefix)) == 0);
+}
+
+
+/**
+ * Return true if 's' starts with the prefix string 'prefix'.
+ */
+inline bool
+startswith(const std::string &s, const char *prefix)
+{
+    return (s.compare(0, strlen(prefix), prefix) == 0);
+}
+
+
+/**
+ * Return true if 's' starts with the prefix string 'prefix'.
+ */
+inline bool
+startswith(const std::string &s, const std::string &prefix)
+{
+    return (s.compare(0, prefix.size(), prefix) == 0);
+}
+
+
 #endif //__STR_HH__