00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef DOUBLE_CONVERSION_UTILS_H_
00029 #define DOUBLE_CONVERSION_UTILS_H_
00030
00031 #include <stdlib.h>
00032 #include <string.h>
00033
00034 #include <assert.h>
00035 #ifndef ASSERT
00036 #define ASSERT(condition) (assert(condition))
00037 #endif
00038 #ifndef UNIMPLEMENTED
00039 #define UNIMPLEMENTED() (abort())
00040 #endif
00041 #ifndef UNREACHABLE
00042 #define UNREACHABLE() (abort())
00043 #endif
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 #if defined(_M_X64) || defined(__x86_64__) || \
00056 defined(__ARMEL__) || defined(__avr32__) || \
00057 defined(__hppa__) || defined(__ia64__) || \
00058 defined(__mips__) || defined(__powerpc__) || \
00059 defined(__sparc__) || defined(__sparc) || defined(__s390__) || \
00060 defined(__SH4__) || defined(__alpha__) || \
00061 defined(_MIPS_ARCH_MIPS32R2)
00062 #define DOUBLE_CONVERSION_CORRECT_DOUBLE_OPERATIONS 1
00063 #elif defined(_M_IX86) || defined(__i386__) || defined(__i386)
00064 #if defined(_WIN32)
00065
00066 #define DOUBLE_CONVERSION_CORRECT_DOUBLE_OPERATIONS 1
00067 #else
00068 #undef DOUBLE_CONVERSION_CORRECT_DOUBLE_OPERATIONS
00069 #endif // _WIN32
00070 #else
00071 #error Target architecture was not detected as supported by Double-Conversion.
00072 #endif
00073
00074
00075 #if defined(_WIN32) && !defined(__MINGW32__)
00076
00077 typedef signed char int8_t;
00078 typedef unsigned char uint8_t;
00079 typedef short int16_t;
00080 typedef unsigned short uint16_t;
00081 typedef int int32_t;
00082 typedef unsigned int uint32_t;
00083 typedef __int64 int64_t;
00084 typedef unsigned __int64 uint64_t;
00085
00086
00087 #else
00088
00089 #include <stdint.h>
00090
00091 #endif
00092
00093
00094
00095
00096 #define UINT64_2PART_C(a, b) (((static_cast<uint64_t>(a) << 32) + 0x##b##u))
00097
00098
00099
00100
00101
00102
00103 #ifndef ARRAY_SIZE
00104 #define ARRAY_SIZE(a) \
00105 ((sizeof(a) / sizeof(*(a))) / \
00106 static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
00107 #endif
00108
00109
00110
00111 #ifndef DISALLOW_COPY_AND_ASSIGN
00112 #define DISALLOW_COPY_AND_ASSIGN(TypeName) \
00113 TypeName(const TypeName&); \
00114 void operator=(const TypeName&)
00115 #endif
00116
00117
00118
00119
00120
00121
00122
00123 #ifndef DISALLOW_IMPLICIT_CONSTRUCTORS
00124 #define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
00125 TypeName(); \
00126 DISALLOW_COPY_AND_ASSIGN(TypeName)
00127 #endif
00128
00129 namespace double_conversion {
00130
00131 static const int kCharSize = sizeof(char);
00132
00133
00134 template <typename T>
00135 static T Max(T a, T b) {
00136 return a < b ? b : a;
00137 }
00138
00139
00140
00141 template <typename T>
00142 static T Min(T a, T b) {
00143 return a < b ? a : b;
00144 }
00145
00146
00147 inline int StrLength(const char* string) {
00148 size_t length = strlen(string);
00149 ASSERT(length == static_cast<size_t>(static_cast<int>(length)));
00150 return static_cast<int>(length);
00151 }
00152
00153
00154 template <typename T>
00155 class Vector {
00156 public:
00157 Vector() : start_(NULL), length_(0) {}
00158 Vector(T* data, int length) : start_(data), length_(length) {
00159 ASSERT(length == 0 || (length > 0 && data != NULL));
00160 }
00161
00162
00163
00164 Vector<T> SubVector(int from, int to) {
00165 ASSERT(to <= length_);
00166 ASSERT(from < to);
00167 ASSERT(0 <= from);
00168 return Vector<T>(start() + from, to - from);
00169 }
00170
00171
00172 int length() const { return length_; }
00173
00174
00175 bool is_empty() const { return length_ == 0; }
00176
00177
00178 T* start() const { return start_; }
00179
00180
00181 T& operator[](int index) const {
00182 ASSERT(0 <= index && index < length_);
00183 return start_[index];
00184 }
00185
00186 T& first() { return start_[0]; }
00187
00188 T& last() { return start_[length_ - 1]; }
00189
00190 private:
00191 T* start_;
00192 int length_;
00193 };
00194
00195
00196
00197
00198
00199 class StringBuilder {
00200 public:
00201 StringBuilder(char* buffer, int size)
00202 : buffer_(buffer, size), position_(0) { }
00203
00204 ~StringBuilder() { if (!is_finalized()) Finalize(); }
00205
00206 int size() const { return buffer_.length(); }
00207
00208
00209 int position() const {
00210 ASSERT(!is_finalized());
00211 return position_;
00212 }
00213
00214
00215 void Reset() { position_ = 0; }
00216
00217
00218
00219
00220 void AddCharacter(char c) {
00221
00222
00223 ASSERT(!is_finalized() && position_ < buffer_.length());
00224 buffer_[position_++] = c;
00225 }
00226
00227
00228
00229 void AddString(const char* s) {
00230 AddSubstring(s, StrLength(s));
00231 }
00232
00233
00234
00235 void AddSubstring(const char* s, int n) {
00236 ASSERT(!is_finalized() && position_ + n < buffer_.length());
00237
00238
00239 memmove(&buffer_[position_], s, n * kCharSize);
00240 position_ += n;
00241 }
00242
00243
00244
00245
00246 void AddPadding(char c, int count) {
00247 for (int i = 0; i < count; i++) {
00248 AddCharacter(c);
00249 }
00250 }
00251
00252
00253 char* Finalize() {
00254 ASSERT(!is_finalized() && position_ < buffer_.length());
00255 buffer_[position_] = '\0';
00256
00257
00258
00259
00260 position_ = -1;
00261 ASSERT(is_finalized());
00262 return buffer_.start();
00263 }
00264
00265 private:
00266 Vector<char> buffer_;
00267 int position_;
00268
00269 bool is_finalized() const { return position_ < 0; }
00270
00271 DISALLOW_IMPLICIT_CONSTRUCTORS(StringBuilder);
00272 };
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298 template <class Dest, class Source>
00299 inline Dest BitCast(const Source& source) {
00300
00301
00302 typedef char VerifySizesAreEqual[sizeof(Dest) == sizeof(Source) ? 1 : -1]
00303 #if __GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ >= 8
00304 __attribute__((unused))
00305 #endif
00306 ;
00307
00308 Dest dest;
00309 memmove(&dest, &source, sizeof(dest));
00310 return dest;
00311 }
00312
00313 template <class Dest, class Source>
00314 inline Dest BitCast(Source* source) {
00315 return BitCast<Dest>(reinterpret_cast<uintptr_t>(source));
00316 }
00317
00318 }
00319
00320 #endif // DOUBLE_CONVERSION_UTILS_H_