From 52473c7bf188f2803a44f0214e416ceb7e20841f Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Mon, 9 Aug 2021 18:19:30 +0200 Subject: [PATCH] Use QByteArrayView::toInt() instead of duplicating its implementation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The code in qEnvironmentVariableIntValue() to parse the text as an int can now delegate it to QByteArrayView, so that keeping in sync with QByteArray::toInt(), as a comment requested, is now automatic. Change-Id: I09a6b7245ecd02f39a850a4ce187f86709282e8c Reviewed-by: MÃ¥rten Nordheim Reviewed-by: Thiago Macieira --- src/corelib/global/qglobal.cpp | 33 ++------------------------------- 1 file changed, 2 insertions(+), 31 deletions(-) diff --git a/src/corelib/global/qglobal.cpp b/src/corelib/global/qglobal.cpp index e613266d7bb..f425942f96e 100644 --- a/src/corelib/global/qglobal.cpp +++ b/src/corelib/global/qglobal.cpp @@ -40,6 +40,7 @@ #include "qplatformdefs.h" #include "qstring.h" +#include "qbytearrayview.h" #include "qlist.h" #include "qdir.h" #include "qdatetime.h" @@ -3548,37 +3549,7 @@ int qEnvironmentVariableIntValue(const char *varName, bool *ok) noexcept return 0; } #endif - bool ok_ = true; - const char *endptr; - const qlonglong value = qstrntoll(buffer, size, &endptr, 0, &ok_); - - // Keep the following checks in sync with QByteArray::toInt() - if (!ok_) { - if (ok) - *ok = false; - return 0; - } - - if (*endptr != '\0') { - while (ascii_isspace(*endptr)) - ++endptr; - } - - if (*endptr != '\0') { - // we stopped at a non-digit character after converting some digits - if (ok) - *ok = false; - return 0; - } - - if (int(value) != value) { - if (ok) - *ok = false; - return 0; - } else if (ok) { - *ok = ok_; - } - return int(value); + return QByteArrayView(buffer, size).toInt(ok, 0); } /*!