]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Minor] Improve words wrap algorithm
authorVsevolod Stakhov <vsevolod@rspamd.com>
Sat, 21 Jan 2023 15:21:11 +0000 (15:21 +0000)
committerVsevolod Stakhov <vsevolod@rspamd.com>
Sat, 21 Jan 2023 15:21:11 +0000 (15:21 +0000)
src/client/rspamc.cxx

index 1a425b881505fe412cc301d7c3231a8503fa903b..78c57fccb133f1c8d3ed2dd3dba8e1f49df97f29 100644 (file)
@@ -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);
                        }