Home | History | Annotate | Download | only in util
      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.
     31 using std::set;
     32 using std::pair;
     33 using std::vector;
     34 using std::string;
     35 using std::min;
     36 using std::max;
     37 using std::ostream;
     38 using std::map;
     39 using std::stack;
     40 using std::sort;
     41 using std::swap;
     42 using 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
     51 using std::tr1::unordered_set;
     52 
     53 #elif defined(__GNUC__) && !defined(USE_CXX0X)
     54 
     55 #include <tr1/unordered_set>
     56 using std::tr1::unordered_set;
     57 
     58 #else
     59 
     60 #include <unordered_set>
     61 using std::unordered_set;
     62 
     63 #endif
     64 
     65 namespace re2 {
     66 
     67 typedef int8_t int8;
     68 typedef uint8_t uint8;
     69 typedef int16_t int16;
     70 typedef uint16_t uint16;
     71 typedef int32_t int32;
     72 typedef uint32_t uint32;
     73 typedef int64_t int64;
     74 typedef uint64_t uint64;
     75 
     76 typedef unsigned long ulong;
     77 typedef unsigned int uint;
     78 typedef unsigned short ushort;
     79 
     80 // COMPILE_ASSERT causes a compile error about msg if expr is not true.
     81 template<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 
    106 class StringPiece;
    107 
    108 string CEscape(const StringPiece& src);
    109 int CEscapeString(const char* src, int src_len, char* dest, int dest_len);
    110 
    111 extern string StringPrintf(const char* format, ...);
    112 extern void SStringPrintf(string* dst, const char* format, ...);
    113 extern void StringAppendF(string* dst, const char* format, ...);
    114 extern string PrefixSuccessor(const StringPiece& prefix);
    115 
    116 uint32 hashword(const uint32*, size_t, uint32);
    117 void hashword2(const uint32*, size_t, uint32*, uint32*);
    118 
    119 static inline uint32 Hash32StringWithSeed(const char* s, int len, uint32 seed) {
    120   return hashword((uint32*)s, len/4, seed);
    121 }
    122 
    123 static 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 
    131 int 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