GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/inspector/node_string.cc Lines: 36 50 72.0 %
Date: 2019-02-26 22:23:30 Branches: 7 14 50.0 %

Line Branch Exec Source
1
#include "node_string.h"
2
#include "node/inspector/protocol/Protocol.h"
3
4
#include <unicode/unistr.h>
5
6
namespace node {
7
namespace inspector {
8
namespace protocol {
9
namespace StringUtil {
10
11
size_t kNotFound = std::string::npos;
12
13
// NOLINTNEXTLINE(runtime/references) V8 API requirement
14
776
void builderAppendQuotedString(StringBuilder& builder, const String& string) {
15
776
  builder.put('"');
16
776
  if (!string.empty()) {
17
    icu::UnicodeString utf16 = icu::UnicodeString::fromUTF8(
18
776
        icu::StringPiece(string.data(), string.length()));
19
    escapeWideStringForJSON(
20
1552
        reinterpret_cast<const uint16_t*>(utf16.getBuffer()), utf16.length(),
21
1552
        &builder);
22
  }
23
776
  builder.put('"');
24
776
}
25
26
13947
std::unique_ptr<Value> parseJSON(const String& string) {
27
13947
  if (string.empty())
28
    return nullptr;
29
30
  icu::UnicodeString utf16 =
31
      icu::UnicodeString::fromUTF8(icu::StringPiece(string.data(),
32
13947
                                                    string.length()));
33
  return parseJSONCharacters(
34
13947
      reinterpret_cast<const uint16_t*>(utf16.getBuffer()), utf16.length());
35
}
36
37
std::unique_ptr<Value> parseJSON(v8_inspector::StringView string) {
38
  if (string.length() == 0)
39
    return nullptr;
40
  if (string.is8Bit())
41
    return parseJSONCharacters(string.characters8(), string.length());
42
  return parseJSONCharacters(string.characters16(), string.length());
43
}
44
45
430430
String StringViewToUtf8(v8_inspector::StringView view) {
46
430430
  if (view.length() == 0)
47
3
    return "";
48
430427
  if (view.is8Bit()) {
49
    return std::string(reinterpret_cast<const char*>(view.characters8()),
50
                       view.length());
51
  }
52
430427
  const uint16_t* source = view.characters16();
53
430427
  const UChar* unicodeSource = reinterpret_cast<const UChar*>(source);
54
  static_assert(sizeof(*source) == sizeof(*unicodeSource),
55
                "sizeof(*source) == sizeof(*unicodeSource)");
56
57
430427
  size_t result_length = view.length() * sizeof(*source);
58
430427
  std::string result(result_length, '\0');
59
860854
  icu::UnicodeString utf16(unicodeSource, view.length());
60
  // ICU components for std::string compatibility are not enabled in build...
61
430427
  bool done = false;
62
1291281
  while (!done) {
63
430427
    icu::CheckedArrayByteSink sink(&result[0], result_length);
64
430427
    utf16.toUTF8(sink);
65
430427
    result_length = sink.NumberOfBytesAppended();
66
430427
    result.resize(result_length);
67
430427
    done = !sink.Overflowed();
68
430427
  }
69
860854
  return result;
70
}
71
72
String fromDouble(double d) {
73
  std::ostringstream stream;
74
  stream.imbue(std::locale("C"));  // Ignore locale
75
  stream << d;
76
  return stream.str();
77
}
78
79
13989
double toDouble(const char* buffer, size_t length, bool* ok) {
80
13989
  std::istringstream stream(std::string(buffer, length));
81
13989
  stream.imbue(std::locale("C"));  // Ignore locale
82
  double d;
83
13989
  stream >> d;
84
13989
  *ok = !stream.fail();
85
13989
  return d;
86
}
87
88
}  // namespace StringUtil
89
}  // namespace protocol
90
}  // namespace inspector
91
}  // namespace node
92