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: 53 69 76.8 %
Date: 2020-02-19 22:14:06 Branches: 49 69 71.0 %

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
711241
enum encoding ParseEncoding(const char* encoding,
14
                            enum encoding default_encoding) {
15

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

15146
      if (encoding[1] == 't' && encoding[2] == 'f') {
19
        // Skip `-`
20
15107
        encoding += encoding[3] == '-' ? 4 : 3;
21

15107
        if (encoding[0] == '8' && encoding[1] == '\0')
22
15090
          return UTF8;
23
17
        if (strncmp(encoding, "16le", 4) == 0)
24
17
          return UCS2;
25
26
      // ucs2
27

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

38
        if (encoding[0] == '2' && encoding[1] == '\0')
30
38
          return UCS2;
31
      }
32
1
      break;
33
    case 'l':
34
      // latin1
35
61943
      if (encoding[1] == 'a') {
36
61941
        if (strncmp(encoding + 2, "tin1", 4) == 0)
37
61941
          return LATIN1;
38
      }
39
2
      break;
40
    case 'b':
41
      // binary
42
316415
      if (encoding[1] == 'i') {
43
19
        if (strncmp(encoding + 2, "nary", 4) == 0)
44
19
          return LATIN1;
45
46
      // buffer
47
316396
      } else if (encoding[1] == 'u') {
48
315900
        if (strncmp(encoding + 2, "ffer", 4) == 0)
49
315900
          return BUFFER;
50
      }
51
496
      break;
52
    case '\0':
53
1
      return default_encoding;
54
    default:
55
317736
      break;
56
  }
57
58
318235
  if (StringEqualNoCase(encoding, "utf8")) {
59
    return UTF8;
60
318235
  } else if (StringEqualNoCase(encoding, "utf-8")) {
61
2
    return UTF8;
62
318233
  } else if (StringEqualNoCase(encoding, "ascii")) {
63
94
    return ASCII;
64
318139
  } else if (StringEqualNoCase(encoding, "base64")) {
65
494
    return BASE64;
66
317645
  } else if (StringEqualNoCase(encoding, "ucs2")) {
67
    return UCS2;
68
317645
  } else if (StringEqualNoCase(encoding, "ucs-2")) {
69
    return UCS2;
70
317645
  } else if (StringEqualNoCase(encoding, "utf16le")) {
71
    return UCS2;
72
317645
  } else if (StringEqualNoCase(encoding, "utf-16le")) {
73
    return UCS2;
74
317645
  } else if (StringEqualNoCase(encoding, "latin1")) {
75
    return LATIN1;
76
317645
  } else if (StringEqualNoCase(encoding, "binary")) {
77
    return LATIN1;  // BINARY is a deprecated alias of LATIN1.
78
317645
  } else if (StringEqualNoCase(encoding, "buffer")) {
79
    return BUFFER;
80
317645
  } else if (StringEqualNoCase(encoding, "hex")) {
81
317640
    return HEX;
82
  } else {
83
5
    return default_encoding;
84
  }
85
}
86
87
88
725863
enum encoding ParseEncoding(Isolate* isolate,
89
                            Local<Value> encoding_v,
90
                            enum encoding default_encoding) {
91
725863
  CHECK(!encoding_v.IsEmpty());
92
93
1451726
  if (!encoding_v->IsString())
94
14622
    return default_encoding;
95
96
1422482
  Utf8Value encoding(isolate, encoding_v);
97
98
711241
  return ParseEncoding(*encoding, default_encoding);
99
}
100
101
2
Local<Value> Encode(Isolate* isolate,
102
                    const char* buf,
103
                    size_t len,
104
                    enum encoding encoding) {
105
2
  CHECK_NE(encoding, UCS2);
106
  Local<Value> error;
107
4
  return StringBytes::Encode(isolate, buf, len, encoding, &error)
108
2
      .ToLocalChecked();
109
}
110
111
Local<Value> Encode(Isolate* isolate, const uint16_t* buf, size_t len) {
112
  Local<Value> error;
113
  return StringBytes::Encode(isolate, buf, len, &error)
114
      .ToLocalChecked();
115
}
116
117
// Returns -1 if the handle was not valid for decoding
118
ssize_t DecodeBytes(Isolate* isolate,
119
                    Local<Value> val,
120
                    enum encoding encoding) {
121
  HandleScope scope(isolate);
122
123
  return StringBytes::Size(isolate, val, encoding).FromMaybe(-1);
124
}
125
126
// Returns number of bytes written.
127
ssize_t DecodeWrite(Isolate* isolate,
128
                    char* buf,
129
                    size_t buflen,
130
                    Local<Value> val,
131
                    enum encoding encoding) {
132
  return StringBytes::Write(isolate, buf, buflen, val, encoding, nullptr);
133
}
134
135
}  // namespace node