0
msg.append(QString("\"settime\": %1000,")  .arg(eventList[i].failureBegin)); // set time

I would like to know if it`s ok to have %1 right next to 000. Since there is only 1 argument then there is obviously no confusion possible for QString but what if I had 10 .arg() Then it would confuse it with %10 right? Is there an escape sequence for this or do I have to break it down into concatenations?

yan bellavance
  • 4,710
  • 20
  • 62
  • 93

1 Answers1

2

It is not ok. You can explore the source code of QString::arg or let me show you.

QString QString::arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const
{
    ArgEscapeData d = findArgEscapes(*this);
    if (d.occurrences == 0) {
        qWarning() << ""QString::arg: Argument missing:"" << *this << ',' << a;
        return *this;
    }
    //.... something not important.
}

This is how findArgEscapes is implemented. I think source code of Qt is much more readable than most STL implementations.

static ArgEscapeData findArgEscapes(const QString &s)
{
    const QChar *uc_begin = s.unicode();
    const QChar *uc_end = uc_begin + s.length();
    ArgEscapeData d;
    d.min_escape = INT_MAX;
    d.occurrences = 0;
    d.escape_len = 0;
    d.locale_occurrences = 0;
    const QChar *c = uc_begin;
    while (c != uc_end) {
        while (c != uc_end && c->unicode() != '%')
            ++c;
        if (c == uc_end)
            break;
        const QChar *escape_start = c;
        if (++c == uc_end)
            break;
        bool locale_arg = false;
        if (c->unicode() == 'L') {
            locale_arg = true;
            if (++c == uc_end)
                break;
        }

        int escape = c->digitValue();
        if (escape == -1)
            continue;
        ++c;
        if (c != uc_end) {
            int next_escape = c->digitValue();
            if (next_escape != -1) {
                escape = (10 * escape) + next_escape;  //*************
                ++c;
            }
        }
        if (escape > d.min_escape)
            continue;
        if (escape < d.min_escape) {
            d.min_escape = escape;
            d.occurrences = 0;
            d.escape_len = 0;
            d.locale_occurrences = 0;
        }
        ++d.occurrences;
        if (locale_arg)
            ++d.locale_occurrences;
        d.escape_len += c - escape_start;
    }
    return d;
}
Zen
  • 5,065
  • 8
  • 29
  • 49