diff --git a/README.md b/README.md index 1fc35cfb15f8e70ba5283cd30f29cdd074bb355c..62e74c5ede9a118a0ce3ba5022c1b400ce9cf33c 100755 --- a/README.md +++ b/README.md @@ -134,8 +134,9 @@ base/compileruntime/js_util_module/ ### Interface description | Interface name | Description | | -------- | -------- | -| readonly encoding : string | In the TextEncoder module, get the encoding format, only UTF-8 is supported. | -| encode(input : string) : Uint8Array | Input string string, encode and output UTF-8 byte stream. | +| constructor(encoding? : string) | Constructor, the parameter encoding indicates the format of encoding. Default utf-8, Support gb18030, gbk, gb2312. | +| readonly encoding : string | In the TextEncoder module, get the encoding format. | +| encode(input : string) : Uint8Array | Input string string, encoding according to encoding and output uint8 byte stream. | | encodeInto(input : string, dest : Uint8Array) : {read : number, written : number} | Enter the string string, dest represents the storage location after encoding, and returns an object, read represents the number of characters that have been encoded,and written represents the size of bytes occupied by the encoded characters. | | constructor(encoding? : string, options? : {fatal? : boolean, ignoreBOM? : boolean}) | Constructor, the first parameter encoding indicates the format of decoding.The second parameter represents some attributes.Fatal in the attribute indicates whether an exception is thrown, and ignoreBOM indicates whether to ignore the bom flag. | | readonly encoding : string | In the TextDecoder module, get the set decoding format. | diff --git a/README_zh.md b/README_zh.md index 4b1002c2b43aed6f9f0f6c4e95fc5d9f315cce7b..554f7588b82c1dc69738df21963415e8b60b7e67 100644 --- a/README_zh.md +++ b/README_zh.md @@ -135,8 +135,9 @@ base/compileruntime/js_util_module/ | 接口名 | 说明 | | -------- | -------- | +| constructor(encoding? : string) | 构造函数,参数encoding表示编码的格式。默认utf-8, 支持gb18030, gbk, gb2312. | | readonly encoding : string | 在TextEncoder类中,获取编码的格式,只支持UTF-8。 | -| encode(input : string) : Uint8Array | 输入stirng字符串,编码并输出UTF-8字节流。 | +| encode(input : string) : Uint8Array | 输入stirng字符串,根据encodeing编码并输出uint8字节流。 | | encodeInto(input : string, dest : Uint8Array) : {read : number, written : number} | 输入stirng字符串,dest表示编码后存放位置,返回一个对象,read表示已经编码的字符的个数,written表示已编码字符所占字节的大小。 | | constructor(encoding? : string, options? : {fatal? : boolean, ignoreBOM? : boolean}) | 构造函数,第一个参数encoding表示解码的格式。第二个参数表示一些属性。属性中fatal表示是否抛出异常,ignoreBOM表示是否忽略bom标志。 | | readonly encoding : string | 在TextDecoder类中,获取设置的解码格式。 | diff --git a/util/js_textencoder.cpp b/util/js_textencoder.cpp index b27a8ea677a228d8d85a870bba9936fae473893a..7be4aa8a46be554321f0bc2bbcf728aaa4748764 100755 --- a/util/js_textencoder.cpp +++ b/util/js_textencoder.cpp @@ -15,11 +15,10 @@ #include "js_textencoder.h" -#include - #include "native_engine.h" #include "securec.h" #include "utils/log.h" + namespace OHOS::Util { napi_value TextEncoder::GetEncoding(napi_env env) const { @@ -32,26 +31,33 @@ namespace OHOS::Util { napi_value TextEncoder::Encode(napi_env env, napi_value src) const { std::string buffer = ""; - size_t bufferSize = 0; - if (napi_get_value_string_utf8(env, src, nullptr, 0, &bufferSize) != napi_ok) { - HILOG_ERROR("can not get src size"); - return nullptr; - } - buffer.reserve(bufferSize + 1); - buffer.resize(bufferSize); - if (napi_get_value_string_utf8(env, src, buffer.data(), bufferSize + 1, &bufferSize) != napi_ok) { - HILOG_ERROR("can not get src value"); - return nullptr; + if (!(encoding_ == "utf-8" || encoding_ == "UTF-8")) { + NativeEngine *engine = reinterpret_cast(env); + NativeValue *nativeValue = reinterpret_cast(src); + engine->EncodeToChinese(nativeValue, buffer, encoding_); + } else { + size_t bufferSize = 0; + if (napi_get_value_string_utf8(env, src, nullptr, 0, &bufferSize) != napi_ok) { + HILOG_ERROR("can not get src size"); + return nullptr; + } + buffer.resize(bufferSize); + if (napi_get_value_string_utf8(env, src, buffer.data(), bufferSize + 1, &bufferSize) != napi_ok) { + HILOG_ERROR("can not get src value"); + return nullptr; + } } + + size_t outLen = buffer.length(); void *data = nullptr; napi_value arrayBuffer = nullptr; - napi_create_arraybuffer(env, bufferSize, &data, &arrayBuffer); - if (memcpy_s(data, bufferSize, reinterpret_cast(buffer.data()), bufferSize) != EOK) { + napi_create_arraybuffer(env, outLen, &data, &arrayBuffer); + if (memcpy_s(data, outLen, reinterpret_cast(buffer.data()), outLen) != EOK) { HILOG_ERROR("copy buffer to arraybuffer error"); return nullptr; } napi_value result = nullptr; - NAPI_CALL(env, napi_create_typedarray(env, napi_uint8_array, bufferSize, arrayBuffer, 0, &result)); + napi_create_typedarray(env, napi_uint8_array, outLen, arrayBuffer, 0, &result); return result; } diff --git a/util/js_textencoder.h b/util/js_textencoder.h index ac5a93f97c3881f250e42df7494044786b28d275..03e2f3eefe7e11e10502850e0852722549101a5e 100755 --- a/util/js_textencoder.h +++ b/util/js_textencoder.h @@ -26,8 +26,9 @@ namespace OHOS::Util { /** * Constructor of textdecoder. * + * @param encoding Encoding format */ - explicit TextEncoder() : encoding_("utf-8") {} + explicit TextEncoder(const std::string &encoding) : encoding_(encoding) {} /** * Destructor of textencoder. diff --git a/util/native_module_util.cpp b/util/native_module_util.cpp index 542620d01db99b44fd5eec689a0efdb34e828cfe..b866bf7534a8933810483eff83bbd17d334c5758 100755 --- a/util/native_module_util.cpp +++ b/util/native_module_util.cpp @@ -392,15 +392,47 @@ namespace OHOS::Util { return retVal; } + static bool CheckEncodingFormat(std::string &encoding) + { + const std::string conventFormat("utf8-t,UTF-8,gbk,GBK,GB2312,gb2312,GB18030,gb18030"); + if (conventFormat.find(encoding.c_str()) != conventFormat.npos) { + return true; + } + return false; + } + // Encoder static napi_value TextEncoderConstructor(napi_env env, napi_callback_info info) { + size_t argc = 0; napi_value thisVar = nullptr; - void *data = nullptr; - NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, &data)); - - auto object = new TextEncoder(); - NAPI_CALL(env, napi_wrap( + napi_value src = nullptr; + napi_get_cb_info(env, info, &argc, &src, &thisVar, nullptr); + std::string enconding = "utf-8"; + if (argc == 1) { + napi_get_cb_info(env, info, &argc, &src, nullptr, nullptr); + + napi_valuetype valuetype; + napi_typeof(env, src, &valuetype); + NAPI_ASSERT(env, valuetype == napi_string, "Wrong argument type. String expected."); + std::string buffer = ""; + size_t bufferSize = 0; + if (napi_get_value_string_utf8(env, src, nullptr, 0, &bufferSize) != napi_ok) { + HILOG_ERROR("can not get src size"); + return nullptr; + } + buffer.reserve(bufferSize + 1); + buffer.resize(bufferSize); + if (napi_get_value_string_utf8(env, src, buffer.data(), bufferSize + 1, &bufferSize) != napi_ok) { + HILOG_ERROR("can not get src value"); + return nullptr; + } + NAPI_ASSERT(env, CheckEncodingFormat(buffer), + "Wrong encoding format, only support utf-8 gbk gb2312 gb18030"); + enconding = buffer; + } + auto object = new TextEncoder(enconding); + napi_wrap( env, thisVar, object, [](napi_env environment, void *data, void *hint) { auto obj = reinterpret_cast(data); @@ -408,7 +440,7 @@ namespace OHOS::Util { delete obj; } }, - nullptr, nullptr)); + nullptr, nullptr); return thisVar; }