X-Git-Url: https://vaikene.ee/gitweb/pswgen11.html?a=blobdiff_plain;f=src%2Flibs%2FCommon%2Futil.cpp;h=30874ebadcaf6bd9df0babe5065b7fe62f9ba376;hb=a81a943bee20df3c7eb34bafb3e3fe878facfe4e;hp=1a07a4a0c33f7d7c081f6a4a3a8cdbbb1e8ef66b;hpb=926606ce83f3a702d67c5c2c42478b091f78fdc4;p=evaf
diff --git a/src/libs/Common/util.cpp b/src/libs/Common/util.cpp
index 1a07a4a..30874eb 100644
--- a/src/libs/Common/util.cpp
+++ b/src/libs/Common/util.cpp
@@ -87,6 +87,11 @@ QVariant eVaf::Common::toVariant(QString const & value, QVariant const & default
if (ok)
return QVariant(c);
}
+ else if (value.startsWith('&')) {
+ QString c = strFromEscapedCharArray(value.toLatin1());
+ if (c.size() > 0)
+ return QVariant(c.at(0));
+ }
else
return QVariant(value.at(0));
}
@@ -98,12 +103,12 @@ QVariant eVaf::Common::toVariant(QString const & value, QVariant const & default
}
}
-QByteArray eVaf::Common::toEscapedString(QString const & str)
+QByteArray eVaf::Common::strToEscapedCharArray(QString const & str)
{
QByteArray rval;
foreach (QChar c, str) {
if (c.unicode() < 32 || c.unicode() >= 127)
- rval.append("" + QByteArray::number(c.unicode()) + ";");
+ rval.append("" + QByteArray::number(c.unicode(), 16) + ";");
else if (c == '\"')
rval.append(""");
else if (c == '&')
@@ -121,7 +126,7 @@ QByteArray eVaf::Common::toEscapedString(QString const & str)
return rval;
}
-QString eVaf::Common::fromEscapedString(QByteArray const & str)
+QString eVaf::Common::strFromEscapedCharArray(QByteArray const & str)
{
QString rval;
@@ -181,3 +186,87 @@ QString eVaf::Common::fromEscapedString(QByteArray const & str)
return rval;
}
+
+QByteArray eVaf::Common::binToEscapedCharArray(QByteArray const & src)
+{
+ QByteArray rval;
+ foreach (uchar c, src) {
+ if (c < 32 || c >= 127)
+ rval.append("" + QByteArray::number(c, 16) + ";");
+ else if (c == '\"')
+ rval.append(""");
+ else if (c == '&')
+ rval.append("&");
+ else if (c == '\'')
+ rval.append("'");
+ else if (c == '<')
+ rval.append("<");
+ else if (c == '>')
+ rval.append(">");
+ else
+ rval.append(c);
+ }
+
+ return rval;
+}
+
+QByteArray eVaf::Common::binFromEscapedCharArray(QByteArray const & str)
+{
+ QByteArray rval;
+
+ bool e = false;
+ QByteArray ref;
+ foreach (char c, str) {
+ if (!e) {
+ if (c == '&') {
+ e = true;
+ ref = "&";
+ }
+ else
+ rval.append(c);
+ }
+ else {
+ ref.append(c);
+ if (c == ';') {
+ e = false;
+ ref = ref.toLower();
+
+ if (ref.startsWith("")) {
+ // Numeric character reference in the HEX format
+ bool ok;
+ uchar ucode = ref.mid(3, ref.size() - 4).toUInt(&ok, 16);
+ if (ok)
+ rval.append(ucode);
+ else
+ // Invalid numeric character reference; output as is
+ rval.append(ref);
+ }
+ else if (ref.startsWith("")) {
+ // Numeric character reference in the DEC format
+ bool ok;
+ uchar ucode = ref.mid(2, ref.size() - 3).toUInt(&ok, 10);
+ if (ok)
+ rval.append(ucode);
+ else
+ // Invalid numeric character reference; output as is
+ rval.append(ref);
+ }
+ else if (ref == """)
+ rval.append('\"');
+ else if (ref == "&")
+ rval.append('&');
+ else if (ref == "'")
+ rval.append('\'');
+ else if (ref == "<")
+ rval.append('<');
+ else if (ref == ">")
+ rval.append('>');
+ else
+ // Unknown reference, output as is
+ rval.append(ref);
+ }
+ }
+ }
+
+ return rval;
+}