blob: ee0474d2c0882db2ffe81e9e293401fa3c45fed3 [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"
6
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007#if V8_OS_WIN
8#define snprintf sprintf_s
9#endif
10
11namespace v8 {
12namespace internal {
13
Emily Bernierd0a1eb72015-03-24 16:35:39 -040014OFStreamBase::OFStreamBase(FILE* f) : f_(f) {}
15
16
17OFStreamBase::~OFStreamBase() {}
18
19
20OFStreamBase::int_type OFStreamBase::sync() {
21 std::fflush(f_);
22 return 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000023}
24
25
Emily Bernierd0a1eb72015-03-24 16:35:39 -040026OFStreamBase::int_type OFStreamBase::overflow(int_type c) {
27 return (c != EOF) ? std::fputc(c, f_) : c;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000028}
29
30
Emily Bernierd0a1eb72015-03-24 16:35:39 -040031OFStream::OFStream(FILE* f) : OFStreamBase(f), std::ostream(this) {
32 DCHECK_NOT_NULL(f);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000033}
34
35
Emily Bernierd0a1eb72015-03-24 16:35:39 -040036OFStream::~OFStream() {}
Ben Murdochb8a8cc12014-11-26 15:28:44 +000037
38
Emily Bernierd0a1eb72015-03-24 16:35:39 -040039namespace {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000040
41// Locale-independent predicates.
Emily Bernierd0a1eb72015-03-24 16:35:39 -040042bool IsPrint(uint16_t c) { return 0x20 <= c && c <= 0x7e; }
43bool IsSpace(uint16_t c) { return (0x9 <= c && c <= 0xd) || c == 0x20; }
44bool IsOK(uint16_t c) { return (IsPrint(c) || IsSpace(c)) && c != '\\'; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000045
46
Emily Bernierd0a1eb72015-03-24 16:35:39 -040047std::ostream& PrintUC16(std::ostream& os, uint16_t c, bool (*pred)(uint16_t)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000048 char buf[10];
49 const char* format = pred(c) ? "%c" : (c <= 0xff) ? "\\x%02x" : "\\u%04x";
50 snprintf(buf, sizeof(buf), format, c);
51 return os << buf;
52}
53
Emily Bernierd0a1eb72015-03-24 16:35:39 -040054} // namespace
Ben Murdochb8a8cc12014-11-26 15:28:44 +000055
Emily Bernierd0a1eb72015-03-24 16:35:39 -040056
57std::ostream& operator<<(std::ostream& os, const AsReversiblyEscapedUC16& c) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000058 return PrintUC16(os, c.value, IsOK);
59}
60
61
Emily Bernierd0a1eb72015-03-24 16:35:39 -040062std::ostream& operator<<(std::ostream& os, const AsUC16& c) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000063 return PrintUC16(os, c.value, IsPrint);
64}
Emily Bernierd0a1eb72015-03-24 16:35:39 -040065
66} // namespace internal
67} // namespace v8