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