From b3ea28abfe92b468ca03ccd3377a081df5796700 Mon Sep 17 00:00:00 2001 From: chenkeyu Date: Mon, 15 Apr 2024 15:12:32 +0800 Subject: [PATCH] Fix coding style Issue:https://gitee.com/openharmony/commonlibrary_c_utils/issues/I9GQBR?from=project-issue Signed-off-by: chenkeyu --- base/include/observer.h | 6 +-- base/include/pubdef.h | 4 +- base/include/safe_queue.h | 1 - base/include/sorted_vector.h | 1 - base/src/ashmem.cpp | 1 - base/src/parcel.cpp | 1 + base/src/rwlock.cpp | 2 +- base/src/string_ex.cpp | 2 +- base/src/unicode_ex.cpp | 75 ++++++++++++++++++++++-------------- 9 files changed, 53 insertions(+), 40 deletions(-) diff --git a/base/include/observer.h b/base/include/observer.h index 35811af..203fb61 100644 --- a/base/include/observer.h +++ b/base/include/observer.h @@ -123,9 +123,5 @@ public: */ virtual void Update(const Observable* o, const ObserverArg* arg) = 0; }; - - } - -#endif - +#endif \ No newline at end of file diff --git a/base/include/pubdef.h b/base/include/pubdef.h index ef3ce61..7df8547 100644 --- a/base/include/pubdef.h +++ b/base/include/pubdef.h @@ -43,9 +43,9 @@ namespace OHOS { #define ARRAY_SIZE(a) (sizeof((a)) / sizeof((a[0]))) // Used to free the space pointed by pointer p -#define FREE_AND_NIL(p) do { if (p) { delete (p); (p) = nullptr; } } while(0) +#define FREE_AND_NIL(p) do {if (p) { delete (p); (p) = nullptr;}} while(0) // Used to free the array space pointed by pointer p -#define FREE_AND_NIL_ARRAY(p) do { if (p) { delete[] (p); (p) = nullptr; } } while(0) +#define FREE_AND_NIL_ARRAY(p) do {if (p) {delete[] (p); (p) = nullptr;}} while(0) // Used to return the maximum of two numbers #define MAX(x, y) (((x) > (y)) ? (x) : (y)) diff --git a/base/include/safe_queue.h b/base/include/safe_queue.h index 08929f1..6cbfab4 100644 --- a/base/include/safe_queue.h +++ b/base/include/safe_queue.h @@ -38,7 +38,6 @@ namespace OHOS { */ template class SafeQueueInner { - public: SafeQueueInner() {} diff --git a/base/include/sorted_vector.h b/base/include/sorted_vector.h index 8da487d..612f7a6 100644 --- a/base/include/sorted_vector.h +++ b/base/include/sorted_vector.h @@ -210,7 +210,6 @@ public: return vec_[index]; } - /** * @brief Merges a vector into this vector. * diff --git a/base/src/ashmem.cpp b/base/src/ashmem.cpp index 48a3bf7..04adab4 100644 --- a/base/src/ashmem.cpp +++ b/base/src/ashmem.cpp @@ -66,7 +66,6 @@ const char* AsCharPtr(const c_void* inPtr) static int AshmemOpenLocked() { int fd = TEMP_FAILURE_RETRY(open("/dev/ashmem", O_RDWR | O_CLOEXEC)); - if (fd < 0) { UTILS_LOGE("%{public}s: fd is invalid, fd = %{public}d", __func__, fd); return fd; diff --git a/base/src/parcel.cpp b/base/src/parcel.cpp index 4d8311d..ef607c5 100644 --- a/base/src/parcel.cpp +++ b/base/src/parcel.cpp @@ -106,6 +106,7 @@ size_t Parcel::CalcNewCapacity(size_t minNewCapacity) size_t newCapacity = 64; while (newCapacity < minNewCapacity) { + // resize capacity by 2 times newCapacity = newCapacity * 2; } diff --git a/base/src/rwlock.cpp b/base/src/rwlock.cpp index 74b21fe..86baaf4 100644 --- a/base/src/rwlock.cpp +++ b/base/src/rwlock.cpp @@ -63,7 +63,7 @@ void RWLock::LockWrite() // Only when no thread has acquired a read lock or a write lock (the lock counter status is FREE) // can the write lock be acquired and the counter set to WRITE; otherwise wait - for(int status = LOCK_STATUS_FREE; !lockCount_.compare_exchange_weak(status, LOCK_STATUS_WRITE); + for (int status = LOCK_STATUS_FREE; !lockCount_.compare_exchange_weak(status, LOCK_STATUS_WRITE); status = LOCK_STATUS_FREE) { } diff --git a/base/src/string_ex.cpp b/base/src/string_ex.cpp index 0082ff7..0aa076b 100644 --- a/base/src/string_ex.cpp +++ b/base/src/string_ex.cpp @@ -233,7 +233,7 @@ bool IsSameTextStr(const string& first, const string& second) } /* - * utf8 rule: + * utf8 rule * 0000 ~ 007F : 0xxxxxxx * 0080 ~ 07FF : 110xxxxx 10xxxxxx * 0800 ~ FFFF : 1110xxxx 10xxxxxx 10xxxxxx diff --git a/base/src/unicode_ex.cpp b/base/src/unicode_ex.cpp index bb73736..c21e39e 100644 --- a/base/src/unicode_ex.cpp +++ b/base/src/unicode_ex.cpp @@ -54,26 +54,44 @@ constexpr char32_t UTF8_FIRST_BYTE_MARK[] = { }; } +#define UTF8_LENGTH_INVALID 0 +#define UTF8_LENGTH_1 1 +#define UTF8_LENGTH_2 2 +#define UTF8_LENGTH_3 3 +#define UTF8_LENGTH_4 4 +#define UTF8_LEN_MASK 3 +#define UTF8_FIRST_BYTE_INDEX 0 +#define UTF8_SECOND_BYTE_INDEX 1 +#define UTF8_THIRD_BYTE_INDEX 2 +#define UTF8_FORTH_BYTE_INDEX 3 +#define UTF8_SHIFT_WIDTH 6 +#define STR16_TO_STR8_SHIFT_WIDTH 10 +#define UTF16_SHIFT_WIDTH 10 +#define UTF32_BYTE_SIZE_1 1 +#define UTF32_BYTE_SIZE_2 2 +#define UTF32_BYTE_SIZE_3 3 +#define UTF32_BYTE_SIZE_4 4 + // inner func and dstP is not nullptr void Utf32CodePointToUtf8(uint8_t* dstP, char32_t srcChar, size_t bytes) { dstP += bytes; - if (bytes >= 4) { + if (bytes >= UTF32_BYTE_SIZE_4) { *--dstP = static_cast((srcChar | UTF8_BYTE_MARK) & UTF8_BYTE_MASK); srcChar >>= UTF8_OFFSET; } - if (bytes >= 3) { + if (bytes >= UTF32_BYTE_SIZE_3) { *--dstP = static_cast((srcChar | UTF8_BYTE_MARK) & UTF8_BYTE_MASK); srcChar >>= UTF8_OFFSET; } - if (bytes >= 2) { + if (bytes >= UTF32_BYTE_SIZE_2) { *--dstP = static_cast((srcChar | UTF8_BYTE_MARK) & UTF8_BYTE_MASK); srcChar >>= UTF8_OFFSET; } - if (bytes >= 1) { + if (bytes >= UTF32_BYTE_SIZE_1) { *--dstP = static_cast(srcChar | UTF8_FIRST_BYTE_MARK[bytes]); } } @@ -81,22 +99,22 @@ void Utf32CodePointToUtf8(uint8_t* dstP, char32_t srcChar, size_t bytes) size_t Utf32CodePointUtf8Length(char32_t srcChar) { if (srcChar < ONE_BYTE_UTF8) { - return 1; + return UTF8_LENGTH_1; } else if (srcChar < TWO_BYTES_UTF8) { - return 2; + return UTF8_LENGTH_2; } else if (srcChar < THREE_BYTES_UTF8) { if ((srcChar < UNICODE_RESERVED_START) || (srcChar > UNICODE_RESERVED_END)) { - return 3; + return UTF8_LENGTH_3; } else { // Surrogates are invalid UTF-32 characters. - return 0; + return UTF8_LENGTH_INVALID; } } else if (srcChar <= UNICODE_MAX_NUM) { // Max code point for Unicode is 0x0010FFFF. - return 4; + return UTF8_LENGTH_4; } else { // Invalid UTF-32 character. - return 0; + return UTF8_LENGTH_INVALID; } } @@ -115,6 +133,7 @@ int Utf16ToUtf8Length(const char16_t* str16, size_t str16Len) && ((*(str16 + 1) & 0xFC00) == 0xDC00)) { // surrogate pairs are always 4 bytes. charLen = 4; + // str16 advance 2 bytes str16 += 2; } else { charLen = Utf32CodePointUtf8Length(static_cast(*str16++)); @@ -139,7 +158,7 @@ void StrncpyStr16ToStr8(const char16_t* utf16Str, size_t str16Len, char* utf8Str // surrogate pairs if (((*curUtf16 & 0xFC00) == 0xD800) && ((curUtf16 + 1) < endUtf16) && (((*(curUtf16 + 1) & 0xFC00)) == 0xDC00)) { - utf32 = (*curUtf16++ - 0xD800) << 10; + utf32 = (*curUtf16++ - 0xD800) << STR16_TO_STR8_SHIFT_WIDTH; utf32 |= *curUtf16++ - 0xDC00; utf32 += 0x10000; } else { @@ -206,12 +225,12 @@ bool String16ToString8(const u16string& str16, string& str8) */ static inline size_t Utf8CodePointLen(uint8_t ch) { - return ((0xe5000000 >> ((ch >> 3) & 0x1e)) & 3) + 1; + return ((0xe5000000 >> ((ch >> UTF8_LEN_MASK) & 0x1e)) & UTF8_LEN_MASK) + 1; } static inline void Utf8ShiftAndMask(uint32_t* codePoint, const uint8_t byte) { - *codePoint <<= 6; + *codePoint <<= UTF8_SHIFT_WIDTH; *codePoint |= 0x3F & byte; } @@ -220,22 +239,22 @@ uint32_t Utf8ToUtf32CodePoint(const char* src, size_t length) uint32_t unicode = 0; switch (length) { - case 1: - return src[0]; - case 2: - unicode = src[0] & 0x1f; - Utf8ShiftAndMask(&unicode, src[1]); + case UTF8_LENGTH_1: + return src[UTF8_FIRST_BYTE_INDEX]; + case UTF8_LENGTH_2: + unicode = src[UTF8_FIRST_BYTE_INDEX] & 0x1f; + Utf8ShiftAndMask(&unicode, src[UTF8_SECOND_BYTE_INDEX]); return unicode; - case 3: - unicode = src[0] & 0x0f; - Utf8ShiftAndMask(&unicode, src[1]); - Utf8ShiftAndMask(&unicode, src[2]); + case UTF8_LENGTH_3: + unicode = src[UTF8_FIRST_BYTE_INDEX] & 0x0f; + Utf8ShiftAndMask(&unicode, src[UTF8_SECOND_BYTE_INDEX]); + Utf8ShiftAndMask(&unicode, src[UTF8_THIRD_BYTE_INDEX]); return unicode; - case 4: - unicode = src[0] & 0x07; - Utf8ShiftAndMask(&unicode, src[1]); - Utf8ShiftAndMask(&unicode, src[2]); - Utf8ShiftAndMask(&unicode, src[3]); + case UTF8_LENGTH_4: + unicode = src[UTF8_FIRST_BYTE_INDEX] & 0x07; + Utf8ShiftAndMask(&unicode, src[UTF8_SECOND_BYTE_INDEX]); + Utf8ShiftAndMask(&unicode, src[UTF8_THIRD_BYTE_INDEX]); + Utf8ShiftAndMask(&unicode, src[UTF8_FORTH_BYTE_INDEX]); return unicode; default: return 0xffff; @@ -286,7 +305,7 @@ char16_t* Utf8ToUtf16(const char* utf8Str, size_t u8len, char16_t* u16str, size_ } else { // Multiple UTF16 characters with surrogates codepoint = codepoint - 0x10000; - *u16cur++ = static_cast((codepoint >> 10) + 0xD800); + *u16cur++ = static_cast((codepoint >> UTF16_SHIFT_WIDTH) + 0xD800); if (u16cur >= u16end) { // Ooops... not enough room for this surrogate pair. return u16cur - 1; -- Gitee