aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@rspamd.com>2023-01-21 15:21:11 +0000
committerVsevolod Stakhov <vsevolod@rspamd.com>2023-01-21 15:21:11 +0000
commit6fea5899c693bb934ba7a9263f602d32baa76fc7 (patch)
tree22849901773f0e257afe47750aa2bee07dd50d41
parent59fcbfaa87fc96c6f1f9ad7e37411445e5473ffc (diff)
downloadrspamd-6fea5899c693bb934ba7a9263f602d32baa76fc7.tar.gz
rspamd-6fea5899c693bb934ba7a9263f602d32baa76fc7.zip
[Minor] Improve words wrap algorithm
-rw-r--r--src/client/rspamc.cxx24
1 files changed, 23 insertions, 1 deletions
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);
}