diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp index 471b9eb986..784c8c3316 100644 --- a/src/console_cmds.cpp +++ b/src/console_cmds.cpp @@ -43,6 +43,7 @@ #include "airport.h" #include "station_base.h" #include "economy_func.h" +#include "string_func_extra.h" #include "safeguards.h" @@ -1063,15 +1064,9 @@ DEF_CONSOLE_CMD(ConRestart) */ static void PrintLineByLine(char *buf) { - char *p = buf; - /* Print output line by line */ - for (char *p2 = buf; *p2 != '\0'; p2++) { - if (*p2 == '\n') { - *p2 = '\0'; - IConsolePrintF(CC_DEFAULT, "%s", p); - p = p2 + 1; - } - } + ProcessLineByLine(buf, [&](const char *line) { + IConsolePrintF(CC_DEFAULT, "%s", line); + }); } DEF_CONSOLE_CMD(ConListAILibs) diff --git a/src/string_func_extra.h b/src/string_func_extra.h index 9c320f3ea2..c7de1a3ed0 100644 --- a/src/string_func_extra.h +++ b/src/string_func_extra.h @@ -20,4 +20,21 @@ static inline void str_validate(std::string &str, StringValidationSettings setti str.resize(str_validate_intl(buf, buf + str.size(), settings) - buf); } +template +inline void ProcessLineByLine(char *buf, F line_functor) +{ + char *p = buf; + char *p2 = buf; + /* Print output line by line */ + for (; *p2 != '\0'; p2++) { + if (*p2 == '\n') { + *p2 = '\0'; + line_functor(p); + p = p2 + 1; + } + } + if (p < p2) line_functor(p); +} + + #endif /* STRING_FUNC_EXTRA_H */