diff options
author | Ulf Hermann <ulf.hermann@theqtcompany.com> | 2016-04-12 12:05:33 +0200 |
---|---|---|
committer | Ulf Hermann <ulf.hermann@theqtcompany.com> | 2016-04-13 12:29:50 +0000 |
commit | 9968c72f7cac511235ab6aab2e948d29da4ff059 (patch) | |
tree | 08b4caf7c3a90219971841afb2155302a1a5c636 /tools | |
parent | e1513d7f904f03617c38f14d65cdaa8afd4ff2b2 (diff) |
Write generated string literals as array of numbers
Having UTF-8 literals in source code is notoriously brittle, as
compilers interpret them differently. Let's just write every byte or
qunicodechar as a number. That should remove any ambguity.
Change-Id: I3d79c7f15dc58a3b74b3a79e9186a612d38cb5a5
Reviewed-by: Simon Hausmann <simon.hausmann@theqtcompany.com>
Diffstat (limited to 'tools')
-rw-r--r-- | tools/qscxmlc/generator.cpp | 56 | ||||
-rw-r--r-- | tools/qscxmlc/scxmlcppdumper.cpp | 21 |
2 files changed, 24 insertions, 53 deletions
diff --git a/tools/qscxmlc/generator.cpp b/tools/qscxmlc/generator.cpp index 16acebe..b37e928 100644 --- a/tools/qscxmlc/generator.cpp +++ b/tools/qscxmlc/generator.cpp @@ -257,11 +257,12 @@ void Generator::generateCode() stringDataLength += thisLength; if (stringDataLength / constCharArraySizeLimit) { // save previous stringdata and start computing the next one. - fprintf(out, " char stringdata%d[%d];\n", stringDataCounter++, stringDataLength - thisLength); + fprintf(out, " unsigned char stringdata%d[%d];\n", stringDataCounter++, + stringDataLength - thisLength); stringDataLength = thisLength; } } - fprintf(out, " char stringdata%d[%d];\n", stringDataCounter, stringDataLength); + fprintf(out, " unsigned char stringdata%d[%d];\n", stringDataCounter, stringDataLength); } fprintf(out, "};\n"); @@ -299,56 +300,22 @@ void Generator::generateCode() } } } - fprintf(out, "\n },\n"); + fprintf(out, " },{\n"); } // // Build stringdata array // - fprintf(out, " \""); - int col = 0; - int len = 0; - int stringDataLength = 0; for (int i = 0; i < strings.size(); ++i) { QByteArray s = strings.at(i); - len = s.length(); - stringDataLength += len + 1; - if (stringDataLength >= constCharArraySizeLimit) { - fprintf(out, "\",\n \""); - stringDataLength = len + 1; - col = 0; - } else if (i) - fputs("\\0", out); // add \0 at the end of each string - - if (col && col + len >= 72) { - fprintf(out, "\"\n \""); - col = 0; - } else if (len && s.at(0) >= '0' && s.at(0) <= '9') { - fprintf(out, "\"\""); - len += 2; - } - int idx = 0; - while (idx < s.length()) { - if (idx > 0) { - col = 0; - fprintf(out, "\"\n \""); - } - int spanLen = qMin(70, s.length() - idx); - // don't cut escape sequences at the end of a line - int backSlashPos = s.lastIndexOf('\\', idx + spanLen - 1); - if (backSlashPos >= idx) { - int escapeLen = lengthOfEscapeSequence(s, backSlashPos); - spanLen = qBound(spanLen, backSlashPos + escapeLen - idx, s.length() - idx); - } - fprintf(out, "%.*s", spanLen, s.constData() + idx); - idx += spanLen; - col += spanLen; - } - col += len + 2; + int len = s.length(); + for (int charPos = 0; charPos < len; ++charPos) + fprintf(out, "0x%.2x,", static_cast<quint8>(s.at(charPos))); + fprintf(out, "0%s // %d: %s\n", i < strings.size() - 1 ? "," : "", i, s.constData()); } // Terminate stringdata struct - fprintf(out, "\"\n};\n"); + fprintf(out, " }};\n"); fprintf(out, "#undef QT_MOC_LITERAL\n\n"); // @@ -579,8 +546,9 @@ void Generator::generateCode() // fprintf(out, "\nvoid *%s::qt_metacast(const char *_clname)\n{\n", cdef->qualified.constData()); fprintf(out, " if (!_clname) return Q_NULLPTR;\n"); - fprintf(out, " if (!strcmp(_clname, qt_meta_stringdata_%s.stringdata0))\n" - " return static_cast<void*>(const_cast< %s*>(this));\n", + fprintf(out, " if (!strcmp(_clname, reinterpret_cast<const char *>(\n" + " qt_meta_stringdata_%s.stringdata0)))\n" + " return static_cast<void*>(const_cast< %s*>(this));\n", qualifiedClassNameIdentifier.constData(), cdef->classname.constData()); for (int i = 1; i < cdef->superclassList.size(); ++i) { // for all superclasses but the first one if (cdef->superclassList.at(i).second == FunctionDef::Private) diff --git a/tools/qscxmlc/scxmlcppdumper.cpp b/tools/qscxmlc/scxmlcppdumper.cpp index f981486..434f0a8 100644 --- a/tools/qscxmlc/scxmlcppdumper.cpp +++ b/tools/qscxmlc/scxmlcppdumper.cpp @@ -1020,17 +1020,20 @@ private: ucharCount += length + 1; return str; }); - t << QStringLiteral("},"); - if (strings.isEmpty()) { - t << QStringLiteral("QT_UNICODE_LITERAL_II(\"\")"); - } else { - for (int i = 0, ei = strings.size(); i < ei; ++i) { - QString s = cEscape(strings.at(i)); - t << QStringLiteral("QT_UNICODE_LITERAL_II(\"%1\") // %3: %2") - .arg(s + QStringLiteral("\\x00"), s, QString::number(i)); + t << QStringLiteral("},{"); + for (int i = 0, ei = strings.size(); i < ei; ++i) { + const QString &string = strings.at(i); + QString result; + for (int charPos = 0, eCharPos = string.size(); charPos < eCharPos; ++charPos) { + result.append(QStringLiteral("0x%1,") + .arg(QString::number(string.at(charPos).unicode(), 16))); } + result.append(QStringLiteral("0%1 // %2: %3") + .arg(QLatin1String(i < ei - 1 ? "," : ""), QString::number(i), + cEscape(string))); + t << result; } - t << QStringLiteral("};") << QStringLiteral(""); + t << QStringLiteral("}};") << QStringLiteral(""); clazz.classFields << QStringLiteral("static struct Strings {") << QStringLiteral(" QArrayData data[%1];").arg(strings.size()) |