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