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 :
8 : #if V8_OS_WIN
9 : #if _MSC_VER < 1900
10 : #define snprintf sprintf_s
11 : #endif
12 : #endif
13 :
14 : namespace v8 {
15 : namespace internal {
16 :
17 349125 : OFStreamBase::OFStreamBase(FILE* f) : f_(f) {}
18 :
19 :
20 349125 : OFStreamBase::~OFStreamBase() {}
21 :
22 :
23 1222 : int OFStreamBase::sync() {
24 1222 : std::fflush(f_);
25 1222 : return 0;
26 : }
27 :
28 :
29 1222 : OFStreamBase::int_type OFStreamBase::overflow(int_type c) {
30 1222 : return (c != EOF) ? std::fputc(c, f_) : c;
31 : }
32 :
33 :
34 46696 : std::streamsize OFStreamBase::xsputn(const char* s, std::streamsize n) {
35 : return static_cast<std::streamsize>(
36 46696 : std::fwrite(s, 1, static_cast<size_t>(n), f_));
37 : }
38 :
39 :
40 698250 : OFStream::OFStream(FILE* f) : std::ostream(nullptr), buf_(f) {
41 : DCHECK_NOT_NULL(f);
42 349125 : rdbuf(&buf_);
43 349124 : }
44 :
45 :
46 698250 : OFStream::~OFStream() {}
47 :
48 :
49 : namespace {
50 :
51 : // Locale-independent predicates.
52 13762 : bool IsPrint(uint16_t c) { return 0x20 <= c && c <= 0x7e; }
53 0 : bool IsSpace(uint16_t c) { return (0x9 <= c && c <= 0xd) || c == 0x20; }
54 13510 : bool IsOK(uint16_t c) { return (IsPrint(c) || IsSpace(c)) && c != '\\'; }
55 :
56 :
57 7007 : std::ostream& PrintUC16(std::ostream& os, uint16_t c, bool (*pred)(uint16_t)) {
58 : char buf[10];
59 7007 : const char* format = pred(c) ? "%c" : (c <= 0xff) ? "\\x%02x" : "\\u%04x";
60 : snprintf(buf, sizeof(buf), format, c);
61 7007 : return os << buf;
62 : }
63 :
64 6755 : 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 6755 : const char* format = pred(c) ? "%c" : "\\u%04x";
69 : snprintf(buf, sizeof(buf), format, c);
70 6755 : return os << buf;
71 : }
72 :
73 1379 : std::ostream& PrintUC32(std::ostream& os, int32_t c, bool (*pred)(uint16_t)) {
74 1379 : if (c <= String::kMaxUtf16CodeUnit) {
75 1323 : return PrintUC16(os, static_cast<uint16_t>(c), pred);
76 : }
77 : char buf[13];
78 : snprintf(buf, sizeof(buf), "\\u{%06x}", c);
79 56 : return os << buf;
80 : }
81 :
82 : } // namespace
83 :
84 :
85 0 : std::ostream& operator<<(std::ostream& os, const AsReversiblyEscapedUC16& c) {
86 0 : return PrintUC16(os, c.value, IsOK);
87 : }
88 :
89 :
90 6755 : std::ostream& operator<<(std::ostream& os, const AsEscapedUC16ForJSON& c) {
91 6755 : if (c.value == '\n') return os << "\\n";
92 6755 : if (c.value == '\r') return os << "\\r";
93 6755 : if (c.value == '\t') return os << "\\t";
94 6755 : if (c.value == '\"') return os << "\\\"";
95 6755 : return PrintUC16ForJSON(os, c.value, IsOK);
96 : }
97 :
98 :
99 5684 : std::ostream& operator<<(std::ostream& os, const AsUC16& c) {
100 5684 : return PrintUC16(os, c.value, IsPrint);
101 : }
102 :
103 :
104 1379 : std::ostream& operator<<(std::ostream& os, const AsUC32& c) {
105 1379 : return PrintUC32(os, c.value, IsPrint);
106 : }
107 :
108 0 : std::ostream& operator<<(std::ostream& os, const AsHex& hex) {
109 : char buf[20];
110 0 : snprintf(buf, sizeof(buf), "%.*" PRIx64, hex.min_width, hex.value);
111 0 : return os << buf;
112 : }
113 :
114 : } // namespace internal
115 : } // namespace v8
|