diff --git a/docs/changes.md b/docs/changes.md index 02b96240c..8801c8f40 100644 --- a/docs/changes.md +++ b/docs/changes.md @@ -1,5 +1,8 @@ # 最新动态 +2024/12/19 + * 增加 wchar32 以及相关函数。 + 2024/12/13 * 增加EVT\_NATIVE\_WINDOW\_FOCUS\_GAINED(感谢兆坤提供补丁) * 用widget\_set\_self\_layout替换widget\_set\_self\_layout\_params(感谢兆坤提供补丁) diff --git a/src/tkc/utils.c b/src/tkc/utils.c index 0e38c67b3..198678bc6 100644 --- a/src/tkc/utils.c +++ b/src/tkc/utils.c @@ -687,7 +687,7 @@ int32_t tk_wstricmp(const wchar_t* a, const wchar_t* b) { return 1; } - return wcs_case_cmp(a, b); + return wcscasecmp(a, b); } char* tk_str_copy(char* dst, const char* src) { @@ -1016,13 +1016,13 @@ ret_t tk_wstr_select_word(const wchar_t* str, uint32_t len, uint32_t index, int3 --i; if (i >= 0) { - if (wcs_chr(no_start_symbols, str[i]) != NULL) { + if (wcschr(no_start_symbols, str[i]) != NULL) { break; } else if (str[i] == '\r' || str[i] == '\n') { break; } else if (tk_isspace(str[i])) { break; - } else if (!tk_isspace(str[i + 1]) && wcs_chr(no_start_symbols, str[i + 1]) == NULL && + } else if (!tk_isspace(str[i + 1]) && wcschr(no_start_symbols, str[i + 1]) == NULL && ((tk_isalpha(str[i]) && !tk_isalpha(str[i + 1])) || (!tk_isalpha(str[i]) && tk_isalpha(str[i + 1])))) { break; @@ -1044,10 +1044,10 @@ ret_t tk_wstr_select_word(const wchar_t* str, uint32_t len, uint32_t index, int3 ++i; if (i >= 0 && i <= len) { - if (wcs_chr(no_start_symbols, str[i - 1]) != NULL) { + if (wcschr(no_start_symbols, str[i - 1]) != NULL) { *right = i - 1; break; - } else if (wcs_chr(no_start_symbols, str[i]) != NULL) { + } else if (wcschr(no_start_symbols, str[i]) != NULL) { *right = i; break; } else if (str[i - 1] == '\r' || str[i] == '\n') { diff --git a/src/tkc/wasm_helper.h b/src/tkc/wasm_helper.h index f4636ed1c..66bd9fbcc 100644 --- a/src/tkc/wasm_helper.h +++ b/src/tkc/wasm_helper.h @@ -74,19 +74,6 @@ int iswalnum(wchar_t ch); int iswspace(wchar_t ch); int islower(int c); -#ifdef WITH_WCSXXX - -size_t wcslen(const wchar_t* s); -int wcscmp(const wchar_t* s1, const wchar_t* s2); -int wcscasecmp(const wchar_t* s1, const wchar_t* s2); -int wcsncmp(const wchar_t* s1, const wchar_t* s2, size_t n); -wchar_t* wcsdup(const wchar_t* s); -wchar_t* wcschr(const wchar_t* s, wchar_t c); -wchar_t* wcscpy(wchar_t* s1, const wchar_t* s2); -wchar_t* wcsncpy(wchar_t* s1, const wchar_t* s2, size_t n); -const wchar_t* wcsstr(const wchar_t* s1, const wchar_t* s2); -#endif /*WITH_WCSXXX*/ - #if defined(__GNUC__) && !defined(__cplusplus) typedef _Bool bool_t; #else diff --git a/src/tkc/wchar32.c b/src/tkc/wchar32.c new file mode 100644 index 000000000..b7790c369 --- /dev/null +++ b/src/tkc/wchar32.c @@ -0,0 +1,504 @@ +/** + * File: wchar32.c + * Author: AWTK Develop Team + * Brief: 32bits wide char + * + * Copyright (c) 2024 - 2024 Guangzhou ZHIYUAN Electronics Co.,Ltd. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * License file for more details. + * + */ + +/** + * History: + * ================================================================ + * 2024-12-19 Li XianJing adapt from uclib + * + */ + +#include "tkc/mem.h" +#include "tkc/wchar32.h" + +wchar32_t* wcs32_init_with_wcs(wchar32_t* s1, const wchar_t* s2) { + wchar32_t* start = s1; + return_value_if_fail(s1 != NULL && s2 != NULL, NULL); + + while ((*s1++ = *s2++) != 0) { + } + + return start; +} + +wchar32_t* wcp32cpy(wchar32_t* s1, wchar32_t* s2) { + return_value_if_fail(s1 != NULL && s2 != NULL, NULL); + + while ((*s1++ = *s2++) != 0) { + } + + return s1 - 1; +} + +wchar32_t* wcp32ncpy(wchar32_t* s1, wchar32_t* s2, size_t n) { + wchar32_t* start = s1; + return_value_if_fail(s1 != NULL && s2 != NULL, NULL); + + while (n-- > 0) { + if ((*s1++ = *s2++) == 0) { + break; + } + } + + return start; +} + +int wcs32casecmp(const wchar32_t* s1, const wchar32_t* s2) { + return_value_if_fail(s1 != NULL && s2 != NULL, 0); + + while (towlower(*s1) == towlower(*s2)) { + if (*s1 == 0) { + return 0; + } + s1++; + s2++; + } + + return tolower(*s1) - tolower(*s2); +} + +wchar32_t* wcs32cat(wchar32_t* s1, const wchar32_t* s2) { + wchar32_t* start = s1; + return_value_if_fail(s1 != NULL && s2 != NULL, NULL); + + while (*s1 != 0) { + s1++; + } + + while ((*s1++ = *s2++) != 0) { + } + + return start; +} + +wchar32_t* wcs32chr(const wchar32_t* s, wchar32_t c) { + return_value_if_fail(s != NULL, NULL); + + while (*s != 0) { + if (*s == c) { + return (wchar32_t*)s; + } + s++; + } + + return NULL; +} + +int wcs32cmp(const wchar32_t* s1, const wchar32_t* s2) { + return_value_if_fail(s1 != NULL && s2 != NULL, 0); + + while (*s1 == *s2) { + if (*s1 == 0) { + return 0; + } + s1++; + s2++; + } + + return *s1 - *s2; +} + +wchar32_t* wcs32cpy(wchar32_t* s1, const wchar32_t* s2) { + wchar32_t* start = s1; + return_value_if_fail(s1 != NULL && s2 != NULL, NULL); + + while ((*s1++ = *s2++) != 0) { + } + + return start; +} + +size_t wcs32cspn(const wchar32_t* s1, const wchar32_t* s2) { + size_t i = 0; + return_value_if_fail(s1 != NULL && s2 != NULL, 0); + + while (s1[i] != 0) { + if (wcs32chr(s2, s1[i]) != NULL) { + break; + } + i++; + } + + return i; +} + +wchar32_t* wcs32dup(const wchar32_t* s) { + wchar32_t* ret = NULL; + uint32_t size = 0; + return_value_if_fail(s != NULL, NULL); + size = (wcs32len(s) + 1) * sizeof(wchar32_t); + ret = (wchar32_t*)TKMEM_ALLOC(size); + return_value_if_fail(ret != NULL, NULL); + + return wcs32cpy(ret, s); +} + +size_t wcs32lcat(wchar32_t* s1, const wchar32_t* s2, size_t n) { + size_t i = 0; + return_value_if_fail(s1 != NULL && s2 != NULL, 0); + + while (*s1 != 0) { + s1++; + } + + while (n-- > 0) { + if ((*s1++ = *s2++) == 0) { + break; + } + i++; + } + + return i; +} + +size_t wcs32lcpy(wchar32_t* s1, const wchar32_t* s2, size_t n) { + size_t i = 0; + return_value_if_fail(s1 != NULL && s2 != NULL, 0); + + while (n-- > 0) { + if ((*s1++ = *s2++) == 0) { + break; + } + i++; + } + + return i; +} + +size_t wcs32len(const wchar32_t* s) { + size_t i = 0; + return_value_if_fail(s != NULL, 0); + + while (s[i] != 0) { + i++; + } + + return i; +} + +int wcs32ncasecmp(const wchar32_t* s1, const wchar32_t* s2, size_t n) { + return_value_if_fail(s1 != NULL && s2 != NULL, 0); + + while (n-- > 0) { + if (towlower(*s1) != towlower(*s2)) { + return tolower(*s1) - tolower(*s2); + } + if (*s1 == 0) { + return 0; + } + s1++; + s2++; + } + + return 0; +} + +wchar32_t* wcs32ncat(wchar32_t* s1, const wchar32_t* s2, size_t n) { + wchar32_t* start = s1; + return_value_if_fail(s1 != NULL && s2 != NULL, NULL); + + while (*s1 != 0) { + s1++; + } + + while (n-- > 0) { + if ((*s1++ = *s2++) == 0) { + break; + } + } + + return start; +} + +int wcs32ncmp(const wchar32_t* s1, const wchar32_t* s2, size_t n) { + return_value_if_fail(s1 != NULL && s2 != NULL, 0); + + while (n-- > 0) { + if (*s1 != *s2) { + return *s1 - *s2; + } + if (*s1 == 0) { + return 0; + } + s1++; + s2++; + } + + return 0; +} + +wchar32_t* wcs32ncpy(wchar32_t* s1, const wchar32_t* s2, size_t n) { + wchar32_t* start = s1; + return_value_if_fail(s1 != NULL && s2 != NULL, NULL); + + while (n-- > 0) { + if ((*s1++ = *s2++) == 0) { + break; + } + } + + return start; +} + +size_t wcs32nlen(const wchar32_t* s, size_t maxlen) { + size_t i = 0; + return_value_if_fail(s != NULL, 0); + + while (maxlen-- > 0) { + if (s[i] == 0) { + break; + } + i++; + } + + return i; +} + +wchar32_t* wcs32pbrk(const wchar32_t* s1, const wchar32_t* s2) { + return_value_if_fail(s1 != NULL && s2 != NULL, NULL); + + while (*s1 != 0) { + if (wcs32chr(s2, *s1) != NULL) { + return (wchar32_t*)s1; + } + s1++; + } + + return NULL; +} + +wchar32_t* wcs32rchr(const wchar32_t* s, wchar32_t c) { + const wchar32_t* start = s; + return_value_if_fail(s != NULL, NULL); + + while (*s != 0) { + s++; + } + + while (s >= start) { + if (*s == c) { + return (wchar32_t*)s; + } + s--; + } + + return NULL; +} + +size_t wcs32spn(const wchar32_t* s1, const wchar32_t* s2) { + size_t i = 0; + return_value_if_fail(s1 != NULL && s2 != NULL, 0); + + while (s1[i] != 0) { + if (wcs32chr(s2, s1[i]) == NULL) { + break; + } + i++; + } + + return i; +} + +wchar32_t* wcs32str(const wchar32_t* s1, const wchar32_t* s2) { + size_t len = wcs32len(s2); + return_value_if_fail(s1 != NULL && s2 != NULL, NULL); + + while (*s1 != 0) { + if (wcs32ncmp(s1, s2, len) == 0) { + return (wchar32_t*)s1; + } + s1++; + } + + return NULL; +} + +wchar32_t* w32memchr(const wchar32_t* s, wchar32_t c, size_t n) { + return_value_if_fail(s != NULL, NULL); + + while (n-- > 0) { + if (*s == c) { + return (wchar32_t*)s; + } + s++; + } + + return NULL; +} + +int w32memcmp(const wchar32_t* s1, const wchar32_t* s2, size_t n) { + return_value_if_fail(s1 != NULL && s2 != NULL, 0); + + while (n-- > 0) { + if (*s1 != *s2) { + return *s1 - *s2; + } + s1++; + s2++; + } + + return 0; +} + +wchar32_t* w32memcpy(wchar32_t* s1, const wchar32_t* s2, size_t n) { + wchar32_t* start = s1; + return_value_if_fail(s1 != NULL && s2 != NULL, NULL); + + while (n-- > 0) { + *s1++ = *s2++; + } + + return start; +} + +wchar32_t* w32memmove(wchar32_t* s1, const wchar32_t* s2, size_t n) { + wchar32_t* start = s1; + return_value_if_fail(s1 != NULL && s2 != NULL, NULL); + + if (s1 < s2) { + while (n-- > 0) { + *s1++ = *s2++; + } + } else { + s1 += n; + s2 += n; + while (n-- > 0) { + *--s1 = *--s2; + } + } + + return start; +} + +wchar32_t* w32memset(wchar32_t* s, wchar32_t c, size_t n) { + wchar32_t* start = s; + return_value_if_fail(s != NULL, NULL); + + while (n-- > 0) { + *s++ = c; + } + + return start; +} + +#ifdef WITH_WCSXXX +wchar_t* wcpcpy(wchar_t* s1, wchar_t* s2) { + return wcp32cpy((wchar32_t*)s1, (wchar32_t*)s2); +} + +wchar_t* wcpncpy(wchar_t* s1, wchar_t* s2, size_t n) { + return wcp32ncpy((wchar32_t*)s1, (wchar32_t*)s2, n); +} + +int wcscasecmp(const wchar_t* s1, const wchar_t* s2) { + return wcs32casecmp((const wchar32_t*)s1, (const wchar32_t*)s2); +} + +wchar_t* wcscat(wchar_t* s1, const wchar_t* s2) { + return (wchar_t*)wcs32cat((wchar32_t*)s1, (const wchar32_t*)s2); +} + +wchar_t* wcschr(const wchar_t* s, wchar_t c) { + return (wchar_t*)wcs32chr((const wchar32_t*)s, c); +} + +int wcscmp(const wchar_t* s1, const wchar_t* s2) { + return wcs32cmp((const wchar32_t*)s1, (const wchar32_t*)s2); +} + +size_t wcscspn(const wchar_t* s1, const wchar_t* s2) { + return wcs32cspn((const wchar32_t*)s1, (const wchar32_t*)s2); +} + +wchar_t* wcsdup(const wchar_t* s) { + return (wchar_t*)wcs32dup((const wchar32_t*)s); +} + +size_t wcslcat(wchar_t* s1, const wchar_t* s2, size_t n) { + return wcs32lcat((wchar32_t*)s1, (const wchar32_t*)s2, n); +} + +size_t wcslcpy(wchar_t* s1, const wchar_t* s2, size_t n) { + return wcs32lcpy((wchar32_t*)s1, (const wchar32_t*)s2, n); +} + +size_t wcslen(const wchar_t* s) { + return wcs32len((const wchar32_t*)s); +} + +int wcsncasecmp(const wchar_t* s1, const wchar_t* s2, size_t n) { + return wcs32ncasecmp((const wchar32_t*)s1, (const wchar32_t*)s2, n); +} + +wchar_t* wcscpy(wchar_t* s1, const wchar_t* s2) { + return (wchar_t*)wcs32cpy((wchar32_t*)s1, (const wchar32_t*)s2); +} + +wchar_t* wcsncat(wchar_t* s1, const wchar_t* s2, size_t n) { + return (wchar_t*)wcs32ncat((wchar32_t*)s1, (const wchar32_t*)s2, n); +} + +int wcsncmp(const wchar_t* s1, const wchar_t* s2, size_t n) { + return wcs32ncmp((const wchar32_t*)s1, (const wchar32_t*)s2, n); +} + +wchar_t* wcsncpy(wchar_t* s1, const wchar_t* s2, size_t n) { + return (wchar_t*)wcs32ncpy((wchar32_t*)s1, (const wchar32_t*)s2, n); +} + +size_t wcsnlen(const wchar_t* s, size_t maxlen) { + return wcs32nlen((const wchar32_t*)s, maxlen); +} + +wchar_t* wcspbrk(const wchar_t* s1, const wchar_t* s2) { + return (wchar_t*)wcs32pbrk((const wchar32_t*)s1, (const wchar32_t*)s2); +} + +wchar_t* wcsrchr(const wchar_t* s, wchar_t c) { + return (wchar_t*)wcs32rchr((const wchar32_t*)s, c); +} + +size_t wcsspn(const wchar_t* s1, const wchar_t* s2) { + return wcs32spn((const wchar32_t*)s1, (const wchar32_t*)s2); +} + +wchar_t* wcsstr(const wchar_t* s1, const wchar_t* s2) { + return (wchar_t*)wcs32str((const wchar32_t*)s1, (const wchar32_t*)s2); +} + +wchar_t* wmemchr(const wchar_t* s, wchar_t c, size_t n) { + return (wchar_t*)w32memchr((const wchar32_t*)s, c, n); +} + +int wmemcmp(const wchar_t* s1, const wchar_t* s2, size_t n) { + return w32memcmp((const wchar32_t*)s1, (const wchar32_t*)s2, n); +} + +wchar_t* wmemcpy(wchar_t* s1, const wchar_t* s2, size_t n) { + return (wchar_t*)w32memcpy((wchar32_t*)s1, (const wchar32_t*)s2, n); +} + +wchar_t* wmemmove(wchar_t* s1, const wchar_t* s2, size_t n) { + return (wchar_t*)w32memmove((wchar32_t*)s1, (const wchar32_t*)s2, n); +} + +wchar_t* wmemset(wchar_t* s, wchar_t c, size_t n) { + return (wchar_t*)w32memset((wchar32_t*)s, c, n); +} + +int iswspace(wint_t ch) { + return ch == ' '; +} +#endif/*WITH_WCSXXX*/ + diff --git a/src/tkc/wchar32.h b/src/tkc/wchar32.h new file mode 100644 index 000000000..978eb339e --- /dev/null +++ b/src/tkc/wchar32.h @@ -0,0 +1,119 @@ +/** + * File: wchar32.h + * Author: AWTK Develop Team + * Brief: 32bits wide char + * + * Copyright (c) 2024 - 2024 Guangzhou ZHIYUAN Electronics Co.,Ltd. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * License file for more details. + * + */ + +/** + * History: + * ================================================================ + * 2024-12-19 Li XianJing adapt from uclib + * + */ + +#ifndef TK_WCHAR32_H +#define TK_WCHAR32_H + +#include "tkc/types_def.h" + +BEGIN_C_DECLS + +#ifdef WIN32 +typedef uint32_t wchar32_t; +#else +typedef wchar_t wchar32_t; +#endif + +wchar32_t* wcs32_init_with_wcs(wchar32_t* s1, const wchar_t* s2); + +wchar32_t* wcp32cpy(wchar32_t* s1, wchar32_t* s2); + +wchar32_t* wcp32ncpy(wchar32_t* s1, wchar32_t* s2, size_t n); + +int wcs32casecmp(const wchar32_t* s1, const wchar32_t* s2); + +wchar32_t* wcs32cat(wchar32_t* s1, const wchar32_t* s2); + +wchar32_t* wcs32chr(const wchar32_t* s, wchar32_t c); + +int wcs32cmp(const wchar32_t* s1, const wchar32_t* s2); + +size_t wcs32cspn(const wchar32_t* s1, const wchar32_t* s2); + +wchar32_t* wcs32dup(const wchar32_t* s); + +size_t wcs32lcat(wchar32_t* s1, const wchar32_t* s2, size_t n); + +size_t wcs32lcpy(wchar32_t* s1, const wchar32_t* s2, size_t n); + +size_t wcs32len(const wchar32_t* s); + +int wcs32ncasecmp(const wchar32_t* s1, const wchar32_t* s2, size_t n); + +wchar32_t* wcs32cpy(wchar32_t* s1, const wchar32_t* s2); + +wchar32_t* wcs32ncat(wchar32_t* s1, const wchar32_t* s2, size_t n); + +int wcs32ncmp(const wchar32_t* s1, const wchar32_t* s2, size_t n); + +wchar32_t* wcs32ncpy(wchar32_t* s1, const wchar32_t* s2, size_t n); + +size_t wcs32nlen(const wchar32_t* s, size_t maxlen); + +wchar32_t* wcs32pbrk(const wchar32_t* s1, const wchar32_t* s2); + +wchar32_t* wcs32rchr(const wchar32_t* s, wchar32_t c); + +size_t wcs32spn(const wchar32_t* s1, const wchar32_t* s2); + +wchar32_t* wcs32str(const wchar32_t* s1, const wchar32_t* s2); + +wchar32_t* w32memchr(const wchar32_t* s, wchar32_t c, size_t n); + +int w32memcmp(const wchar32_t* s1, const wchar32_t* s2, size_t n); + +wchar32_t* w32memcpy(wchar32_t* s1, const wchar32_t* s2, size_t n); + +wchar32_t* w32memmove(wchar32_t* s1, const wchar32_t* s2, size_t n); +wchar32_t* w32memset(wchar32_t* s, wchar32_t c, size_t n); + +#ifdef WITH_WCSXXX +wchar_t* wcpcpy(wchar_t* s1, wchar_t* s2); +wchar_t* wcpncpy(wchar_t* s1, wchar_t* s2, size_t n); +int wcscasecmp(const wchar_t* s1, const wchar_t* s2); +wchar_t* wcscat(wchar_t* s1, const wchar_t* s2); +wchar_t* wcschr(const wchar_t* s, wchar_t c); +int wcscmp(const wchar_t* s1, const wchar_t* s2); +size_t wcscspn(const wchar_t* s1, const wchar_t* s2); +wchar_t* wcsdup(const wchar_t* s); +size_t wcslcat(wchar_t* s1, const wchar_t* s2, size_t n); +size_t wcslcpy(wchar_t* s1, const wchar_t* s2, size_t n); +size_t wcslen(const wchar_t* s); +int wcsncasecmp(const wchar_t* s1, const wchar_t* s2, size_t n); +wchar_t* wcscpy(wchar_t* s1, const wchar_t* s2); +wchar_t* wcsncat(wchar_t* s1, const wchar_t* s2, size_t n); +int wcsncmp(const wchar_t* s1, const wchar_t* s2, size_t n); +wchar_t* wcsncpy(wchar_t* s1, const wchar_t* s2, size_t n); +size_t wcsnlen(const wchar_t* s, size_t maxlen); +wchar_t* wcspbrk(const wchar_t* s1, const wchar_t* s2); +wchar_t* wcsrchr(const wchar_t* s, wchar_t c); +size_t wcsspn(const wchar_t* s1, const wchar_t* s2); +wchar_t* wcsstr(const wchar_t* s1, const wchar_t* s2); +wchar_t* wmemchr(const wchar_t* s, wchar_t c, size_t n); +int wmemcmp(const wchar_t* s1, const wchar_t* s2, size_t n); +wchar_t* wmemcpy(wchar_t* s1, const wchar_t* s2, size_t n); +wchar_t* wmemmove(wchar_t* s1, const wchar_t* s2, size_t n); +wchar_t* wmemset(wchar_t* s, wchar_t c, size_t n); +#endif /*WITH_WCSXXX*/ + +END_C_DECLS + +#endif /*TK_WCHAR32_H*/ diff --git a/src/tkc/wstr.c b/src/tkc/wstr.c index b2f888957..562319ab7 100644 --- a/src/tkc/wstr.c +++ b/src/tkc/wstr.c @@ -24,170 +24,6 @@ #include "tkc/wstr.h" #include "tkc/utils.h" -const wchar_t* wcs_chr(const wchar_t* s, wchar_t c) { - const wchar_t* p = s; - return_value_if_fail(s != NULL, NULL); - - while (*p && *p != c) p++; - - return *p ? p : NULL; -} - -wchar_t* wcs_ncpy(wchar_t* s1, const wchar_t* s2, uint32_t n) { - wchar_t* d = s1; - const wchar_t* s = s2; - return_value_if_fail(s1 != NULL && s2 != NULL, NULL); - - while (*s && n-- > 0) { - *d++ = *s++; - } - *d = '\0'; - - return s1; -} - -wchar_t* wcs_cpy(wchar_t* s1, const wchar_t* s2) { - wchar_t* d = s1; - const wchar_t* s = s2; - return_value_if_fail(s1 != NULL && s2 != NULL, NULL); - - while (*s) { - *d++ = *s++; - } - *d = '\0'; - - return s1; -} - -int wcs_ncmp(const wchar_t* s1, const wchar_t* s2, uint32_t n) { - wchar_t c1 = L'\0'; - wchar_t c2 = L'\0'; - return_value_if_fail(s1 != NULL && s2 != NULL, -1); - - if (n >= 4) { - size_t n4 = n >> 2; - do { - c1 = *s1++; - c2 = *s2++; - if (c1 == L'\0' || c1 != c2) { - return c1 > c2 ? 1 : (c1 < c2 ? -1 : 0); - } - c1 = *s1++; - c2 = *s2++; - if (c1 == L'\0' || c1 != c2) { - return c1 > c2 ? 1 : (c1 < c2 ? -1 : 0); - } - c1 = *s1++; - c2 = *s2++; - if (c1 == L'\0' || c1 != c2) { - return c1 > c2 ? 1 : (c1 < c2 ? -1 : 0); - } - c1 = *s1++; - c2 = *s2++; - if (c1 == L'\0' || c1 != c2) { - return c1 > c2 ? 1 : (c1 < c2 ? -1 : 0); - } - } while (--n4 > 0); - n &= 3; - } - - while (n > 0) { - c1 = *s1++; - c2 = *s2++; - if (c1 == L'\0' || c1 != c2) { - return c1 > c2 ? 1 : (c1 < c2 ? -1 : 0); - } - n--; - } - - return 0; -} - -int wcs_cmp(const wchar_t* s1, const wchar_t* s2) { - uint32_t i = 0; - return_value_if_fail(s1 != NULL && s2 != NULL, -1); - - for (i = 0; s1[i] && s2[i]; i++) { - int ret = s1[i] - s2[i]; - if (ret) { - return ret; - } - } - - return s1[i] - s2[i]; -} - -int wcs_case_cmp(const wchar_t* s1, const wchar_t* s2) { - uint32_t i = 0; - return_value_if_fail(s1 != NULL && s2 != NULL, -1); - - for (i = 0; s1[i] && s2[i]; i++) { - int ret = tolower(s1[i]) - tolower(s2[i]); - if (ret) { - return ret; - } - } - - return tolower(s1[i]) - tolower(s2[i]); -} - -wchar_t* wcs_dup(const wchar_t* s) { - wchar_t* s1 = TKMEM_ZALLOCN(wchar_t, wcslen(s) + 1); - - return wcs_cpy(s1, s); -} - -size_t wcs_len(const wchar_t* s) { - size_t size = 0; - const wchar_t* p = s; - return_value_if_fail(s != NULL, 0); - - while (*p++) { - size++; - } - - return size; -} - -#ifdef WITH_WCSXXX -size_t wcslen(const wchar_t* s) { - return wcs_len(s); -} - -int wcsncmp(const wchar_t* s1, const wchar_t* s2, size_t n) { - return wcs_ncmp(s1, s2, n); -} - -int wcscmp(const wchar_t* s1, const wchar_t* s2) { - return wcs_cmp(s1, s2); -} - -int wcscasecmp(const wchar_t* s1, const wchar_t* s2) { - return wcs_case_cmp(s1, s2); -} - -wchar_t* wcscpy(wchar_t* s1, const wchar_t* s2) { - return wcs_cpy(s1, s2); -} - -wchar_t* wcsncpy(wchar_t* s1, const wchar_t* s2, size_t n) { - return wcs_ncpy(s1, s2, n); -} - -wchar_t* wcschr(const wchar_t* s, wchar_t c) { - return (wchar_t*)wcs_chr(s, c); -} - -wchar_t* wcsdup(const wchar_t* s) { - return wcs_dup(s); -} - -int iswspace(wint_t ch) { - return ch == ' '; -} - -#endif /*WITH_WCSXXX*/ - ret_t wstr_extend(wstr_t* str, uint32_t capacity) { return_value_if_fail(str != NULL, RET_BAD_PARAMS); diff --git a/src/tkc/wstr.h b/src/tkc/wstr.h index d850794c7..738076bed 100644 --- a/src/tkc/wstr.h +++ b/src/tkc/wstr.h @@ -1,7 +1,7 @@ /** * File: wstr.h * Author: AWTK Develop Team - * Brief: width char + * Brief: wide char string * * Copyright (c) 2018 - 2024 Guangzhou ZHIYUAN Electronics Co.,Ltd. * @@ -470,81 +470,13 @@ uint32_t wstr_count_char(wstr_t* str, wchar_t c); */ ret_t wstr_reset(wstr_t* str); -/** - * @method wcs_chr - * 查找字符位置 - * @annotation ["global"] - * @param {const wchar_t*} s wchar_t*。 - * @param {wchar_t} c wchar_t。 - * - * @return {const wchar_t*} 返回找到c的地址。 - */ -const wchar_t* wcs_chr(const wchar_t* s, wchar_t c); - -/** - * @method wcs_cpy - * 复制字符串 - * @annotation ["global"] - * @param {wchar_t*} s1 目标串。 - * @param {const wchar_t*} s2 源串。 - * - * @return {wchar_t*} 复制后的串地址。 - */ -wchar_t* wcs_cpy(wchar_t* s1, const wchar_t* s2); - -/** - * @method wcs_ncpy - * 按照长度来复制字符串 - * @annotation ["global"] - * @param {wchar_t*} s1 目标串。 - * @param {const wchar_t*} s2 源串。 - * @param {uint32_t} n 拷贝长度。 - * - * @return {wchar_t*} 复制后的串地址。 - */ -wchar_t* wcs_ncpy(wchar_t* s1, const wchar_t* s2, uint32_t n); - -/** - * @method wcs_cmp - * 比较字符串 - * @annotation ["global"] - * @param {const wchar_t*} s1 目标串。 - * @param {const wchar_t*} s2 源串。 - * - * @return {int} 小于0表示s1s2。 - */ -int wcs_cmp(const wchar_t* s1, const wchar_t* s2); - -/** - * @method wcs_case_cmp - * 字符串比较函数(不区分大小写) - * @annotation ["global"] - * @param {const wchar_t*} s1 目标串。 - * @param {const wchar_t*} s2 源串。 - * - * @return {int} 小于0表示s1s2。 - */ -int wcs_case_cmp(const wchar_t* s1, const wchar_t* s2); - -/** - * @method wcs_len - * 获取字符串长度。 - * @annotation ["global"] - * @param {const wchar_t*} s 串地址。 - * - * @return {size_t} 返回串长度'\0'结尾。 - */ -size_t wcs_len(const wchar_t* s); - -/** - * @method wcs_dup - * 获取一个新的内存保存s串。 - * @annotation ["global"] - * @param {const wchar_t*} s 源字符串。 - * - * @return {wchar_t*} 返回新的字符串地址。 - */ -wchar_t* wcs_dup(const wchar_t* s); +#define wcs_dup wcsdup +#define wcs_len wcslen +#define wcs_case_cmp wcscasecmp +#define wcs_cmp wcscmp +#define wcs_ncpy wcsncpy +#define wcs_cpy wcscpy +#define wcs_chr wcschr #ifdef WITH_WCSXXX #ifndef WITH_WASM diff --git a/tests/wchar32_test.cc b/tests/wchar32_test.cc new file mode 100644 index 000000000..af4b898b4 --- /dev/null +++ b/tests/wchar32_test.cc @@ -0,0 +1,290 @@ +#include "tkc/wchar32.h" +#include "tkc/mem.h" +#include "gtest/gtest.h" + +TEST(Wchar32, wcp32cpy) { + wchar32_t src[10] = {0}; + wchar32_t dst[10] = {0}; + wcs32_init_with_wcs(src, L"hello"); + wchar32_t* ret = wcp32cpy(dst, src); + EXPECT_EQ(wcs32len(dst), 5); + EXPECT_EQ(wcs32cmp(dst, src), 0); + EXPECT_EQ(ret, dst + 5); +} + +TEST(Wchar32, wcp32ncpy) { + wchar32_t src[10] = {0}; + wchar32_t dst[10] = {0}; + wcs32_init_with_wcs(src, L"hello"); + wchar32_t* ret = wcp32ncpy(dst, src, 5); + EXPECT_EQ(wcs32len(dst), 5); + EXPECT_EQ(wcs32cmp(dst, src), 0); + EXPECT_EQ(ret, dst); +} + +TEST(Wchar32, wcs32casecmp) { + wchar32_t src[10] = {0}; + wchar32_t dst[10] = {0}; + wcs32_init_with_wcs(src, L"hello"); + wcs32_init_with_wcs(dst, L"hEllo"); + + EXPECT_EQ(wcs32casecmp(dst, src), 0); + EXPECT_EQ(wcs32casecmp(dst, dst), 0); +} + +TEST(Wchar32, wcs32cat) { + wchar32_t src[10] = {0}; + wchar32_t dst[20] = {0}; + wchar32_t result[20] = {0}; + wcs32_init_with_wcs(src, L"hello"); + wcs32_init_with_wcs(dst, L"hEllo"); + wcs32_init_with_wcs(result, L"hEllohello"); + + wchar32_t* ret = wcs32cat(dst, src); + EXPECT_EQ(wcs32cmp(ret, result), 0); +} + +TEST(Wchar32, wcs32chr) { + wchar32_t str[10] = {0}; + wcs32_init_with_wcs(str, L"hEllo"); + + wchar32_t* ret = wcs32chr(str, L'E'); + EXPECT_EQ(wcs32cmp(ret, str + 1), 0); + + ret = wcs32chr(str, L'x'); + EXPECT_EQ(ret, (wchar32_t*)NULL); +} + +TEST(Wchar32, wcs32cpy) { + wchar32_t src[10] = {0}; + wchar32_t dst[20] = {0}; + wcs32_init_with_wcs(src, L"hello"); + wcs32_init_with_wcs(dst, L"hEllo"); + + wchar32_t* ret = wcs32cpy(dst, src); + EXPECT_EQ(wcs32cmp(dst, src), 0); + EXPECT_EQ(wcs32cmp(ret, src), 0); +} + +TEST(Wchar32, wcs32cspn) { + wchar32_t src[20] = {0}; + wchar32_t dst[20] = {0}; + wcs32_init_with_wcs(src, L"Hi, hello, world"); + wcs32_init_with_wcs(dst, L", "); + + size_t ret = wcs32cspn(src, dst); + ASSERT_EQ(ret, 2); +} + +TEST(Wchar32, wcs32dup) { + wchar32_t src[20] = {0}; + wcs32_init_with_wcs(src, L"Hi, hello, world"); + + wchar32_t* ret = wcs32dup(src); + EXPECT_EQ(wcs32cmp(ret, src), 0); + TKMEM_FREE(ret); +} + + +TEST(Wchar32, wcs32lcat) { + wchar32_t src[10] = {0}; + wchar32_t dst[20] = {0}; + wchar32_t result[20] = {0}; + wcs32_init_with_wcs(src, L"hello"); + wcs32_init_with_wcs(dst, L"hEllo"); + wcs32_init_with_wcs(result, L"hEllohello"); + + size_t ret = wcs32lcat(dst, src, 5); + EXPECT_EQ(wcs32cmp(dst, result), 0); + EXPECT_EQ(ret, 5); + + wcs32_init_with_wcs(dst, L"hEllo"); + ret = wcs32lcat(dst, src, 30); + EXPECT_EQ(wcs32cmp(dst, result), 0); + EXPECT_EQ(ret, 5); +} + + +TEST(Wchar32, wcs32lcpy) { + wchar32_t src[10] = {0}; + wchar32_t dst[20] = {0}; + wchar32_t result[20] = {0}; + wcs32_init_with_wcs(src, L"hello"); + wcs32_init_with_wcs(dst, L"hEllo"); + wcs32_init_with_wcs(result, L"hEllohello"); + + size_t ret = wcs32lcpy(dst, src, 5); + EXPECT_EQ(wcs32cmp(dst, src), 0); + EXPECT_EQ(ret, 5); + + ret = wcs32lcpy(dst, src, 50); + EXPECT_EQ(wcs32cmp(dst, src), 0); + EXPECT_EQ(ret, 5); +} + +TEST(Wchar32, wcs32ncasecmp) { + wchar32_t src[10] = {0}; + wchar32_t dst[10] = {0}; + wcs32_init_with_wcs(src, L"hello"); + wcs32_init_with_wcs(dst, L"hEllo"); + + EXPECT_EQ(wcs32ncasecmp(dst, src, 5), 0); + EXPECT_EQ(wcs32ncasecmp(dst, dst, 5), 0); +} + +TEST(Wchar32, wcs32ncat) { + wchar32_t src[10] = {0}; + wchar32_t dst[20] = {0}; + wchar32_t result[20] = {0}; + wcs32_init_with_wcs(src, L"hello"); + wcs32_init_with_wcs(dst, L"hEllo"); + wcs32_init_with_wcs(result, L"hEllohello"); + + wchar32_t* ret = wcs32ncat(dst, src, 5); + EXPECT_EQ(wcs32cmp(dst, result), 0); + EXPECT_EQ(ret, dst); + + wcs32_init_with_wcs(dst, L"hEllo"); + ret = wcs32ncat(dst, src, 30); + EXPECT_EQ(wcs32cmp(dst, result), 0); + EXPECT_EQ(ret, dst); +} + +TEST(Wchar32, wcs32ncmp) { + wchar32_t src[10] = {0}; + wchar32_t dst[10] = {0}; + wcs32_init_with_wcs(src, L"hello"); + wcs32_init_with_wcs(dst, L"hello"); + + EXPECT_EQ(wcs32ncmp(dst, src, 5), 0); + EXPECT_EQ(wcs32ncmp(dst, dst, 5), 0); +} + +TEST(Wchar32, wcs32ncpy) { + wchar32_t src[10] = {0}; + wchar32_t dst[20] = {0}; + wchar32_t result[20] = {0}; + wcs32_init_with_wcs(src, L"hello"); + wcs32_init_with_wcs(dst, L"hEllo"); + wcs32_init_with_wcs(result, L"hEllohello"); + + wchar32_t* ret = wcs32ncpy(dst, src, 5); + EXPECT_EQ(wcs32cmp(dst, src), 0); + EXPECT_EQ(ret, dst); + + ret = wcs32ncpy(dst, src, 50); + EXPECT_EQ(wcs32cmp(dst, src), 0); + EXPECT_EQ(ret, dst); +} + +TEST(Wchar32, wcs32nlen) { + wchar32_t src[10] = {0}; + wchar32_t dst[20] = {0}; + wchar32_t result[20] = {0}; + wcs32_init_with_wcs(src, L"hello"); + wcs32_init_with_wcs(dst, L"hEllo"); + wcs32_init_with_wcs(result, L"hEllohello"); + EXPECT_EQ(wcs32nlen(dst, 5), 5); + EXPECT_EQ(wcs32nlen(dst, 50), 5); + + EXPECT_EQ(wcs32nlen(result, 5), 5); + EXPECT_EQ(wcs32nlen(result, 50), 10); + + EXPECT_EQ(wcs32nlen(src, 5), 5); + EXPECT_EQ(wcs32nlen(src, 50), 5); +} + +TEST(Wchar32, wcs32pbrk) { + wchar32_t src[20] = {0}; + wchar32_t dst[20] = {0}; + wcs32_init_with_wcs(src, L"Hi, hello, world"); + wcs32_init_with_wcs(dst, L", "); + + wchar32_t* ret = wcs32pbrk(src, dst); + EXPECT_EQ(wcs32cmp(ret, src + 2), 0); +} + +TEST(Wchar32, wcs32rchr) { + wchar32_t src[20] = {0}; + wchar32_t dst[20] = {0}; + wcs32_init_with_wcs(src, L"Hi, hello, world"); + wcs32_init_with_wcs(dst, L", "); + + wchar32_t* ret = wcs32rchr(src, L','); + EXPECT_EQ(ret, src + 9); +} + +TEST(Wchar32, wcs32spn) { + wchar32_t src[20] = {0}; + wchar32_t dst[20] = {0}; + wcs32_init_with_wcs(src, L"Hi, hello, world"); + wcs32_init_with_wcs(dst, L"Hi"); + + size_t ret = wcs32spn(src, dst); + ASSERT_EQ(ret, 2); +} + +TEST(Wchar32, wcs32str) { + wchar32_t src[20] = {0}; + wchar32_t dst[20] = {0}; + wcs32_init_with_wcs(src, L"Hi, hello, world"); + wcs32_init_with_wcs(dst, L"hello"); + + wchar32_t* ret = wcs32str(src, dst); + EXPECT_EQ(wcs32cmp(ret, src + 4), 0); + + ret = wcs32str(dst, src); + EXPECT_EQ(ret, (wchar32_t*)NULL); +} + +TEST(Wchar32, w32memchr) { + wchar32_t str[10] = {0}; + wcs32_init_with_wcs(str, L"hEllo"); + + wchar32_t* ret = w32memchr(str, L'E', 5); + EXPECT_EQ(wcs32cmp(ret, str + 1), 0); + + ret = w32memchr(str, L'x', 5); + EXPECT_EQ(ret, (wchar32_t*)NULL); +} + +TEST(Wchar32, w32memcmp) { + wchar32_t src[10] = {0}; + wchar32_t dst[10] = {0}; + wcs32_init_with_wcs(src, L"hello"); + wcs32_init_with_wcs(dst, L"hello"); + + EXPECT_EQ(w32memcmp(dst, src, 5), 0); + EXPECT_EQ(w32memcmp(dst, dst, 5), 0); +} + +TEST(Wchar32, w32memcpy) { + wchar32_t src[10] = {0}; + wchar32_t dst[10] = {0}; + wcs32_init_with_wcs(src, L"hello"); + wchar32_t* ret = w32memcpy(dst, src, 5); + EXPECT_EQ(wcs32len(dst), 5); + EXPECT_EQ(wcs32cmp(dst, src), 0); + EXPECT_EQ(ret, dst); +} + +TEST(Wchar32, w32memmove) { + wchar32_t src[10] = {0}; + wchar32_t dst[10] = {0}; + wcs32_init_with_wcs(src, L"hello"); + wchar32_t* ret = w32memmove(dst, src, 5); + EXPECT_EQ(wcs32len(dst), 5); + EXPECT_EQ(wcs32cmp(dst, src), 0); + EXPECT_EQ(ret, dst); +} + +TEST(Wchar32, w32memset) { + wchar32_t src[10] = {0}; + wchar32_t dst[10] = {0}; + memset(dst, 0, sizeof(dst)); + wcs32_init_with_wcs(src, L"AAAAA"); + wchar32_t* ret = w32memset(dst, 'A', 5); + EXPECT_EQ(wcs32len(dst), 5); + EXPECT_EQ(wcs32cmp(dst, src), 0); + EXPECT_EQ(ret, dst); +} \ No newline at end of file