Signed-off-by: @lixingyang-li <lixingyang13@huawei.com>
Rectification of super large functions in public basic libraries
This commit is contained in:
@lixingyang-li 2024-06-06 18:56:24 +08:00
parent 52178403a3
commit e2aa3dce1f
4 changed files with 71 additions and 64 deletions

View File

@ -27,14 +27,37 @@ bool IsOneByte(uint8_t u8Char)
return (u8Char & 0x80) == 0; return (u8Char & 0x80) == 0;
} }
void Utf8ToUtf16BEToData(const unsigned char *data, u16string &u16Str, string::size_type &index, uint8_t &c1)
{
uint8_t c2 = data[++index]; // The second byte
uint8_t c3 = data[++index]; // The third byte
uint8_t c4 = data[++index]; // The forth byte
// Calculate the UNICODE code point value (3 bits lower for the first byte, 6 bits for the other)
// 3 : shift left 3 times of UTF8_VALID_BITS
uint32_t codePoint = ((c1 & LOWER_3_BITS_MASK) << (3 * UTF8_VALID_BITS)) |
// 2 : shift left 2 times of UTF8_VALID_BITS
((c2 & LOWER_6_BITS_MASK) << (2 * UTF8_VALID_BITS)) |
((c3 & LOWER_6_BITS_MASK) << UTF8_VALID_BITS) |
(c4 & LOWER_6_BITS_MASK);
// In UTF-16, U+10000 to U+10FFFF represent surrogate pairs with two 16-bit units
if (codePoint >= UTF16_SPECIAL_VALUE) {
codePoint -= UTF16_SPECIAL_VALUE;
// 10 : a half of 20 , shift right 10 bits
u16Str.push_back(static_cast<char16_t>((codePoint >> 10) | HIGH_AGENT_MASK));
u16Str.push_back(static_cast<char16_t>((codePoint & LOWER_10_BITS_MASK) | LOW_AGENT_MASK));
} else { // In UTF-16, U+0000 to U+D7FF and U+E000 to U+FFFF are Unicode code point values
// U+D800 to U+DFFF are invalid characters, for simplicity,
// assume it does not exist (if any, not encoded)
u16Str.push_back(static_cast<char16_t>(codePoint));
}
}
u16string Utf8ToUtf16BE(const string &u8Str, bool *ok) u16string Utf8ToUtf16BE(const string &u8Str, bool *ok)
{ {
u16string u16Str = u""; u16string u16Str = u"";
u16Str.reserve(u8Str.size()); u16Str.reserve(u8Str.size());
string::size_type len = u8Str.length(); string::size_type len = u8Str.length();
const unsigned char *data = reinterpret_cast<const unsigned char *>(u8Str.data()); const unsigned char *data = reinterpret_cast<const unsigned char *>(u8Str.data());
bool isOk = true; bool isOk = true;
for (string::size_type i = 0; i < len; ++i) { for (string::size_type i = 0; i < len; ++i) {
uint8_t c1 = data[i]; // The first byte uint8_t c1 = data[i]; // The first byte
@ -44,28 +67,7 @@ u16string Utf8ToUtf16BE(const string &u8Str, bool *ok)
} }
switch (c1 & HIGER_4_BITS_MASK) { switch (c1 & HIGER_4_BITS_MASK) {
case FOUR_BYTES_STYLE: { // 4 byte characters, from 0x10000 to 0x10FFFF case FOUR_BYTES_STYLE: { // 4 byte characters, from 0x10000 to 0x10FFFF
uint8_t c2 = data[++i]; // The second byte Utf8ToUtf16BEToData(data, u16Str, i, c1);
uint8_t c3 = data[++i]; // The third byte
uint8_t c4 = data[++i]; // The forth byte
// Calculate the UNICODE code point value (3 bits lower for the first byte, 6 bits for the other)
// 3 : shift left 3 times of UTF8_VALID_BITS
uint32_t codePoint = ((c1 & LOWER_3_BITS_MASK) << (3 * UTF8_VALID_BITS)) |
// 2 : shift left 2 times of UTF8_VALID_BITS
((c2 & LOWER_6_BITS_MASK) << (2 * UTF8_VALID_BITS)) |
((c3 & LOWER_6_BITS_MASK) << UTF8_VALID_BITS) |
(c4 & LOWER_6_BITS_MASK);
// In UTF-16, U+10000 to U+10FFFF represent surrogate pairs with two 16-bit units
if (codePoint >= UTF16_SPECIAL_VALUE) {
codePoint -= UTF16_SPECIAL_VALUE;
// 10 : a half of 20 , shift right 10 bits
u16Str.push_back(static_cast<char16_t>((codePoint >> 10) | HIGH_AGENT_MASK));
u16Str.push_back(static_cast<char16_t>((codePoint & LOWER_10_BITS_MASK) | LOW_AGENT_MASK));
} else { // In UTF-16, U+0000 to U+D7FF and U+E000 to U+FFFF are Unicode code point values
// U+D800 to U+DFFF are invalid characters, for simplicity,
// assume it does not exist (if any, not encoded)
u16Str.push_back(static_cast<char16_t>(codePoint));
}
break; break;
} }
case THREE_BYTES_STYLE: { // 3 byte characters, from 0x800 to 0xFFFF case THREE_BYTES_STYLE: { // 3 byte characters, from 0x800 to 0xFFFF
@ -99,7 +101,6 @@ u16string Utf8ToUtf16BE(const string &u8Str, bool *ok)
if (ok != nullptr) { if (ok != nullptr) {
*ok = isOk; *ok = isOk;
} }
return u16Str; return u16Str;
} }

View File

@ -874,11 +874,47 @@ static napi_value ToBase64Url(napi_env env, napi_callback_info info)
return result; return result;
} }
uint32_t GetValue(napi_env env, EncodingType &eType, std::string &str, const char *&data, napi_value &args)
{
std::u16string u16Str;
uint32_t len = 0;
switch (eType) {
case ASCII:
case LATIN1:
case BINARY:
str = GetStringASCII(env, args);
data = str.c_str();
break;
case UTF8:
str = GetStringUtf8(env, args);
data = str.c_str();
break;
case UTF16LE: {
u16Str = GetStringUtf16LE(env, args);
data = reinterpret_cast<char *>(const_cast<char16_t *>(u16Str.c_str()));
len = u16Str.length() * 2; // 2 : 2 means the length of wide char String is 2 times of char String
break;
}
case BASE64:
case BASE64URL:
str = GetStringBase64(env, args, eType);
data = str.c_str();
break;
case HEX:
str = GetStringHex(env, args);
data = str.c_str();
break;
default:
break;
}
return len;
}
static napi_value IndexOf(napi_env env, napi_callback_info info) static napi_value IndexOf(napi_env env, napi_callback_info info)
{ {
napi_value thisVar = nullptr; napi_value thisVar = nullptr;
size_t argc = 4; size_t argc = 4; // 4:The number of parameters is 4
napi_value args[4] = { nullptr }; napi_value args[4] = { nullptr }; // 4:The number of parameters is 4
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr)); NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
uint32_t offset = 0; uint32_t offset = 0;
NAPI_CALL(env, napi_get_value_uint32(env, args[1], &offset)); NAPI_CALL(env, napi_get_value_uint32(env, args[1], &offset));
@ -886,40 +922,10 @@ static napi_value IndexOf(napi_env env, napi_callback_info info)
// 2 : the third argument // 2 : the third argument
string type = GetStringASCII(env, args[2]); string type = GetStringASCII(env, args[2]);
EncodingType eType = Buffer::GetEncodingType(type); EncodingType eType = Buffer::GetEncodingType(type);
std::string str; std::string str = "";
std::u16string u16Str;
uint32_t len = 0; uint32_t len = 0;
const char *data = nullptr; const char *data = nullptr;
switch (eType) { len = GetValue(env, eType, str, data, args[0]);
case ASCII:
case LATIN1:
case BINARY:
str = GetStringASCII(env, args[0]);
data = str.c_str();
break;
case UTF8:
str = GetStringUtf8(env, args[0]);
data = str.c_str();
break;
case UTF16LE: {
u16Str = GetStringUtf16LE(env, args[0]);
data = reinterpret_cast<char *>(const_cast<char16_t *>(u16Str.c_str()));
len = u16Str.length() * 2; // 2 : 2 means the length of wide char String is 2 times of char String
break;
}
case BASE64:
case BASE64URL:
str = GetStringBase64(env, args[0], eType);
data = str.c_str();
break;
case HEX:
str = GetStringHex(env, args[0]);
data = str.c_str();
break;
default:
break;
}
Buffer *buf = nullptr; Buffer *buf = nullptr;
NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&buf))); NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&buf)));
bool isReverse = false; bool isReverse = false;

