GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/json_utils.cc Lines: 37 37 100.0 %
Date: 2020-05-27 22:15:15 Branches: 20 20 100.0 %

Line Branch Exec Source
1
#include "json_utils.h"
2
3
namespace node {
4
5
11747
std::string EscapeJsonChars(const std::string& str) {
6
  const std::string control_symbols[0x20] = {
7
      "\\u0000", "\\u0001", "\\u0002", "\\u0003", "\\u0004", "\\u0005",
8
      "\\u0006", "\\u0007", "\\b", "\\t", "\\n", "\\v", "\\f", "\\r",
9
      "\\u000e", "\\u000f", "\\u0010", "\\u0011", "\\u0012", "\\u0013",
10
      "\\u0014", "\\u0015", "\\u0016", "\\u0017", "\\u0018", "\\u0019",
11
      "\\u001a", "\\u001b", "\\u001c", "\\u001d", "\\u001e", "\\u001f"
12
23494
  };
13
14
11747
  std::string ret;
15
11747
  size_t last_pos = 0;
16
11747
  size_t pos = 0;
17
329311
  for (; pos < str.size(); ++pos) {
18
317564
    std::string replace;
19
158782
    char ch = str[pos];
20
158782
    if (ch == '\\') {
21
1
      replace = "\\\\";
22
158781
    } else if (ch == '\"') {
23
5
      replace = "\\\"";
24
    } else {
25
158776
      size_t num = static_cast<size_t>(ch);
26
158776
      if (num < 0x20) replace = control_symbols[num];
27
    }
28
158782
    if (!replace.empty()) {
29
72
      if (pos > last_pos) {
30
40
        ret += str.substr(last_pos, pos - last_pos);
31
      }
32
72
      last_pos = pos + 1;
33
72
      ret += replace;
34
    }
35
  }
36
  // Append any remaining symbols.
37
11747
  if (last_pos < str.size()) {
38
11658
    ret += str.substr(last_pos, pos - last_pos);
39
  }
40
23494
  return ret;
41
}
42
43
2
std::string Reindent(const std::string& str, int indent_depth) {
44
4
  std::string indent;
45
2
  for (int i = 0; i < indent_depth; i++) indent += ' ';
46
47
2
  std::string out;
48
2
  std::string::size_type pos = 0;
49
  do {
50
1041
    std::string::size_type prev_pos = pos;
51
1041
    pos = str.find('\n', pos);
52
53
1041
    out.append(indent);
54
55
1041
    if (pos == std::string::npos) {
56
2
      out.append(str, prev_pos, std::string::npos);
57
2
      break;
58
    } else {
59
1039
      pos++;
60
1039
      out.append(str, prev_pos, pos - prev_pos);
61
1039
    }
62
  } while (true);
63
64
4
  return out;
65
}
66
67
}  // namespace node