QtTest: port approx_wide_len() from std::clamp() to qt_saturate()

The patch that originally added the function used std::clamp() because
qt_saturate() wasn't available in all the branches to which the patch
was backported. This patch modernizes the code for newer branches.

Pick-to: 6.7 6.5
Change-Id: I1b764d303e00ec04858643efed3dcc71f2c7ce4c
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
(cherry picked from commit d042c14c9940c6aabaa5b39d3061c0f9e50fff06)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Marc Mutz 2024-07-29 21:38:26 +02:00 committed by Qt Cherry-pick Bot
parent 12667b77ff
commit e7aa8aa118

View File

@ -12,7 +12,8 @@
#include <QtTest/qtestassert.h>
#include <QtTest/qtesteventloop.h>
#include <algorithm>
#include <QtCore/private/qnumeric_p.h>
#include <climits>
#include <cwchar>
@ -334,11 +335,10 @@ static int approx_wide_len(const char *s)
std::mbstate_t state = {};
// QNX might stop at max when dst == nullptr, so pass INT_MAX,
// being the largest value this function will return:
constexpr size_t max = INT_MAX;
auto r = std::mbsrtowcs(nullptr, &s, max, &state);
auto r = std::mbsrtowcs(nullptr, &s, INT_MAX, &state);
if (r == size_t(-1)) // encoding error, fall back to strlen()
r = strlen(s); // `s` was not advanced since `dst == nullptr`
return int(std::clamp(r, size_t(0), max));
return qt_saturate<int>(r);
}
// Overload to format failures for "const char *" - no need to strdup().