Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cleanup] Replace some const std::string & by std::string_view #684

Merged
merged 1 commit into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/include/font.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<std::string, std::string> 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);
Expand Down Expand Up @@ -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 <const bool CLIP>
int DoDrawText(int x, int y, std::string_view text, const CFontColor *fc) const;
Expand Down
4 changes: 3 additions & 1 deletion src/include/menus.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

//@{

#include <string_view>

/*----------------------------------------------------------------------------
-- Defines/Declarations
----------------------------------------------------------------------------*/
Expand All @@ -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();
Expand Down
13 changes: 7 additions & 6 deletions src/include/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@
#include "filesystem.h"
#include "stratagus.h"

#include <cstdlib>
#include <cstdint>
#include <string>
#include <algorithm>
#include <cstdint>
#include <cstdlib>
#include <numeric>
#include <string>
#include <string_view>

/*----------------------------------------------------------------------------
-- Random
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/stratagus/script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/stratagus/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint16_t> alphas;
size_t consideredChars = 0;
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
12 changes: 6 additions & 6 deletions src/ui/popup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/ui/uibuttons_proc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
25 changes: 12 additions & 13 deletions src/video/font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -745,14 +745,14 @@ int CLabel::DrawReverseClip(int x, int y, int number) const
return DoDrawText<true>(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<false>(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<false>(x - dx / 2, y, text, reverse);
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}


Expand Down
4 changes: 2 additions & 2 deletions tests/stratagus/test_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,5 @@ TEST_CASE("fletcher32")

// TODO: int getopt(int argc, char *const argv[], const char *optstring);
// TODO: std::optional<std::string> 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);
Loading