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" |
| 6 |
5 #include <algorithm> | 7 #include <algorithm> |
6 #include <cctype> | |
7 #include <cmath> | 8 #include <cmath> |
8 | 9 |
9 #include "src/base/platform/platform.h" // For isinf/isnan with MSVC | 10 #include "src/base/platform/platform.h" // For isinf/isnan with MSVC |
10 #include "src/ostreams.h" | |
11 | 11 |
12 #if V8_OS_WIN | 12 #if V8_OS_WIN |
13 #define snprintf sprintf_s | 13 #define snprintf sprintf_s |
14 #endif | 14 #endif |
15 | 15 |
16 namespace v8 { | 16 namespace v8 { |
17 namespace internal { | 17 namespace internal { |
18 | 18 |
19 // Be lazy and delegate the value=>char conversion to snprintf. | 19 // Be lazy and delegate the value=>char conversion to snprintf. |
20 template<class T> | 20 template<class T> |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 return *this; | 157 return *this; |
158 } | 158 } |
159 | 159 |
160 | 160 |
161 OFStream& OFStream::flush() { | 161 OFStream& OFStream::flush() { |
162 if (f_) fflush(f_); | 162 if (f_) fflush(f_); |
163 return *this; | 163 return *this; |
164 } | 164 } |
165 | 165 |
166 | 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]; |
| 175 const char* format = pred(c) ? "%c" : (c <= 0xff) ? "\\x%02x" : "\\u%04x"; |
| 176 snprintf(buf, sizeof(buf), format, c); |
| 177 return os << buf; |
| 178 } |
| 179 |
| 180 |
167 OStream& operator<<(OStream& os, const AsReversiblyEscapedUC16& c) { | 181 OStream& operator<<(OStream& os, const AsReversiblyEscapedUC16& c) { |
168 char buf[10]; | 182 return PrintUC16(os, c.value, IsOK); |
169 const char* format = | |
170 (std::isprint(c.value) || std::isspace(c.value)) && c.value != '\\' | |
171 ? "%c" | |
172 : (c.value <= 0xff) ? "\\x%02x" : "\\u%04x"; | |
173 snprintf(buf, sizeof(buf), format, c.value); | |
174 return os << buf; | |
175 } | 183 } |
176 | 184 |
177 | 185 |
178 OStream& operator<<(OStream& os, const AsUC16& c) { | 186 OStream& operator<<(OStream& os, const AsUC16& c) { |
179 char buf[10]; | 187 return PrintUC16(os, c.value, IsPrint); |
180 const char* format = | |
181 std::isprint(c.value) ? "%c" : (c.value <= 0xff) ? "\\x%02x" : "\\u%04x"; | |
182 snprintf(buf, sizeof(buf), format, c.value); | |
183 return os << buf; | |
184 } | 188 } |
185 } } // namespace v8::internal | 189 } } // namespace v8::internal |
OLD | NEW |