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 #include "src/objects.h" |
6 | 7 |
7 #if V8_OS_WIN | 8 #if V8_OS_WIN |
8 #if _MSC_VER < 1900 | 9 #if _MSC_VER < 1900 |
9 #define snprintf sprintf_s | 10 #define snprintf sprintf_s |
10 #endif | 11 #endif |
11 #endif | 12 #endif |
12 | 13 |
13 namespace v8 { | 14 namespace v8 { |
14 namespace internal { | 15 namespace internal { |
15 | 16 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 bool IsOK(uint16_t c) { return (IsPrint(c) || IsSpace(c)) && c != '\\'; } | 54 bool IsOK(uint16_t c) { return (IsPrint(c) || IsSpace(c)) && c != '\\'; } |
54 | 55 |
55 | 56 |
56 std::ostream& PrintUC16(std::ostream& os, uint16_t c, bool (*pred)(uint16_t)) { | 57 std::ostream& PrintUC16(std::ostream& os, uint16_t c, bool (*pred)(uint16_t)) { |
57 char buf[10]; | 58 char buf[10]; |
58 const char* format = pred(c) ? "%c" : (c <= 0xff) ? "\\x%02x" : "\\u%04x"; | 59 const char* format = pred(c) ? "%c" : (c <= 0xff) ? "\\x%02x" : "\\u%04x"; |
59 snprintf(buf, sizeof(buf), format, c); | 60 snprintf(buf, sizeof(buf), format, c); |
60 return os << buf; | 61 return os << buf; |
61 } | 62 } |
62 | 63 |
| 64 |
| 65 std::ostream& PrintUC32(std::ostream& os, int32_t c, bool (*pred)(uint16_t)) { |
| 66 if (c <= String::kMaxUtf16CodeUnit) { |
| 67 return PrintUC16(os, static_cast<uint16_t>(c), pred); |
| 68 } |
| 69 char buf[13]; |
| 70 snprintf(buf, sizeof(buf), "\\u{%06x}", c); |
| 71 return os << buf; |
| 72 } |
| 73 |
63 } // namespace | 74 } // namespace |
64 | 75 |
65 | 76 |
66 std::ostream& operator<<(std::ostream& os, const AsReversiblyEscapedUC16& c) { | 77 std::ostream& operator<<(std::ostream& os, const AsReversiblyEscapedUC16& c) { |
67 return PrintUC16(os, c.value, IsOK); | 78 return PrintUC16(os, c.value, IsOK); |
68 } | 79 } |
69 | 80 |
70 | 81 |
71 std::ostream& operator<<(std::ostream& os, const AsEscapedUC16ForJSON& c) { | 82 std::ostream& operator<<(std::ostream& os, const AsEscapedUC16ForJSON& c) { |
72 if (c.value == '\n') return os << "\\n"; | 83 if (c.value == '\n') return os << "\\n"; |
73 if (c.value == '\r') return os << "\\r"; | 84 if (c.value == '\r') return os << "\\r"; |
74 if (c.value == '\t') return os << "\\t"; | 85 if (c.value == '\t') return os << "\\t"; |
75 if (c.value == '\"') return os << "\\\""; | 86 if (c.value == '\"') return os << "\\\""; |
76 return PrintUC16(os, c.value, IsOK); | 87 return PrintUC16(os, c.value, IsOK); |
77 } | 88 } |
78 | 89 |
79 | 90 |
80 std::ostream& operator<<(std::ostream& os, const AsUC16& c) { | 91 std::ostream& operator<<(std::ostream& os, const AsUC16& c) { |
81 return PrintUC16(os, c.value, IsPrint); | 92 return PrintUC16(os, c.value, IsPrint); |
82 } | 93 } |
83 | 94 |
| 95 |
| 96 std::ostream& operator<<(std::ostream& os, const AsUC32& c) { |
| 97 return PrintUC32(os, c.value, IsPrint); |
| 98 } |
| 99 |
84 } // namespace internal | 100 } // namespace internal |
85 } // namespace v8 | 101 } // namespace v8 |
OLD | NEW |