2014-06-27 08:42:17 +00:00
|
|
|
// Copyright 2014 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2014-09-09 13:51:03 +00:00
|
|
|
#include "src/ostreams.h"
|
2016-01-21 13:33:02 +00:00
|
|
|
#include "src/objects.h"
|
2014-09-09 13:51:03 +00:00
|
|
|
|
2014-07-07 07:21:10 +00:00
|
|
|
#if V8_OS_WIN
|
2015-04-09 09:02:52 +00:00
|
|
|
#if _MSC_VER < 1900
|
2014-06-27 08:42:17 +00:00
|
|
|
#define snprintf sprintf_s
|
|
|
|
#endif
|
2015-04-09 09:02:52 +00:00
|
|
|
#endif
|
2014-06-27 08:42:17 +00:00
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
2014-09-30 10:29:32 +00:00
|
|
|
OFStreamBase::OFStreamBase(FILE* f) : f_(f) {}
|
2014-06-27 08:42:17 +00:00
|
|
|
|
|
|
|
|
2014-09-30 10:29:32 +00:00
|
|
|
OFStreamBase::~OFStreamBase() {}
|
2014-06-27 08:42:17 +00:00
|
|
|
|
|
|
|
|
2015-01-19 14:41:39 +00:00
|
|
|
int OFStreamBase::sync() {
|
2014-10-01 08:22:39 +00:00
|
|
|
std::fflush(f_);
|
|
|
|
return 0;
|
|
|
|
}
|
2014-06-27 08:42:17 +00:00
|
|
|
|
|
|
|
|
2014-09-30 10:29:32 +00:00
|
|
|
OFStreamBase::int_type OFStreamBase::overflow(int_type c) {
|
|
|
|
return (c != EOF) ? std::fputc(c, f_) : c;
|
2014-06-27 08:42:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-19 14:41:39 +00:00
|
|
|
std::streamsize OFStreamBase::xsputn(const char* s, std::streamsize n) {
|
|
|
|
return static_cast<std::streamsize>(
|
|
|
|
std::fwrite(s, 1, static_cast<size_t>(n), f_));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
OFStream::OFStream(FILE* f) : std::ostream(nullptr), buf_(f) {
|
2014-12-08 12:26:14 +00:00
|
|
|
DCHECK_NOT_NULL(f);
|
2015-01-19 14:41:39 +00:00
|
|
|
rdbuf(&buf_);
|
2014-12-08 12:26:14 +00:00
|
|
|
}
|
2014-06-27 08:42:17 +00:00
|
|
|
|
|
|
|
|
2014-09-30 10:29:32 +00:00
|
|
|
OFStream::~OFStream() {}
|
2014-06-27 08:42:17 +00:00
|
|
|
|
|
|
|
|
2014-09-30 10:29:32 +00:00
|
|
|
namespace {
|
2014-07-07 09:57:29 +00:00
|
|
|
|
2014-09-09 13:51:03 +00:00
|
|
|
// Locale-independent predicates.
|
2014-09-30 10:29:32 +00:00
|
|
|
bool IsPrint(uint16_t c) { return 0x20 <= c && c <= 0x7e; }
|
|
|
|
bool IsSpace(uint16_t c) { return (0x9 <= c && c <= 0xd) || c == 0x20; }
|
|
|
|
bool IsOK(uint16_t c) { return (IsPrint(c) || IsSpace(c)) && c != '\\'; }
|
2014-09-09 13:51:03 +00:00
|
|
|
|
|
|
|
|
2014-09-30 10:29:32 +00:00
|
|
|
std::ostream& PrintUC16(std::ostream& os, uint16_t c, bool (*pred)(uint16_t)) {
|
2014-08-28 07:30:58 +00:00
|
|
|
char buf[10];
|
2014-09-09 13:51:03 +00:00
|
|
|
const char* format = pred(c) ? "%c" : (c <= 0xff) ? "\\x%02x" : "\\u%04x";
|
|
|
|
snprintf(buf, sizeof(buf), format, c);
|
2014-08-28 07:30:58 +00:00
|
|
|
return os << buf;
|
|
|
|
}
|
|
|
|
|
2016-01-21 13:33:02 +00:00
|
|
|
|
|
|
|
std::ostream& PrintUC32(std::ostream& os, int32_t c, bool (*pred)(uint16_t)) {
|
|
|
|
if (c <= String::kMaxUtf16CodeUnit) {
|
|
|
|
return PrintUC16(os, static_cast<uint16_t>(c), pred);
|
|
|
|
}
|
|
|
|
char buf[13];
|
|
|
|
snprintf(buf, sizeof(buf), "\\u{%06x}", c);
|
|
|
|
return os << buf;
|
|
|
|
}
|
|
|
|
|
2014-09-30 10:29:32 +00:00
|
|
|
} // namespace
|
2014-08-28 07:30:58 +00:00
|
|
|
|
2014-09-30 10:29:32 +00:00
|
|
|
|
|
|
|
std::ostream& operator<<(std::ostream& os, const AsReversiblyEscapedUC16& c) {
|
2014-09-09 13:51:03 +00:00
|
|
|
return PrintUC16(os, c.value, IsOK);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-29 12:14:55 +00:00
|
|
|
std::ostream& operator<<(std::ostream& os, const AsEscapedUC16ForJSON& c) {
|
|
|
|
if (c.value == '\n') return os << "\\n";
|
|
|
|
if (c.value == '\r') return os << "\\r";
|
2015-11-30 16:43:39 +00:00
|
|
|
if (c.value == '\t') return os << "\\t";
|
2015-01-29 12:14:55 +00:00
|
|
|
if (c.value == '\"') return os << "\\\"";
|
|
|
|
return PrintUC16(os, c.value, IsOK);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-09-30 10:29:32 +00:00
|
|
|
std::ostream& operator<<(std::ostream& os, const AsUC16& c) {
|
2014-09-09 13:51:03 +00:00
|
|
|
return PrintUC16(os, c.value, IsPrint);
|
2014-07-07 09:57:29 +00:00
|
|
|
}
|
2014-09-30 10:29:32 +00:00
|
|
|
|
2016-01-21 13:33:02 +00:00
|
|
|
|
|
|
|
std::ostream& operator<<(std::ostream& os, const AsUC32& c) {
|
|
|
|
return PrintUC32(os, c.value, IsPrint);
|
|
|
|
}
|
|
|
|
|
2014-09-30 10:29:32 +00:00
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|