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