GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/api/encoding.cc Lines: 57 79 72.2 %
Date: 2020-07-19 22:14:24 Branches: 57 88 64.8 %

Line Branch Exec Source
1
#include "node.h"
2
#include "string_bytes.h"
3
#include "util-inl.h"
4
#include "v8.h"
5
6
namespace node {
7
8
using v8::HandleScope;
9
using v8::Isolate;
10
using v8::Local;
11
using v8::Value;
12
13
1002749
enum encoding ParseEncoding(const char* encoding,
14
                            enum encoding default_encoding) {
15

1002749
  switch (encoding[0]) {
16
    case 'u':
17
    case 'U':
18
      // utf8, utf16le
19

76846
      if (encoding[1] == 't' && encoding[2] == 'f') {
20
        // Skip `-`
21
76805
        const size_t skip = encoding[3] == '-' ? 4 : 3;
22

76805
        if (encoding[skip] == '8' && encoding[skip + 1] == '\0')
23
76785
          return UTF8;
24
20
        if (strncmp(encoding + skip, "16le", 5) == 0)
25
17
          return UCS2;
26
      // ucs2
27

41
      } else if (encoding[1] == 'c' && encoding[2] == 's') {
28
38
        const size_t skip = encoding[3] == '-' ? 4 : 3;
29

38
        if (encoding[skip] == '2' && encoding[skip + 1] == '\0')
30
38
          return UCS2;
31
      }
32
6
      if (StringEqualNoCase(encoding, "utf8"))
33
        return UTF8;
34
6
      if (StringEqualNoCase(encoding, "utf-8"))
35
2
        return UTF8;
36
4
      if (StringEqualNoCase(encoding, "ucs2"))
37
        return UCS2;
38
4
      if (StringEqualNoCase(encoding, "ucs-2"))
39
        return UCS2;
40
4
      if (StringEqualNoCase(encoding, "utf16le"))
41
        return UCS2;
42
4
      if (StringEqualNoCase(encoding, "utf-16le"))
43
1
        return UCS2;
44
3
      break;
45
46
    case 'l':
47
    case 'L':
48
      // latin1
49
235502
      if (encoding[1] == 'a') {
50
235500
        if (strncmp(encoding + 2, "tin1", 5) == 0)
51
235500
          return LATIN1;
52
      }
53
2
      if (StringEqualNoCase(encoding, "latin1"))
54
        return LATIN1;
55
2
      break;
56
57
    case 'b':
58
    case 'B':
59
      // binary is a deprecated alias of latin1
60
345696
      if (encoding[1] == 'i') {
61
19
        if (strncmp(encoding + 2, "nary", 5) == 0)
62
19
          return LATIN1;
63
      // buffer
64
345677
      } else if (encoding[1] == 'u') {
65
344159
        if (strncmp(encoding + 2, "ffer", 5) == 0)
66
344159
          return BUFFER;
67
      // base64
68
1518
      } else if (encoding[1] == 'a') {
69
1516
        if (strncmp(encoding + 2, "se64", 5) == 0)
70
1516
          return BASE64;
71
      }
72
2
      if (StringEqualNoCase(encoding, "binary"))
73
        return LATIN1;  // BINARY is a deprecated alias of LATIN1.
74
2
      if (StringEqualNoCase(encoding, "buffer"))
75
        return BUFFER;
76
2
      if (StringEqualNoCase(encoding, "base64"))
77
        return BASE64;
78
2
      break;
79
80
    case 'a':
81
    case 'A':
82
      // ascii
83
97
      if (encoding[1] == 's') {
84
97
        if (strncmp(encoding + 2, "cii", 4) == 0)
85
97
          return ASCII;
86
      }
87
      if (StringEqualNoCase(encoding, "ascii"))
88
        return ASCII;
89
      break;
90
91
    case 'h':
92
    case 'H':
93
      // hex
94
344607
      if (encoding[1] == 'e')
95

344607
        if (encoding[2] == 'x' && encoding[3] == '\0')
96
344607
          return HEX;
97
      if (StringEqualNoCase(encoding, "hex"))
98
        return HEX;
99
      break;
100
  }
101
8
  return default_encoding;
102
}
103
104
105
1019025
enum encoding ParseEncoding(Isolate* isolate,
106
                            Local<Value> encoding_v,
107
                            enum encoding default_encoding) {
108
1019025
  CHECK(!encoding_v.IsEmpty());
109
110
2038050
  if (!encoding_v->IsString())
111
16276
    return default_encoding;
112
113
2005498
  Utf8Value encoding(isolate, encoding_v);
114
115
1002749
  return ParseEncoding(*encoding, default_encoding);
116
}
117
118
2
Local<Value> Encode(Isolate* isolate,
119
                    const char* buf,
120
                    size_t len,
121
                    enum encoding encoding) {
122
2
  CHECK_NE(encoding, UCS2);
123
  Local<Value> error;
124
4
  return StringBytes::Encode(isolate, buf, len, encoding, &error)
125
2
      .ToLocalChecked();
126
}
127
128
Local<Value> Encode(Isolate* isolate, const uint16_t* buf, size_t len) {
129
  Local<Value> error;
130
  return StringBytes::Encode(isolate, buf, len, &error)
131
      .ToLocalChecked();
132
}
133
134
// Returns -1 if the handle was not valid for decoding
135
ssize_t DecodeBytes(Isolate* isolate,
136
                    Local<Value> val,
137
                    enum encoding encoding) {
138
  HandleScope scope(isolate);
139
140
  return StringBytes::Size(isolate, val, encoding).FromMaybe(-1);
141
}
142
143
// Returns number of bytes written.
144
ssize_t DecodeWrite(Isolate* isolate,
145
                    char* buf,
146
                    size_t buflen,
147
                    Local<Value> val,
148
                    enum encoding encoding) {
149
  return StringBytes::Write(isolate, buf, buflen, val, encoding, nullptr);
150
}
151
152

14970
}  // namespace node