Improve code style

This commit is contained in:
halx99 2021-12-01 11:49:42 +08:00 committed by GitHub
parent 6a41776f20
commit 629b882230
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 15 deletions

View File

@ -182,7 +182,7 @@ namespace ui {
return label->getContentSize().width; return label->getContentSize().width;
} }
static std::string internalUTF8MoveLeft(const std::string& utf8Text, int length/* default utf8Text.length() */) static std::string internalUTF8MoveLeft(std::string_view utf8Text, int length/* default utf8Text.length() */)
{ {
if (!utf8Text.empty() && length > 0) { if (!utf8Text.empty() && length > 0) {
@ -194,17 +194,14 @@ namespace ui {
++deleteLen; ++deleteLen;
} }
std::string temp = utf8Text; return std::string{utf8Text.data(), static_cast<size_t>(length - deleteLen)};
temp.resize(length - deleteLen);
return temp;
} }
else { else {
return utf8Text; return utf8Text;
} }
} }
static std::string internalUTF8MoveRight(const std::string& utf8Text, int length/* default utf8Text.length() */) static std::string internalUTF8MoveRight(std::string_view utf8Text, int length/* default utf8Text.length() */)
{ {
if (!utf8Text.empty() && length >= 0) { if (!utf8Text.empty() && length >= 0) {
@ -216,10 +213,7 @@ namespace ui {
++addLen; ++addLen;
} }
std::string temp = utf8Text; return std::string{utf8Text.data(), static_cast<size_t>(length + addLen)};
temp.resize(length + addLen);
return temp;
} }
else { else {
return utf8Text; return utf8Text;
@ -945,17 +939,17 @@ namespace ui {
if (newOffset > 0 && newOffset <= this->charCount) { if (newOffset > 0 && newOffset <= this->charCount) {
const std::string* displayText = nullptr; std::string_view displayText;
if (!secureTextEntry) if (!secureTextEntry)
displayText = &this->getString(); displayText = this->getString();
else if(!this->inputText.empty()) else if(!this->inputText.empty())
displayText = (const std::string*) &renderLabel->getString(); displayText = renderLabel->getString();
if (direction < 0) if (direction < 0)
{ {
this->insertPos = internalUTF8MoveLeft(this->inputText, this->insertPos).size(); this->insertPos = internalUTF8MoveLeft(this->inputText, this->insertPos).size();
auto s = internalUTF8MoveLeft(*displayText, this->cursorPos); auto s = internalUTF8MoveLeft(displayText, this->cursorPos);
auto width = internalCalcStringWidth(s, this->fontName, this->fontSize); auto width = internalCalcStringWidth(s, this->fontName, this->fontSize);
this->cursor->setPosition(Point(width, this->getContentSize().height / 2)); this->cursor->setPosition(Point(width, this->getContentSize().height / 2));
@ -964,7 +958,7 @@ namespace ui {
else { else {
this->insertPos = internalUTF8MoveRight(this->inputText, this->insertPos).size(); this->insertPos = internalUTF8MoveRight(this->inputText, this->insertPos).size();
auto s = internalUTF8MoveRight(*displayText, this->cursorPos); auto s = internalUTF8MoveRight(displayText, this->cursorPos);
auto width = internalCalcStringWidth(s, this->fontName, this->fontSize); auto width = internalCalcStringWidth(s, this->fontName, this->fontSize);
this->cursor->setPosition(Point(width, this->getContentSize().height / 2)); this->cursor->setPosition(Point(width, this->getContentSize().height / 2));
this->cursorPos = s.length(); this->cursorPos = s.length();