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 | |
Michael Starzinger
2014/06/25 22:00:57
Can we use SNPrintF from utils.h instead of using
Sven Panne
2014/06/26 06:56:19
I intentionally avoided OS::SNPrintF because we in
| |
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<<(int x) { | |
26 return print(hex_ ? "%x" : "%d", x); | |
27 } | |
28 | |
29 | |
30 OStream& OStream::operator<<(unsigned int x) { | |
31 return print(hex_ ? "%x" : "%u", x); | |
32 } | |
33 | |
34 | |
35 OStream& OStream::operator<<(long x) { // NOLINT(runtime/int) | |
36 return print(hex_ ? "%lx" : "%ld", x); | |
37 } | |
38 | |
39 | |
40 OStream& OStream::operator<<(unsigned long x) { // NOLINT(runtime/int) | |
41 return print(hex_ ? "%lx" : "%lu", x); | |
42 } | |
43 | |
44 | |
45 OStream& OStream::operator<<(long long x) { // NOLINT(runtime/int) | |
46 return print(hex_ ? "%llx" : "%lld", x); | |
47 } | |
48 | |
49 | |
50 OStream& OStream::operator<<(unsigned long long x) { // NOLINT(runtime/int) | |
51 return print(hex_ ? "%llx" : "%llu", x); | |
52 } | |
53 | |
54 | |
55 OStream& OStream::operator<<(double x) { | |
56 return print("%g", x); | |
57 } | |
58 | |
59 | |
60 OStream& OStream::operator<<(void* x) { | |
61 return print("%p", x); | |
62 } | |
63 | |
64 | |
65 OStream& OStream::operator<<(char x) { | |
66 return put(x); | |
67 } | |
68 | |
69 | |
70 OStream& OStream::operator<<(signed char x) { | |
71 return put(x); | |
72 } | |
73 | |
74 | |
75 OStream& OStream::operator<<(unsigned char x) { | |
76 return put(x); | |
77 } | |
78 | |
79 | |
80 OStream& OStream::dec() { | |
81 hex_ = false; | |
82 return *this; | |
83 } | |
84 | |
85 | |
86 OStream& OStream::hex() { | |
87 hex_ = true; | |
88 return *this; | |
89 } | |
90 | |
91 | |
92 OStream& flush(OStream& os) { // NOLINT(runtime/references) | |
93 return os.flush(); | |
94 } | |
95 | |
96 | |
97 OStream& endl(OStream& os) { // NOLINT(runtime/references) | |
98 return flush(os.put('\n')); | |
99 } | |
100 | |
101 | |
102 OStream& hex(OStream& os) { // NOLINT(runtime/references) | |
103 return os.hex(); | |
104 } | |
105 | |
106 | |
107 OStream& dec(OStream& os) { // NOLINT(runtime/references) | |
108 return os.dec(); | |
109 } | |
110 | |
111 | |
112 OStringStream& OStringStream::write(const char* s, size_t n) { | |
113 size_t new_size = size_ + n; | |
114 if (new_size < size_) return *this; // Overflow => no-op. | |
115 reserve(new_size); | |
116 memcpy(data_ + size_, s, n); | |
117 size_ = new_size; | |
118 return *this; | |
119 } | |
120 | |
121 | |
122 OStringStream& OStringStream::flush() { | |
123 return *this; | |
124 } | |
125 | |
126 | |
127 void OStringStream::reserve(size_t requested_capacity) { | |
128 if (requested_capacity <= capacity_) return; | |
129 size_t new_capacity = // Handle possible overflow by not doubling. | |
130 std::max(std::max(capacity_ * 2, capacity_), requested_capacity); | |
131 char * new_data = allocate(new_capacity); | |
132 memcpy(new_data, data_, size_); | |
133 deallocate(data_, capacity_); | |
134 capacity_ = new_capacity; | |
135 data_ = new_data; | |
136 } | |
137 | |
138 | |
139 void OFStream::close() { | |
140 if (own_ && f_) fclose(f_); | |
141 } | |
142 | |
143 | |
144 OFStream& OFStream::write(const char* s, size_t n) { | |
145 if (f_) fwrite(s, n, 1, f_); | |
146 return *this; | |
147 } | |
148 | |
149 | |
150 OFStream& OFStream::flush() { | |
151 if (f_) fflush(f_); | |
152 return *this; | |
153 } | |
154 | |
155 } } // namespace v8::internal | |
OLD | NEW |