GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node_i18n.h Lines: 13 16 81.2 %
Date: 2022-12-31 04:22:30 Branches: 2 2 100.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_NODE_I18N_H_
23
#define SRC_NODE_I18N_H_
24
25
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
26
27
#if defined(NODE_HAVE_I18N_SUPPORT)
28
29
#include "base_object.h"
30
#include "env.h"
31
#include "util.h"
32
#include "v8.h"
33
34
#include <unicode/ucnv.h>
35
36
#include <string>
37
38
namespace node {
39
namespace i18n {
40
41
bool InitializeICUDirectory(const std::string& path);
42
43
void SetDefaultTimeZone(const char* tzid);
44
45
enum class idna_mode {
46
  // Default mode for maximum compatibility.
47
  kDefault,
48
  // Ignore all errors in IDNA conversion, if possible.
49
  kLenient,
50
  // Enforce STD3 rules (UseSTD3ASCIIRules) and DNS length restrictions
51
  // (VerifyDnsLength). Corresponds to `beStrict` flag in the "domain to ASCII"
52
  // algorithm.
53
  kStrict
54
};
55
56
// Implements the WHATWG URL Standard "domain to ASCII" algorithm.
57
// https://url.spec.whatwg.org/#concept-domain-to-ascii
58
int32_t ToASCII(MaybeStackBuffer<char>* buf,
59
                const char* input,
60
                size_t length,
61
                idna_mode mode = idna_mode::kDefault);
62
63
// Implements the WHATWG URL Standard "domain to Unicode" algorithm.
64
// https://url.spec.whatwg.org/#concept-domain-to-unicode
65
int32_t ToUnicode(MaybeStackBuffer<char>* buf,
66
                  const char* input,
67
                  size_t length);
68
69
struct ConverterDeleter {
70
269
  void operator()(UConverter* pointer) const { ucnv_close(pointer); }
71
};
72
using ConverterPointer = std::unique_ptr<UConverter, ConverterDeleter>;
73
74
class Converter {
75
 public:
76
  explicit Converter(const char* name, const char* sub = nullptr);
77
  explicit Converter(UConverter* converter, const char* sub = nullptr);
78
79
1469
  UConverter* conv() const { return conv_.get(); }
80
81
  size_t max_char_size() const;
82
  size_t min_char_size() const;
83
  void reset();
84
  void set_subst_chars(const char* sub = nullptr);
85
86
 private:
87
  ConverterPointer conv_;
88
};
89
90
class ConverterObject : public BaseObject, Converter {
91
 public:
92
  enum ConverterFlags {
93
    CONVERTER_FLAGS_FLUSH      = 0x1,
94
    CONVERTER_FLAGS_FATAL      = 0x2,
95
    CONVERTER_FLAGS_IGNORE_BOM = 0x4,
96
    CONVERTER_FLAGS_UNICODE    = 0x8,
97
    CONVERTER_FLAGS_BOM_SEEN   = 0x10,
98
  };
99
100
  static void Create(const v8::FunctionCallbackInfo<v8::Value>& args);
101
  static void Decode(const v8::FunctionCallbackInfo<v8::Value>& args);
102
  static void Has(const v8::FunctionCallbackInfo<v8::Value>& args);
103
104
  SET_NO_MEMORY_INFO()
105
  SET_MEMORY_INFO_NAME(ConverterObject)
106
  SET_SELF_SIZE(ConverterObject)
107
108
 protected:
109
  ConverterObject(Environment* env,
110
                  v8::Local<v8::Object> wrap,
111
                  UConverter* converter,
112
                  int flags,
113
                  const char* sub = nullptr);
114
115
446
  void set_bom_seen(bool seen) {
116
446
    if (seen)
117
167
      flags_ |= CONVERTER_FLAGS_BOM_SEEN;
118
    else
119
279
      flags_ &= ~CONVERTER_FLAGS_BOM_SEEN;
120
446
  }
121
122
654
  bool bom_seen() const {
123
654
    return (flags_ & CONVERTER_FLAGS_BOM_SEEN) == CONVERTER_FLAGS_BOM_SEEN;
124
  }
125
126
754
  bool unicode() const {
127
754
    return (flags_ & CONVERTER_FLAGS_UNICODE) == CONVERTER_FLAGS_UNICODE;
128
  }
129
130
748
  bool ignore_bom() const {
131
748
    return (flags_ & CONVERTER_FLAGS_IGNORE_BOM) == CONVERTER_FLAGS_IGNORE_BOM;
132
  }
133
134
 private:
135
  int flags_ = 0;
136
};
137
138
}  // namespace i18n
139
}  // namespace node
140
141
#endif  // NODE_HAVE_I18N_SUPPORT
142
143
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
144
145
#endif  // SRC_NODE_I18N_H_