Skip to content

Commit

Permalink
Add C4NullableBasicStringView for std::string_view construction where…
Browse files Browse the repository at this point in the history
… nullptr may be passed (fixes #111)
  • Loading branch information
Fulgen301 committed Sep 24, 2023
1 parent 60c64cd commit f3c8a68
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
9 changes: 6 additions & 3 deletions src/C4GuiEdit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,15 @@ void Edit::DeleteSelection()
OnTextChange();
}

bool Edit::InsertText(std::string_view text, bool fUser)
bool Edit::InsertText(const C4NullableStringView text, bool fUser)
{
// empty previous selection
if (iSelectionStart != iSelectionEnd) DeleteSelection();

const std::string_view view{text};

// check buffer length
auto iTextLen = text.size();
auto iTextLen = view.size();
const auto iTextEnd = SLen(Text);
bool fBufferOK = (iTextLen + iTextEnd <= (iMaxTextLength - 1));
if (!fBufferOK) iTextLen -= iTextEnd + iTextLen - (iMaxTextLength - 1);
Expand All @@ -173,7 +176,7 @@ bool Edit::InsertText(std::string_view text, bool fUser)
int32_t i;
for (i = iTextEnd; i >= iCursorPos; --i) Text[i + iTextLen] = Text[i];
// insert buffer into text
for (i = iTextLen; i; --i) Text[iCursorPos + i - 1] = text[i - 1];
for (i = iTextLen; i; --i) Text[iCursorPos + i - 1] = view[i - 1];
if (fUser)
{
// advance cursor
Expand Down
4 changes: 2 additions & 2 deletions src/C4GuiEdit.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ class Edit : public Control
void Deselect(); // clear selection range

public:
bool InsertText(std::string_view text, bool fUser); // insert text at cursor pos (returns whether all text could be inserted)
bool InsertText(C4NullableStringView text, bool fUser); // insert text at cursor pos (returns whether all text could be inserted)
void ClearText(); // remove all the text
void DeleteSelection(); // deletes the selected text. Adjust cursor position if necessary
bool SetText(std::string_view text, bool fUser) { ClearText(); return InsertText(text, fUser); }
bool SetText(const C4NullableStringView text, bool fUser) { ClearText(); return InsertText(text, fUser); }
void SetPasswordMask(char cNewPasswordMask) { cPasswordMask = cNewPasswordMask; } // mask edit box contents using the given character

private:
Expand Down
39 changes: 39 additions & 0 deletions src/C4Strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,42 @@ inline int ssprintf(char (&str)[N], const char *fmt, Args... args)
if (m >= N) { m = N - 1; str[m] = 0; }
return m;
}

template<typename Char, typename Traits = std::char_traits<Char>>
struct C4NullableBasicStringView
{
std::basic_string_view<Char, Traits> View;

constexpr C4NullableBasicStringView(auto &&...args)
: View{std::forward<decltype(args)>(args)...} {}

constexpr C4NullableBasicStringView(const Char *const ptr)
{
if (ptr)
{
View = ptr;
}
else
{
View = {};
}
}

constexpr C4NullableBasicStringView(const C4NullableBasicStringView &other) noexcept
{
View = other.View;
}

constexpr C4NullableBasicStringView &operator=(const C4NullableBasicStringView &other) noexcept
{
View = other.View;
return *this;
}

constexpr operator std::basic_string_view<Char, Traits>() const noexcept
{
return View;
}
};

using C4NullableStringView = C4NullableBasicStringView<char>;

0 comments on commit f3c8a68

Please sign in to comment.