OLD | NEW |
| (Empty) |
1 // Copyright 2009 The RE2 Authors. All Rights Reserved. | |
2 // Use of this source code is governed by a BSD-style | |
3 // license that can be found in the LICENSE file. | |
4 | |
5 #ifndef RE2_UTIL_UTIL_H__ | |
6 #define RE2_UTIL_UTIL_H__ | |
7 | |
8 // C | |
9 #include <stdio.h> | |
10 #include <string.h> | |
11 #include <stdint.h> | |
12 #include <stddef.h> // For size_t | |
13 #include <assert.h> | |
14 #include <stdarg.h> | |
15 #include <time.h> // For clock_gettime, CLOCK_REALTIME | |
16 #include <ctype.h> // For isdigit, isalpha | |
17 | |
18 #if !defined(_WIN32) | |
19 #include <sys/time.h> // For gettimeofday | |
20 #endif | |
21 | |
22 // C++ | |
23 #include <ctime> | |
24 #include <vector> | |
25 #include <string> | |
26 #include <algorithm> | |
27 #include <iosfwd> | |
28 #include <map> | |
29 #include <stack> | |
30 #include <ostream> | |
31 #include <utility> | |
32 #include <set> | |
33 | |
34 // Use std names. | |
35 using std::set; | |
36 using std::pair; | |
37 using std::vector; | |
38 using std::string; | |
39 using std::min; | |
40 using std::max; | |
41 using std::ostream; | |
42 using std::map; | |
43 using std::stack; | |
44 using std::sort; | |
45 using std::swap; | |
46 using std::make_pair; | |
47 | |
48 #if defined(__GNUC__) && !defined(USE_CXX0X) && !defined(_LIBCPP_ABI_VERSION) | |
49 | |
50 #include <tr1/unordered_set> | |
51 using std::tr1::unordered_set; | |
52 | |
53 #else | |
54 | |
55 #include <unordered_set> | |
56 #if defined(_WIN32) | |
57 using std::tr1::unordered_set; | |
58 #else | |
59 using std::unordered_set; | |
60 #endif | |
61 | |
62 #endif | |
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 | |
75 namespace re2 { | |
76 | |
77 typedef int8_t int8; | |
78 typedef uint8_t uint8; | |
79 typedef int16_t int16; | |
80 typedef uint16_t uint16; | |
81 typedef int32_t int32; | |
82 typedef uint32_t uint32; | |
83 typedef int64_t int64; | |
84 typedef uint64_t uint64; | |
85 | |
86 typedef unsigned long ulong; | |
87 typedef unsigned int uint; | |
88 typedef unsigned short ushort; | |
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 | |
99 // COMPILE_ASSERT causes a compile error about msg if expr is not true. | |
100 #if __cplusplus >= 201103L | |
101 #define COMPILE_ASSERT(expr, msg) static_assert(expr, #msg) | |
102 #else | |
103 template<bool> struct CompileAssert {}; | |
104 #define COMPILE_ASSERT(expr, msg) \ | |
105 typedef CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1] ATTRIBUTE_UNUSED | |
106 #endif | |
107 | |
108 // DISALLOW_COPY_AND_ASSIGN disallows the copy and operator= functions. | |
109 // It goes in the private: declarations in a class. | |
110 #define DISALLOW_COPY_AND_ASSIGN(TypeName) \ | |
111 TypeName(const TypeName&); \ | |
112 void operator=(const TypeName&) | |
113 | |
114 #define arraysize(array) (int)(sizeof(array)/sizeof((array)[0])) | |
115 | |
116 class StringPiece; | |
117 | |
118 string CEscape(const StringPiece& src); | |
119 int CEscapeString(const char* src, int src_len, char* dest, int dest_len); | |
120 | |
121 extern string StringPrintf(const char* format, ...); | |
122 extern void SStringPrintf(string* dst, const char* format, ...); | |
123 extern void StringAppendF(string* dst, const char* format, ...); | |
124 extern string PrefixSuccessor(const StringPiece& prefix); | |
125 | |
126 uint32 hashword(const uint32*, size_t, uint32); | |
127 void hashword2(const uint32*, size_t, uint32*, uint32*); | |
128 | |
129 static inline uint32 Hash32StringWithSeed(const char* s, int len, uint32 seed) { | |
130 return hashword((uint32*)s, len/4, seed); | |
131 } | |
132 | |
133 static inline uint64 Hash64StringWithSeed(const char* s, int len, uint32 seed) { | |
134 uint32 x, y; | |
135 x = seed; | |
136 y = 0; | |
137 hashword2((uint32*)s, len/4, &x, &y); | |
138 return ((uint64)x << 32) | y; | |
139 } | |
140 | |
141 bool RunningOnValgrind(); | |
142 | |
143 } // namespace re2 | |
144 | |
145 #include "util/logging.h" | |
146 #include "util/mutex.h" | |
147 #include "util/utf.h" | |
148 | |
149 #endif // RE2_UTIL_UTIL_H__ | |
OLD | NEW |