From 4634de335bdf2c23659e84195ededc4ef4702ba4 Mon Sep 17 00:00:00 2001 From: Frank Winklmeier Date: Thu, 17 Feb 2022 13:26:55 +0100 Subject: cmCTestTestHandler: refactor CleanTestOutput method Refactor the code to skip over UTF-8 multi-bytes into its own lambda function so it can more easily be re-used. --- Source/CTest/cmCTestTestHandler.cxx | 39 ++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/Source/CTest/cmCTestTestHandler.cxx b/Source/CTest/cmCTestTestHandler.cxx index 5a3a8d0..958c51c 100644 --- a/Source/CTest/cmCTestTestHandler.cxx +++ b/Source/CTest/cmCTestTestHandler.cxx @@ -2101,24 +2101,31 @@ void cmCTestTestHandler::CleanTestOutput(std::string& output, size_t length) return; } - // Truncate at given length but do not break in the middle of a multi-byte - // UTF-8 encoding. - char const* const begin = output.c_str(); - char const* const end = begin + output.size(); - char const* const truncate = begin + length; - char const* current = begin; - while (current < truncate) { - unsigned int ch; - if (const char* next = cm_utf8_decode_character(current, end, &ch)) { - if (next > truncate) { - break; + // Advance n bytes in string delimited by begin/end but do not break in the + // middle of a multi-byte UTF-8 encoding. + auto utf8_advance = [](char const* const begin, char const* const end, + size_t n) -> const char* { + char const* const stop = begin + n; + char const* current = begin; + while (current < stop) { + unsigned int ch; + if (const char* next = cm_utf8_decode_character(current, end, &ch)) { + if (next > stop) { + break; + } + current = next; + } else // Bad byte will be handled by cmXMLWriter. + { + ++current; } - current = next; - } else // Bad byte will be handled by cmXMLWriter. - { - ++current; } - } + return current; + }; + + // Truncate at given length respecting UTF-8 words + char const* const begin = output.c_str(); + char const* const end = begin + output.size(); + char const* current = utf8_advance(begin, end, length); output.erase(current - begin); // Append truncation message. -- cgit v0.12