COMMON: Add helper functions for converting strings

This commit is contained in:
Cameron Cawley 2020-09-06 16:54:58 +01:00 committed by Eugene Sandulenko
parent 89ae351ca5
commit 6e0c93dc46
3 changed files with 12 additions and 5 deletions

View File

@ -78,6 +78,14 @@ class Encoding {
*/
static char *convert(const String &to, const String &from, const char *string, size_t length);
static char *convert(const String &to, const String &from, const String &s) {
return convert(to, from, s.c_str(), s.size());
}
static char *convert(const String &to, const U32String &s) {
return convert(to, "UTF-32", (const char *)s.c_str(), s.size() * 4);
}
/**
* @return The encoding, which is currently being converted from
*/

View File

@ -345,7 +345,7 @@ U32String String::decode(CodePage page) const {
page >= ARRAYSIZE(g_codePageMap)) {
error("Invalid codepage");
}
char *result = Encoding::convert("UTF-32", g_codePageMap[page], _str, _size);
char *result = Encoding::convert("UTF-32", g_codePageMap[page], *this);
if (result) {
U32String unicodeString((uint32 *)result);
free(result);
@ -419,7 +419,7 @@ String U32String::encode(CodePage page) const {
page >= ARRAYSIZE(g_codePageMap)) {
error("Invalid codepage");
}
char *result = Encoding::convert(g_codePageMap[page], "UTF-32", (const char *)_str, _size * 4);
char *result = Encoding::convert(g_codePageMap[page], *this);
if (result) {
// Encodings in CodePage all use '\0' as string ending
// That would be problematic if CodePage has UTF-16 or UTF-32

View File

@ -175,9 +175,8 @@ public:
* It will convert to UTF-32 before passing along to the intended method.
*/
bool say(const String &str, Action action, String charset = "UTF-8") {
Encoding speakWithCustomCharset("UTF-32", charset);
char *res = speakWithCustomCharset.convert(str.c_str(), str.size());
U32String textToSpeak(reinterpret_cast<uint32*>(res));
uint32 *res = (uint32 *)Encoding::convert("UTF-32", charset, str);
U32String textToSpeak(res);
free(res);
return say(textToSpeak, action);