1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-07-02 16:38:41 +00:00

Add nodiscard attribute to helper functions where it makes sense

This commit is contained in:
sfan5 2025-03-26 21:56:09 +01:00
parent e6acc4e7ed
commit ae0f955a0e
10 changed files with 71 additions and 29 deletions

View file

@ -72,8 +72,8 @@ struct FlagDesc {
// Try to avoid converting between wide and UTF-8 unless you need to
// input/output stuff via Irrlicht
std::wstring utf8_to_wide(std::string_view input);
std::string wide_to_utf8(std::wstring_view input);
[[nodiscard]] std::wstring utf8_to_wide(std::string_view input);
[[nodiscard]] std::string wide_to_utf8(std::wstring_view input);
void wide_add_codepoint(std::wstring &result, char32_t codepoint);
@ -287,6 +287,7 @@ MAKE_VARIANT(str_ends_with, std::basic_string_view<T>, const T*)
* @return An std::vector<std::basic_string<T> > of the component parts
*/
template <typename T>
[[nodiscard]]
inline std::vector<std::basic_string<T> > str_split(
const std::basic_string<T> &str,
T delimiter)
@ -306,6 +307,7 @@ inline std::vector<std::basic_string<T> > str_split(
* @param str
* @return A copy of \p str converted to all lowercase characters.
*/
[[nodiscard]]
inline std::string lowercase(std::string_view str)
{
std::string s2;
@ -331,6 +333,7 @@ inline bool my_isspace(const wchar_t c)
* @return A view of \p str with leading and trailing whitespace removed.
*/
template<typename T>
[[nodiscard]]
inline std::basic_string_view<T> trim(std::basic_string_view<T> str)
{
size_t front = 0;
@ -354,6 +357,7 @@ inline std::basic_string_view<T> trim(std::basic_string_view<T> str)
* @return A copy of \p str with leading and trailing whitespace removed.
*/
template<typename T>
[[nodiscard]]
inline std::basic_string<T> trim(std::basic_string<T> &&str)
{
std::basic_string<T> ret(trim(std::basic_string_view<T>(str)));
@ -361,6 +365,7 @@ inline std::basic_string<T> trim(std::basic_string<T> &&str)
}
template<typename T>
[[nodiscard]]
inline std::basic_string_view<T> trim(const std::basic_string<T> &str)
{
return trim(std::basic_string_view<T>(str));
@ -368,6 +373,7 @@ inline std::basic_string_view<T> trim(const std::basic_string<T> &str)
// The above declaration causes ambiguity with char pointers so we have to fix that:
template<typename T>
[[nodiscard]]
inline std::basic_string_view<T> trim(const T *str)
{
return trim(std::basic_string_view<T>(str));
@ -556,6 +562,7 @@ std::string wrap_rows(std::string_view from, unsigned row_len, bool has_color_co
* Removes backslashes from an escaped string (FormSpec strings)
*/
template <typename T>
[[nodiscard]]
inline std::basic_string<T> unescape_string(const std::basic_string<T> &s)
{
std::basic_string<T> res;
@ -580,6 +587,7 @@ inline std::basic_string<T> unescape_string(const std::basic_string<T> &s)
* @return \p s, with escape sequences removed.
*/
template <typename T>
[[nodiscard]]
std::basic_string<T> unescape_enriched(const std::basic_string<T> &s)
{
std::basic_string<T> output;
@ -610,6 +618,7 @@ std::basic_string<T> unescape_enriched(const std::basic_string<T> &s)
}
template <typename T>
[[nodiscard]]
std::vector<std::basic_string<T> > split(const std::basic_string<T> &s, T delim)
{
std::vector<std::basic_string<T> > tokens;
@ -641,10 +650,13 @@ std::vector<std::basic_string<T> > split(const std::basic_string<T> &s, T delim)
return tokens;
}
[[nodiscard]]
std::wstring translate_string(std::wstring_view s, Translations *translations);
[[nodiscard]]
std::wstring translate_string(std::wstring_view s);
[[nodiscard]]
inline std::wstring unescape_translate(std::wstring_view s)
{
return unescape_enriched(translate_string(s));
@ -730,6 +742,7 @@ inline const std::string duration_to_string(int sec)
*
* @return A std::string
*/
[[nodiscard]]
inline std::string str_join(const std::vector<std::string> &list,
std::string_view delimiter)
{
@ -748,6 +761,7 @@ inline std::string str_join(const std::vector<std::string> &list,
/**
* Create a UTF8 std::string from an irr::core::stringw.
*/
[[nodiscard]]
inline std::string stringw_to_utf8(const irr::core::stringw &input)
{
std::wstring_view sv(input.c_str(), input.size());
@ -757,6 +771,7 @@ inline std::string stringw_to_utf8(const irr::core::stringw &input)
/**
* Create an irr::core:stringw from a UTF8 std::string.
*/
[[nodiscard]]
inline irr::core::stringw utf8_to_stringw(std::string_view input)
{
std::wstring str = utf8_to_wide(input);
@ -770,6 +785,7 @@ inline irr::core::stringw utf8_to_stringw(std::string_view input)
* and add a prefix to them
* 2. Remove 'unsafe' characters from the name by replacing them with '_'
*/
[[nodiscard]]
std::string sanitizeDirName(std::string_view str, std::string_view optional_prefix);
/**