From eb3c6edc3f99433ffadf4993b0cba9dc4ec74300 Mon Sep 17 00:00:00 2001 From: Jarod42 Date: Fri, 31 May 2024 09:01:42 +0200 Subject: [PATCH] [cleanup] Replace some `const std::string &` by `std::string_view` --- src/include/font.h | 8 ++++---- src/include/menus.h | 4 +++- src/include/util.h | 13 +++++++------ src/stratagus/script.cpp | 2 +- src/stratagus/util.cpp | 6 +++--- src/ui/popup.cpp | 12 ++++++------ src/ui/uibuttons_proc.cpp | 2 +- src/video/font.cpp | 25 ++++++++++++------------- tests/stratagus/test_util.cpp | 4 ++-- 9 files changed, 39 insertions(+), 37 deletions(-) diff --git a/src/include/font.h b/src/include/font.h index 6e3141f45c..d7adc96978 100644 --- a/src/include/font.h +++ b/src/include/font.h @@ -86,7 +86,7 @@ class CFont : public gcn::Font static CFont *Get(std::string_view ident); int Height() const; - int Width(const std::string &text) const; + int Width(std::string_view text) const; int Width(const int number) const; bool IsLoaded() const; @@ -164,7 +164,7 @@ extern void SetDefaultTextColors(const std::string &normal, const std::string &r /// Get the default text colors for normal and reverse text extern std::pair GetDefaultTextColors(); /// Return the 'line' line of the string 's'. -extern std::string GetLineFont(unsigned int line, const std::string &s, unsigned int maxlen, const CFont *font); +extern std::string_view GetLineFont(unsigned int line, std::string_view s, unsigned int maxlen, const CFont *font); /// Get the hot key from a string extern int GetHotKey(const std::string &text); @@ -204,8 +204,8 @@ class CLabel int DrawReverseClip(int x, int y, std::string_view text) const; int DrawReverseClip(int x, int y, int number) const; - int DrawCentered(int x, int y, const std::string &text) const; - int DrawReverseCentered(int x, int y, const std::string &text) const; + int DrawCentered(int x, int y, std::string_view text) const; + int DrawReverseCentered(int x, int y, std::string_view text) const; private: template int DoDrawText(int x, int y, std::string_view text, const CFontColor *fc) const; diff --git a/src/include/menus.h b/src/include/menus.h index 9263d37ebd..4a66260850 100644 --- a/src/include/menus.h +++ b/src/include/menus.h @@ -32,6 +32,8 @@ //@{ +#include + /*---------------------------------------------------------------------------- -- Defines/Declarations ----------------------------------------------------------------------------*/ @@ -47,7 +49,7 @@ class ButtonStyle; /// Draw menu button extern void DrawUIButton(ButtonStyle *style, unsigned flags, - int x, int y, const std::string &text, int player = -1); + int x, int y, std::string_view text, int player = -1); /// Pre menu setup extern void PreMenuSetup(); diff --git a/src/include/util.h b/src/include/util.h index 73293e896d..ef46046633 100644 --- a/src/include/util.h +++ b/src/include/util.h @@ -35,11 +35,12 @@ #include "filesystem.h" #include "stratagus.h" -#include -#include -#include #include +#include +#include #include +#include +#include /*---------------------------------------------------------------------------- -- Random @@ -77,7 +78,7 @@ void clamp(T *value, T minValue, T maxValue) *value = std::clamp(*value, minValue, maxValue); } -extern uint32_t fletcher32(const std::string &content); +extern uint32_t fletcher32(std::string_view content); /*---------------------------------------------------------------------------- -- Strings @@ -141,8 +142,8 @@ void SetClipboard(const std::string &str); -- UTF8 ----------------------------------------------------------------------------*/ -int UTF8GetNext(const std::string &text, int curpos); -int UTF8GetPrev(const std::string &text, int curpos); +int UTF8GetNext(std::string_view text, int curpos); +int UTF8GetPrev(std::string_view text, int curpos); void append_unicode(std::string &s, std::uint32_t unicode); std::string to_utf8(std::uint32_t unicode); diff --git a/src/stratagus/script.cpp b/src/stratagus/script.cpp index 6796007661..83c476936d 100644 --- a/src/stratagus/script.cpp +++ b/src/stratagus/script.cpp @@ -1157,7 +1157,7 @@ std::string StringDescLine::eval() const if (line <= 0) { return std::string(""); } - return GetLineFont(line, s, maxlen, font); + return std::string(GetLineFont(line, s, maxlen, font)); } std::string StringDescPlayerName::eval() const diff --git a/src/stratagus/util.cpp b/src/stratagus/util.cpp index 19099deec1..8002530ed0 100644 --- a/src/stratagus/util.cpp +++ b/src/stratagus/util.cpp @@ -174,7 +174,7 @@ long isqrt(long num) // from wikipedia, simple checksumming of our lua files. only considers a subset of 7-bit // ascii chars to hopefully avoid issues with filesystem encodings -uint32_t fletcher32(const std::string &content) +uint32_t fletcher32(std::string_view content) { std::vector alphas; size_t consideredChars = 0; @@ -430,7 +430,7 @@ void SetClipboard(const std::string &str) { -- UTF8 ----------------------------------------------------------------------------*/ -int UTF8GetPrev(const std::string &text, int curpos) +int UTF8GetPrev(std::string_view text, int curpos) { --curpos; if (curpos < 0) { @@ -448,7 +448,7 @@ int UTF8GetPrev(const std::string &text, int curpos) return 0; } -int UTF8GetNext(const std::string &text, int curpos) +int UTF8GetNext(std::string_view text, int curpos) { if (curpos >= (int)text.size()) { return curpos + 1; diff --git a/src/ui/popup.cpp b/src/ui/popup.cpp index 6bb4f5f434..9857d80b18 100644 --- a/src/ui/popup.cpp +++ b/src/ui/popup.cpp @@ -62,14 +62,14 @@ int CPopupContentTypeButtonInfo::GetWidth(const ButtonAction &button, int *) con const std::string draw = GetDraw(this->InfoType, button); int width = 0; - std::string sub; + std::string_view sub; if (draw.length()) { if (this->MaxWidth) { return std::min((unsigned int)font.getWidth(draw), this->MaxWidth); } int i = 1; while (!(sub = GetLineFont(i++, draw, 0, &font)).empty()) { - width = std::max(width, font.getWidth(sub)); + width = std::max(width, font.Width(sub)); } } return width; @@ -106,7 +106,7 @@ void CPopupContentTypeButtonInfo::Draw(int x, unsigned int width = this->MaxWidth ? std::min(this->MaxWidth, popupWidth - 2 * popup.MarginX) : 0; - std::string sub; + std::string_view sub; while ((sub = GetLineFont(++i, draw, width, &font)).length()) { label.Draw(x, y_off, sub); y_off += font.Height() + 2; @@ -154,10 +154,10 @@ int CPopupContentTypeText::GetWidth(const ButtonAction &button, int *) const /* return std::min((unsigned int)font.getWidth(this->Text), this->MaxWidth); } int width = 0; - std::string sub; + std::string_view sub; int i = 1; while (!(sub = GetLineFont(i++, this->Text, 0, &font)).empty()) { - width = std::max(width, font.getWidth(sub)); + width = std::max(width, font.Width(sub)); } return width; } @@ -182,7 +182,7 @@ void CPopupContentTypeText::Draw(int x, { const CFont &font = this->Font ? *this->Font : GetSmallFont(); CLabel label(font, this->TextColor, this->HighlightColor); - std::string sub; + std::string_view sub; int i = 0; int y_off = y; unsigned int width = this->MaxWidth diff --git a/src/ui/uibuttons_proc.cpp b/src/ui/uibuttons_proc.cpp index d5511a8883..bea31ab79c 100644 --- a/src/ui/uibuttons_proc.cpp +++ b/src/ui/uibuttons_proc.cpp @@ -58,7 +58,7 @@ ** @param text text to print on button */ void DrawUIButton(ButtonStyle *style, unsigned flags, int x, int y, - const std::string &text, int player) + std::string_view text, int player) { ButtonStyleProperties *p; diff --git a/src/video/font.cpp b/src/video/font.cpp index e16fc58128..c30c6477c4 100644 --- a/src/video/font.cpp +++ b/src/video/font.cpp @@ -420,9 +420,9 @@ static int CodepageIndexFromUTF8(const char text[], const size_t len, size_t &po /** ** Get the next codepage index to render from a utf8 encoded string */ -static int CodepageIndexFromUTF8(const std::string &text, size_t &pos, size_t &subpos) +static int CodepageIndexFromUTF8(std::string_view text, size_t &pos, size_t &subpos) { - return CodepageIndexFromUTF8(text.c_str(), text.size(), pos, subpos); + return CodepageIndexFromUTF8(text.data(), text.size(), pos, subpos); } int CFont::Height() const @@ -463,7 +463,7 @@ int CFont::Width(const int number) const ** ** @return The width in pixels of the text. */ -int CFont::Width(const std::string &text) const +int CFont::Width(std::string_view text) const { int width = 0; bool isformat = false; @@ -745,14 +745,14 @@ int CLabel::DrawReverseClip(int x, int y, int number) const return DoDrawText(x, y, text, reverse); } -int CLabel::DrawCentered(int x, int y, const std::string &text) const +int CLabel::DrawCentered(int x, int y, std::string_view text) const { int dx = font->Width(text); DoDrawText(x - dx / 2, y, text, normal); return dx / 2; } -int CLabel::DrawReverseCentered(int x, int y, const std::string &text) const +int CLabel::DrawReverseCentered(int x, int y, std::string_view text) const { int dx = font->Width(text); DoDrawText(x - dx / 2, y, text, reverse); @@ -769,7 +769,7 @@ int CLabel::DrawReverseCentered(int x, int y, const std::string &text) const ** ** @return computed value. */ -static int strchrlen(const std::string &s, char c, unsigned int maxlen, const CFont *font) +static int strchrlen(std::string_view s, char c, unsigned int maxlen, const CFont *font) { if (s.empty()) { return 0; @@ -813,22 +813,21 @@ static int strchrlen(const std::string &s, char c, unsigned int maxlen, const CF ** ** @return computed value. */ -std::string GetLineFont(unsigned int line, const std::string &s, unsigned int maxlen, const CFont *font) +std::string_view GetLineFont(unsigned int line, std::string_view s, unsigned int maxlen, const CFont *font) { unsigned int res; - std::string s1 = s; Assert(0 < line); for (unsigned int i = 1; i < line; ++i) { - res = strchrlen(s1, '\n', maxlen, font); - if (!res || res >= s1.size()) { + res = strchrlen(s, '\n', maxlen, font); + if (!res || res >= s.size()) { return ""; } - s1 = s1.substr(res + 1); + s = s.substr(res + 1); } - res = strchrlen(s1, '\n', maxlen, font); - return s1.substr(0, res); + res = strchrlen(s, '\n', maxlen, font); + return s.substr(0, res); } diff --git a/tests/stratagus/test_util.cpp b/tests/stratagus/test_util.cpp index fd2e9dc814..cc878ba2c6 100644 --- a/tests/stratagus/test_util.cpp +++ b/tests/stratagus/test_util.cpp @@ -115,5 +115,5 @@ TEST_CASE("fletcher32") // TODO: int getopt(int argc, char *const argv[], const char *optstring); // TODO: std::optional GetClipboard(); -// TODO: int UTF8GetNext(const std::string &text, int curpos); -// TODO: int UTF8GetPrev(const std::string &text, int curpos); +// TODO: int UTF8GetNext(std::string_view text, int curpos); +// TODO: int UTF8GetPrev(std::string_view text, int curpos);