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 #include "src/objects.h" |
7 | 7 |
8 #if V8_OS_WIN | 8 #if V8_OS_WIN |
9 #if _MSC_VER < 1900 | 9 #if _MSC_VER < 1900 |
10 #define snprintf sprintf_s | 10 #define snprintf sprintf_s |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 bool IsOK(uint16_t c) { return (IsPrint(c) || IsSpace(c)) && c != '\\'; } | 54 bool IsOK(uint16_t c) { return (IsPrint(c) || IsSpace(c)) && c != '\\'; } |
55 | 55 |
56 | 56 |
57 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)) { |
58 char buf[10]; | 58 char buf[10]; |
59 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"; |
60 snprintf(buf, sizeof(buf), format, c); | 60 snprintf(buf, sizeof(buf), format, c); |
61 return os << buf; | 61 return os << buf; |
62 } | 62 } |
63 | 63 |
| 64 std::ostream& PrintUC16ForJSON(std::ostream& os, uint16_t c, |
| 65 bool (*pred)(uint16_t)) { |
| 66 // JSON does not allow \x99; must use \u0099. |
| 67 char buf[10]; |
| 68 const char* format = pred(c) ? "%c" : "\\u%04x"; |
| 69 snprintf(buf, sizeof(buf), format, c); |
| 70 return os << buf; |
| 71 } |
64 | 72 |
65 std::ostream& PrintUC32(std::ostream& os, int32_t c, bool (*pred)(uint16_t)) { | 73 std::ostream& PrintUC32(std::ostream& os, int32_t c, bool (*pred)(uint16_t)) { |
66 if (c <= String::kMaxUtf16CodeUnit) { | 74 if (c <= String::kMaxUtf16CodeUnit) { |
67 return PrintUC16(os, static_cast<uint16_t>(c), pred); | 75 return PrintUC16(os, static_cast<uint16_t>(c), pred); |
68 } | 76 } |
69 char buf[13]; | 77 char buf[13]; |
70 snprintf(buf, sizeof(buf), "\\u{%06x}", c); | 78 snprintf(buf, sizeof(buf), "\\u{%06x}", c); |
71 return os << buf; | 79 return os << buf; |
72 } | 80 } |
73 | 81 |
74 } // namespace | 82 } // namespace |
75 | 83 |
76 | 84 |
77 std::ostream& operator<<(std::ostream& os, const AsReversiblyEscapedUC16& c) { | 85 std::ostream& operator<<(std::ostream& os, const AsReversiblyEscapedUC16& c) { |
78 return PrintUC16(os, c.value, IsOK); | 86 return PrintUC16(os, c.value, IsOK); |
79 } | 87 } |
80 | 88 |
81 | 89 |
82 std::ostream& operator<<(std::ostream& os, const AsEscapedUC16ForJSON& c) { | 90 std::ostream& operator<<(std::ostream& os, const AsEscapedUC16ForJSON& c) { |
83 if (c.value == '\n') return os << "\\n"; | 91 if (c.value == '\n') return os << "\\n"; |
84 if (c.value == '\r') return os << "\\r"; | 92 if (c.value == '\r') return os << "\\r"; |
85 if (c.value == '\t') return os << "\\t"; | 93 if (c.value == '\t') return os << "\\t"; |
86 if (c.value == '\"') return os << "\\\""; | 94 if (c.value == '\"') return os << "\\\""; |
87 return PrintUC16(os, c.value, IsOK); | 95 return PrintUC16ForJSON(os, c.value, IsOK); |
88 } | 96 } |
89 | 97 |
90 | 98 |
91 std::ostream& operator<<(std::ostream& os, const AsUC16& c) { | 99 std::ostream& operator<<(std::ostream& os, const AsUC16& c) { |
92 return PrintUC16(os, c.value, IsPrint); | 100 return PrintUC16(os, c.value, IsPrint); |
93 } | 101 } |
94 | 102 |
95 | 103 |
96 std::ostream& operator<<(std::ostream& os, const AsUC32& c) { | 104 std::ostream& operator<<(std::ostream& os, const AsUC32& c) { |
97 return PrintUC32(os, c.value, IsPrint); | 105 return PrintUC32(os, c.value, IsPrint); |
98 } | 106 } |
99 | 107 |
100 std::ostream& operator<<(std::ostream& os, const AsHex& hex) { | 108 std::ostream& operator<<(std::ostream& os, const AsHex& hex) { |
101 char buf[20]; | 109 char buf[20]; |
102 snprintf(buf, sizeof(buf), "%.*" PRIx64, hex.min_width, hex.value); | 110 snprintf(buf, sizeof(buf), "%.*" PRIx64, hex.min_width, hex.value); |
103 return os << buf; | 111 return os << buf; |
104 } | 112 } |
105 | 113 |
106 } // namespace internal | 114 } // namespace internal |
107 } // namespace v8 | 115 } // namespace v8 |
OLD | NEW |