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