]> vaikene.ee Git - evaf/blobdiff - src/libs/Common/util.cpp
Added functions to convert unicode strings to/from escaped 7-bit character arrays.
[evaf] / src / libs / Common / util.cpp
index eeedb61ff19fddeab479a322c9dc0eedec681c36..1a07a4a0c33f7d7c081f6a4a3a8cdbbb1e8ef66b 100644 (file)
@@ -97,3 +97,87 @@ QVariant eVaf::Common::toVariant(QString const & value, QVariant const & default
             return QVariant(value);
     }
 }
+
+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("&quot;");
+        else if (c == '&')
+            rval.append("&amp;");
+        else if (c == '\'')
+            rval.append("&apos;");
+        else if (c == '<')
+            rval.append("&lt;");
+        else if (c == '>')
+            rval.append("&gt;");
+        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 == "&quot;")
+                    rval.append('\"');
+                else if (ref == "&amp;")
+                    rval.append('&');
+                else if (ref == "&apos;")
+                    rval.append('\'');
+                else if (ref == "&lt;")
+                    rval.append('<');
+                else if (ref == "&gt;")
+                    rval.append('>');
+                else
+                    // Unknown reference, output as is
+                    rval.append(ref);
+            }
+        }
+    }
+
+    return rval;
+}