1
0
mirror of https://github.com/Kitware/CMake.git synced 2025-10-23 00:48:55 +08:00

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.
This commit is contained in:
Frank Winklmeier
2022-02-17 13:26:55 +01:00
committed by Brad King
parent 2ac3db2d42
commit 4634de335b

View File

@@ -2101,16 +2101,16 @@ void cmCTestTestHandler::CleanTestOutput(std::string& output, size_t length)
return; return;
} }
// Truncate at given length but do not break in the middle of a multi-byte // Advance n bytes in string delimited by begin/end but do not break in the
// UTF-8 encoding. // middle of a multi-byte UTF-8 encoding.
char const* const begin = output.c_str(); auto utf8_advance = [](char const* const begin, char const* const end,
char const* const end = begin + output.size(); size_t n) -> const char* {
char const* const truncate = begin + length; char const* const stop = begin + n;
char const* current = begin; char const* current = begin;
while (current < truncate) { while (current < stop) {
unsigned int ch; unsigned int ch;
if (const char* next = cm_utf8_decode_character(current, end, &ch)) { if (const char* next = cm_utf8_decode_character(current, end, &ch)) {
if (next > truncate) { if (next > stop) {
break; break;
} }
current = next; current = next;
@@ -2119,6 +2119,13 @@ void cmCTestTestHandler::CleanTestOutput(std::string& output, size_t length)
++current; ++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); output.erase(current - begin);
// Append truncation message. // Append truncation message.