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 <sys/time.h>
16 #include <time.h>
17
18 // C++
19 #include <vector>
20 #include <string>
21 #include <algorithm>
22 #include <iosfwd>
23 #include <map>
24 #include <stack>
25 #include <iostream>
26 #include <utility>
27 #include <set>
28
29 // Use std names.
30 using std::set;
31 using std::pair;
32 using std::vector;
33 using std::string;
34 using std::min;
35 using std::max;
36 using std::ostream;
37 using std::map;
38 using std::stack;
39 using std::sort;
40 using std::swap;
41 using std::make_pair;
42
43 #if defined(ANDROID)
44
45 #include <unordered_set>
46 using std::tr1::unordered_set;
47
48 #elif defined(__GNUC__) && !defined(USE_CXX0X)
49
50 #include <tr1/unordered_set>
51 using std::tr1::unordered_set;
52
53 #else
54
55 #include <unordered_set>
56 using std::unordered_set;
57
58 #endif
59
60 namespace re2 {
61
62 typedef int8_t int8;
63 typedef uint8_t uint8;
64 typedef int16_t int16;
65 typedef uint16_t uint16;
66 typedef int32_t int32;
67 typedef uint32_t uint32;
68 typedef int64_t int64;
69 typedef uint64_t uint64;
70
71 typedef unsigned long ulong;
72 typedef unsigned int uint;
73 typedef unsigned short ushort;
74
75 // COMPILE_ASSERT causes a compile error about msg if expr is not true.
76 template<bool> struct CompileAssert {};
77 #define COMPILE_ASSERT(expr, msg) \
78 typedef CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1]
79
80 // DISALLOW_EVIL_CONSTRUCTORS disallows the copy and operator= functions.
81 // It goes in the private: declarations in a class.
82 #define DISALLOW_EVIL_CONSTRUCTORS(TypeName) \
83 TypeName(const TypeName&); \
84 void operator=(const TypeName&)
85
86 #define arraysize(array) (sizeof(array)/sizeof((array)[0]))
87
88 // Fake lock annotations. For real ones, see
89 // http://code.google.com/p/data-race-test/
90 #define ANNOTATE_PUBLISH_MEMORY_RANGE(a, b)
91 #define ANNOTATE_IGNORE_WRITES_BEGIN()
92 #define ANNOTATE_IGNORE_WRITES_END()
93 #define ANNOTATE_BENIGN_RACE(a, b)
94 #define NO_THREAD_SAFETY_ANALYSIS
95 #define ANNOTATE_HAPPENS_BEFORE(x)
96 #define ANNOTATE_HAPPENS_AFTER(x)
97
98 class StringPiece;
99
100 string CEscape(const StringPiece& src);
101 int CEscapeString(const char* src, int src_len, char* dest, int dest_len);
102
103 extern string StringPrintf(const char* format, ...);
104 extern void SStringPrintf(string* dst, const char* format, ...);
105 extern void StringAppendF(string* dst, const char* format, ...);
106 extern string PrefixSuccessor(const StringPiece& prefix);
107
108 uint32 hashword(const uint32*, size_t, uint32);
109 void hashword2(const uint32*, size_t, uint32*, uint32*);
110
Hash32StringWithSeed(const char * s,int len,uint32 seed)111 static inline uint32 Hash32StringWithSeed(const char* s, int len, uint32 seed) {
112 return hashword((uint32*)s, len/4, seed);
113 }
114
Hash64StringWithSeed(const char * s,int len,uint32 seed)115 static inline uint64 Hash64StringWithSeed(const char* s, int len, uint32 seed) {
116 uint32 x, y;
117 x = seed;
118 y = 0;
119 hashword2((uint32*)s, len/4, &x, &y);
120 return ((uint64)x << 32) | y;
121 }
122
123 int RunningOnValgrind();
124
125 } // namespace re2
126
127 #include "util/arena.h"
128 #include "util/logging.h"
129 #include "util/mutex.h"
130 #include "util/utf.h"
131
132 #endif // RE2_UTIL_UTIL_H__
133