diff options
author | James M Snell <jasnell@gmail.com> | 2020-03-23 01:33:34 +0300 |
---|---|---|
committer | Myles Borins <mylesborins@google.com> | 2020-04-01 01:08:59 +0300 |
commit | 5080491ae45b07f2cbb4d555c3a993d82c858c36 (patch) | |
tree | 5b3bfe3e8aa26af4337b34c3a087e6260f1c2afe /src/node_i18n.h | |
parent | 4f98b7178dcfd4411e2f0878b11f7dd0ec1180de (diff) |
src: refactoring and cleanup of node_i18n
Signed-off-by: James M Snell <jasnell@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/32438
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Diffstat (limited to 'src/node_i18n.h')
-rw-r--r-- | src/node_i18n.h | 78 |
1 files changed, 76 insertions, 2 deletions
diff --git a/src/node_i18n.h b/src/node_i18n.h index a0a398ac818..5c1501ea190 100644 --- a/src/node_i18n.h +++ b/src/node_i18n.h @@ -24,11 +24,16 @@ #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS +#if defined(NODE_HAVE_I18N_SUPPORT) + +#include "base_object.h" +#include "env.h" #include "util.h" +#include "v8.h" -#include <string> +#include <unicode/ucnv.h> -#if defined(NODE_HAVE_I18N_SUPPORT) +#include <string> namespace node { @@ -60,6 +65,75 @@ int32_t ToUnicode(MaybeStackBuffer<char>* buf, const char* input, size_t length); +struct ConverterDeleter { + void operator()(UConverter* pointer) const { ucnv_close(pointer); } +}; +using ConverterPointer = std::unique_ptr<UConverter, ConverterDeleter>; + +class Converter { + public: + explicit Converter(const char* name, const char* sub = nullptr); + explicit Converter(UConverter* converter, const char* sub = nullptr); + + UConverter* conv() const { return conv_.get(); } + + size_t max_char_size() const; + size_t min_char_size() const; + void reset(); + void set_subst_chars(const char* sub = nullptr); + + private: + ConverterPointer conv_; +}; + +class ConverterObject : public BaseObject, Converter { + public: + enum ConverterFlags { + CONVERTER_FLAGS_FLUSH = 0x1, + CONVERTER_FLAGS_FATAL = 0x2, + CONVERTER_FLAGS_IGNORE_BOM = 0x4, + CONVERTER_FLAGS_UNICODE = 0x8, + CONVERTER_FLAGS_BOM_SEEN = 0x10, + }; + + static void Create(const v8::FunctionCallbackInfo<v8::Value>& args); + static void Decode(const v8::FunctionCallbackInfo<v8::Value>& args); + static void Has(const v8::FunctionCallbackInfo<v8::Value>& args); + + SET_NO_MEMORY_INFO() + SET_MEMORY_INFO_NAME(ConverterObject) + SET_SELF_SIZE(ConverterObject) + + protected: + ConverterObject(Environment* env, + v8::Local<v8::Object> wrap, + UConverter* converter, + int flags, + const char* sub = nullptr); + + void set_bom_seen(bool seen) { + if (seen) + flags_ |= CONVERTER_FLAGS_BOM_SEEN; + else + flags_ &= ~CONVERTER_FLAGS_BOM_SEEN; + } + + bool bom_seen() const { + return (flags_ & CONVERTER_FLAGS_BOM_SEEN) == CONVERTER_FLAGS_BOM_SEEN; + } + + bool unicode() const { + return (flags_ & CONVERTER_FLAGS_UNICODE) == CONVERTER_FLAGS_UNICODE; + } + + bool ignore_bom() const { + return (flags_ & CONVERTER_FLAGS_IGNORE_BOM) == CONVERTER_FLAGS_IGNORE_BOM; + } + + private: + int flags_ = 0; +}; + } // namespace i18n } // namespace node |