!125 encode adds gb18030 gbk gb2312 format

Merge pull request !125 from 王犇/master
This commit is contained in:
openharmony_ci
2022-06-17 10:39:04 +00:00
committed by Gitee
5 changed files with 65 additions and 24 deletions
+3 -2
View File
@@ -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. |
+2 -1
View File
@@ -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类中,获取设置的解码格式。 |
+21 -15
View File
@@ -15,11 +15,10 @@
#include "js_textencoder.h"
#include <string>
#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<NativeEngine*>(env);
NativeValue *nativeValue = reinterpret_cast<NativeValue*>(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<void*>(buffer.data()), bufferSize) != EOK) {
napi_create_arraybuffer(env, outLen, &data, &arrayBuffer);
if (memcpy_s(data, outLen, reinterpret_cast<void*>(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;
}
+2 -1
View File
@@ -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.
+37 -5
View File
@@ -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));
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);
auto object = new TextEncoder();
NAPI_CALL(env, napi_wrap(
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<TextEncoder*>(data);
@@ -408,7 +440,7 @@ namespace OHOS::Util {
delete obj;
}
},
nullptr, nullptr));
nullptr, nullptr);
return thisVar;
}