Line data Source code
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 "src/ostreams.h"
6 : #include "src/objects.h"
7 : #include "src/objects/string.h"
8 :
9 : #if V8_OS_WIN
10 : #if _MSC_VER < 1900
11 : #define snprintf sprintf_s
12 : #endif
13 : #endif
14 :
15 : namespace v8 {
16 : namespace internal {
17 :
18 266653 : OFStreamBase::OFStreamBase(FILE* f) : f_(f) {}
19 :
20 :
21 266653 : OFStreamBase::~OFStreamBase() {}
22 :
23 :
24 863 : int OFStreamBase::sync() {
25 863 : std::fflush(f_);
26 863 : return 0;
27 : }
28 :
29 :
30 863 : OFStreamBase::int_type OFStreamBase::overflow(int_type c) {
31 863 : return (c != EOF) ? std::fputc(c, f_) : c;
32 : }
33 :
34 :
35 35753 : std::streamsize OFStreamBase::xsputn(const char* s, std::streamsize n) {
36 : return static_cast<std::streamsize>(
37 35753 : std::fwrite(s, 1, static_cast<size_t>(n), f_));
38 : }
39 :
40 :
41 533306 : OFStream::OFStream(FILE* f) : std::ostream(nullptr), buf_(f) {
42 : DCHECK_NOT_NULL(f);
43 266653 : rdbuf(&buf_);
44 266653 : }
45 :
46 :
47 533306 : OFStream::~OFStream() {}
48 :
49 :
50 : namespace {
51 :
52 : // Locale-independent predicates.
53 12132 : bool IsPrint(uint16_t c) { return 0x20 <= c && c <= 0x7e; }
54 0 : bool IsSpace(uint16_t c) { return (0x9 <= c && c <= 0xd) || c == 0x20; }
55 13164 : bool IsOK(uint16_t c) { return (IsPrint(c) || IsSpace(c)) && c != '\\'; }
56 :
57 :
58 5550 : std::ostream& PrintUC16(std::ostream& os, uint16_t c, bool (*pred)(uint16_t)) {
59 : char buf[10];
60 5550 : const char* format = pred(c) ? "%c" : (c <= 0xff) ? "\\x%02x" : "\\u%04x";
61 : snprintf(buf, sizeof(buf), format, c);
62 5550 : return os << buf;
63 : }
64 :
65 6582 : std::ostream& PrintUC16ForJSON(std::ostream& os, uint16_t c,
66 : bool (*pred)(uint16_t)) {
67 : // JSON does not allow \x99; must use \u0099.
68 : char buf[10];
69 6582 : const char* format = pred(c) ? "%c" : "\\u%04x";
70 : snprintf(buf, sizeof(buf), format, c);
71 6582 : return os << buf;
72 : }
73 :
74 1182 : std::ostream& PrintUC32(std::ostream& os, int32_t c, bool (*pred)(uint16_t)) {
75 1182 : if (c <= String::kMaxUtf16CodeUnit) {
76 1134 : return PrintUC16(os, static_cast<uint16_t>(c), pred);
77 : }
78 : char buf[13];
79 : snprintf(buf, sizeof(buf), "\\u{%06x}", c);
80 48 : return os << buf;
81 : }
82 :
83 : } // namespace
84 :
85 :
86 0 : std::ostream& operator<<(std::ostream& os, const AsReversiblyEscapedUC16& c) {
87 0 : return PrintUC16(os, c.value, IsOK);
88 : }
89 :
90 :
91 6582 : std::ostream& operator<<(std::ostream& os, const AsEscapedUC16ForJSON& c) {
92 6582 : if (c.value == '\n') return os << "\\n";
93 6582 : if (c.value == '\r') return os << "\\r";
94 6582 : if (c.value == '\t') return os << "\\t";
95 6582 : if (c.value == '\"') return os << "\\\"";
96 6582 : return PrintUC16ForJSON(os, c.value, IsOK);
97 : }
98 :
99 :
100 4416 : std::ostream& operator<<(std::ostream& os, const AsUC16& c) {
101 4416 : return PrintUC16(os, c.value, IsPrint);
102 : }
103 :
104 :
105 1182 : std::ostream& operator<<(std::ostream& os, const AsUC32& c) {
106 1182 : return PrintUC32(os, c.value, IsPrint);
107 : }
108 :
109 53 : std::ostream& operator<<(std::ostream& os, const AsHex& hex) {
110 : // Each byte uses up to two characters. Plus two characters for the prefix,
111 : // plus null terminator.
112 : DCHECK_GE(sizeof(hex.value) * 2, hex.min_width);
113 : static constexpr size_t kMaxHexLength = 3 + sizeof(hex.value) * 2;
114 : char buf[kMaxHexLength];
115 : snprintf(buf, kMaxHexLength, "%s%.*" PRIx64, hex.with_prefix ? "0x" : "",
116 53 : hex.min_width, hex.value);
117 53 : return os << buf;
118 : }
119 :
120 18 : std::ostream& operator<<(std::ostream& os, const AsHexBytes& hex) {
121 18 : uint8_t bytes = hex.min_bytes;
122 18 : while (bytes < sizeof(hex.value) && (hex.value >> (bytes * 8) != 0)) ++bytes;
123 42 : for (uint8_t b = 0; b < bytes; ++b) {
124 42 : if (b) os << " ";
125 : uint8_t printed_byte =
126 42 : hex.byte_order == AsHexBytes::kLittleEndian ? b : bytes - b - 1;
127 84 : os << AsHex((hex.value >> (8 * printed_byte)) & 0xff, 2);
128 : }
129 18 : return os;
130 : }
131 :
132 : } // namespace internal
133 : } // namespace v8
|