util.h revision c94c4501fe83e3ad77ce597b55bbbfbf533c10ee
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.
30using std::set;
31using std::pair;
32using std::vector;
33using std::string;
34using std::min;
35using std::max;
36using std::ostream;
37using std::map;
38using std::stack;
39using std::sort;
40using std::swap;
41using std::make_pair;
42
43#if defined(ANDROID)
44
45#if defined(_STLPORT_VERSION)
46#include <unordered_set>      // using stlport
47#else
48#include <tr1/unordered_set>  // using gnustl
49#endif
50
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#define ANNOTATE_PUBLISH_MEMORY_RANGE(a, b)
96#define ANNOTATE_IGNORE_WRITES_BEGIN()
97#define ANNOTATE_IGNORE_WRITES_END()
98#define ANNOTATE_BENIGN_RACE(a, b)
99#define NO_THREAD_SAFETY_ANALYSIS
100#define ANNOTATE_HAPPENS_BEFORE(x)
101#define ANNOTATE_HAPPENS_AFTER(x)
102
103class StringPiece;
104
105string CEscape(const StringPiece& src);
106int CEscapeString(const char* src, int src_len, char* dest, int dest_len);
107
108extern string StringPrintf(const char* format, ...);
109extern void SStringPrintf(string* dst, const char* format, ...);
110extern void StringAppendF(string* dst, const char* format, ...);
111extern string PrefixSuccessor(const StringPiece& prefix);
112
113uint32 hashword(const uint32*, size_t, uint32);
114void hashword2(const uint32*, size_t, uint32*, uint32*);
115
116static inline uint32 Hash32StringWithSeed(const char* s, int len, uint32 seed) {
117  return hashword((uint32*)s, len/4, seed);
118}
119
120static inline uint64 Hash64StringWithSeed(const char* s, int len, uint32 seed) {
121  uint32 x, y;
122  x = seed;
123  y = 0;
124  hashword2((uint32*)s, len/4, &x, &y);
125  return ((uint64)x << 32) | y;
126}
127
128int RunningOnValgrind();
129
130}  // namespace re2
131
132#include "util/arena.h"
133#include "util/logging.h"
134#include "util/mutex.h"
135#include "util/utf.h"
136
137#endif // RE2_UTIL_UTIL_H__
138