Convert WordWrapper to use std::string_view

This commit is contained in:
Henrik Rydgård 2024-05-24 12:43:38 +02:00
parent f6ca8101e0
commit 4b45bde38f
6 changed files with 16 additions and 15 deletions

View file

@ -141,7 +141,7 @@ void WordWrapper::AppendWord(int endIndex, int lastChar, bool addNewline) {
// This will include the newline.
if (x_ <= maxW_) {
out_.append(str_ + lastWordStartIndex, str_ + endIndex);
out_.append(str_.data() + lastWordStartIndex, str_.data() + endIndex);
} else {
scanForNewline_ = true;
}
@ -178,10 +178,10 @@ void WordWrapper::AppendWord(int endIndex, int lastChar, bool addNewline) {
void WordWrapper::Wrap() {
// First, let's check if it fits as-is.
size_t len = strlen(str_);
if (MeasureWidth(std::string_view(str_, len)) <= maxW_) {
size_t len = str_.length();
if (MeasureWidth(str_) <= maxW_) {
// If it fits, we don't need to go through each character.
out_ = str_;
out_ = std::string(str_);
return;
}
@ -219,7 +219,7 @@ void WordWrapper::Wrap() {
}
// Measure the entire word for kerning purposes. May not be 100% perfect.
float newWordWidth = MeasureWidth(std::string_view(str_ + lastIndex_, afterIndex - lastIndex_));
float newWordWidth = MeasureWidth(str_.substr(lastIndex_, afterIndex - lastIndex_));
// Is this the end of a word (space)? We'll also output up to a soft hyphen.
if (wordWidth_ > 0.0f && IsSpaceOrShy(c)) {

View file

@ -5,7 +5,7 @@
class WordWrapper {
public:
WordWrapper(const char *str, float maxW, int flags)
WordWrapper(std::string_view str, float maxW, int flags)
: str_(str), maxW_(maxW), flags_(flags) {
}
virtual ~WordWrapper() {}
@ -27,7 +27,7 @@ protected:
return IsSpace(c) || IsShy(c);
}
const char *const str_;
const std::string_view str_;
const float maxW_;
const int flags_;
std::string out_;

View file

@ -446,7 +446,8 @@ void DrawBuffer::DrawImage2GridH(ImageID atlas_image, float x1, float y1, float
class AtlasWordWrapper : public WordWrapper {
public:
// Note: maxW may be height if rotated.
AtlasWordWrapper(const AtlasFont &atlasfont, float scale, const char *str, float maxW, int flags) : WordWrapper(str, maxW, flags), atlasfont_(atlasfont), scale_(scale) {
AtlasWordWrapper(const AtlasFont &atlasfont, float scale, std::string_view str, float maxW, int flags)
: WordWrapper(str, maxW, flags), atlasfont_(atlasfont), scale_(scale) {
}
protected:
@ -535,7 +536,7 @@ void DrawBuffer::MeasureTextRect(FontID font_id, std::string_view text, const Bo
*h = 0.0f;
return;
}
AtlasWordWrapper wrapper(*font, fontscalex, toMeasure.c_str(), bounds.w, wrap);
AtlasWordWrapper wrapper(*font, fontscalex, toMeasure, bounds.w, wrap);
toMeasure = wrapper.Wrapped();
}
MeasureText(font_id, toMeasure, w, h);
@ -578,7 +579,7 @@ void DrawBuffer::DrawTextRect(FontID font, const char *text, float x, float y, f
if (!atlasfont)
atlasfont = atlas->getFont(font);
if (wrap && atlasfont) {
AtlasWordWrapper wrapper(*atlasfont, fontscalex, toDraw.c_str(), w, wrap);
AtlasWordWrapper wrapper(*atlasfont, fontscalex, toDraw, w, wrap);
toDraw = wrapper.Wrapped();
}
@ -610,7 +611,7 @@ void DrawBuffer::DrawTextRect(FontID font, const char *text, float x, float y, f
// ROTATE_* doesn't yet work right.
void DrawBuffer::DrawText(FontID font, std::string_view text, float x, float y, Color color, int align) {
// rough estimate
int textLen = text.length();
int textLen = (int)text.length();
if (count_ + textLen * 6 > MAX_VERTS) {
Flush(true);
if (textLen * 6 >= MAX_VERTS) {

View file

@ -26,7 +26,7 @@ float TextDrawerWordWrapper::MeasureWidth(std::string_view str) {
return w;
}
void TextDrawer::WrapString(std::string &out, const char *str, float maxW, int flags) {
void TextDrawer::WrapString(std::string &out, std::string_view str, float maxW, int flags) {
TextDrawerWordWrapper wrapper(this, str, maxW, flags);
out = wrapper.Wrapped();
}

View file

@ -67,7 +67,7 @@ protected:
Draw::DrawContext *draw_;
virtual void ClearCache() = 0;
void WrapString(std::string &out, const char *str, float maxWidth, int flags);
void WrapString(std::string &out, std::string_view str, float maxWidth, int flags);
struct CacheKey {
bool operator < (const CacheKey &other) const {
@ -90,7 +90,7 @@ protected:
class TextDrawerWordWrapper : public WordWrapper {
public:
TextDrawerWordWrapper(TextDrawer *drawer, const char *str, float maxW, int flags)
TextDrawerWordWrapper(TextDrawer *drawer, std::string_view str, float maxW, int flags)
: WordWrapper(str, maxW, flags), drawer_(drawer) {}
protected:

View file

@ -788,7 +788,7 @@ static bool TestAndroidContentURI() {
class UnitTestWordWrapper : public WordWrapper {
public:
UnitTestWordWrapper(const char *str, float maxW, int flags)
UnitTestWordWrapper(std::string_view str, float maxW, int flags)
: WordWrapper(str, maxW, flags) {
}