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: 8 9 88.9 %
Date: 2019-09-07 22:28:56 Branches: 1 2 50.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
802024
  class InlineDecoder : public MaybeStackBuffer<char> {
37
   public:
38
401012
    inline v8::Maybe<bool> Decode(Environment* env,
39
                                  v8::Local<v8::String> string,
40
                                  enum encoding enc) {
41
      size_t storage;
42
802024
      if (!StringBytes::StorageSize(env->isolate(), string, enc).To(&storage))
43
        return v8::Nothing<bool>();
44
401012
      AllocateSufficientStorage(storage);
45
      const size_t length =
46
401012
          StringBytes::Write(env->isolate(), out(), storage, string, enc);
47
48
      // No zero terminator is included when using this method.
49
401012
      SetLength(length);
50
401012
      return v8::Just(true);
51
    }
52
53
401012
    inline size_t size() const { return length(); }
54
  };
55
56
  // Fast, but can be 2 bytes oversized for Base64, and
57
  // as much as triple UTF-8 strings <= 65536 chars in length
58
  static v8::Maybe<size_t> StorageSize(v8::Isolate* isolate,
59
                                       v8::Local<v8::Value> val,
60
                                       enum encoding enc);
61
62
  // Precise byte count, but slightly slower for Base64 and
63
  // very much slower for UTF-8
64
  static v8::Maybe<size_t> Size(v8::Isolate* isolate,
65
                                v8::Local<v8::Value> val,
66
                                enum encoding enc);
67
68
  // Write the bytes from the string or buffer into the char*
69
  // returns the number of bytes written, which will always be
70
  // <= buflen.  Use StorageSize/Size first to know how much
71
  // memory to allocate.
72
  static size_t Write(v8::Isolate* isolate,
73
                      char* buf,
74
                      size_t buflen,
75
                      v8::Local<v8::Value> val,
76
                      enum encoding enc,
77
                      int* chars_written = nullptr);
78
79
  // Take the bytes in the src, and turn it into a Buffer or String.
80
  static v8::MaybeLocal<v8::Value> Encode(v8::Isolate* isolate,
81
                                          const char* buf,
82
                                          size_t buflen,
83
                                          enum encoding encoding,
84
                                          v8::Local<v8::Value>* error);
85
86
  // Warning: This reverses endianness on BE platforms, even though the
87
  // signature using uint16_t implies that it should not.
88
  // However, the brokenness is already public API and can't therefore
89
  // be changed easily.
90
  static v8::MaybeLocal<v8::Value> Encode(v8::Isolate* isolate,
91
                                          const uint16_t* buf,
92
                                          size_t buflen,
93
                                          v8::Local<v8::Value>* error);
94
95
  static v8::MaybeLocal<v8::Value> Encode(v8::Isolate* isolate,
96
                                          const char* buf,
97
                                          enum encoding encoding,
98
                                          v8::Local<v8::Value>* error);
99
100
 private:
101
  static size_t WriteUCS2(v8::Isolate* isolate,
102
                          char* buf,
103
                          size_t buflen,
104
                          v8::Local<v8::String> str,
105
                          int flags,
106
                          size_t* chars_written);
107
};
108
109
}  // namespace node
110
111
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
112
113
#endif  // SRC_STRING_BYTES_H_