Wystąpił problem przy użyciu QString::arg()
, gdy ciąg zawiera cyfrę zaraz za znacznikiem miejsca. To nie wynika z QString::arg()
opisie funkcji, co stałoby się w przypadku takiej wymiany:Rozwiązywanie problemu QString QTtring() niejednoznaczności
QString("String for replacement %1234").arg("blah");
Spowoduje to "String for replacement blah234"
lub "String for replacement blah34"
?
Sprawdziłem kod źródłowy QT, aby odpowiedzieć na to pytanie. Wydaje się, że algorytm, który szuka markerów miejsca, jest "chciwy" i w powyższym przykładzie potrzebowałby obu cyfr.
Oto źródło funkcji QT, który jest używany wewnątrz QString::arg()
(Qt 4.8.4):
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;
}
if (c->digitValue() == -1)
continue;
int escape = c->digitValue();
++c;
if (c != uc_end && c->digitValue() != -1) {
escape = (10 * escape) + c->digitValue();
++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;
}
Czy istnieje lepszy sposób na rozwiązanie takiej dwuznaczności niż zawsze przy użyciu 2-cyfrową miejsce markery?
To doskonałą obserwację, nigdy nie myślałem o tym. Jedyne, co mógłbym zrobić, to 'QString (" String for replacement% 1% 2 "). Arg (" blah "). Arg (234);', ale to dość brzydkie. –