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