+
+QByteArray eVaf::Common::toEscapedString(QString const & str)
+{
+ QByteArray rval;
+ foreach (QChar c, str) {
+ if (c.unicode() < 32 || c.unicode() >= 127)
+ rval.append("&#" + QByteArray::number(c.unicode()) + ";");
+ 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((char const)c.unicode());
+ }
+
+ return rval;
+}
+
+QString eVaf::Common::fromEscapedString(QByteArray const & str)
+{
+ QString rval;
+
+ bool e = false;
+ QByteArray ref;
+ foreach (char c, str) {
+ if (!e) {
+ if (c == '&') {
+ e = true;
+ ref = "&";
+ }
+ else
+ rval.append(QChar((ushort)c));
+ }
+ else {
+ ref.append(c);
+ if (c == ';') {
+ e = false;
+ ref = ref.toLower();
+
+ if (ref.startsWith("&#x")) {
+ // Numeric character reference in the HEX format
+ bool ok;
+ ushort ucode = ref.mid(3, ref.size() - 4).toUInt(&ok, 16);
+ if (ok)
+ rval.append(QChar(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;
+ ushort ucode = ref.mid(2, ref.size() - 3).toUInt(&ok, 10);
+ if (ok)
+ rval.append(QChar(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;
+}