QUnicodeTools: Use QVarLengthArray in Thai support code

This replaces an ad-hoc solution.

As a drive-by, remove a check that was always true.

Change-Id: I72166ee75a2c474dc91bc699c790f256b78b3b7a
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
(cherry picked from commit 42f555f861bbc7daaf211bc83fd57795f45f7a86)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Ievgenii Meshcheriakov 2023-01-13 12:52:20 +01:00 committed by Qt Cherry-pick Bot
parent 20a69a0c84
commit 2b5ffaf1d4

View File

@ -1492,11 +1492,9 @@ static void to_tis620(const char16_t *string, qsizetype len, char *cstr)
*/ */
static void thaiAssignAttributes(const char16_t *string, qsizetype len, QCharAttributes *attributes) static void thaiAssignAttributes(const char16_t *string, qsizetype len, QCharAttributes *attributes)
{ {
char s[128]; constexpr qsizetype Prealloc = 128;
char *cstr = s; QVarLengthArray<char, Prealloc + 1> s(len + 1);
int *break_positions = nullptr; QVarLengthArray<int, Prealloc> break_positions(len);
int brp[128];
size_t brp_size = 0;
qsizetype numbreaks, i; qsizetype numbreaks, i;
struct thcell_t tis_cell; struct thcell_t tis_cell;
@ -1504,10 +1502,7 @@ static void thaiAssignAttributes(const char16_t *string, qsizetype len, QCharAtt
if (!libThai || !libThai->isInitialized()) if (!libThai || !libThai->isInitialized())
return; return;
if (len >= 128) to_tis620(string, len, s.data());
cstr = static_cast<char *>(malloc (len * sizeof(char) + 1));
to_tis620(string, len, cstr);
for (i = 0; i < len; ++i) { for (i = 0; i < len; ++i) {
attributes[i].wordBreak = false; attributes[i].wordBreak = false;
@ -1516,22 +1511,12 @@ static void thaiAssignAttributes(const char16_t *string, qsizetype len, QCharAtt
attributes[i].lineBreak = false; attributes[i].lineBreak = false;
} }
if (len > 128) {
break_positions = static_cast<int *>(malloc (sizeof(int) * len));
memset (break_positions, 0, sizeof(int) * len);
brp_size = size_t(len);
}
else {
break_positions = brp;
brp_size = 128;
}
if (break_positions) {
attributes[0].wordBreak = true; attributes[0].wordBreak = true;
attributes[0].wordStart = true; attributes[0].wordStart = true;
attributes[0].wordEnd = false; attributes[0].wordEnd = false;
numbreaks = libThai->brk_find_breaks(reinterpret_cast<const unsigned char *>(cstr), numbreaks = libThai->brk_find_breaks(reinterpret_cast<const unsigned char *>(s.data()),
break_positions, brp_size); break_positions.data(),
static_cast<size_t>(break_positions.size()));
for (i = 0; i < numbreaks; ++i) { for (i = 0; i < numbreaks; ++i) {
attributes[break_positions[i]].wordBreak = true; attributes[break_positions[i]].wordBreak = true;
attributes[break_positions[i]].wordStart = true; attributes[break_positions[i]].wordStart = true;
@ -1541,14 +1526,11 @@ static void thaiAssignAttributes(const char16_t *string, qsizetype len, QCharAtt
if (numbreaks > 0) if (numbreaks > 0)
attributes[break_positions[numbreaks - 1]].wordStart = false; attributes[break_positions[numbreaks - 1]].wordStart = false;
if (break_positions != brp)
free(break_positions);
}
/* manage grapheme boundaries */ /* manage grapheme boundaries */
i = 0; i = 0;
while (i < len) { while (i < len) {
size_t cell_length = libThai->next_cell(reinterpret_cast<const unsigned char *>(cstr) + i, size_t cell_length =
libThai->next_cell(reinterpret_cast<const unsigned char *>(s.data()) + i,
size_t(len - i), &tis_cell, true); size_t(len - i), &tis_cell, true);
attributes[i].graphemeBoundary = true; attributes[i].graphemeBoundary = true;
@ -1557,9 +1539,6 @@ static void thaiAssignAttributes(const char16_t *string, qsizetype len, QCharAtt
i += cell_length; i += cell_length;
} }
if (len >= 128)
free(cstr);
} }
#endif // QT_CONFIG(library) #endif // QT_CONFIG(library)