GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage/nodes/benchmark/out/../src/node_encoding.cc Lines: 53 68 77.9 %
Date: 2019-01-07 12:15:22 Branches: 49 69 71.0 %

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

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

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

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

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

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