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"
|
|
|
|
|
2014-07-07 07:21:10 +00:00
|
|
|
#if V8_OS_WIN
|
2014-06-27 08:42:17 +00:00
|
|
|
#define snprintf sprintf_s
|
|
|
|
#endif
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
2014-10-01 08:22:39 +00:00
|
|
|
OFStreamBase::int_type OFStreamBase::sync() {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-09-30 10:29:32 +00:00
|
|
|
OFStream::OFStream(FILE* f) : OFStreamBase(f), std::ostream(this) {}
|
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;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|