]> git.saurik.com Git - apple/system_cmds.git/blob - CPPUtil/UtilString.hpp
85801633b7ba6532fa2b9eef6d4afa99f2c74820
[apple/system_cmds.git] / CPPUtil / UtilString.hpp
1 //
2 // UtilString.hpp
3 // CPPUtil
4 //
5 // Created by James McIlree on 4/16/13.
6 // Copyright (c) 2013 Apple. All rights reserved.
7 //
8
9 #ifndef CPPUtil_UtilString_hpp
10 #define CPPUtil_UtilString_hpp
11
12 struct ConstCharHash {
13 //
14 // Okay, by design std::hash<char*> hashes on the pointer,
15 // not the contents of that pointer.
16 //
17 // The C++11 std::hash<std::string> hash works, but must
18 // construct a copy of the passed in string to hash.
19 //
20 // That's 3x slower than this, minimum.
21 //
22 // This is just the __gnu_cxx hash code inlined.
23 //
24 std::size_t operator()(const char* __s) const {
25 unsigned long __h = 0;
26 for ( ; *__s; ++__s)
27 __h = 5 * __h + *__s;
28 return size_t(__h);
29 };
30
31 };
32
33 struct ConstCharEqualTo {
34 bool operator() (const char* s1, const char* s2) const {
35 return strcmp(s1, s2) == 0;
36 }
37 };
38
39 bool ends_with(std::string& str, std::string postfix);
40
41 #endif