diff options
author | Joyee Cheung <joyeec9h3@gmail.com> | 2019-01-17 03:00:55 +0800 |
---|---|---|
committer | Joyee Cheung <joyeec9h3@gmail.com> | 2019-02-01 07:43:27 +0800 |
commit | ca9e24e8b43b89dc705227a4ed29172c2a95f57f (patch) | |
tree | daba09ee200dab1bff3fb7bc02124b88756b133d /src/api/encoding.cc | |
parent | 99c3243b229d4e552eadf1abf296440e20c604f8 (diff) | |
download | android-node-v8-ca9e24e8b43b89dc705227a4ed29172c2a95f57f.tar.gz android-node-v8-ca9e24e8b43b89dc705227a4ed29172c2a95f57f.tar.bz2 android-node-v8-ca9e24e8b43b89dc705227a4ed29172c2a95f57f.zip |
src: move public C++ APIs into src/api/*.cc
This patch moves most of the public C++ APIs into src/api/*.cc
so that it's easier to tell that we need to be careful about
the compatibility of these code.
Some APIs, like `node::LoadEnvironmet()`, `node::Start()` and
`node::Init()` still stay in `node.cc` because they are still
very specific to our use cases and do not work quite well yet
for embedders anyway - we could not even manage to write cctest for
them at the moment.
PR-URL: https://github.com/nodejs/node/pull/25541
Reviewed-By: Gus Caplan <me@gus.host>
Diffstat (limited to 'src/api/encoding.cc')
-rw-r--r-- | src/api/encoding.cc | 136 |
1 files changed, 136 insertions, 0 deletions
diff --git a/src/api/encoding.cc b/src/api/encoding.cc new file mode 100644 index 0000000000..21d3275097 --- /dev/null +++ b/src/api/encoding.cc @@ -0,0 +1,136 @@ +#include "node.h" +#include "env-inl.h" +#include "string_bytes.h" +#include "util-inl.h" +#include "v8.h" + +namespace node { + +using v8::HandleScope; +using v8::Isolate; +using v8::Local; +using v8::Value; + +enum encoding ParseEncoding(const char* encoding, + enum encoding default_encoding) { + switch (encoding[0]) { + case 'u': + // utf8, utf16le + if (encoding[1] == 't' && encoding[2] == 'f') { + // Skip `-` + encoding += encoding[3] == '-' ? 4 : 3; + if (encoding[0] == '8' && encoding[1] == '\0') + return UTF8; + if (strncmp(encoding, "16le", 4) == 0) + return UCS2; + + // ucs2 + } else if (encoding[1] == 'c' && encoding[2] == 's') { + encoding += encoding[3] == '-' ? 4 : 3; + if (encoding[0] == '2' && encoding[1] == '\0') + return UCS2; + } + break; + case 'l': + // latin1 + if (encoding[1] == 'a') { + if (strncmp(encoding + 2, "tin1", 4) == 0) + return LATIN1; + } + break; + case 'b': + // binary + if (encoding[1] == 'i') { + if (strncmp(encoding + 2, "nary", 4) == 0) + return LATIN1; + + // buffer + } else if (encoding[1] == 'u') { + if (strncmp(encoding + 2, "ffer", 4) == 0) + return BUFFER; + } + break; + case '\0': + return default_encoding; + default: + break; + } + + if (StringEqualNoCase(encoding, "utf8")) { + return UTF8; + } else if (StringEqualNoCase(encoding, "utf-8")) { + return UTF8; + } else if (StringEqualNoCase(encoding, "ascii")) { + return ASCII; + } else if (StringEqualNoCase(encoding, "base64")) { + return BASE64; + } else if (StringEqualNoCase(encoding, "ucs2")) { + return UCS2; + } else if (StringEqualNoCase(encoding, "ucs-2")) { + return UCS2; + } else if (StringEqualNoCase(encoding, "utf16le")) { + return UCS2; + } else if (StringEqualNoCase(encoding, "utf-16le")) { + return UCS2; + } else if (StringEqualNoCase(encoding, "latin1")) { + return LATIN1; + } else if (StringEqualNoCase(encoding, "binary")) { + return LATIN1; // BINARY is a deprecated alias of LATIN1. + } else if (StringEqualNoCase(encoding, "buffer")) { + return BUFFER; + } else if (StringEqualNoCase(encoding, "hex")) { + return HEX; + } else { + return default_encoding; + } +} + + +enum encoding ParseEncoding(Isolate* isolate, + Local<Value> encoding_v, + enum encoding default_encoding) { + CHECK(!encoding_v.IsEmpty()); + + if (!encoding_v->IsString()) + return default_encoding; + + Utf8Value encoding(isolate, encoding_v); + + return ParseEncoding(*encoding, default_encoding); +} + +Local<Value> Encode(Isolate* isolate, + const char* buf, + size_t len, + enum encoding encoding) { + CHECK_NE(encoding, UCS2); + Local<Value> error; + return StringBytes::Encode(isolate, buf, len, encoding, &error) + .ToLocalChecked(); +} + +Local<Value> Encode(Isolate* isolate, const uint16_t* buf, size_t len) { + Local<Value> error; + return StringBytes::Encode(isolate, buf, len, &error) + .ToLocalChecked(); +} + +// Returns -1 if the handle was not valid for decoding +ssize_t DecodeBytes(Isolate* isolate, + Local<Value> val, + enum encoding encoding) { + HandleScope scope(isolate); + + return StringBytes::Size(isolate, val, encoding).FromMaybe(-1); +} + +// Returns number of bytes written. +ssize_t DecodeWrite(Isolate* isolate, + char* buf, + size_t buflen, + Local<Value> val, + enum encoding encoding) { + return StringBytes::Write(isolate, buf, buflen, val, encoding, nullptr); +} + +} // namespace node |