static QString evilBytes(const QString& str, bool isUtf8, int format, const QByteArray &codecName) { //qDebug() << "EVIL: " << str << isUtf8 << format << codecName; if (isUtf8) return protect(str); if (format == 20) return protect(str); if (codecName == "UTF-8") return protect(str); QTextCodec *codec = QTextCodec::codecForName(codecName); if (!codec) return protect(str); QString t = QString::fromLatin1(codec->fromUnicode(protect(str)).data()); int len = (int) t.length(); QString result; // FIXME: Factor is sensible only for latin scripts, probably. result.reserve(t.length() * 2); for (int k = 0; k < len; k++) { if (t[k].unicode() >= 0x7f) result += numericEntity(t[k].unicode()); else result += t[k]; } return result; }
static QString protect(const QString &str) { QString result; result.reserve(str.length() * 12 / 10); for (int i = 0; i != str.size(); ++i) { uint c = str.at(i).unicode(); switch (c) { case '\"': result += QLatin1String("""); break; case '&': result += QLatin1String("&"); break; case '>': result += QLatin1String(">"); break; case '<': result += QLatin1String("<"); break; case '\'': result += QLatin1String("'"); break; default: if (c < 0x20 && c != '\r' && c != '\n' && c != '\t') result += numericEntity(c); else // this also covers surrogates result += QChar(c); } } return result; }
static QString protect(const QCString& str) { QString result; int len = (int) str.length(); for (int k = 0; k < len; k++) { switch (str[k]) { case '\"': result += QString("""); break; case '&': result += QString("&"); break; case '>': result += QString(">"); break; case '<': result += QString("<"); break; case '\'': result += QString("'"); break; default: if (uchar(str[k]) < 0x20 && str[k] != '\n') result += numericEntity(uchar(str[k])); else result += str[k]; } } return result; }
static QString protect( const QByteArray& str ) { QString result; int len = (int) str.length(); for ( int k = 0; k < len; k++ ) { switch( str[k] ) { case '\"': result += QLatin1String( """ ); break; case '&': result += QLatin1String( "&" ); break; case '>': result += QLatin1String( ">" ); break; case '<': result += QLatin1String( "<" ); break; case '\'': result += QLatin1String( "'" ); break; default: if ( (uchar) str[k] < 0x20 ) result += numericEntity( (uchar) str[k] ); else result += QLatin1Char(str[k]); } } return result; }
static QString evilBytes( const QCString& str, bool utf8 ) { if ( utf8 ) { return protect( str ); } else { QString result; QCString t = protect( str ).latin1(); int len = (int) t.length(); for ( int k = 0; k < len; k++ ) { if ( (uchar) t[k] >= 0x7f ) result += numericEntity( (uchar) t[k] ); else result += QChar( t[k] ); } return result; } }