[Interest] QByteArray vs QString, arg, why is there no arg()?
Konstantin Tokarev
annulen at yandex.ru
Wed Sep 18 16:15:01 CEST 2019
18.09.2019, 16:59, "Jason H" <jhihn at gmx.com>:
>> Sent: Wednesday, September 18, 2019 at 2:50 PM
>> From: "Giuseppe D'Angelo via Interest" <interest at qt-project.org>
>> To: interest at qt-project.org
>> Subject: Re: [Interest] QByteArray vs QString, arg, why is there no arg()?
>>
>> Il 18/09/19 13:16, Jason H ha scritto:
>> > What's the best way to zero-pad a QByteArray?
>> > What I want is QByteArray("%1").arg(6, 10, 10, '0')
>>
>> Mostly it has to do with the fact that QByteArray is sitting between two
>> worlds; on one side it's just a container of bytes, on the other side it
>> has _some_ manipulation functions for ASCII-like strings. "Some"
>> because, as you've noticed, stuff like the arg() convenience is missing.
>>
>> If you really need a QByteArray you can work around this by e.g. using a
>> printf-like function, what you're looking for is the "%010d" formatting.
>>
>> Pseudocode:
>>
>> int n = 123;
>> const char *format = "The number is %010d";
>> auto size = qsnprintf(nullptr, 0, format, n);
>>
>> QByteArray result(size, Qt::uninitialized);
>> qsnprintf(result.data(), result.size(), format, n);
>
> That helps, but wasn't the answer I wanted to hear. I have to call qsnprintf() twice. Granted, for valid reasons.and it might be faster than my gluing things together with +.
Gluing things together with + in a single expression is probably faster than using arg() if you build your sources with -DQT_USE_QSTRINGBUILDER (or use operator % instead of + if you don't)
>
> I do prefer python's approach where the character size change does not come with an API change. Maybe discussion for Qt6?. I would think they C++ way would be to have 1 API and a template class? (Though I think some people cringe at that idea)
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> https://lists.qt-project.org/listinfo/interest
--
Regards,
Konstantin
More information about the Interest
mailing list