OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <algorithm> |
| 6 |
| 7 #include "src/ostreams.h" |
| 8 |
| 9 #if V8_CC_MSVC |
| 10 #define snprintf sprintf_s |
| 11 #endif |
| 12 |
| 13 namespace v8 { |
| 14 namespace internal { |
| 15 |
| 16 // Be lazy and delegate the value=>char conversion to snprintf. |
| 17 template<class T> |
| 18 OStream& OStream::print(const char* format, T x) { |
| 19 char buf[32]; |
| 20 int n = snprintf(buf, sizeof(buf), format, x); |
| 21 return (n < 0) ? *this : write(buf, n); |
| 22 } |
| 23 |
| 24 |
| 25 OStream& OStream::operator<<(short x) { // NOLINT(runtime/int) |
| 26 return print(hex_ ? "%hx" : "%hd", x); |
| 27 } |
| 28 |
| 29 |
| 30 OStream& OStream::operator<<(unsigned short x) { // NOLINT(runtime/int) |
| 31 return print(hex_ ? "%hx" : "%hu", x); |
| 32 } |
| 33 |
| 34 |
| 35 OStream& OStream::operator<<(int x) { |
| 36 return print(hex_ ? "%x" : "%d", x); |
| 37 } |
| 38 |
| 39 |
| 40 OStream& OStream::operator<<(unsigned int x) { |
| 41 return print(hex_ ? "%x" : "%u", x); |
| 42 } |
| 43 |
| 44 |
| 45 OStream& OStream::operator<<(long x) { // NOLINT(runtime/int) |
| 46 return print(hex_ ? "%lx" : "%ld", x); |
| 47 } |
| 48 |
| 49 |
| 50 OStream& OStream::operator<<(unsigned long x) { // NOLINT(runtime/int) |
| 51 return print(hex_ ? "%lx" : "%lu", x); |
| 52 } |
| 53 |
| 54 |
| 55 OStream& OStream::operator<<(long long x) { // NOLINT(runtime/int) |
| 56 return print(hex_ ? "%llx" : "%lld", x); |
| 57 } |
| 58 |
| 59 |
| 60 OStream& OStream::operator<<(unsigned long long x) { // NOLINT(runtime/int) |
| 61 return print(hex_ ? "%llx" : "%llu", x); |
| 62 } |
| 63 |
| 64 |
| 65 OStream& OStream::operator<<(double x) { |
| 66 return print("%g", x); |
| 67 } |
| 68 |
| 69 |
| 70 OStream& OStream::operator<<(void* x) { |
| 71 return print("%p", x); |
| 72 } |
| 73 |
| 74 |
| 75 OStream& OStream::operator<<(char x) { |
| 76 return put(x); |
| 77 } |
| 78 |
| 79 |
| 80 OStream& OStream::operator<<(signed char x) { |
| 81 return put(x); |
| 82 } |
| 83 |
| 84 |
| 85 OStream& OStream::operator<<(unsigned char x) { |
| 86 return put(x); |
| 87 } |
| 88 |
| 89 |
| 90 OStream& OStream::dec() { |
| 91 hex_ = false; |
| 92 return *this; |
| 93 } |
| 94 |
| 95 |
| 96 OStream& OStream::hex() { |
| 97 hex_ = true; |
| 98 return *this; |
| 99 } |
| 100 |
| 101 |
| 102 OStream& flush(OStream& os) { // NOLINT(runtime/references) |
| 103 return os.flush(); |
| 104 } |
| 105 |
| 106 |
| 107 OStream& endl(OStream& os) { // NOLINT(runtime/references) |
| 108 return flush(os.put('\n')); |
| 109 } |
| 110 |
| 111 |
| 112 OStream& hex(OStream& os) { // NOLINT(runtime/references) |
| 113 return os.hex(); |
| 114 } |
| 115 |
| 116 |
| 117 OStream& dec(OStream& os) { // NOLINT(runtime/references) |
| 118 return os.dec(); |
| 119 } |
| 120 |
| 121 |
| 122 OStringStream& OStringStream::write(const char* s, size_t n) { |
| 123 size_t new_size = size_ + n; |
| 124 if (new_size < size_) return *this; // Overflow => no-op. |
| 125 reserve(new_size + 1); |
| 126 memcpy(data_ + size_, s, n); |
| 127 size_ = new_size; |
| 128 data_[size_] = '\0'; |
| 129 return *this; |
| 130 } |
| 131 |
| 132 |
| 133 OStringStream& OStringStream::flush() { |
| 134 return *this; |
| 135 } |
| 136 |
| 137 |
| 138 void OStringStream::reserve(size_t requested_capacity) { |
| 139 if (requested_capacity <= capacity_) return; |
| 140 size_t new_capacity = // Handle possible overflow by not doubling. |
| 141 std::max(std::max(capacity_ * 2, capacity_), requested_capacity); |
| 142 char * new_data = allocate(new_capacity); |
| 143 memcpy(new_data, data_, size_); |
| 144 deallocate(data_, capacity_); |
| 145 capacity_ = new_capacity; |
| 146 data_ = new_data; |
| 147 } |
| 148 |
| 149 |
| 150 OFStream& OFStream::write(const char* s, size_t n) { |
| 151 if (f_) fwrite(s, n, 1, f_); |
| 152 return *this; |
| 153 } |
| 154 |
| 155 |
| 156 OFStream& OFStream::flush() { |
| 157 if (f_) fflush(f_); |
| 158 return *this; |
| 159 } |
| 160 |
| 161 } } // namespace v8::internal |
OLD | NEW |