Skip to content

Commit 1cd17ee

Browse files
committed
Cleanup String::mapArg() implementation details
Avoids redundant ptr addition at return, uses more idomatic isEmpty() and utilizes static decoders (since they are stateless and an error would cause termination, they can be reused), though they need to be thead_local to ensure multiple threads can use this function.
1 parent d6941e2 commit 1cd17ee

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

lib/core/src/qx-string.cpp

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -345,27 +345,30 @@ QString String::mapArg(QAnyStringView s, const QMap<QString, QString>& args, Qt:
345345

346346
for(const QAnyStringView& view : resultBp.views())
347347
{
348-
// QString::arg() uses size() here instead of isEmpty(), so we keep consistent
349348
resultRaw = view.visit(qxFuncAggregate{
350349
[resultRaw](QLatin1StringView v){
351-
if(v.size())
352-
{
353-
auto fromLatin1 = QStringDecoder(QStringDecoder::Latin1, QStringDecoder::Flag::Stateless);
354-
auto postAppend = fromLatin1.appendToBuffer(resultRaw, v);
355-
Q_ASSERT(!fromLatin1.hasError());
356-
return postAppend;
357-
}
358-
return resultRaw + v.size();
350+
if(v.isEmpty())
351+
return resultRaw;
352+
353+
thread_local static auto fromLatin1 = QStringDecoder(QStringDecoder::Latin1, QStringDecoder::Flag::Stateless);
354+
auto postAppend = fromLatin1.appendToBuffer(resultRaw, v);
355+
Q_ASSERT(!fromLatin1.hasError());
356+
return postAppend;
359357
},
360358
[resultRaw](QUtf8StringView v){
361-
auto fromUtf8 = QStringDecoder(QStringDecoder::Utf8, QStringDecoder::Flag::Stateless);
359+
if(v.isEmpty())
360+
return resultRaw;
361+
362+
thread_local static auto fromUtf8 = QStringDecoder(QStringDecoder::Utf8, QStringDecoder::Flag::Stateless);
362363
auto postAppend = fromUtf8.appendToBuffer(resultRaw, v);
363364
Q_ASSERT(!fromUtf8.hasError());
364365
return postAppend;
365366
},
366367
[resultRaw](QStringView v){
367-
if(v.size())
368-
memcpy(resultRaw, v.data(), v.size() * sizeof(QChar));
368+
if(v.isEmpty())
369+
return resultRaw;
370+
371+
memcpy(resultRaw, v.data(), v.size() * sizeof(QChar));
369372
return resultRaw + v.size();
370373
}
371374
});

0 commit comments

Comments
 (0)