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