From 926606ce83f3a702d67c5c2c42478b091f78fdc4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Enar=20V=C3=A4ikene?= Date: Tue, 29 Nov 2011 17:00:10 +0200 Subject: [PATCH] Added functions to convert unicode strings to/from escaped 7-bit character arrays. --- src/libs/Common/util.cpp | 84 +++++++++++++++++++++++++++++++++++++++ src/libs/Common/util.h | 21 ++++++++++ src/libs/Common/version.h | 4 +- 3 files changed, 107 insertions(+), 2 deletions(-) diff --git a/src/libs/Common/util.cpp b/src/libs/Common/util.cpp index eeedb61..1a07a4a 100644 --- a/src/libs/Common/util.cpp +++ b/src/libs/Common/util.cpp @@ -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("""); + 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; +} diff --git a/src/libs/Common/util.h b/src/libs/Common/util.h index 4b5e4c9..de094b3 100644 --- a/src/libs/Common/util.h +++ b/src/libs/Common/util.h @@ -76,6 +76,27 @@ inline bool isFalse(QString const & str) COMMON_EXPORT QVariant toVariant(QString const & value, QVariant const & defaultValue); +/** + * Converts unicode strings to escaped 7-bit character arrays. + * @param str Unicode string + * @return Escaped 7-bit character array + * + * This function converts a unicode (or any) string to the escaped 7-bit character array. Characters that cannot + * be output directly as a printable 7-bit character are output as numeric character references. The result can be + * directly inserted into XML or HTML documents and later converted back with the eVaf::Common::fromEscapedString() + * function. + */ +COMMON_EXPORT QByteArray toEscapedString(QString const & str); + +/** + * Converts escaped 7-bit character arrays to unicode string. + * @param str Escaped 7-bit character array + * @return Unicode string + * + * This function converts an escaped 7-bit character array to a unicode string. Numeric character references and + * character entity references are expanded to actual unicode characters. + */ +COMMON_EXPORT QString fromEscapedString(QByteArray const & str); } // namespace eVaf::Common } // namespace eVaf diff --git a/src/libs/Common/version.h b/src/libs/Common/version.h index 30d4175..dc569f4 100644 --- a/src/libs/Common/version.h +++ b/src/libs/Common/version.h @@ -25,12 +25,12 @@ /** * Module/library version number in the form major,minor,release,build */ -#define VER_FILE_VERSION 0,1,4,7 +#define VER_FILE_VERSION 0,2,1,8 /** * Module/library version number in the string format (shall end with \0) */ -#define VER_FILE_VERSION_STR "0.1.4.7\0" +#define VER_FILE_VERSION_STR "0.2.1.8\0" /** * Module/library name (shall end with \0) -- 2.45.0