GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/string_bytes.h Lines: 12 13 92.3 %
Date: 2019-07-28 22:34:34 Branches: 3 4 75.0 %

Line Branch Exec Source
1
// Copyright Joyent, Inc. and other Node contributors.
2
//
3
// Permission is hereby granted, free of charge, to any person obtaining a
4
// copy of this software and associated documentation files (the
5
// "Software"), to deal in the Software without restriction, including
6
// without limitation the rights to use, copy, modify, merge, publish,
7
// distribute, sublicense, and/or sell copies of the Software, and to permit
8
// persons to whom the Software is furnished to do so, subject to the
9
// following conditions:
10
//
11
// The above copyright notice and this permission notice shall be included
12
// in all copies or substantial portions of the Software.
13
//
14
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22
#ifndef SRC_STRING_BYTES_H_
23
#define SRC_STRING_BYTES_H_
24
25
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
26
27
// Decodes a v8::Local<v8::String> or Buffer to a raw char*
28
29
#include "v8.h"
30
#include "env.h"
31
32
namespace node {
33
34
class StringBytes {
35
 public:
36
805222
  class InlineDecoder : public MaybeStackBuffer<char> {
37
   public:
38
402611
    inline v8::Maybe<bool> Decode(Environment* env,
39
                                  v8::Local<v8::String> string,
40
                                  v8::Local<v8::Value> encoding,
41
                                  enum encoding _default) {
42
402611
      enum encoding enc = ParseEncoding(env->isolate(), encoding, _default);
43
402611
      if (!StringBytes::IsValidString(string, enc)) {
44
3
        env->ThrowTypeError("Bad input string");
45
3
        return v8::Nothing<bool>();
46
      }
47
48
      size_t storage;
49
805216
      if (!StringBytes::StorageSize(env->isolate(), string, enc).To(&storage))
50
        return v8::Nothing<bool>();
51
402608
      AllocateSufficientStorage(storage);
52
      const size_t length =
53
402608
          StringBytes::Write(env->isolate(), out(), storage, string, enc);
54
55
      // No zero terminator is included when using this method.
56
402608
      SetLength(length);
57
402608
      return v8::Just(true);
58
    }
59
60
402608
    inline size_t size() const { return length(); }
61
  };
62
63
  // Does the string match the encoding? Quick but non-exhaustive.
64
  // Example: a HEX string must have a length that's a multiple of two.
65
  // FIXME(bnoordhuis) IsMaybeValidString()? Naming things is hard...
66
  static bool IsValidString(v8::Local<v8::String> string,
67
                            enum encoding enc);
68
69
  // Fast, but can be 2 bytes oversized for Base64, and
70
  // as much as triple UTF-8 strings <= 65536 chars in length
71
  static v8::Maybe<size_t> StorageSize(v8::Isolate* isolate,
72
                                       v8::Local<v8::Value> val,
73
                                       enum encoding enc);
74
75
  // Precise byte count, but slightly slower for Base64 and
76
  // very much slower for UTF-8
77
  static v8::Maybe<size_t> Size(v8::Isolate* isolate,
78
                                v8::Local<v8::Value> val,
79
                                enum encoding enc);
80
81
  // Write the bytes from the string or buffer into the char*
82
  // returns the number of bytes written, which will always be
83
  // <= buflen.  Use StorageSize/Size first to know how much
84
  // memory to allocate.
85
  static size_t Write(v8::Isolate* isolate,
86
                      char* buf,
87
                      size_t buflen,
88
                      v8::Local<v8::Value> val,
89
                      enum encoding enc,
90
                      int* chars_written = nullptr);
91
92
  // Take the bytes in the src, and turn it into a Buffer or String.
93
  static v8::MaybeLocal<v8::Value> Encode(v8::Isolate* isolate,
94
                                          const char* buf,
95
                                          size_t buflen,
96
                                          enum encoding encoding,
97
                                          v8::Local<v8::Value>* error);
98
99
  // Warning: This reverses endianness on BE platforms, even though the
100
  // signature using uint16_t implies that it should not.
101
  // However, the brokenness is already public API and can't therefore
102
  // be changed easily.
103
  static v8::MaybeLocal<v8::Value> Encode(v8::Isolate* isolate,
104
                                          const uint16_t* buf,
105
                                          size_t buflen,
106
                                          v8::Local<v8::Value>* error);
107
108
  static v8::MaybeLocal<v8::Value> Encode(v8::Isolate* isolate,
109
                                          const char* buf,
110
                                          enum encoding encoding,
111
                                          v8::Local<v8::Value>* error);
112
113
 private:
114
  static size_t WriteUCS2(v8::Isolate* isolate,
115
                          char* buf,
116
                          size_t buflen,
117
                          v8::Local<v8::String> str,
118
                          int flags,
119
                          size_t* chars_written);
120
};
121
122
}  // namespace node
123
124
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
125
126
#endif  // SRC_STRING_BYTES_H_