From 6554bd615e7c35259dafcdd7fc23c0a6be6d3a47 Mon Sep 17 00:00:00 2001 From: Sabine Date: Tue, 6 Aug 2024 16:20:09 +0200 Subject: [PATCH 1/5] chore(common): replace codecvt in module u16 --- developer/src/kmcmplib/src/kmx_u16.cpp | 423 ++++++++++++++++--------- developer/src/kmcmplib/src/kmx_u16.h | 84 +++-- 2 files changed, 346 insertions(+), 161 deletions(-) diff --git a/developer/src/kmcmplib/src/kmx_u16.cpp b/developer/src/kmcmplib/src/kmx_u16.cpp index 02e59f8c12..6d1281c1ff 100644 --- a/developer/src/kmcmplib/src/kmx_u16.cpp +++ b/developer/src/kmcmplib/src/kmx_u16.cpp @@ -10,150 +10,224 @@ #include #include #include +#include "utfcodec.hpp" -//String <- wstring -std::string string_from_wstring(std::wstring const str) { - std::wstring_convert, wchar_t> converter; - return converter.to_bytes(str); +/** string <- wstring + * @brief Obtain a std::string from a std::wstring + * @param wstr the std::wstring to be converted + * @return a std::string + */ +std::string string_from_wstring(std::wstring const wstr) { + return convert((const std::wstring)wstr); } -//wstring <- string + +/** wstring <- string + * @brief Obtain a std::wstring from a std::string + * @param str the std::string to be converted + * @return a std::wstring + */ std::wstring wstring_from_string(std::string const str) { - std::wstring_convert, wchar_t> converter; - return converter.from_bytes(str); + return convert((const std::string)str); } -//u16String <- string +/** u16string <- string + * @brief Obtain a std::u16string from a std::string + * @param str the std::string to be converted + * @return a std::u16string + */ std::u16string u16string_from_string(std::string const str) { - std::wstring_convert, char16_t> converter; - return converter.from_bytes(str); + return convert((const std::string)str); } -//string <- u16string -std::string string_from_u16string(std::u16string const str) { - std::wstring_convert, char16_t> converter; - return converter.to_bytes(str); +/** string <- u16string + * @brief Obtain a std::string from a std::u16string + * @param str16 the std::u16string to be converted + * @return a std::string + */ +std::string string_from_u16string(std::u16string const str16) { + return convert((const std::u16string)str16); } -// often used with c_str() e.g. u16fmt( DEBUGSTORE_MATCH).c_str() -// UTF16 (= const char16_t*) -> UTF8 (= std::string) -> UTF16 ( = std::wstring 16 bit) -std::wstring u16fmt(const KMX_WCHAR * str) { - std::wstring_convert, wchar_t> convert_wstring; - std::wstring_convert, char16_t> convert; - - // UTF16 (= const char16_t*) -> UTF8 (= std::string) -> UTF16 ( = std::wstring 16 bit) - std::string utf8str = convert.to_bytes(str); // UTF16 (= const char16_t*) -> UTF8 (= std::string) - std::wstring wstr = convert_wstring.from_bytes(utf8str); // UTF8 (= std::string) -> UTF16 ( = std::wstring 16 bit) - return wstr; +/** wstring <- u16string + * @brief Obtain a std::wstring from a std::u16string + * @param str16 the std::u16string to be converted + * @return a std::wstring + */ +std::wstring wstring_from_u16string(std::u16string const str16) { + return convert((const std::u16string)str16); } -void u16sprintf(KMX_WCHAR * dst, const size_t sz, const wchar_t* fmt, ...) { - // UTF16 (=const wchar_t*) -> -> std::string -> std::u16string -> UTF16 ( = char16_t*) - wchar_t* wbuf = new wchar_t[sz]; - va_list args; - va_start(args, fmt); - vswprintf(wbuf, sz, fmt, args); - va_end(args); - - std::wstring_convert, wchar_t> convert_wstring; - std::wstring_convert, char16_t> convert; - - // UTF16 (=const wchar_t*) -> -> std::string -> std::u16string -> UTF16 ( = char16_t*) - std::string utf8str = convert_wstring.to_bytes(wbuf); // UTF16 ( = const wchar_t*) -> std::string - std::u16string u16str = convert.from_bytes(utf8str); // std::string -> std::u16string - u16ncpy(dst, u16str.c_str(), sz); // std::u16string.c_str() -> char16_t* - delete[] wbuf; +/** u16string <- wstring + * @brief Obtain a std::u16string from a std::wstring + * @param wstr the std::wstring to be converted + * @return a std::u16string + */ +std::u16string u16string_from_wstring(std::wstring const wstr) { + return convert((const std::wstring)wstr); } - std::wstring convert_pchar16T_To_wstr(KMX_WCHAR *Name){ - // convert char16_t* -> std::u16string -> std::string -> std::wstring - // char16_t* -> std::u16string - std::u16string u16str(Name); - // std::u16string -> std::string +/** + * @brief Convert pointer to wchar_t to pointer to char16_t and copy sz elements into dst + * @param dst destination + * @param sz nr of characters to be copied + * @param fmt source to convert and copy + */ +void u16sprintf(KMX_WCHAR* dst, const size_t sz, const wchar_t* fmt, ...) { + wchar_t* wbuf = new wchar_t[sz]; + va_list args; + va_start(args, fmt); + vswprintf(wbuf, sz, fmt, args); + va_end(args); + + std::u16string u16str = u16string_from_wstring(wbuf); + u16ncpy(dst, u16str.c_str(), sz); + delete[] wbuf; +} + +/** wstring <- u16string + * @brief Obtain a std::wstring from a std::u16tring + * the same as an above function but since it is used in Keyman we wrap it here + * @param str16 the std::string to be converted + * @return a std::wstring + */ +std::wstring u16fmt(const KMX_WCHAR* str16) { + return wstring_from_u16string(str16); +} + +/** + * @brief Convert pointer to wchar_t to pointer to char16_t and copy sz elements into dst + * the same as an above function but since it is used in Keyman we wrap it here + * @param ch16 the char16 to be converted + * @return a std::wstring + */ +std::wstring convert_pchar16T_To_wstr(KMX_WCHAR* ch16) { + std::u16string u16str(ch16); std::string stri = string_from_u16string(u16str); - // std::string -> std::wstring - std::wstring wstr = wstring_from_string(stri); + std::wstring wstr = wstring_from_string(stri); return wstr; - } +} -long int u16tol(const KMX_WCHAR* str, KMX_WCHAR** endptr, int base) -{ +/** + * @brief Convert u16string to long integer + * @param str u16string beginning with the representation of an integral number. + * @param endptr Reference to the next character in str + * @param base Numerical base (radix) that determines the valid characters and their interpretation + * @return a long + */ +long int u16tol(const KMX_WCHAR* str, KMX_WCHAR** endptr, int base) { auto s = string_from_u16string(str); char* t; long int result = strtol(s.c_str(), &t, base); - if (endptr != nullptr) *endptr = (KMX_WCHAR*)str + (t - s.c_str()); + if (endptr != nullptr) + *endptr = (KMX_WCHAR*)str + (t - s.c_str()); return result; } std::string toHex(int num1) { - if (num1 == 0) - return "0"; - int num = num1; - std::string s = ""; - while (num) { - int temp = num % 16; - if (temp <= 9) - s += (48 + temp); - else - s += (87 + temp); - num = num / 16; - } - std::reverse(s.begin(), s.end()); - return s; + if (num1 == 0) + return "0"; + int num = num1; + std::string s = ""; + while (num) { + int temp = num % 16; + if (temp <= 9) + s += (48 + temp); + else + s += (87 + temp); + num = num / 16; + } + std::reverse(s.begin(), s.end()); + return s; } -const KMX_WCHAR * u16ncat(KMX_WCHAR *dst, const KMX_WCHAR *src, size_t max) { +/** + * @brief Append max characters from u16string + * @param dst Pointer to the destination array + * @param src u16string to be appended + * @param max Maximum number of characters to be appended. + * @return Pointer to dst + */ +const KMX_WCHAR* u16ncat(KMX_WCHAR* dst, const KMX_WCHAR* src, size_t max) { KMX_WCHAR* o = dst; - dst = (KMX_WCHAR*) u16chr(dst, 0); - //max -= (dst-o); + dst = (KMX_WCHAR*)u16chr(dst, 0); + // max -= (dst-o); while (*src && max > 0) { *dst++ = *src++; max--; } - if(max > 0) - *dst = 0; + if (max > 0) + *dst = 0; return o; } -const KMX_WCHAR* u16rchr_slash(KMX_WCHAR const* Name) -{ +/** + * @brief Find last '/' or '\\' in an array of char16_t + * @param name Pointer to the source + * @return Pointer to the last slash/backslash + */ +const KMX_WCHAR* u16rchr_slash(KMX_WCHAR const* name) { const KMX_WCHAR* cp = NULL; - cp = u16rchr(Name, '\\'); + cp = u16rchr(name, '\\'); if (cp == NULL) - cp = u16rchr(Name, '/'); + cp = u16rchr(name, '/'); return cp; } -KMX_CHAR* strrchr_slash(KMX_CHAR* Name) -{ +/** + * @brief Find last '/' or '\\' in an array of char + * @param name Pointer to the source + * @return Pointer to the last slash/backslash + */ +KMX_CHAR* strrchr_slash(KMX_CHAR* name) { KMX_CHAR* cp = NULL; - cp = strrchr(Name, '\\'); + cp = strrchr(name, '\\'); if (cp == NULL) - cp = strrchr(Name, '/'); + cp = strrchr(name, '/'); return cp; } -// u16rchr returns last occurence of ch in p; It returns NULL if ch = '\0' and NULL if ch is not found +/** + * @brief Locate last occurrence of character in u16string + * @param p Pointer to the source + * @param ch The character to be found + * @return A pointer to the last occurrence of character in u16str + */ const KMX_WCHAR* u16rchr(const KMX_WCHAR* p, KMX_WCHAR ch) { const KMX_WCHAR* p_end = p + u16len(p) - 1; - if (ch == '\0') return p_end + 1; - while (p_end >= p) { - if (*p_end == ch) return p_end; - p_end--; - } - return NULL; + if (ch == '\0') + return p_end + 1; + while (p_end >= p) { + if (*p_end == ch) + return p_end; + p_end--; + } + return NULL; } -const KMX_WCHAR * u16chr(const KMX_WCHAR *p, KMX_WCHAR ch) { +/** + * @brief Locate first occurrence of character in u16string + * @param p Pointer to the source + * @param ch The character to be found + * @return A pointer to the first occurrence of character in u16str + */ +const KMX_WCHAR* u16chr(const KMX_WCHAR* p, KMX_WCHAR ch) { while (*p) { - if (*p == ch) return p; + if (*p == ch) + return p; p++; } return ch == 0 ? p : NULL; } -const KMX_WCHAR * u16cpy(KMX_WCHAR *dst, const KMX_WCHAR *src) { - KMX_WCHAR *o = dst; +/** + * @brief Copy the u16string pointed to by scr into the array pointed to by dst + * @param dst Pointer to the destination + * @param src Pointer to the source to be copied + * @return Pointer to dst + */ +const KMX_WCHAR* u16cpy(KMX_WCHAR* dst, const KMX_WCHAR* src) { + KMX_WCHAR* o = dst; while (*src) { *dst++ = *src++; } @@ -161,19 +235,31 @@ const KMX_WCHAR * u16cpy(KMX_WCHAR *dst, const KMX_WCHAR *src) { return o; } -const KMX_WCHAR * u16ncpy(KMX_WCHAR *dst, const KMX_WCHAR *src, size_t max) { - KMX_WCHAR *o = dst; +/** + * @brief Copy max characters of the u16string pointed to by src into the array pointed by dst + * @param dst Pointer to the destination + * @param src Pointer to the source to be copied + * @param max Maximum number of characters to be copied + * @return Pointer to dst + */ +const KMX_WCHAR* u16ncpy(KMX_WCHAR* dst, const KMX_WCHAR* src, size_t max) { + KMX_WCHAR* o = dst; while (*src && max > 0) { *dst++ = *src++; max--; } - if(max > 0) { + if (max > 0) { *dst = 0; } return o; } -size_t u16len(const KMX_WCHAR *p) { +/** + * @brief Return the length of the u16string str + * @param p Pointer to the source + * @return The length of u16string + */ +size_t u16len(const KMX_WCHAR* p) { int i = 0; while (*p) { p++; @@ -182,18 +268,35 @@ size_t u16len(const KMX_WCHAR *p) { return i; } -int u16cmp(const KMX_WCHAR *p, const KMX_WCHAR *q) { +/** + * @brief Compare two u16strings + * @param p Pointer one u16string + * @param q Pointer another u16string + * @return 0 if strings are equal + * ! = 0 if unequal + */ +int u16cmp(const KMX_WCHAR* p, const KMX_WCHAR* q) { while (*p && *q) { - if (*p != *q) return *p - *q; + if (*p != *q) + return *p - *q; p++; q++; } return *p - *q; } -int u16nicmp(const KMX_WCHAR *p, const KMX_WCHAR *q, size_t count) { +/** + * @brief Case insensitive comparison of up to count characters in two strings + * @param p Pointer one u16string + * @param q Pointer another u16string + * @param count Maximum number of characters to compare + * @return 0 if strings are equal + * ! = 0 if unequal + */ +int u16nicmp(const KMX_WCHAR* p, const KMX_WCHAR* q, size_t count) { while (*p && *q && count) { - if (toupper(*p) != toupper(*q)) return *p - *q; + if (toupper(*p) != toupper(*q)) + return *p - *q; p++; q++; count--; @@ -203,18 +306,35 @@ int u16nicmp(const KMX_WCHAR *p, const KMX_WCHAR *q, size_t count) { return 0; } -int u16icmp(const KMX_WCHAR *p, const KMX_WCHAR *q) { +/** + * @brief Case insensitive comparison of two strings + * @param p Pointer one u16string + * @param q Pointer another u16string + * @return 0 if strings are equal + * ! = 0 if unequal + */ +int u16icmp(const KMX_WCHAR* p, const KMX_WCHAR* q) { while (*p && *q) { - if (toupper(*p) != toupper(*q)) return *p - *q; + if (toupper(*p) != toupper(*q)) + return *p - *q; p++; q++; } return *p - *q; } -int u16ncmp(const KMX_WCHAR *p, const KMX_WCHAR *q, size_t count) { +/** + * @brief Comparison of up to count characters in two strings + * @param p Pointer one u16string + * @param q Pointer another u16string + * @param count Maximum number of characters to compare + * @return 0 if strings are equal + * ! = 0 if unequal + */ +int u16ncmp(const KMX_WCHAR* p, const KMX_WCHAR* q, size_t count) { while (*p && *q && count) { - if (*p != *q) return *p - *q; + if (*p != *q) + return *p - *q; p++; q++; count--; @@ -224,70 +344,89 @@ int u16ncmp(const KMX_WCHAR *p, const KMX_WCHAR *q, size_t count) { return 0; } -KMX_WCHAR * u16tok(KMX_WCHAR *p, const KMX_WCHAR ch, KMX_WCHAR **ctx) { +/** + * @brief Split u16string into tokens + * @param p Pointer to u16string to parse. + * @param ch the delimiter character + * @param ctx the remaining string after the first delimiter + * @return Pointer to the first token in p + */ +KMX_WCHAR* u16tok(KMX_WCHAR* p, const KMX_WCHAR ch, KMX_WCHAR** ctx) { if (!p) { p = *ctx; - if (!p) return NULL; + if (!p) + return NULL; } - KMX_WCHAR *q = p; + KMX_WCHAR* q = p; while (*q && *q != ch) { q++; } if (*q) { *q = 0; q++; - while (*q == ch) q++; + while (*q == ch) + q++; *ctx = q; - } - else { + } else { *ctx = NULL; } return *p ? p : NULL; } -KMX_WCHAR * u16tok(KMX_WCHAR* p, const KMX_WCHAR* delim, KMX_WCHAR** ctx) { - if (!p) { - p = *ctx; - if (!p) return NULL; - } +/** + * @brief Split u16string into tokens + * @param p Pointer to u16string to parse. + * @param delimiters an array of delimiter characters + * @param ctx the remaining string after the first delimiter + * @return Pointer to the first token in p + */ +KMX_WCHAR* u16tok(KMX_WCHAR* p, const KMX_WCHAR* delimiters, KMX_WCHAR** ctx) { + if (!p) { + p = *ctx; + if (!p) + return NULL; + } - KMX_WCHAR * q = p; - while (*q && !u16chr(delim, *q)) { - q++; - } - if (*q) { - *q = 0; - q++; - while (*q && u16chr(delim, *q)) q++; - *ctx = q; - } - else { - *ctx = NULL; - } + KMX_WCHAR* q = p; + while (*q && !u16chr(delimiters, *q)) { + q++; + } + if (*q) { + *q = 0; + q++; + while (*q && u16chr(delimiters, *q)) + q++; + *ctx = q; + } else { + *ctx = NULL; + } return *p ? p : NULL; } -double u16tof( KMX_WCHAR* str) -{ - double val = 0; - int offsetdot=0; - char digit; +/** + * @brief Convert a u16string to a double + * @param str Pointer to u16string + * @return double value equivalent to the string + */ +double u16tof(KMX_WCHAR* str) { + double val = 0; + int offsetdot = 0; + char digit; - PKMX_WCHAR q = (PKMX_WCHAR)u16chr(str, '.'); - size_t pos_dot = (q-str < 0) ? u16len(str) : q-str; + PKMX_WCHAR q = (PKMX_WCHAR)u16chr(str, '.'); + size_t pos_dot = (q - str < 0) ? u16len(str) : q - str; - for (size_t i = 0; i < u16len(str); i++) - { - digit = static_cast(towupper(*str)); + for (size_t i = 0; i < u16len(str); i++) { + digit = static_cast(towupper(*str)); - if (i > pos_dot - 1) - offsetdot = 1; + if (i > pos_dot - 1) + offsetdot = 1; - if (digit != '.') - val =val+ ((int(digit)) - 48) * pow(10, (pos_dot - 1- i + offsetdot)); + if (digit != '.') + val = val + ((int(digit)) - 48) * pow(10, (pos_dot - 1 - i + offsetdot)); - str++; - } - return val; + str++; + } + return val; } diff --git a/developer/src/kmcmplib/src/kmx_u16.h b/developer/src/kmcmplib/src/kmx_u16.h index 450c400e2f..fab15fe1a3 100644 --- a/developer/src/kmcmplib/src/kmx_u16.h +++ b/developer/src/kmcmplib/src/kmx_u16.h @@ -7,32 +7,78 @@ #include #include "kmcompx.h" -std::string string_from_wstring(std::wstring const str); +/** @brief Obtain a std::string from a std::wstring */ +std::string string_from_wstring(std::wstring const wstr); + +/** @brief Obtain a std::wstring from a std::string */ std::wstring wstring_from_string(std::string const str); + +/** @brief Obtain a std::u16string from a std::string */ std::u16string u16string_from_string(std::string const str); -std::string string_from_u16string(std::u16string const str); -std::wstring u16fmt(const KMX_WCHAR * str); -void u16sprintf(KMX_WCHAR * dst, const size_t sz, const wchar_t* fmt, ...) ; +/** @brief Obtain a std::string from a std::u16string */ +std::string string_from_u16string(std::u16string const str16); -std::wstring convert_pchar16T_To_wstr(KMX_WCHAR *Name); +/** @brief Obtain a std::wstring from a std::u16string */ +std::wstring wstring_from_u16string(std::u16string const str16); -size_t u16len(const KMX_WCHAR *p); -int u16cmp(const KMX_WCHAR *p, const KMX_WCHAR *q); -int u16icmp(const KMX_WCHAR *p, const KMX_WCHAR *q); -int u16ncmp(const KMX_WCHAR *p, const KMX_WCHAR *q, size_t count); -int u16nicmp(const KMX_WCHAR *p, const KMX_WCHAR *q, size_t count) ; -const KMX_WCHAR * u16ncpy(KMX_WCHAR *dst, const KMX_WCHAR *src, size_t max); -const KMX_WCHAR * u16cpy(KMX_WCHAR *dst, const KMX_WCHAR *src); -const KMX_WCHAR * u16rchr(const KMX_WCHAR *p, KMX_WCHAR ch) ; -const KMX_WCHAR * u16chr(const KMX_WCHAR *p, KMX_WCHAR ch) ; -const KMX_WCHAR * u16ncat(KMX_WCHAR *dst, const KMX_WCHAR *src, size_t max); -KMX_WCHAR * u16tok(KMX_WCHAR *p, const KMX_WCHAR ch, KMX_WCHAR **ctx) ; -KMX_WCHAR * u16tok(KMX_WCHAR* p, const KMX_WCHAR* ch, KMX_WCHAR** ctx) ; -long int u16tol(const KMX_WCHAR* str, KMX_WCHAR** endptr, int base) ; -double u16tof( KMX_WCHAR* str); +/** @brief Obtain a std::u16string from a std::wstring */ +std::u16string u16string_from_wstring(std::wstring const wstr); +/** @brief Obtain a std::wstring from a std::u16tring */ +std::wstring u16fmt(const KMX_WCHAR* str); + +/** @brief Convert pointer to wchar_t to pointer to char16_t and copy sz elements into dst */ +void u16sprintf(KMX_WCHAR* dst, const size_t sz, const wchar_t* fmt, ...); + +std::wstring convert_pchar16T_To_wstr(KMX_WCHAR* Name); + +/** @brief Return the length of the u16string str */ +size_t u16len(const KMX_WCHAR* p); + +/** @brief Compare two u16strings */ +int u16cmp(const KMX_WCHAR* p, const KMX_WCHAR* q); + +/** @brief Case insensitive comparison of two strings */ +int u16icmp(const KMX_WCHAR* p, const KMX_WCHAR* q); + +/** @brief Comparison of up to count characters in two strings */ +int u16ncmp(const KMX_WCHAR* p, const KMX_WCHAR* q, size_t count); + +/** @brief Case insensitive comparison of up to count characters in two strings */ +int u16nicmp(const KMX_WCHAR* p, const KMX_WCHAR* q, size_t count); + +/** @brief Copy max characters of the u16string pointed to by src into the array pointed to by dst */ +const KMX_WCHAR* u16ncpy(KMX_WCHAR* dst, const KMX_WCHAR* src, size_t max); + +/** @brief Copy the u16string pointed to by src into the array pointed to by dst */ +const KMX_WCHAR* u16cpy(KMX_WCHAR* dst, const KMX_WCHAR* src); + +/** @brief Locate last occurrence of character in u16string */ +const KMX_WCHAR* u16rchr(const KMX_WCHAR* p, KMX_WCHAR ch); + +/** @brief Locate first occurrence of character in u16string */ +const KMX_WCHAR* u16chr(const KMX_WCHAR* p, KMX_WCHAR ch); + +/** @brief Append max characters from u16string */ +const KMX_WCHAR* u16ncat(KMX_WCHAR* dst, const KMX_WCHAR* src, size_t max); + +/** @brief Split u16string into tokens */ +KMX_WCHAR* u16tok(KMX_WCHAR* p, const KMX_WCHAR ch, KMX_WCHAR** ctx); + +/** @brief Split u16string into tokens */ +KMX_WCHAR* u16tok(KMX_WCHAR* p, const KMX_WCHAR* delimiters, KMX_WCHAR** ctx); + +/** @brief Convert a u16string to a double */ +long int u16tol(const KMX_WCHAR* str, KMX_WCHAR** endptr, int base); + +/** @brief Convert a u16string to a double */ +double u16tof(KMX_WCHAR* str); + +/** @brief find last '/' or '\\' in an array of char */ KMX_CHAR* strrchr_slash(KMX_CHAR* Name); + +/** @brief find last '/' or '\\' in an array of char16_t */ const KMX_WCHAR* u16rchr_slash(KMX_WCHAR const* Name); std::string toHex(int num1); From cbe712d2cd923105845af5f9840d31e566bd01b4 Mon Sep 17 00:00:00 2001 From: Sabine Date: Wed, 7 Aug 2024 11:30:14 +0200 Subject: [PATCH 2/5] chore(common): replace u16fmt and convert_pchar16T_To_wstr with wstring_from_u16string --- developer/src/kmcmplib/src/Compiler.cpp | 8 +++---- .../src/kmcmplib/src/UnreachableRules.cpp | 2 +- developer/src/kmcmplib/src/kmx_u16.cpp | 23 ------------------- developer/src/kmcmplib/src/kmx_u16.h | 5 ---- .../src/kmcmplib/tests/util_filesystem.cpp | 4 ++-- 5 files changed, 7 insertions(+), 35 deletions(-) diff --git a/developer/src/kmcmplib/src/Compiler.cpp b/developer/src/kmcmplib/src/Compiler.cpp index 361a97c2d7..e626b140f5 100644 --- a/developer/src/kmcmplib/src/Compiler.cpp +++ b/developer/src/kmcmplib/src/Compiler.cpp @@ -580,7 +580,7 @@ KMX_BOOL ParseLine(PFILE_KEYBOARD fk, PKMX_WCHAR str) { //swprintf(tstr, "%d", fk->currentGroup); /* Record a system store for the line number of the begin statement */ //wcscpy(tstr, DEBUGSTORE_MATCH); - u16sprintf(tstr, _countof(tstr), L"%ls%d ", u16fmt(DEBUGSTORE_MATCH).c_str(), (int) fk->currentGroup); + u16sprintf(tstr, _countof(tstr), L"%ls%d ", wstring_from_u16string(DEBUGSTORE_MATCH).c_str(), (int) fk->currentGroup); u16ncat(tstr, gp->szName, _countof(tstr)); AddDebugStore(fk, tstr); @@ -619,7 +619,7 @@ KMX_BOOL ParseLine(PFILE_KEYBOARD fk, PKMX_WCHAR str) { { KMX_WCHAR tstr[128]; /* Record a system store for the line number of the begin statement */ - u16sprintf(tstr, _countof(tstr), L"%ls%d ", u16fmt(DEBUGSTORE_NOMATCH).c_str(), (int) fk->currentGroup); + u16sprintf(tstr, _countof(tstr), L"%ls%d ", wstring_from_u16string(DEBUGSTORE_NOMATCH).c_str(), (int) fk->currentGroup); u16ncat(tstr, gp->szName, _countof(tstr)); AddDebugStore(fk, tstr); } @@ -683,7 +683,7 @@ KMX_BOOL ProcessGroupLine(PFILE_KEYBOARD fk, PKMX_WCHAR p) { KMX_WCHAR tstr[128]; /* Record a system store for the line number of the begin statement */ - u16sprintf(tstr, _countof(tstr), L"%ls%d ", u16fmt(DEBUGSTORE_GROUP).c_str(), fk->cxGroupArray - 1); + u16sprintf(tstr, _countof(tstr), L"%ls%d ", wstring_from_u16string(DEBUGSTORE_GROUP).c_str(), fk->cxGroupArray - 1); u16ncat(tstr, gp->szName, _countof(tstr)); AddDebugStore(fk, tstr); } @@ -3761,7 +3761,7 @@ void kmcmp::RecordDeadkeyNames(PFILE_KEYBOARD fk) KMX_DWORD i; for (i = 0; i < fk->cxDeadKeyArray; i++) { - u16sprintf(buf, _countof(buf), L"%ls%d ", u16fmt(DEBUGSTORE_DEADKEY).c_str(), (int)i); + u16sprintf(buf, _countof(buf), L"%ls%d ", wstring_from_u16string(DEBUGSTORE_DEADKEY).c_str(), (int)i); u16ncat(buf, fk->dpDeadKeyArray[i].szName, _countof(buf)); AddDebugStore(fk, buf); diff --git a/developer/src/kmcmplib/src/UnreachableRules.cpp b/developer/src/kmcmplib/src/UnreachableRules.cpp index c84bdfb1f5..40965db4d6 100644 --- a/developer/src/kmcmplib/src/UnreachableRules.cpp +++ b/developer/src/kmcmplib/src/UnreachableRules.cpp @@ -18,7 +18,7 @@ namespace kmcmp { std::wstringstream key; key << kp->Key << "," << kp->ShiftFlags << ","; if (kp->dpContext) { - std::wstring Context_ws = u16fmt((const PKMX_WCHAR) kp->dpContext); + std::wstring Context_ws = wstring_from_u16string((const PKMX_WCHAR) kp->dpContext); key << Context_ws; } return key.str(); diff --git a/developer/src/kmcmplib/src/kmx_u16.cpp b/developer/src/kmcmplib/src/kmx_u16.cpp index 6d1281c1ff..04054fe4ad 100644 --- a/developer/src/kmcmplib/src/kmx_u16.cpp +++ b/developer/src/kmcmplib/src/kmx_u16.cpp @@ -84,29 +84,6 @@ void u16sprintf(KMX_WCHAR* dst, const size_t sz, const wchar_t* fmt, ...) { delete[] wbuf; } -/** wstring <- u16string - * @brief Obtain a std::wstring from a std::u16tring - * the same as an above function but since it is used in Keyman we wrap it here - * @param str16 the std::string to be converted - * @return a std::wstring - */ -std::wstring u16fmt(const KMX_WCHAR* str16) { - return wstring_from_u16string(str16); -} - -/** - * @brief Convert pointer to wchar_t to pointer to char16_t and copy sz elements into dst - * the same as an above function but since it is used in Keyman we wrap it here - * @param ch16 the char16 to be converted - * @return a std::wstring - */ -std::wstring convert_pchar16T_To_wstr(KMX_WCHAR* ch16) { - std::u16string u16str(ch16); - std::string stri = string_from_u16string(u16str); - std::wstring wstr = wstring_from_string(stri); - return wstr; -} - /** * @brief Convert u16string to long integer * @param str u16string beginning with the representation of an integral number. diff --git a/developer/src/kmcmplib/src/kmx_u16.h b/developer/src/kmcmplib/src/kmx_u16.h index fab15fe1a3..4ebcdb601d 100644 --- a/developer/src/kmcmplib/src/kmx_u16.h +++ b/developer/src/kmcmplib/src/kmx_u16.h @@ -25,14 +25,9 @@ std::wstring wstring_from_u16string(std::u16string const str16); /** @brief Obtain a std::u16string from a std::wstring */ std::u16string u16string_from_wstring(std::wstring const wstr); -/** @brief Obtain a std::wstring from a std::u16tring */ -std::wstring u16fmt(const KMX_WCHAR* str); - /** @brief Convert pointer to wchar_t to pointer to char16_t and copy sz elements into dst */ void u16sprintf(KMX_WCHAR* dst, const size_t sz, const wchar_t* fmt, ...); -std::wstring convert_pchar16T_To_wstr(KMX_WCHAR* Name); - /** @brief Return the length of the u16string str */ size_t u16len(const KMX_WCHAR* p); diff --git a/developer/src/kmcmplib/tests/util_filesystem.cpp b/developer/src/kmcmplib/tests/util_filesystem.cpp index 7f9edd6b36..4ca210cff9 100644 --- a/developer/src/kmcmplib/tests/util_filesystem.cpp +++ b/developer/src/kmcmplib/tests/util_filesystem.cpp @@ -132,8 +132,8 @@ FILE* Open_File(const KMX_WCHART* Filename, const KMX_WCHART* mode) { FILE* Open_File(const KMX_WCHAR* Filename, const KMX_WCHAR* mode) { #ifdef _MSC_VER - std::wstring cpath = convert_pchar16T_To_wstr((KMX_WCHAR*) Filename); - std::wstring cmode = convert_pchar16T_To_wstr((KMX_WCHAR*) mode); + std::wstring cpath = wstring_from_u16string(Filename); + std::wstring cmode = wstring_from_u16string(mode); std::replace(cpath.begin(), cpath.end(), '/', '\\'); return _wfsopen(cpath.c_str(), cmode.c_str(), _SH_DENYWR); #else From a3a7bd38ffb1e96ec6cf1323a021b39fe290ba59 Mon Sep 17 00:00:00 2001 From: Sabine Date: Wed, 7 Aug 2024 12:22:51 +0200 Subject: [PATCH 3/5] chore(common): use strtof() in u16tof() --- developer/src/kmcmplib/src/kmx_u16.cpp | 24 ++++-------------------- 1 file changed, 4 insertions(+), 20 deletions(-) diff --git a/developer/src/kmcmplib/src/kmx_u16.cpp b/developer/src/kmcmplib/src/kmx_u16.cpp index 04054fe4ad..708c82dc04 100644 --- a/developer/src/kmcmplib/src/kmx_u16.cpp +++ b/developer/src/kmcmplib/src/kmx_u16.cpp @@ -386,24 +386,8 @@ KMX_WCHAR* u16tok(KMX_WCHAR* p, const KMX_WCHAR* delimiters, KMX_WCHAR** ctx) { * @param str Pointer to u16string * @return double value equivalent to the string */ -double u16tof(KMX_WCHAR* str) { - double val = 0; - int offsetdot = 0; - char digit; - - PKMX_WCHAR q = (PKMX_WCHAR)u16chr(str, '.'); - size_t pos_dot = (q - str < 0) ? u16len(str) : q - str; - - for (size_t i = 0; i < u16len(str); i++) { - digit = static_cast(towupper(*str)); - - if (i > pos_dot - 1) - offsetdot = 1; - - if (digit != '.') - val = val + ((int(digit)) - 48) * pow(10, (pos_dot - 1 - i + offsetdot)); - - str++; - } - return val; +double u16tof(KMX_WCHAR* str16) { + char* pEnd; + std::string str = string_from_u16string(str16); + return strtof(str.c_str(), &pEnd); } From 07c8d13f5323bc16e1d4cc50957b2f2d6d519589 Mon Sep 17 00:00:00 2001 From: Sabine Date: Tue, 13 Aug 2024 15:24:19 +0200 Subject: [PATCH 4/5] chore(common):use googletests for kmx_u16 --- developer/src/kmcmplib/src/meson.build | 1 + 1 file changed, 1 insertion(+) diff --git a/developer/src/kmcmplib/src/meson.build b/developer/src/kmcmplib/src/meson.build index 2b9a431e09..260df09b83 100644 --- a/developer/src/kmcmplib/src/meson.build +++ b/developer/src/kmcmplib/src/meson.build @@ -85,6 +85,7 @@ lib = library('kmcmplib', 'versioning.cpp', 'virtualcharkeys.cpp', 'xstring.cpp', + '../../../../common/cpp/utfcodec.cpp', '../../../../common/windows/cpp/src/ConvertUTF.c', '../../../../common/windows/cpp/src/crc32.cpp', '../../../../common/windows/cpp/src/vkeys.cpp', From 30493f94bb2dcbc45e2336f8eac9b8f02d66064b Mon Sep 17 00:00:00 2001 From: Sabine Date: Tue, 13 Aug 2024 15:59:43 +0200 Subject: [PATCH 5/5] chore(common): replace DEBUGSTORE_, DEBUGSTORE_...C with DEBUGSTORE_..._L , DEBUGSTORE_..._U --- developer/src/kmcmplib/src/Compiler.cpp | 8 ++++---- developer/src/kmcmplib/src/debugstore.h | 16 ++++++++-------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/developer/src/kmcmplib/src/Compiler.cpp b/developer/src/kmcmplib/src/Compiler.cpp index e626b140f5..03f3982366 100644 --- a/developer/src/kmcmplib/src/Compiler.cpp +++ b/developer/src/kmcmplib/src/Compiler.cpp @@ -580,7 +580,7 @@ KMX_BOOL ParseLine(PFILE_KEYBOARD fk, PKMX_WCHAR str) { //swprintf(tstr, "%d", fk->currentGroup); /* Record a system store for the line number of the begin statement */ //wcscpy(tstr, DEBUGSTORE_MATCH); - u16sprintf(tstr, _countof(tstr), L"%ls%d ", wstring_from_u16string(DEBUGSTORE_MATCH).c_str(), (int) fk->currentGroup); + u16sprintf(tstr, _countof(tstr), L"%ls%d ", DEBUGSTORE_MATCH_L, (int) fk->currentGroup); u16ncat(tstr, gp->szName, _countof(tstr)); AddDebugStore(fk, tstr); @@ -619,7 +619,7 @@ KMX_BOOL ParseLine(PFILE_KEYBOARD fk, PKMX_WCHAR str) { { KMX_WCHAR tstr[128]; /* Record a system store for the line number of the begin statement */ - u16sprintf(tstr, _countof(tstr), L"%ls%d ", wstring_from_u16string(DEBUGSTORE_NOMATCH).c_str(), (int) fk->currentGroup); + u16sprintf(tstr, _countof(tstr), L"%ls%d ", DEBUGSTORE_NOMATCH_L, (int) fk->currentGroup); u16ncat(tstr, gp->szName, _countof(tstr)); AddDebugStore(fk, tstr); } @@ -683,7 +683,7 @@ KMX_BOOL ProcessGroupLine(PFILE_KEYBOARD fk, PKMX_WCHAR p) { KMX_WCHAR tstr[128]; /* Record a system store for the line number of the begin statement */ - u16sprintf(tstr, _countof(tstr), L"%ls%d ", wstring_from_u16string(DEBUGSTORE_GROUP).c_str(), fk->cxGroupArray - 1); + u16sprintf(tstr, _countof(tstr), L"%ls%d ", DEBUGSTORE_GROUP_L, fk->cxGroupArray - 1); u16ncat(tstr, gp->szName, _countof(tstr)); AddDebugStore(fk, tstr); } @@ -3761,7 +3761,7 @@ void kmcmp::RecordDeadkeyNames(PFILE_KEYBOARD fk) KMX_DWORD i; for (i = 0; i < fk->cxDeadKeyArray; i++) { - u16sprintf(buf, _countof(buf), L"%ls%d ", wstring_from_u16string(DEBUGSTORE_DEADKEY).c_str(), (int)i); + u16sprintf(buf, _countof(buf), L"%ls%d ", DEBUGSTORE_DEADKEY_L, (int)i); u16ncat(buf, fk->dpDeadKeyArray[i].szName, _countof(buf)); AddDebugStore(fk, buf); diff --git a/developer/src/kmcmplib/src/debugstore.h b/developer/src/kmcmplib/src/debugstore.h index c54e3b45c3..d8bd4643df 100644 --- a/developer/src/kmcmplib/src/debugstore.h +++ b/developer/src/kmcmplib/src/debugstore.h @@ -5,16 +5,16 @@ #define DEBUGSTORE_BEGIN u"B" #define DEBUGSTORE_BEGIN_C u'B' -#define DEBUGSTORE_MATCH u"M" -#define DEBUGSTORE_MATCH_C u'M' +#define DEBUGSTORE_MATCH_U u"M" +#define DEBUGSTORE_MATCH_L L"M" -#define DEBUGSTORE_NOMATCH u"N" -#define DEBUGSTORE_NOMATCH_C u'N' +#define DEBUGSTORE_NOMATCH_U u"M" +#define DEBUGSTORE_NOMATCH_L L"M" -#define DEBUGSTORE_GROUP u"G" -#define DEBUGSTORE_GROUP_C u'G' +#define DEBUGSTORE_GROUP_U u"G" +#define DEBUGSTORE_GROUP_L L"G" -#define DEBUGSTORE_DEADKEY u"D" -#define DEBUGSTORE_DEADKEY_C u'D' +#define DEBUGSTORE_DEADKEY_U u"D" +#define DEBUGSTORE_DEADKEY_L L"D" #endif /* DEBUGSTORE_H */