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#include <unordered_set> 46using std::tr1::unordered_set; 47 48#elif defined(__GNUC__) && !defined(USE_CXX0X) 49 50#include <tr1/unordered_set> 51using std::tr1::unordered_set; 52 53#else 54 55#include <unordered_set> 56using std::unordered_set; 57 58#endif 59 60namespace re2 { 61 62typedef int8_t int8; 63typedef uint8_t uint8; 64typedef int16_t int16; 65typedef uint16_t uint16; 66typedef int32_t int32; 67typedef uint32_t uint32; 68typedef int64_t int64; 69typedef uint64_t uint64; 70 71typedef unsigned long ulong; 72typedef unsigned int uint; 73typedef unsigned short ushort; 74 75// COMPILE_ASSERT causes a compile error about msg if expr is not true. 76template<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 98class StringPiece; 99 100string CEscape(const StringPiece& src); 101int CEscapeString(const char* src, int src_len, char* dest, int dest_len); 102 103extern string StringPrintf(const char* format, ...); 104extern void SStringPrintf(string* dst, const char* format, ...); 105extern void StringAppendF(string* dst, const char* format, ...); 106extern string PrefixSuccessor(const StringPiece& prefix); 107 108uint32 hashword(const uint32*, size_t, uint32); 109void hashword2(const uint32*, size_t, uint32*, uint32*); 110 111static inline uint32 Hash32StringWithSeed(const char* s, int len, uint32 seed) { 112 return hashword((uint32*)s, len/4, seed); 113} 114 115static 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 123int 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