OLD | NEW |
1 // Copyright 2009 The RE2 Authors. All Rights Reserved. | 1 // Copyright 2009 The RE2 Authors. All Rights Reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 #ifndef RE2_UTIL_UTIL_H__ | 5 #ifndef RE2_UTIL_UTIL_H__ |
6 #define RE2_UTIL_UTIL_H__ | 6 #define RE2_UTIL_UTIL_H__ |
7 | 7 |
8 // C | 8 // C |
9 #include <stdio.h> | 9 #include <stdio.h> |
10 #include <string.h> | 10 #include <string.h> |
11 #include <stdint.h> | 11 #include <stdint.h> |
12 #include <stddef.h> // For size_t | 12 #include <stddef.h> // For size_t |
13 #include <assert.h> | 13 #include <assert.h> |
14 #include <stdarg.h> | 14 #include <stdarg.h> |
15 #ifndef WIN32 | 15 #include <time.h> // For clock_gettime, CLOCK_REALTIME |
16 #include <sys/time.h> | 16 #include <ctype.h> // For isdigit, isalpha |
| 17 |
| 18 #if !defined(_WIN32) |
| 19 #include <sys/time.h> // For gettimeofday |
17 #endif | 20 #endif |
18 #include <time.h> | |
19 #include <ctype.h> // For isdigit, isalpha. | |
20 | 21 |
21 // C++ | 22 // C++ |
| 23 #include <ctime> |
22 #include <vector> | 24 #include <vector> |
23 #include <string> | 25 #include <string> |
24 #include <algorithm> | 26 #include <algorithm> |
25 #include <iosfwd> | 27 #include <iosfwd> |
26 #include <map> | 28 #include <map> |
27 #include <stack> | 29 #include <stack> |
28 #include <ostream> | 30 #include <ostream> |
29 #include <utility> | 31 #include <utility> |
30 #include <set> | 32 #include <set> |
31 | 33 |
32 #include "build/build_config.h" | |
33 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | |
34 | |
35 // Use std names. | 34 // Use std names. |
36 using std::set; | 35 using std::set; |
37 using std::pair; | 36 using std::pair; |
38 using std::vector; | 37 using std::vector; |
39 using std::string; | 38 using std::string; |
40 using std::min; | 39 using std::min; |
41 using std::max; | 40 using std::max; |
42 using std::ostream; | 41 using std::ostream; |
43 using std::map; | 42 using std::map; |
44 using std::stack; | 43 using std::stack; |
45 using std::sort; | 44 using std::sort; |
46 using std::swap; | 45 using std::swap; |
47 using std::make_pair; | 46 using std::make_pair; |
48 | 47 |
49 #if defined(__GNUC__) && !defined(USE_CXX0X) && !defined(_LIBCPP_ABI_VERSION) &&
!defined(OS_ANDROID) | 48 #if defined(__GNUC__) && !defined(USE_CXX0X) && !defined(_LIBCPP_ABI_VERSION) |
50 | 49 |
51 #include <tr1/unordered_set> | 50 #include <tr1/unordered_set> |
52 using std::tr1::unordered_set; | 51 using std::tr1::unordered_set; |
53 | 52 |
54 #else | 53 #else |
55 | 54 |
56 #include <unordered_set> | 55 #include <unordered_set> |
57 #if defined(WIN32) || (defined(OS_ANDROID) && !defined(_LIBCPP_ABI_VERSION)) | 56 #if defined(_WIN32) |
58 using std::tr1::unordered_set; | 57 using std::tr1::unordered_set; |
59 #else | 58 #else |
60 using std::unordered_set; | 59 using std::unordered_set; |
61 #endif | 60 #endif |
62 | 61 |
63 #endif | 62 #endif |
64 | 63 |
| 64 #ifdef _WIN32 |
| 65 |
| 66 #define snprintf _snprintf_s |
| 67 #define stricmp _stricmp |
| 68 #define strtof strtod /* not really correct but best we can do */ |
| 69 #define strtoll _strtoi64 |
| 70 #define strtoull _strtoui64 |
| 71 #define vsnprintf vsnprintf_s |
| 72 |
| 73 #endif |
| 74 |
65 namespace re2 { | 75 namespace re2 { |
66 | 76 |
67 typedef int8_t int8; | 77 typedef int8_t int8; |
68 typedef uint8_t uint8; | 78 typedef uint8_t uint8; |
69 typedef int16_t int16; | 79 typedef int16_t int16; |
70 typedef uint16_t uint16; | 80 typedef uint16_t uint16; |
71 typedef int32_t int32; | 81 typedef int32_t int32; |
72 typedef uint32_t uint32; | 82 typedef uint32_t uint32; |
73 typedef int64_t int64; | 83 typedef int64_t int64; |
74 typedef uint64_t uint64; | 84 typedef uint64_t uint64; |
75 | 85 |
76 typedef unsigned long ulong; | 86 typedef unsigned long ulong; |
77 typedef unsigned int uint; | 87 typedef unsigned int uint; |
78 typedef unsigned short ushort; | 88 typedef unsigned short ushort; |
79 | 89 |
| 90 // Prevent the compiler from complaining about or optimizing away variables |
| 91 // that appear unused. |
| 92 #undef ATTRIBUTE_UNUSED |
| 93 #if defined(__GNUC__) |
| 94 #define ATTRIBUTE_UNUSED __attribute__ ((unused)) |
| 95 #else |
| 96 #define ATTRIBUTE_UNUSED |
| 97 #endif |
| 98 |
80 // COMPILE_ASSERT causes a compile error about msg if expr is not true. | 99 // COMPILE_ASSERT causes a compile error about msg if expr is not true. |
81 #if __cplusplus >= 201103L | 100 #if __cplusplus >= 201103L |
82 #define COMPILE_ASSERT(expr, msg) static_assert(expr, #msg) | 101 #define COMPILE_ASSERT(expr, msg) static_assert(expr, #msg) |
83 #else | 102 #else |
84 template<bool> struct CompileAssert {}; | 103 template<bool> struct CompileAssert {}; |
85 #define COMPILE_ASSERT(expr, msg) \ | 104 #define COMPILE_ASSERT(expr, msg) \ |
86 typedef CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1] | 105 typedef CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1] ATTRIBUTE_UNUSED |
87 #endif | 106 #endif |
88 | 107 |
89 // DISALLOW_EVIL_CONSTRUCTORS disallows the copy and operator= functions. | 108 // DISALLOW_COPY_AND_ASSIGN disallows the copy and operator= functions. |
90 // It goes in the private: declarations in a class. | 109 // It goes in the private: declarations in a class. |
91 #define DISALLOW_EVIL_CONSTRUCTORS(TypeName) \ | 110 #define DISALLOW_COPY_AND_ASSIGN(TypeName) \ |
92 TypeName(const TypeName&); \ | 111 TypeName(const TypeName&); \ |
93 void operator=(const TypeName&) | 112 void operator=(const TypeName&) |
94 | 113 |
95 #define arraysize(array) (sizeof(array)/sizeof((array)[0])) | 114 #define arraysize(array) (int)(sizeof(array)/sizeof((array)[0])) |
96 | |
97 // Fake lock annotations. For real ones, see | |
98 // http://code.google.com/p/data-race-test/ | |
99 #ifndef ANNOTATE_PUBLISH_MEMORY_RANGE | |
100 #define ANNOTATE_PUBLISH_MEMORY_RANGE(a, b) | |
101 #define ANNOTATE_IGNORE_WRITES_BEGIN() | |
102 #define ANNOTATE_IGNORE_WRITES_END() | |
103 #define ANNOTATE_BENIGN_RACE(a, b) | |
104 #define NO_THREAD_SAFETY_ANALYSIS | |
105 #define ANNOTATE_HAPPENS_BEFORE(x) | |
106 #define ANNOTATE_HAPPENS_AFTER(x) | |
107 #define ANNOTATE_UNPROTECTED_READ(x) (x) | |
108 #endif | |
109 | 115 |
110 class StringPiece; | 116 class StringPiece; |
111 | 117 |
112 string CEscape(const StringPiece& src); | 118 string CEscape(const StringPiece& src); |
113 int CEscapeString(const char* src, int src_len, char* dest, int dest_len); | 119 int CEscapeString(const char* src, int src_len, char* dest, int dest_len); |
114 | 120 |
115 extern string StringPrintf(const char* format, ...); | 121 extern string StringPrintf(const char* format, ...); |
116 extern void SStringPrintf(string* dst, const char* format, ...); | 122 extern void SStringPrintf(string* dst, const char* format, ...); |
117 extern void StringAppendF(string* dst, const char* format, ...); | 123 extern void StringAppendF(string* dst, const char* format, ...); |
118 extern string PrefixSuccessor(const StringPiece& prefix); | 124 extern string PrefixSuccessor(const StringPiece& prefix); |
119 | 125 |
120 uint32 hashword(const uint32*, size_t, uint32); | 126 uint32 hashword(const uint32*, size_t, uint32); |
121 void hashword2(const uint32*, size_t, uint32*, uint32*); | 127 void hashword2(const uint32*, size_t, uint32*, uint32*); |
122 | 128 |
123 static inline uint32 Hash32StringWithSeed(const char* s, int len, uint32 seed) { | 129 static inline uint32 Hash32StringWithSeed(const char* s, int len, uint32 seed) { |
124 return hashword((uint32*)s, len/4, seed); | 130 return hashword((uint32*)s, len/4, seed); |
125 } | 131 } |
126 | 132 |
127 static inline uint64 Hash64StringWithSeed(const char* s, int len, uint32 seed) { | 133 static inline uint64 Hash64StringWithSeed(const char* s, int len, uint32 seed) { |
128 uint32 x, y; | 134 uint32 x, y; |
129 x = seed; | 135 x = seed; |
130 y = 0; | 136 y = 0; |
131 hashword2((uint32*)s, len/4, &x, &y); | 137 hashword2((uint32*)s, len/4, &x, &y); |
132 return ((uint64)x << 32) | y; | 138 return ((uint64)x << 32) | y; |
133 } | 139 } |
134 | 140 |
135 inline bool RunningOnValgrindOrMemorySanitizer() { | 141 bool RunningOnValgrind(); |
136 #if defined(MEMORY_SANITIZER) | |
137 return true; | |
138 #else | |
139 return RunningOnValgrind(); | |
140 #endif | |
141 } | |
142 | 142 |
143 } // namespace re2 | 143 } // namespace re2 |
144 | 144 |
145 #include "util/arena.h" | |
146 #include "util/logging.h" | 145 #include "util/logging.h" |
147 #include "util/mutex.h" | 146 #include "util/mutex.h" |
148 #include "util/utf.h" | 147 #include "util/utf.h" |
149 | 148 |
150 #endif // RE2_UTIL_UTIL_H__ | 149 #endif // RE2_UTIL_UTIL_H__ |
OLD | NEW |