]> git.saurik.com Git - apple/system_cmds.git/blobdiff - CPPUtil/UtilString.hpp
system_cmds-643.30.1.tar.gz
[apple/system_cmds.git] / CPPUtil / UtilString.hpp
diff --git a/CPPUtil/UtilString.hpp b/CPPUtil/UtilString.hpp
new file mode 100644 (file)
index 0000000..8580163
--- /dev/null
@@ -0,0 +1,41 @@
+//
+//  UtilString.hpp
+//  CPPUtil
+//
+//  Created by James McIlree on 4/16/13.
+//  Copyright (c) 2013 Apple. All rights reserved.
+//
+
+#ifndef CPPUtil_UtilString_hpp
+#define CPPUtil_UtilString_hpp
+
+struct ConstCharHash {
+       //
+       // Okay, by design std::hash<char*> hashes on the pointer,
+       // not the contents of that pointer.
+       //
+       // The C++11 std::hash<std::string> hash works, but must
+       // construct a copy of the passed in string to hash.
+       //
+       // That's 3x slower than this, minimum.
+       //
+       // This is just the __gnu_cxx hash code inlined.
+       //
+       std::size_t operator()(const char* __s) const {
+               unsigned long __h = 0;
+               for ( ; *__s; ++__s)
+                       __h = 5 * __h + *__s;
+               return size_t(__h);
+       };
+
+};
+
+struct ConstCharEqualTo {
+       bool operator() (const char* s1, const char* s2) const {
+               return strcmp(s1, s2) == 0;
+       }
+};
+
+bool ends_with(std::string& str, std::string postfix);
+
+#endif