ostreams.cc 3.47 KB
Newer Older
1 2 3 4
// 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.

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 12
#define snprintf sprintf_s
#endif
13
#endif
14 15 16 17

namespace v8 {
namespace internal {

18
OFStreamBase::OFStreamBase(FILE* f) : f_(f) {}
19 20


21
OFStreamBase::~OFStreamBase() {}
22 23


svenpanne's avatar
svenpanne committed
24
int OFStreamBase::sync() {
25 26 27
  std::fflush(f_);
  return 0;
}
28 29


30 31
OFStreamBase::int_type OFStreamBase::overflow(int_type c) {
  return (c != EOF) ? std::fputc(c, f_) : c;
32 33 34
}


svenpanne's avatar
svenpanne committed
35 36 37 38 39 40 41
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) {
42
  DCHECK_NOT_NULL(f);
svenpanne's avatar
svenpanne committed
43
  rdbuf(&buf_);
44
}
45 46


47
OFStream::~OFStream() {}
48 49


50
namespace {
51

52
// Locale-independent predicates.
53 54 55
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 != '\\'; }
56 57


58
std::ostream& PrintUC16(std::ostream& os, uint16_t c, bool (*pred)(uint16_t)) {
59
  char buf[10];
60 61
  const char* format = pred(c) ? "%c" : (c <= 0xff) ? "\\x%02x" : "\\u%04x";
  snprintf(buf, sizeof(buf), format, c);
62 63 64
  return os << buf;
}

65 66 67 68 69 70 71 72
std::ostream& PrintUC16ForJSON(std::ostream& os, uint16_t c,
                               bool (*pred)(uint16_t)) {
  // JSON does not allow \x99; must use \u0099.
  char buf[10];
  const char* format = pred(c) ? "%c" : "\\u%04x";
  snprintf(buf, sizeof(buf), format, c);
  return os << buf;
}
73 74 75 76 77 78 79 80 81 82

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;
}

83
}  // namespace
84

85 86

std::ostream& operator<<(std::ostream& os, const AsReversiblyEscapedUC16& c) {
87 88 89 90
  return PrintUC16(os, c.value, IsOK);
}


danno's avatar
danno committed
91 92 93
std::ostream& operator<<(std::ostream& os, const AsEscapedUC16ForJSON& c) {
  if (c.value == '\n') return os << "\\n";
  if (c.value == '\r') return os << "\\r";
94
  if (c.value == '\t') return os << "\\t";
danno's avatar
danno committed
95
  if (c.value == '\"') return os << "\\\"";
96
  return PrintUC16ForJSON(os, c.value, IsOK);
danno's avatar
danno committed
97 98 99
}


100
std::ostream& operator<<(std::ostream& os, const AsUC16& c) {
101
  return PrintUC16(os, c.value, IsPrint);
102
}
103

104 105 106 107 108

std::ostream& operator<<(std::ostream& os, const AsUC32& c) {
  return PrintUC32(os, c.value, IsPrint);
}

109
std::ostream& operator<<(std::ostream& os, const AsHex& hex) {
110 111 112 113 114 115 116
  // Each byte uses up to two characters. Plus two characters for the prefix,
  // plus null terminator.
  DCHECK_GE(sizeof(hex.value) * 2, hex.min_width);
  static constexpr size_t kMaxHexLength = 3 + sizeof(hex.value) * 2;
  char buf[kMaxHexLength];
  snprintf(buf, kMaxHexLength, "%s%.*" PRIx64, hex.with_prefix ? "0x" : "",
           hex.min_width, hex.value);
117 118 119
  return os << buf;
}

120 121 122 123 124 125 126 127 128 129 130 131
std::ostream& operator<<(std::ostream& os, const AsHexBytes& hex) {
  uint8_t bytes = hex.min_bytes;
  while (bytes < sizeof(hex.value) && (hex.value >> (bytes * 8) != 0)) ++bytes;
  for (uint8_t b = 0; b < bytes; ++b) {
    if (b) os << " ";
    uint8_t printed_byte =
        hex.byte_order == AsHexBytes::kLittleEndian ? b : bytes - b - 1;
    os << AsHex((hex.value >> (8 * printed_byte)) & 0xff, 2);
  }
  return os;
}

132 133
}  // namespace internal
}  // namespace v8