blob: 45f41bb012089fd52a8beb14d4e6e4ad37cabb9f [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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"
Ben Murdoch097c5b22016-05-18 11:27:45 +01006#include "src/objects.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008#if V8_OS_WIN
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009#if _MSC_VER < 1900
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010#define snprintf sprintf_s
11#endif
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013
14namespace v8 {
15namespace internal {
16
Emily Bernierd0a1eb72015-03-24 16:35:39 -040017OFStreamBase::OFStreamBase(FILE* f) : f_(f) {}
18
19
20OFStreamBase::~OFStreamBase() {}
21
22
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000023int OFStreamBase::sync() {
Emily Bernierd0a1eb72015-03-24 16:35:39 -040024 std::fflush(f_);
25 return 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000026}
27
28
Emily Bernierd0a1eb72015-03-24 16:35:39 -040029OFStreamBase::int_type OFStreamBase::overflow(int_type c) {
30 return (c != EOF) ? std::fputc(c, f_) : c;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000031}
32
33
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000034std::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
40OFStream::OFStream(FILE* f) : std::ostream(nullptr), buf_(f) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -040041 DCHECK_NOT_NULL(f);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000042 rdbuf(&buf_);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000043}
44
45
Emily Bernierd0a1eb72015-03-24 16:35:39 -040046OFStream::~OFStream() {}
Ben Murdochb8a8cc12014-11-26 15:28:44 +000047
48
Emily Bernierd0a1eb72015-03-24 16:35:39 -040049namespace {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000050
51// Locale-independent predicates.
Emily Bernierd0a1eb72015-03-24 16:35:39 -040052bool IsPrint(uint16_t c) { return 0x20 <= c && c <= 0x7e; }
53bool IsSpace(uint16_t c) { return (0x9 <= c && c <= 0xd) || c == 0x20; }
54bool IsOK(uint16_t c) { return (IsPrint(c) || IsSpace(c)) && c != '\\'; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000055
56
Emily Bernierd0a1eb72015-03-24 16:35:39 -040057std::ostream& PrintUC16(std::ostream& os, uint16_t c, bool (*pred)(uint16_t)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000058 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
Ben Murdoch097c5b22016-05-18 11:27:45 +010064
65std::ostream& PrintUC32(std::ostream& os, int32_t c, bool (*pred)(uint16_t)) {
66 if (c <= String::kMaxUtf16CodeUnit) {
67 return PrintUC16(os, static_cast<uint16_t>(c), pred);
68 }
69 char buf[13];
70 snprintf(buf, sizeof(buf), "\\u{%06x}", c);
71 return os << buf;
72}
73
Emily Bernierd0a1eb72015-03-24 16:35:39 -040074} // namespace
Ben Murdochb8a8cc12014-11-26 15:28:44 +000075
Emily Bernierd0a1eb72015-03-24 16:35:39 -040076
77std::ostream& operator<<(std::ostream& os, const AsReversiblyEscapedUC16& c) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000078 return PrintUC16(os, c.value, IsOK);
79}
80
81
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000082std::ostream& operator<<(std::ostream& os, const AsEscapedUC16ForJSON& c) {
83 if (c.value == '\n') return os << "\\n";
84 if (c.value == '\r') return os << "\\r";
85 if (c.value == '\t') return os << "\\t";
86 if (c.value == '\"') return os << "\\\"";
87 return PrintUC16(os, c.value, IsOK);
88}
89
90
Emily Bernierd0a1eb72015-03-24 16:35:39 -040091std::ostream& operator<<(std::ostream& os, const AsUC16& c) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000092 return PrintUC16(os, c.value, IsPrint);
93}
Emily Bernierd0a1eb72015-03-24 16:35:39 -040094
Ben Murdoch097c5b22016-05-18 11:27:45 +010095
96std::ostream& operator<<(std::ostream& os, const AsUC32& c) {
97 return PrintUC32(os, c.value, IsPrint);
98}
99
Ben Murdoch61f157c2016-09-16 13:49:30 +0100100std::ostream& operator<<(std::ostream& os, const AsHex& hex) {
101 char buf[20];
102 snprintf(buf, sizeof(buf), "%.*" PRIx64, hex.min_width, hex.value);
103 return os << buf;
104}
105
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400106} // namespace internal
107} // namespace v8