QUrl: restore copying of the input QString in setXxx functions

Complements the previous commit by using templates so we copy the input
QString if it is a QString, duplicate the input if QStringView.

Change-Id: I1646ead1283e16029fc6fffd850daf12b6f6f913
Reviewed-by: David Faure <david.faure@kdab.com>
This commit is contained in:
Thiago Macieira 2025-04-02 20:04:24 -07:00
parent be0c9e7638
commit 31207b539a

View File

@ -528,13 +528,13 @@ public:
// the "end" parameters are like STL iterators: they point to one past the last valid element // the "end" parameters are like STL iterators: they point to one past the last valid element
bool setScheme(const QString &value, qsizetype len, bool doSetError); bool setScheme(const QString &value, qsizetype len, bool doSetError);
void setAuthority(const QString &auth, qsizetype from, qsizetype end, QUrl::ParsingMode mode); void setAuthority(const QString &auth, qsizetype from, qsizetype end, QUrl::ParsingMode mode);
void setUserInfo(QStringView value); template <typename String> void setUserInfo(String value);
void setUserName(QStringView value); template <typename String> void setUserName(String value);
void setPassword(QStringView value); template <typename String> void setPassword(String value);
bool setHost(const QString &value, qsizetype from, qsizetype end, QUrl::ParsingMode mode); bool setHost(const QString &value, qsizetype from, qsizetype end, QUrl::ParsingMode mode);
void setPath(QStringView value); template <typename String> void setPath(String value);
void setQuery(QStringView value); template <typename String> void setQuery(String value);
void setFragment(QStringView value); template <typename String> void setFragment(String value);
inline bool hasScheme() const { return sectionIsPresent & Scheme; } inline bool hasScheme() const { return sectionIsPresent & Scheme; }
inline bool hasAuthority() const { return sectionIsPresent & Authority; } inline bool hasAuthority() const { return sectionIsPresent & Authority; }
@ -814,8 +814,15 @@ static inline void parseDecodedComponent(QString &data, QUrlPrivate::Section sec
data.replace(u'[', "%5B"_L1).replace(u']', "%5D"_L1); data.replace(u'[', "%5B"_L1).replace(u']', "%5D"_L1);
} }
static void static void recodeFromUser(QString &output, const QString &input, const ushort *actions)
recodeFromUser(QString &output, QStringView input, const ushort *actions) {
output.resize(0);
if (qt_urlRecode(output, input, {}, actions))
return;
output = input;
}
static void recodeFromUser(QString &output, QStringView input, const ushort *actions)
{ {
output.resize(0); output.resize(0);
if (qt_urlRecode(output, input, {}, actions)) if (qt_urlRecode(output, input, {}, actions))
@ -1086,7 +1093,7 @@ inline void QUrlPrivate::setAuthority(const QString &auth, qsizetype from, qsize
port = -1; port = -1;
} }
inline void QUrlPrivate::setUserInfo(QStringView value) template <typename String> void QUrlPrivate::setUserInfo(String value)
{ {
qsizetype delimIndex = value.indexOf(u':'); qsizetype delimIndex = value.indexOf(u':');
if (delimIndex < 0) { if (delimIndex < 0) {
@ -1100,31 +1107,31 @@ inline void QUrlPrivate::setUserInfo(QStringView value)
} }
} }
inline void QUrlPrivate::setUserName(QStringView value) template <typename String> inline void QUrlPrivate::setUserName(String value)
{ {
sectionIsPresent |= UserName; sectionIsPresent |= UserName;
recodeFromUser(userName, value, userNameInIsolation); recodeFromUser(userName, value, userNameInIsolation);
} }
inline void QUrlPrivate::setPassword(QStringView value) template <typename String> inline void QUrlPrivate::setPassword(String value)
{ {
sectionIsPresent |= Password; sectionIsPresent |= Password;
recodeFromUser(password, value, passwordInIsolation); recodeFromUser(password, value, passwordInIsolation);
} }
inline void QUrlPrivate::setPath(QStringView value) template <typename String> inline void QUrlPrivate::setPath(String value)
{ {
// sectionIsPresent |= Path; // not used, save some cycles // sectionIsPresent |= Path; // not used, save some cycles
recodeFromUser(path, value, pathInIsolation); recodeFromUser(path, value, pathInIsolation);
} }
inline void QUrlPrivate::setFragment(QStringView value) template <typename String> inline void QUrlPrivate::setFragment(String value)
{ {
sectionIsPresent |= Fragment; sectionIsPresent |= Fragment;
recodeFromUser(fragment, value, fragmentInIsolation); recodeFromUser(fragment, value, fragmentInIsolation);
} }
inline void QUrlPrivate::setQuery(QStringView value) template <typename String> inline void QUrlPrivate::setQuery(String value)
{ {
sectionIsPresent |= Query; sectionIsPresent |= Query;
recodeFromUser(query, value, queryInIsolation); recodeFromUser(query, value, queryInIsolation);