From a9d1aaa774b9e986b8ee3c6cb1e7baa1d9ba3ea5 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Fri, 9 Aug 2013 23:41:59 +0300 Subject: improve size() methods usage. --- src/being.cpp | 9 ++++++--- src/gui/widgets/characterviewsmall.cpp | 2 +- src/gui/widgets/chattab.cpp | 9 +++++---- src/gui/widgets/textbox.cpp | 10 +++++----- src/guichan/widgets/textbox.cpp | 15 +++++++++------ src/guichan/widgets/textfield.cpp | 6 +++--- src/inputmanager.cpp | 3 ++- src/utils/stringutils.cpp | 27 +++++++++++++++++++-------- src/utils/translation/poparser.cpp | 14 ++++++++------ 9 files changed, 58 insertions(+), 37 deletions(-) (limited to 'src') diff --git a/src/being.cpp b/src/being.cpp index 2087b6e0b..287cc3ecd 100644 --- a/src/being.cpp +++ b/src/being.cpp @@ -420,9 +420,12 @@ void Being::setSpeech(const std::string &text, const std::string &channel, if (mSpeech.empty()) return; - const size_t sz = mSpeech.size(); - if (!time && sz < 200) - time = static_cast(SPEECH_TIME - 300 + (3 * sz)); + if (!time) + { + const size_t sz = mSpeech.size(); + if (sz < 200) + time = static_cast(SPEECH_TIME - 300 + (3 * sz)); + } if (time < static_cast(SPEECH_MIN_TIME)) time = static_cast(SPEECH_MIN_TIME); diff --git a/src/gui/widgets/characterviewsmall.cpp b/src/gui/widgets/characterviewsmall.cpp index fa654b94d..6cdb01c7e 100644 --- a/src/gui/widgets/characterviewsmall.cpp +++ b/src/gui/widgets/characterviewsmall.cpp @@ -67,7 +67,7 @@ void CharacterViewSmall::show(const int i) if (i >= sz) mSelected = 0; else if (i < 0) - mSelected = mCharacterEntries->size() - 1; + mSelected = sz - 1; else mSelected = i; mSelectedEntry = (*mCharacterEntries)[mSelected]; diff --git a/src/gui/widgets/chattab.cpp b/src/gui/widgets/chattab.cpp index 03d1aaf78..37e94ef1b 100644 --- a/src/gui/widgets/chattab.cpp +++ b/src/gui/widgets/chattab.cpp @@ -346,7 +346,8 @@ void ChatTab::chatInput(const std::string &message) // Check for item link size_t start = msg.find('['); - while (start + 1 < msg.size() && start != std::string::npos + size_t sz = msg.size(); + while (start + 1 < sz && start != std::string::npos && msg[start + 1] != '@') { const size_t end = msg.find(']', start); @@ -360,9 +361,8 @@ void ChatTab::chatInput(const std::string &message) start = msg.find('[', start + 1); } - std::string temp(""); - if (start + 1 < msg.length() && end < msg.length() - && end > start + 1) + std::string temp; + if (start + 1 < sz && end < sz && end > start + 1) { temp = msg.substr(start + 1, end - start - 1); @@ -373,6 +373,7 @@ void ChatTab::chatInput(const std::string &message) msg.insert(start + 1, "|"); msg.insert(start + 1, toString(itemInfo.getId())); msg.insert(start + 1, "@@"); + sz = msg.size(); } } } diff --git a/src/gui/widgets/textbox.cpp b/src/gui/widgets/textbox.cpp index 4eccbe76c..660655033 100644 --- a/src/gui/widgets/textbox.cpp +++ b/src/gui/widgets/textbox.cpp @@ -89,13 +89,14 @@ void TextBox::setTextWrapped(const std::string &text, const int minDimension) xpos = 0; gcn::Font *const font = getFont(); const int spaceWidth = font->getWidth(" "); + size_t sz = line.size(); do { spacePos = line.find(" ", lastSpacePos); if (spacePos == std::string::npos) - spacePos = line.size(); + spacePos = sz; const std::string word = line.substr(lastSpacePos, spacePos - lastSpacePos); @@ -130,14 +131,13 @@ void TextBox::setTextWrapped(const std::string &text, const int minDimension) mMinWidth = minWidth; wrappedStream.clear(); wrappedStream.str(""); -// spacePos = 0; lastNewlinePos = 0; newlinePos = text.find("\n", lastNewlinePos); if (newlinePos == std::string::npos) newlinePos = textSize; line = text.substr(lastNewlinePos, newlinePos - lastNewlinePos); -// width = 0; + sz = line.size(); break; } else @@ -148,14 +148,14 @@ void TextBox::setTextWrapped(const std::string &text, const int minDimension) } lastSpacePos = spacePos + 1; } - while (spacePos != line.size()); + while (spacePos != sz); if (text.find("\n", lastNewlinePos) != std::string::npos) wrappedStream << "\n"; lastNewlinePos = newlinePos + 1; } - while (newlinePos != text.size()); + while (newlinePos != textSize); if (xpos > minWidth) minWidth = xpos; diff --git a/src/guichan/widgets/textbox.cpp b/src/guichan/widgets/textbox.cpp index 3f1c51402..512471391 100644 --- a/src/guichan/widgets/textbox.cpp +++ b/src/guichan/widgets/textbox.cpp @@ -137,8 +137,9 @@ namespace gcn { mCaretRow = mouseEvent.getY() / getFont()->getHeight(); - if (mCaretRow >= static_cast(mTextRows.size())) - mCaretRow = static_cast(mTextRows.size() - 1); + const int sz = static_cast(mTextRows.size()); + if (mCaretRow >= sz) + mCaretRow = sz - 1; mCaretColumn = getFont()->getStringIndexAt( mTextRows[mCaretRow], mouseEvent.getX()); @@ -210,8 +211,9 @@ namespace gcn { mCaretRow = row; - if (mCaretRow >= static_cast(mTextRows.size())) - mCaretRow = static_cast(mTextRows.size() - 1); + const int sz = static_cast(mTextRows.size()); + if (mCaretRow >= sz) + mCaretRow = sz - 1; if (mCaretRow < 0) mCaretRow = 0; @@ -228,8 +230,9 @@ namespace gcn { mCaretColumn = column; - if (mCaretColumn > static_cast(mTextRows[mCaretRow].size())) - mCaretColumn = static_cast(mTextRows[mCaretRow].size()); + const int sz = static_cast(mTextRows[mCaretRow].size()); + if (mCaretColumn > sz) + mCaretColumn = sz; if (mCaretColumn < 0) mCaretColumn = 0; diff --git a/src/guichan/widgets/textfield.cpp b/src/guichan/widgets/textfield.cpp index e5ecc172b..f0d464995 100644 --- a/src/guichan/widgets/textfield.cpp +++ b/src/guichan/widgets/textfield.cpp @@ -89,9 +89,9 @@ namespace gcn void TextField::setText(const std::string& text) { - if (text.size() < mCaretPosition) - mCaretPosition = static_cast(text.size()); - + const size_t sz = text.size(); + if (sz < mCaretPosition) + mCaretPosition = sz; mText = text; } diff --git a/src/inputmanager.cpp b/src/inputmanager.cpp index 3b99c3498..b0a31b442 100644 --- a/src/inputmanager.cpp +++ b/src/inputmanager.cpp @@ -108,6 +108,7 @@ void InputManager::retrieve() mNameMap[cf] = i; KeyFunction &kf = mKey[i]; const std::string keyStr = config.getValue(cf, ""); + const int keyStrSize = keyStr.size(); if (keyStr.empty()) continue; @@ -118,7 +119,7 @@ void InputManager::retrieve() it != it_end && i2 < KeyFunctionSize; ++ it) { std::string keyStr2 = *it; - if (keyStr.size() < 2) + if (keyStrSize < 2) continue; int type = INPUT_KEYBOARD; if ((keyStr2[0] < '0' || keyStr2[0] > '9') diff --git a/src/utils/stringutils.cpp b/src/utils/stringutils.cpp index a54d9d5ab..09cd41fc2 100644 --- a/src/utils/stringutils.cpp +++ b/src/utils/stringutils.cpp @@ -286,20 +286,30 @@ std::string removeSpriteIndex(std::string str) const char* getSafeUtf8String(std::string text) { - const int size = static_cast(text.size()) + UTF8_MAX_SIZE; + const size_t sz = text.size(); + const size_t size = sz + UTF8_MAX_SIZE; char *const buf = new char[size]; - memcpy(buf, text.c_str(), text.size()); - memset(buf + text.size(), 0, UTF8_MAX_SIZE); + memcpy(buf, text.c_str(), sz); + memset(buf + sz, 0, UTF8_MAX_SIZE); return buf; } void getSafeUtf8String(std::string text, char *const buf) { - const int size = static_cast(text.size()) + UTF8_MAX_SIZE; + const size_t sz = text.size(); + const size_t size = sz + UTF8_MAX_SIZE; if (size > 65500) + { text = text.substr(0, 65500); - memcpy(buf, text.c_str(), text.size()); - memset(buf + text.size(), 0, UTF8_MAX_SIZE); + const size_t sz1 = text.size(); + memcpy(buf, text.c_str(), sz1); + memset(buf + sz1, 0, UTF8_MAX_SIZE); + } + else + { + memcpy(buf, text.c_str(), sz); + memset(buf + sz, 0, UTF8_MAX_SIZE); + } return; } @@ -366,11 +376,12 @@ void replaceSpecialChars(std::string &text) while (pos1 != std::string::npos) { const size_t idx = pos1 + 1; - if (idx >= text.size()) + const size_t sz = text.size(); + if (idx >= sz) break; size_t f; - for (f = idx; f < text.size(); f ++) + for (f = idx; f < sz; f ++) { if (text[f] < '0' || text[f] > '9') break; diff --git a/src/utils/translation/poparser.cpp b/src/utils/translation/poparser.cpp index e6c41580c..55d4e01e2 100644 --- a/src/utils/translation/poparser.cpp +++ b/src/utils/translation/poparser.cpp @@ -161,9 +161,10 @@ bool PoParser::readMsgId() if (strStartWith(mLine, msgId1)) { mReadingId = true; + const size_t msgId1Size = msgId1.size(); // reading text from: msgid "text" - mMsgId.append(mLine.substr(msgId1.size(), - mLine.size() - 1 - msgId1.size())); + mMsgId.append(mLine.substr(msgId1Size, + mLine.size() - 1 - msgId1Size)); mLine.clear(); return true; } @@ -201,9 +202,10 @@ bool PoParser::readMsgStr() if (strStartWith(mLine, msgStr1)) { mReadingStr = true; + const size_t msgStr1Size = msgStr1.size(); // reading text from: msgid "text" - mMsgStr.append(mLine.substr(msgStr1.size(), - mLine.size() - 1 - msgStr1.size())); + mMsgStr.append(mLine.substr(msgStr1Size, + mLine.size() - 1 - msgStr1Size)); mLine.clear(); return true; } @@ -215,9 +217,9 @@ bool PoParser::readMsgStr() bool PoParser::checkLine() { + const size_t sz = mLine.size(); // check is line in format: "text" - return mLine.size() > 2 && mLine[0] == '\"' - && mLine[mLine.size() - 1] == '\"'; + return sz > 2 && mLine[0] == '\"' && mLine[sz - 1] == '\"'; } PoDict *PoParser::getEmptyDict() -- cgit v1.2.3-70-g09d2