V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
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 #include "src/objects.h"
7 #include "src/objects/string.h"
8 
9 #if V8_OS_WIN
10 #if _MSC_VER < 1900
11 #define snprintf sprintf_s
12 #endif
13 #endif
14 
15 #if defined(ANDROID) && !defined(V8_ANDROID_LOG_STDOUT)
16 #define LOG_TAG "v8"
17 #include <android/log.h> // NOLINT
18 #endif
19 
20 namespace v8 {
21 namespace internal {
22 
23 OFStreamBase::OFStreamBase(FILE* f) : f_(f) {}
24 
25 int OFStreamBase::sync() {
26  std::fflush(f_);
27  return 0;
28 }
29 
30 
31 OFStreamBase::int_type OFStreamBase::overflow(int_type c) {
32  return (c != EOF) ? std::fputc(c, f_) : c;
33 }
34 
35 
36 std::streamsize OFStreamBase::xsputn(const char* s, std::streamsize n) {
37  return static_cast<std::streamsize>(
38  std::fwrite(s, 1, static_cast<size_t>(n), f_));
39 }
40 
41 OFStream::OFStream(FILE* f) : std::ostream(nullptr), buf_(f) {
42  DCHECK_NOT_NULL(f);
43  rdbuf(&buf_);
44 }
45 
46 #if defined(ANDROID) && !defined(V8_ANDROID_LOG_STDOUT)
47 AndroidLogStream::~AndroidLogStream() {
48  // If there is anything left in the line buffer, print it now, even though it
49  // was not terminated by a newline.
50  if (!line_buffer_.empty()) {
51  __android_log_write(ANDROID_LOG_INFO, LOG_TAG, line_buffer_.c_str());
52  }
53 }
54 
55 std::streamsize AndroidLogStream::xsputn(const char* s, std::streamsize n) {
56  const char* const e = s + n;
57  while (s < e) {
58  const char* newline = reinterpret_cast<const char*>(memchr(s, '\n', e - s));
59  size_t line_chars = (newline ? newline : e) - s;
60  line_buffer_.append(s, line_chars);
61  // Without terminating newline, keep the characters in the buffer for the
62  // next invocation.
63  if (!newline) break;
64  // Otherwise, write out the first line, then continue.
65  __android_log_write(ANDROID_LOG_INFO, LOG_TAG, line_buffer_.c_str());
66  line_buffer_.clear();
67  s = newline + 1;
68  }
69  return n;
70 }
71 #endif
72 
73 namespace {
74 
75 // Locale-independent predicates.
76 bool IsPrint(uint16_t c) { return 0x20 <= c && c <= 0x7E; }
77 bool IsSpace(uint16_t c) { return (0x9 <= c && c <= 0xD) || c == 0x20; }
78 bool IsOK(uint16_t c) { return (IsPrint(c) || IsSpace(c)) && c != '\\'; }
79 
80 
81 std::ostream& PrintUC16(std::ostream& os, uint16_t c, bool (*pred)(uint16_t)) {
82  char buf[10];
83  const char* format = pred(c) ? "%c" : (c <= 0xFF) ? "\\x%02x" : "\\u%04x";
84  snprintf(buf, sizeof(buf), format, c);
85  return os << buf;
86 }
87 
88 std::ostream& PrintUC16ForJSON(std::ostream& os, uint16_t c,
89  bool (*pred)(uint16_t)) {
90  // JSON does not allow \x99; must use \u0099.
91  char buf[10];
92  const char* format = pred(c) ? "%c" : "\\u%04x";
93  snprintf(buf, sizeof(buf), format, c);
94  return os << buf;
95 }
96 
97 std::ostream& PrintUC32(std::ostream& os, int32_t c, bool (*pred)(uint16_t)) {
98  if (c <= String::kMaxUtf16CodeUnit) {
99  return PrintUC16(os, static_cast<uint16_t>(c), pred);
100  }
101  char buf[13];
102  snprintf(buf, sizeof(buf), "\\u{%06x}", c);
103  return os << buf;
104 }
105 
106 } // namespace
107 
108 
109 std::ostream& operator<<(std::ostream& os, const AsReversiblyEscapedUC16& c) {
110  return PrintUC16(os, c.value, IsOK);
111 }
112 
113 
114 std::ostream& operator<<(std::ostream& os, const AsEscapedUC16ForJSON& c) {
115  if (c.value == '\n') return os << "\\n";
116  if (c.value == '\r') return os << "\\r";
117  if (c.value == '\t') return os << "\\t";
118  if (c.value == '\"') return os << "\\\"";
119  return PrintUC16ForJSON(os, c.value, IsOK);
120 }
121 
122 
123 std::ostream& operator<<(std::ostream& os, const AsUC16& c) {
124  return PrintUC16(os, c.value, IsPrint);
125 }
126 
127 
128 std::ostream& operator<<(std::ostream& os, const AsUC32& c) {
129  return PrintUC32(os, c.value, IsPrint);
130 }
131 
132 std::ostream& operator<<(std::ostream& os, const AsHex& hex) {
133  // Each byte uses up to two characters. Plus two characters for the prefix,
134  // plus null terminator.
135  DCHECK_GE(sizeof(hex.value) * 2, hex.min_width);
136  static constexpr size_t kMaxHexLength = 3 + sizeof(hex.value) * 2;
137  char buf[kMaxHexLength];
138  snprintf(buf, kMaxHexLength, "%s%.*" PRIx64, hex.with_prefix ? "0x" : "",
139  hex.min_width, hex.value);
140  return os << buf;
141 }
142 
143 std::ostream& operator<<(std::ostream& os, const AsHexBytes& hex) {
144  uint8_t bytes = hex.min_bytes;
145  while (bytes < sizeof(hex.value) && (hex.value >> (bytes * 8) != 0)) ++bytes;
146  for (uint8_t b = 0; b < bytes; ++b) {
147  if (b) os << " ";
148  uint8_t printed_byte =
149  hex.byte_order == AsHexBytes::kLittleEndian ? b : bytes - b - 1;
150  os << AsHex((hex.value >> (8 * printed_byte)) & 0xFF, 2);
151  }
152  return os;
153 }
154 
155 } // namespace internal
156 } // namespace v8
157 
158 #undef snprintf
159 #undef LOG_TAG
STL namespace.
Definition: libplatform.h:13