15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright 2009 The RE2 Authors.  All Rights Reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// license that can be found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef RE2_UTIL_UTIL_H__
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define RE2_UTIL_UTIL_H__
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// C
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <stdio.h>
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string.h>
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <stdint.h>
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <stddef.h>         // For size_t
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <assert.h>
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <stdarg.h>
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef WIN32
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <sys/time.h>
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <time.h>
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <ctype.h>	// For isdigit, isalpha.
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// C++
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector>
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <algorithm>
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <iosfwd>
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <map>
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <stack>
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <ostream>
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <utility>
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <set>
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "build/build_config.h"
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/third_party/dynamic_annotations/dynamic_annotations.h"
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use std names.
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using std::set;
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using std::pair;
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using std::vector;
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using std::string;
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using std::min;
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using std::max;
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using std::ostream;
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using std::map;
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using std::stack;
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using std::sort;
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using std::swap;
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using std::make_pair;
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#if defined(__GNUC__) && !defined(USE_CXX0X) && !defined(OS_ANDROID) && \
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    !defined(_LIBCPP_ABI_VERSION)
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <tr1/unordered_set>
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using std::tr1::unordered_set;
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <unordered_set>
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(WIN32) || defined(OS_ANDROID)
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using std::tr1::unordered_set;
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using std::unordered_set;
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace re2 {
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef int8_t int8;
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef uint8_t uint8;
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef int16_t int16;
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef uint16_t uint16;
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef int32_t int32;
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef uint32_t uint32;
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef int64_t int64;
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef uint64_t uint64;
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef unsigned long ulong;
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef unsigned int uint;
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef unsigned short ushort;
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// COMPILE_ASSERT causes a compile error about msg if expr is not true.
821320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#if __cplusplus >= 201103L
831320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#define COMPILE_ASSERT(expr, msg) static_assert(expr, #msg)
841320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#else
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)template<bool> struct CompileAssert {};
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define COMPILE_ASSERT(expr, msg) \
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  typedef CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1]
881320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#endif
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// DISALLOW_EVIL_CONSTRUCTORS disallows the copy and operator= functions.
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// It goes in the private: declarations in a class.
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DISALLOW_EVIL_CONSTRUCTORS(TypeName) \
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TypeName(const TypeName&);                 \
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void operator=(const TypeName&)
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define arraysize(array) (sizeof(array)/sizeof((array)[0]))
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Fake lock annotations.  For real ones, see
992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// http://code.google.com/p/data-race-test/
1002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#ifndef ANNOTATE_PUBLISH_MEMORY_RANGE
1012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define ANNOTATE_PUBLISH_MEMORY_RANGE(a, b)
1022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define ANNOTATE_IGNORE_WRITES_BEGIN()
1032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define ANNOTATE_IGNORE_WRITES_END()
1042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define ANNOTATE_BENIGN_RACE(a, b)
1052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define NO_THREAD_SAFETY_ANALYSIS
1062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define ANNOTATE_HAPPENS_BEFORE(x)
1072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define ANNOTATE_HAPPENS_AFTER(x)
1082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define ANNOTATE_UNPROTECTED_READ(x) (x)
1092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif
1102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class StringPiece;
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)string CEscape(const StringPiece& src);
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int CEscapeString(const char* src, int src_len, char* dest, int dest_len);
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern string StringPrintf(const char* format, ...);
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern void SStringPrintf(string* dst, const char* format, ...);
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern void StringAppendF(string* dst, const char* format, ...);
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern string PrefixSuccessor(const StringPiece& prefix);
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)uint32 hashword(const uint32*, size_t, uint32);
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void hashword2(const uint32*, size_t, uint32*, uint32*);
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static inline uint32 Hash32StringWithSeed(const char* s, int len, uint32 seed) {
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return hashword((uint32*)s, len/4, seed);
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static inline uint64 Hash64StringWithSeed(const char* s, int len, uint32 seed) {
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  uint32 x, y;
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  x = seed;
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  y = 0;
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  hashword2((uint32*)s, len/4, &x, &y);
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return ((uint64)x << 32) | y;
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)inline bool RunningOnValgrindOrMemorySanitizer() {
1375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#if defined(MEMORY_SANITIZER)
1385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return true;
1395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#else
1405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return RunningOnValgrind();
1415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif
1425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace re2
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "util/arena.h"
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "util/logging.h"
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "util/mutex.h"
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "util/utf.h"
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif // RE2_UTIL_UTIL_H__
152