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.
5 #include "src/ostreams.h"
8 #define snprintf sprintf_s
14 OFStreamBase::OFStreamBase(FILE* f) : f_(f) {}
17 OFStreamBase::~OFStreamBase() {}
20 OFStreamBase::int_type OFStreamBase::sync() {
26 OFStreamBase::int_type OFStreamBase::overflow(int_type c) {
27 return (c != EOF) ? std::fputc(c, f_) : c;
31 OFStream::OFStream(FILE* f) : OFStreamBase(f), std::ostream(this) {}
34 OFStream::~OFStream() {}
39 // Locale-independent predicates.
40 bool IsPrint(uint16_t c) { return 0x20 <= c && c <= 0x7e; }
41 bool IsSpace(uint16_t c) { return (0x9 <= c && c <= 0xd) || c == 0x20; }
42 bool IsOK(uint16_t c) { return (IsPrint(c) || IsSpace(c)) && c != '\\'; }
45 std::ostream& PrintUC16(std::ostream& os, uint16_t c, bool (*pred)(uint16_t)) {
47 const char* format = pred(c) ? "%c" : (c <= 0xff) ? "\\x%02x" : "\\u%04x";
48 snprintf(buf, sizeof(buf), format, c);
55 std::ostream& operator<<(std::ostream& os, const AsReversiblyEscapedUC16& c) {
56 return PrintUC16(os, c.value, IsOK);
60 std::ostream& operator<<(std::ostream& os, const AsUC16& c) {
61 return PrintUC16(os, c.value, IsPrint);
64 } // namespace internal