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.
5 #ifndef RE2_UTIL_UTIL_H__
6 #define RE2_UTIL_UTIL_H__
12 #include <stddef.h> // For size_t
19 #include <ctype.h> // For isdigit, isalpha.
32 #include "build/build_config.h"
33 #include "base/third_party/dynamic_annotations/dynamic_annotations.h"
49 #if defined(__GNUC__) && !defined(USE_CXX0X) && !defined(_LIBCPP_ABI_VERSION) && !defined(OS_ANDROID)
51 #include <tr1/unordered_set>
52 using std::tr1::unordered_set
;
56 #include <unordered_set>
57 #if defined(WIN32) || (defined(OS_ANDROID) && !defined(_LIBCPP_ABI_VERSION))
58 using std::tr1::unordered_set
;
60 using std::unordered_set
;
68 typedef uint8_t uint8
;
69 typedef int16_t int16
;
70 typedef uint16_t uint16
;
71 typedef int32_t int32
;
72 typedef uint32_t uint32
;
73 typedef int64_t int64
;
74 typedef uint64_t uint64
;
76 typedef unsigned long ulong
;
77 typedef unsigned int uint
;
78 typedef unsigned short ushort
;
80 // COMPILE_ASSERT causes a compile error about msg if expr is not true.
81 #if __cplusplus >= 201103L
82 #define COMPILE_ASSERT(expr, msg) static_assert(expr, #msg)
84 template<bool> struct CompileAssert
{};
85 #define COMPILE_ASSERT(expr, msg) \
86 typedef CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1]
89 // DISALLOW_EVIL_CONSTRUCTORS disallows the copy and operator= functions.
90 // It goes in the private: declarations in a class.
91 #define DISALLOW_EVIL_CONSTRUCTORS(TypeName) \
92 TypeName(const TypeName&); \
93 void operator=(const TypeName&)
95 #define arraysize(array) (sizeof(array)/sizeof((array)[0]))
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)
112 string
CEscape(const StringPiece
& src
);
113 int CEscapeString(const char* src
, int src_len
, char* dest
, int dest_len
);
115 extern string
StringPrintf(const char* format
, ...);
116 extern void SStringPrintf(string
* dst
, const char* format
, ...);
117 extern void StringAppendF(string
* dst
, const char* format
, ...);
118 extern string
PrefixSuccessor(const StringPiece
& prefix
);
120 uint32
hashword(const uint32
*, size_t, uint32
);
121 void hashword2(const uint32
*, size_t, uint32
*, uint32
*);
123 static inline uint32
Hash32StringWithSeed(const char* s
, int len
, uint32 seed
) {
124 return hashword((uint32
*)s
, len
/4, seed
);
127 static inline uint64
Hash64StringWithSeed(const char* s
, int len
, uint32 seed
) {
131 hashword2((uint32
*)s
, len
/4, &x
, &y
);
132 return ((uint64
)x
<< 32) | y
;
135 inline bool RunningOnValgrindOrMemorySanitizer() {
136 #if defined(MEMORY_SANITIZER)
139 return RunningOnValgrind();
145 #include "util/arena.h"
146 #include "util/logging.h"
147 #include "util/mutex.h"
148 #include "util/utf.h"
150 #endif // RE2_UTIL_UTIL_H__