commit 4043939: [Minor] Some more neats to the string wrapping function
Vsevolod Stakhov
vsevolod at rspamd.com
Sun Jan 22 12:56:03 UTC 2023
Author: Vsevolod Stakhov
Date: 2023-01-22 12:37:29 +0000
URL: https://github.com/rspamd/rspamd/commit/4043939da9cb74e9bb11902490133e1812588c05
[Minor] Some more neats to the string wrapping function
---
src/client/rspamc.cxx | 90 ++++++++++++++++++++++++---------------------------
1 file changed, 43 insertions(+), 47 deletions(-)
diff --git a/src/client/rspamc.cxx b/src/client/rspamc.cxx
index 78c57fccb..c757000db 100644
--- a/src/client/rspamc.cxx
+++ b/src/client/rspamc.cxx
@@ -825,6 +825,46 @@ add_options(GQueue *opts)
}
}
+template<std::size_t maxlen, std::size_t indent>
+static auto
+rspamc_print_indented_line(FILE *out, std::string_view line) -> void
+{
+ static_assert(maxlen > 0, "maxlen must be > 0");
+ static_assert(maxlen > indent, "maxlen must be more than indent");
+
+ for (size_t pos = 0; pos < line.size(); ) {
+ /*
+ * 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 = 0ul;
+ auto suffix = 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 = line.substr(pos, split_len);
+ if (indent && pos) {
+ fmt::print(out, "{:>{}}", " ", indent);
+ }
+
+ fmt::print(out, "{}\n", s);
+ pos += s.size();
+ }
+}
+
static void
rspamc_symbol_human_output(FILE *out, const ucl_object_t *obj)
{
@@ -870,43 +910,7 @@ rspamc_symbol_human_output(FILE *out, const ucl_object_t *obj)
line += '\n';
}
- auto print_indented_line = [&](size_t maxlen, size_t indent) {
- if (maxlen < 1 || maxlen < indent) {
- return;
- }
- for (size_t pos = 0; pos < line.size(); ) {
- /*
- * 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);
- }
- fmt::print(out, "{}\n", s);
- pos += s.size();
- }
- };
-
- print_indented_line(78, 28);
+ rspamc_print_indented_line<78, 28>(out, line);
}
static void
@@ -1164,11 +1168,7 @@ rspamc_symbols_output(FILE *out, ucl_object_t *obj)
if (humanreport) {
if (emitted && strcmp(emitted, "[]") != 0) {
- auto folded_line = rspamd_header_value_fold("Domains found: ", sizeof("Domains found: ") - 1,
- emitted, strlen(emitted), 78,
- RSPAMD_TASK_NEWLINES_LF, nullptr);
- fmt::print("Domains found: {}\n", folded_line->str);
- g_string_free(folded_line, true);
+ rspamc_print_indented_line<78, 4>(out, fmt::format("Domains found: {}", emitted));
}
}
else {
@@ -1190,11 +1190,7 @@ rspamc_symbols_output(FILE *out, ucl_object_t *obj)
if (humanreport) {
if (emitted && strcmp(emitted, "[]") != 0) {
- auto folded_line = rspamd_header_value_fold("Emails found: ", sizeof("Emails found: ") - 1,
- emitted, strlen(emitted), 78,
- RSPAMD_TASK_NEWLINES_LF, nullptr);
- fmt::print("Emails found: {}\n", folded_line->str);
- g_string_free(folded_line, true);
+ rspamc_print_indented_line<78, 4>(out, fmt::format("Emails found: {}", emitted));
}
}
else {
More information about the Commits
mailing list