Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / v8 / src / ostreams.cc
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.
4
5 #include "src/ostreams.h"
6
7 #if V8_OS_WIN
8 #define snprintf sprintf_s
9 #endif
10
11 namespace v8 {
12 namespace internal {
13
14 OFStreamBase::OFStreamBase(FILE* f) : f_(f) {}
15
16
17 OFStreamBase::~OFStreamBase() {}
18
19
20 OFStreamBase::int_type OFStreamBase::sync() {
21   std::fflush(f_);
22   return 0;
23 }
24
25
26 OFStreamBase::int_type OFStreamBase::overflow(int_type c) {
27   return (c != EOF) ? std::fputc(c, f_) : c;
28 }
29
30
31 OFStream::OFStream(FILE* f) : OFStreamBase(f), std::ostream(this) {}
32
33
34 OFStream::~OFStream() {}
35
36
37 namespace {
38
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 != '\\'; }
43
44
45 std::ostream& PrintUC16(std::ostream& os, uint16_t c, bool (*pred)(uint16_t)) {
46   char buf[10];
47   const char* format = pred(c) ? "%c" : (c <= 0xff) ? "\\x%02x" : "\\u%04x";
48   snprintf(buf, sizeof(buf), format, c);
49   return os << buf;
50 }
51
52 }  // namespace
53
54
55 std::ostream& operator<<(std::ostream& os, const AsReversiblyEscapedUC16& c) {
56   return PrintUC16(os, c.value, IsOK);
57 }
58
59
60 std::ostream& operator<<(std::ostream& os, const AsUC16& c) {
61   return PrintUC16(os, c.value, IsPrint);
62 }
63
64 }  // namespace internal
65 }  // namespace v8