commit 6fea589: [Minor] Improve words wrap algorithm

Vsevolod Stakhov vsevolod at rspamd.com
Sat Jan 21 15:28:06 UTC 2023


Author: Vsevolod Stakhov
Date: 2023-01-21 15:21:11 +0000
URL: https://github.com/rspamd/rspamd/commit/6fea5899c693bb934ba7a9263f602d32baa76fc7 (HEAD -> master)

[Minor] Improve words wrap algorithm

---
 src/client/rspamc.cxx | 24 +++++++++++++++++++++++-
 1 file changed, 23 insertions(+), 1 deletion(-)

diff --git a/src/client/rspamc.cxx b/src/client/rspamc.cxx
index 1a425b881..78c57fccb 100644
--- a/src/client/rspamc.cxx
+++ b/src/client/rspamc.cxx
@@ -875,7 +875,29 @@ rspamc_symbol_human_output(FILE *out, const ucl_object_t *obj)
 			return;
 		}
 		for (size_t pos = 0; pos < line.size(); ) {
-			auto s = line.substr(pos, pos ? (maxlen-indent) : maxlen);
+			/*
+			 * First, find the longest sequence of words, delimited by space of punctuation,
+			 * and adjust `maxlen` if needed
+			 */
+			auto split_len = pos ? (maxlen-indent) : maxlen;
+			auto word_len = 0;
+			auto suffix = std::string_view(line).substr(pos);
+			for (;;) {
+				auto delim_pos = suffix.find_first_of(" \t,;[]():");
+				if (word_len + delim_pos + 1 < split_len && delim_pos != std::string_view::npos && delim_pos < suffix.size()) {
+					word_len += delim_pos + 1;
+					suffix = suffix.substr(delim_pos + 1);
+				}
+				else {
+					break;
+				}
+			}
+
+			if (word_len > 0 && word_len < split_len && line.size() + pos > split_len) {
+				split_len = word_len;
+			}
+
+			auto s = std::string_view(line).substr(pos, split_len);
 			if (indent && pos) {
 				fmt::print(out, "{:>{}}", " ", indent);
 			}


More information about the Commits mailing list