From 1f7d6a9f3aa9f305323f4278e7e3bf8459873567 Mon Sep 17 00:00:00 2001 From: Jordan Sherer Date: Thu, 21 Nov 2019 23:52:12 -0500 Subject: [PATCH] Added some unicode escaping routines for later --- TransmitTextEdit.cpp | 7 +++++++ varicode.cpp | 37 +++++++++++++++++++++++++++++++++---- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/TransmitTextEdit.cpp b/TransmitTextEdit.cpp index 920e8f1..df1dee1 100644 --- a/TransmitTextEdit.cpp +++ b/TransmitTextEdit.cpp @@ -1,5 +1,7 @@ #include "TransmitTextEdit.h" +#include "varicode.h" + #include #include @@ -243,9 +245,14 @@ void TransmitTextEdit::on_textContentsChanged(int /*pos*/, int rem, int add){ } QString normalized = text.normalized(QString::NormalizationForm_KD); + QString result; std::copy_if(normalized.begin(), normalized.end(), std::back_inserter(result), [](QChar& c) { +#if JS8_ALLOW_UNICODE + return (c == 10 || c == 0x1A || (c > 31 && c < 128)) || c.isPrint()); +#else return c.toLatin1() != 0 && (c == 10 || c == 0x1A || (c > 31 && c < 128)); +#endif }); if(result != text){ diff --git a/varicode.cpp b/varicode.cpp index a2499e4..b905967 100644 --- a/varicode.cpp +++ b/varicode.cpp @@ -336,13 +336,42 @@ int dbmTomwatts(int dbm){ } QString Varicode::escape(const QString &text){ - // TODO: support different escapes? - return text; + static const int size = 6; + QString escaped; + escaped.reserve(size * text.size()); + for(QString::const_iterator it = text.begin(); it != text.end(); ++it){ + QChar ch = *it; + ushort code = ch.unicode(); + if (code < 0x80) { + escaped += ch; + } else { +#if JS8_USE_ESCAPE_SUB_CHAR + //escaped += "\x1A"; // substitute char +#else + escaped += "\\U"; // "U+"; // substitute char +#endif + escaped += QString::number(code, 16).rightJustified(4, '0'); + } + } + + return escaped; } QString Varicode::unescape(const QString &text){ - // TODO: support different escapes? - return text; + QString unescaped(text); +#if JS8_USE_ESCAPE_SUB_CHAR + static const int size = 5; + QRegExp r("([\\x1A][0-9a-fA-F]{4})"); +#else + static const int size = 6; + QRegExp r("(([uU][+]|\\\\[uU])[0-9a-fA-F]{4})"); +#endif + int pos = 0; + while ((pos = r.indexIn(unescaped, pos)) != -1) { + unescaped.replace(pos++, size, QChar(r.cap(1).right(4).toUShort(0, 16))); + } + + return unescaped; } QString Varicode::rstrip(const QString& str) {