Re: [Interest] QByteArray vs QString, arg, why is there no arg()?
On Wednesday, 18 September 2019 06:57:11 PDT Jason H wrote: > 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) No, that's not going to happen because templates are not magic. It would mean increasing the size of the library so we support both types of string formatting. That's also why we don't have arg() today in QByteArray. The new std::format-like API that is being developed will likely be QString only too. -- Thiago Macieira - thiago.macieira (AT) intel.com Software Architect - Intel System Software Products ___ Interest mailing list Interest@qt-project.org https://lists.qt-project.org/listinfo/interest
Re: [Interest] QByteArray vs QString, arg, why is there no arg()?
18.09.2019, 16:59, "Jason H" : >> Sent: Wednesday, September 18, 2019 at 2:50 PM >> From: "Giuseppe D'Angelo via Interest" >> To: interest@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@qt-project.org > https://lists.qt-project.org/listinfo/interest -- Regards, Konstantin ___ Interest mailing list Interest@qt-project.org https://lists.qt-project.org/listinfo/interest
Re: [Interest] QByteArray vs QString, arg, why is there no arg()?
> Sent: Wednesday, September 18, 2019 at 2:50 PM > From: "Giuseppe D'Angelo via Interest" > To: interest@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 +. 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@qt-project.org https://lists.qt-project.org/listinfo/interest
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); HTH, -- Giuseppe D'Angelo | giuseppe.dang...@kdab.com | Senior Software Engineer KDAB (France) S.A.S., a KDAB Group company Tel. France +33 (0)4 90 84 08 53, http://www.kdab.com KDAB - The Qt, C++ and OpenGL Experts smime.p7s Description: Firma crittografica S/MIME ___ Interest mailing list Interest@qt-project.org https://lists.qt-project.org/listinfo/interest
[Interest] QByteArray vs QString, arg, why is there no arg()?
Shiver me timbers! I often find myself using QString::arg() overloads for formatting, but nothing like that exists for when I'm working with just bytes, say for a socket or serial port. So I find myself constructing it as a QString and then .toLocal8bit() , which just seems wasteful. What's the best way to zero-pad a QByteArray? What I want is QByteArray("%1").arg(6, 10, 10, '0') I'm guessing they is a good reason it hasn't been added yet? ___ Interest mailing list Interest@qt-project.org https://lists.qt-project.org/listinfo/interest