COMMON: introduce substr for U32String (#2585)

It's safer than direct array manipulation. Replace current uses
of subsetting with substr.
This commit is contained in:
Vladimir Serbinenko 2020-11-01 22:53:03 +01:00 committed by GitHub
parent 0128d5e3b5
commit 3fc822e55c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 31 additions and 21 deletions

View File

@ -131,6 +131,15 @@ U32String operator+(const U32String &x, const U32String::value_type y) {
return temp;
}
U32String U32String::substr(size_t pos, size_t len) const {
if (pos >= _size)
return U32String();
else if (len == npos)
return U32String(_str + pos);
else
return U32String(_str + pos, MIN((size_t)_size - pos, len));
}
void U32String::insertString(const char *s, uint32 p) {
while (*s != '\0') {
BaseString<u32char_type_t>::insertChar(*s++, p++);

View File

@ -139,6 +139,9 @@ public:
void insertString(const char *s, uint32 p);
void insertString(const String &s, uint32 p);
/** Return a substring of this string */
U32String substr(size_t pos = 0, size_t len = npos) const;
const uint32 *u32_str() const {
return (const uint32 *) _str;
}

View File

@ -252,7 +252,7 @@ RenderedText *TTFont::renderText(const Std::string &text, unsigned int &remainin
if (iter->_cursor != Std::string::npos) {
assert(iter->_cursor <= iter->_text.size());
unicodeText = Common::U32String(unicodeText.c_str(), iter->_cursor);
unicodeText = unicodeText.substr(0, iter->_cursor);
int w = _ttfFont->getStringWidth(unicodeText);

View File

@ -121,7 +121,7 @@ int BaseFontTT::getTextWidth(const byte *text, int maxLength) {
}
if (maxLength >= 0 && textStr.size() > (uint32)maxLength) {
textStr = WideString(textStr.c_str(), (uint32)maxLength);
textStr = textStr.substr(0, (uint32)maxLength);
}
//text = text.substr(0, MaxLength); // TODO: Remove
@ -167,7 +167,7 @@ void BaseFontTT::drawText(const byte *text, int x, int y, int width, TTextAlign
}
if (maxLength >= 0 && textStr.size() > (uint32)maxLength) {
textStr = WideString(textStr.c_str(), (uint32)maxLength);
textStr = textStr.substr(0, (uint32)maxLength);
}
//text = text.substr(0, MaxLength); // TODO: Remove

View File

@ -128,8 +128,7 @@ bool SXString::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack
str = StringUtil::ansiToWide(_string);
}
//WideString subStr = str.substr(start, end - start + 1);
WideString subStr(str.c_str() + start, end - start + 1);
WideString subStr = str.substr(start, end - start + 1);
if (_gameRef->_textEncoding == TEXT_UTF8) {
stack->pushString(StringUtil::wideToUtf8(subStr).c_str());
@ -170,8 +169,7 @@ bool SXString::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack
str = StringUtil::ansiToWide(_string);
}
// WideString subStr = str.substr(start, len);
WideString subStr(str.c_str() + start, len);
WideString subStr = str.substr(start, len);
if (_gameRef->_textEncoding == TEXT_UTF8) {
stack->pushString(StringUtil::wideToUtf8(subStr).c_str());
@ -323,7 +321,7 @@ bool SXString::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack
uint32 ch = (i == str.size()) ? '\0' : str[i];
if (ch =='\0' || delims.contains(ch)) {
if (i != start) {
parts.push_back(WideString(str.c_str() + start, i - start));
parts.push_back(str.substr(start, i - start));
} else {
parts.push_back(WideString());
}

View File

@ -619,8 +619,8 @@ void MacMenu::processSubmenuTabs(MacMenuSubMenu *submenu) {
haveTabs = true;
Common::U32String start(item->unicodeText.c_str(), &item->unicodeText.c_str()[pos]);
Common::U32String end(&item->unicodeText.c_str()[pos + 1]);
Common::U32String start = item->unicodeText.substr(0, pos);
Common::U32String end = item->unicodeText.substr(pos + 1);
res = start;
res += Common::U32String(" ");
@ -648,8 +648,8 @@ void MacMenu::processSubmenuTabs(MacMenuSubMenu *submenu) {
if (pos == Common::U32String::npos)
continue;
Common::U32String start(item->unicodeText.c_str(), &item->unicodeText.c_str()[pos]);
Common::U32String end(&item->unicodeText.c_str()[pos + 1]);
Common::U32String start = item->unicodeText.substr(0, pos);
Common::U32String end = item->unicodeText.substr(pos + 1);
Common::U32String res;
Common::U32String spaces(" ");
int width;

View File

@ -614,7 +614,7 @@ int MacText::getLineWidth(int line, bool enforce, int col) {
if (col >= (int)_textLines[line].chunks[i].text.size()) {
col -= _textLines[line].chunks[i].text.size();
} else {
Common::U32String tmp(_textLines[line].chunks[i].text.c_str(), col);
Common::U32String tmp = _textLines[line].chunks[i].text.substr(0, col);
width += _textLines[line].chunks[i].getFont()->getStringWidth(tmp);
@ -1053,7 +1053,7 @@ void MacText::setSelection(int pos, bool start) {
if (pos < getLineCharWidth(row)) {
for (uint i = 0; i < _textLines[row].chunks.size(); i++) {
if ((uint)pos < _textLines[row].chunks[i].text.size()) {
colX += _textLines[row].chunks[i].getFont()->getStringWidth(Common::U32String(_textLines[row].chunks[i].text.c_str(), pos));
colX += _textLines[row].chunks[i].getFont()->getStringWidth(_textLines[row].chunks[i].text.substr(0, pos));
col += pos + 1;
pos = 0;
break;
@ -1439,10 +1439,10 @@ Common::U32String MacText::getTextChunk(int startRow, int startCol, int endRow,
if (endCol >= (int)_textLines[i].chunks[chunk].text.size())
res += _textLines[i].chunks[chunk].text;
else
res += Common::U32String(_textLines[i].chunks[chunk].text.c_str(), endCol);
res += _textLines[i].chunks[chunk].text.substr(0, endCol);
} else if ((int)_textLines[i].chunks[chunk].text.size() > startCol) {
ADDFORMATTING();
res += Common::U32String(_textLines[i].chunks[chunk].text.c_str() + startCol, endCol - startCol);
res += _textLines[i].chunks[chunk].text.substr(startCol, endCol - startCol);
}
startCol -= _textLines[i].chunks[chunk].text.size();
@ -1462,7 +1462,7 @@ Common::U32String MacText::getTextChunk(int startRow, int startCol, int endRow,
res += _textLines[i].chunks[chunk].text;
} else if ((int)_textLines[i].chunks[chunk].text.size() > startCol) {
ADDFORMATTING();
res += Common::U32String(_textLines[i].chunks[chunk].text.c_str() + startCol);
res += _textLines[i].chunks[chunk].text.substr(startCol);
}
startCol -= _textLines[i].chunks[chunk].text.size();
@ -1482,7 +1482,7 @@ Common::U32String MacText::getTextChunk(int startRow, int startCol, int endRow,
if (endCol >= (int)_textLines[i].chunks[chunk].text.size())
res += _textLines[i].chunks[chunk].text;
else
res += Common::U32String(_textLines[i].chunks[chunk].text.c_str(), endCol);
res += _textLines[i].chunks[chunk].text.substr(0, endCol);
endCol -= _textLines[i].chunks[chunk].text.size();
@ -1605,8 +1605,8 @@ void MacText::addNewLine(int *row, int *col) {
MacFontRun newchunk = line->chunks[ch];
MacTextLine newline;
newchunk.text = &line->chunks[ch].text.c_str()[pos];
line->chunks[ch].text = Common::U32String(line->chunks[ch].text.c_str(), pos);
newchunk.text = line->chunks[ch].text.substr(pos);
line->chunks[ch].text = line->chunks[ch].text.substr(0, pos);
newline.chunks.push_back(newchunk);
for (uint i = ch + 1; i < line->chunks.size(); i++) {

View File

@ -286,7 +286,7 @@ Common::U32String MacTextWindow::cutSelection() {
return Common::U32String("");
}
Common::U32String newInput = Common::U32String(_inputText.c_str(), selPos) + Common::U32String(_inputText.c_str() + selPos + selection.size());
Common::U32String newInput = _inputText.substr(0, selPos) + _inputText.substr(selPos + selection.size());
clearSelection();
clearInput();