View File

@ -504,7 +504,7 @@ void SetPrimitive(napi_env env, napi_value map, const size_t& length, napi_value
} }
void ProcessNestedObject(napi_env env, napi_value item, napi_value& map, std::map<std::string, bool>& initialMap, void ProcessNestedObject(napi_env env, napi_value item, napi_value& map, std::map<std::string, bool>& initialMap,
napi_value& valuesKeyArray, size_t index) size_t index)
{ {
napi_value keys = nullptr; napi_value keys = nullptr;
napi_object_get_keys(env, item, &keys); napi_object_get_keys(env, item, &keys);
@ -584,7 +584,7 @@ napi_value Console::ProcessTabularData(napi_env env, napi_value tabularData)
hasPrimitive = true; hasPrimitive = true;
napi_set_element(env, valuesKeyArray, i, item); napi_set_element(env, valuesKeyArray, i, item);
} else { } else {
ProcessNestedObject(env, item, map, initialMap, valuesKeyArray, i); ProcessNestedObject(env, item, map, initialMap, i);
} }
} }
// set outputKeysArray // set outputKeysArray

View File

@ -45,7 +45,7 @@ namespace OHOS::JsSysModule::Process {
constexpr int FIRST_APPLICATION_UID = 10000; // 10000 : bundleId lower limit constexpr int FIRST_APPLICATION_UID = 10000; // 10000 : bundleId lower limit
constexpr int LAST_APPLICATION_UID = 65535; // 65535 : bundleId upper limit constexpr int LAST_APPLICATION_UID = 65535; // 65535 : bundleId upper limit
thread_local std::multimap<std::string, napi_ref> eventMap; thread_local std::multimap<std::string, napi_ref> eventMap;
static std::mutex sharedTimedMutex; static std::mutex g_sharedTimedMutex;
thread_local std::map<napi_ref, napi_ref> pendingUnHandledRejections; thread_local std::map<napi_ref, napi_ref> pendingUnHandledRejections;
// support events // support events
thread_local std::string events = "UnHandleRejection"; thread_local std::string events = "UnHandleRejection";
@ -249,7 +249,7 @@ namespace OHOS::JsSysModule::Process {
HILOG_ERROR("illegal event"); HILOG_ERROR("illegal event");
return; return;
} }
std::unique_lock<std::mutex> lock(sharedTimedMutex); std::unique_lock<std::mutex> lock(g_sharedTimedMutex);
eventMap.insert(std::make_pair(result, myCallRef)); eventMap.insert(std::make_pair(result, myCallRef));
} }
} }
@ -274,7 +274,7 @@ namespace OHOS::JsSysModule::Process {
auto iter = eventMap.equal_range(temp); auto iter = eventMap.equal_range(temp);
while (iter.first != iter.second) { while (iter.first != iter.second) {
NAPI_CALL(env, napi_delete_reference(env, iter.first->second)); NAPI_CALL(env, napi_delete_reference(env, iter.first->second));
std::unique_lock<std::mutex> lock(sharedTimedMutex); std::unique_lock<std::mutex> lock(g_sharedTimedMutex);
iter.first = eventMap.erase(iter.first); iter.first = eventMap.erase(iter.first);
flag = true; flag = true;
} }