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 68 77.9 %
Date: 2019-10-08 22:34:21 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
1754108
enum encoding ParseEncoding(const char* encoding,
15
                            enum encoding default_encoding) {
16

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

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

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

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

38
        if (encoding[0] == '2' && encoding[1] == '\0')
31
38
          return UCS2;
32
      }
33
1
      break;
34
    case 'l':
35
      // latin1
36
276203
      if (encoding[1] == 'a') {
37
276201
        if (strncmp(encoding + 2, "tin1", 4) == 0)
38
276201
          return LATIN1;
39
      }
40
2
      break;
41
    case 'b':
42
      // binary
43
406062
      if (encoding[1] == 'i') {
44
19
        if (strncmp(encoding + 2, "nary", 4) == 0)
45
19
          return LATIN1;
46
47
      // buffer
48
406043
      } else if (encoding[1] == 'u') {
49
405421
        if (strncmp(encoding + 2, "ffer", 4) == 0)
50
405421
          return BUFFER;
51
      }
52
622
      break;
53
    case '\0':
54
1
      return default_encoding;
55
    default:
56
504101
      break;
57
  }
58
59
504726
  if (StringEqualNoCase(encoding, "utf8")) {
60
    return UTF8;
61
504726
  } else if (StringEqualNoCase(encoding, "utf-8")) {
62
2
    return UTF8;
63
504724
  } else if (StringEqualNoCase(encoding, "ascii")) {
64
98386
    return ASCII;
65
406338
  } else if (StringEqualNoCase(encoding, "base64")) {
66
620
    return BASE64;
67
405718
  } else if (StringEqualNoCase(encoding, "ucs2")) {
68
    return UCS2;
69
405718
  } else if (StringEqualNoCase(encoding, "ucs-2")) {
70
    return UCS2;
71
405718
  } else if (StringEqualNoCase(encoding, "utf16le")) {
72
    return UCS2;
73
405718
  } else if (StringEqualNoCase(encoding, "utf-16le")) {
74
    return UCS2;
75
405718
  } else if (StringEqualNoCase(encoding, "latin1")) {
76
    return LATIN1;
77
405718
  } else if (StringEqualNoCase(encoding, "binary")) {
78
    return LATIN1;  // BINARY is a deprecated alias of LATIN1.
79
405718
  } else if (StringEqualNoCase(encoding, "buffer")) {
80
    return BUFFER;
81
405718
  } else if (StringEqualNoCase(encoding, "hex")) {
82
405713
    return HEX;
83
  } else {
84
5
    return default_encoding;
85
  }
86
}
87
88
89
1769795
enum encoding ParseEncoding(Isolate* isolate,
90
                            Local<Value> encoding_v,
91
                            enum encoding default_encoding) {
92
1769795
  CHECK(!encoding_v.IsEmpty());
93
94
3539590
  if (!encoding_v->IsString())
95
15687
    return default_encoding;
96
97
1754108
  Utf8Value encoding(isolate, encoding_v);
98
99
1754108
  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