From bc0def4342e3a097cf5f8e6e4a362f91dd4dba3a Mon Sep 17 00:00:00 2001 From: Ryan Williams Date: Mon, 30 May 2022 19:58:46 -0700 Subject: [PATCH] MiscUtils word_wrap: Add option to trim only leading whitespace after wrapping (#2169) * Update changelog.txt --- docs/changelog.txt | 1 + library/MiscUtils.cpp | 16 ++++++++++------ library/include/MiscUtils.h | 8 +++++++- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/docs/changelog.txt b/docs/changelog.txt index b2ef0e90a..3b75390dd 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -47,6 +47,7 @@ changelog.txt uses a syntax similar to RST, with a few special sequences: ## Documentation ## API +- ``word_wrap``: argument ``bool collapse_whitespace`` converted to enum ``word_wrap_whitespace_mode mode``, with valid modes ``WSMODE_KEEP_ALL``, ``WSMODE_COLLAPSE_ALL``, and ``WSMODE_TRIM_LEADING``. ## Lua - ``widgets.HotkeyLabel``: the ``key_sep`` string is now configurable diff --git a/library/MiscUtils.cpp b/library/MiscUtils.cpp index 6ab63d5eb..b959e756e 100644 --- a/library/MiscUtils.cpp +++ b/library/MiscUtils.cpp @@ -168,15 +168,15 @@ std::string to_search_normalized(const std::string &str) return result; } - -bool word_wrap(std::vector *out, const std::string &str, - size_t line_length, bool collapse_whitespace) +bool word_wrap(std::vector *out, const std::string &str, size_t line_length, + word_wrap_whitespace_mode mode) { if (line_length == 0) line_length = SIZE_MAX; std::string line; size_t break_pos = 0; + bool ignore_whitespace = false; for (auto &c : str) { @@ -185,19 +185,22 @@ bool word_wrap(std::vector *out, const std::string &str, out->push_back(line); line.clear(); break_pos = 0; + ignore_whitespace = (mode == WSMODE_TRIM_LEADING); continue; } if (isspace(c)) { - if (break_pos == line.length() && collapse_whitespace) + if (ignore_whitespace || (mode == WSMODE_COLLAPSE_ALL && break_pos == line.length())) continue; - line.push_back(collapse_whitespace ? ' ' : c); + line.push_back((mode == WSMODE_COLLAPSE_ALL) ? ' ' : c); break_pos = line.length(); } - else { + else + { line.push_back(c); + ignore_whitespace = false; } if (line.length() > line_length) @@ -215,6 +218,7 @@ bool word_wrap(std::vector *out, const std::string &str, } line = line.substr(break_pos); break_pos = 0; + ignore_whitespace = (mode == WSMODE_TRIM_LEADING); } } if (line.length()) diff --git a/library/include/MiscUtils.h b/library/include/MiscUtils.h index 764b11413..56506f7bd 100644 --- a/library/include/MiscUtils.h +++ b/library/include/MiscUtils.h @@ -389,10 +389,16 @@ DFHACK_EXPORT std::string toUpper(const std::string &str); DFHACK_EXPORT std::string toLower(const std::string &str); DFHACK_EXPORT std::string to_search_normalized(const std::string &str); +enum word_wrap_whitespace_mode { + WSMODE_KEEP_ALL, + WSMODE_COLLAPSE_ALL, + WSMODE_TRIM_LEADING +}; + DFHACK_EXPORT bool word_wrap(std::vector *out, const std::string &str, size_t line_length = 80, - bool collapse_whitespace = false); + word_wrap_whitespace_mode mode = WSMODE_KEEP_ALL); inline bool bits_match(unsigned required, unsigned ok, unsigned mask) {