Optimization time-consuming process

https://gitee.com/openharmony/commonlibrary_ets_utils/issues/I9PWB1

Signed-off-by: jiangkai43 <jiangkai43@huawei.com>
This commit is contained in:
jiangkai43 2024-05-16 17:48:16 +08:00
parent 3dec48d51d
commit ee4ebd9d0c
3 changed files with 17 additions and 16 deletions

View File

@ -74,11 +74,11 @@ namespace OHOS::Util {
static_cast<uint8_t>(ConverterFlags::FLUSH_FLG);
napi_typedarray_type type;
size_t length = 0;
void *data1 = nullptr;
void *data = nullptr;
size_t byteOffset = 0;
napi_value arrayBuffer = nullptr;
NAPI_CALL(env, napi_get_typedarray_info(env, src, &type, &length, &data1, &arrayBuffer, &byteOffset));
const char *source = static_cast<char*>(data1);
NAPI_CALL(env, napi_get_typedarray_info(env, src, &type, &length, &data, &arrayBuffer, &byteOffset));
const char *source = ReplaceNull(data, length);
size_t limit = GetMinByteSize() * length;
size_t len = limit * sizeof(UChar);
UChar *arr = nullptr;
@ -100,7 +100,6 @@ namespace OHOS::Util {
if (codeFlag != U_ZERO_ERROR) {
return ThrowError(env, "TextDecoder decoding error.");
}
size_t resultLength = 0;
bool omitInitialBom = false;
DecodeArr decArr(target, tarStartPos, limit);
@ -217,4 +216,17 @@ namespace OHOS::Util {
NAPI_CALL(env, napi_get_undefined(env, &res));
return res;
}
const char* TextDecoder::ReplaceNull(void *data, size_t length) const
{
char *str = static_cast<char*>(data);
if (encStr_ == "utf-8") {
for (size_t i = 0; i < length; ++i) {
if (str[i] == '\0') {
str[i] = ' ';
}
}
}
return const_cast<const char*>(str);
}
}

View File

@ -160,6 +160,7 @@ namespace OHOS::Util {
void SetBomFlag(const UChar *arr, const UErrorCode codeFlag, const DecodeArr decArr,
size_t& rstLen, bool& bomFlag);
void FreedMemory(UChar *pData);
const char* ReplaceNull(void *data, size_t length) const;
napi_value ThrowError(napi_env env, const char* errMessage);
uint32_t label_ {};
std::string encStr_ {};

View File

@ -793,19 +793,7 @@ class TextDecoder {
}
public decodeWithStream(input: Uint8Array, options?: { stream?: boolean }): string {
const space: number = 32; // space
const endString: number = 0; // null
let uint8: Uint8Array = new Uint8Array(input);
if (uint8.length === 0) {
return "";
}
if (TextDecoder.encodeStr === 'utf-8' || TextDecoder.encodeStr === undefined) {
uint8.forEach((element, index, array) => {
if (element === endString) {
array[index] = space;
}
});
}
if (arguments.length === 1) {
return this.textDecoder.decodeWithStream(uint8);
}