28 #include <QtCore/QRegExp>
29 #include <QtCore/QCharRef>
30 #include <QtCore/QMutableStringListIterator>
39 if ( text.isEmpty() ) {
43 const QString strippedText = text.trimmed();
48 result.replace(strippedText, words.join(space));
55 for ( QStringList::Iterator it = tmp.begin(); it != tmp.end(); ++it ) {
56 *it = ( *it )[ 0 ].toUpper() + ( *it ).mid( 1 );
64 if (str.length() > maxlen) {
66 return QString::fromLatin1(
"...") + str.right(part);
73 if (str.length() > maxlen && maxlen > 3) {
74 const int part = (maxlen-3)/2;
75 return str.left(part) + QLatin1String(
"...") + str.right(part);
82 if (str.length() > maxlen) {
84 return str.left(part) + QLatin1String(
"...");
91 bool ignoreMax = 0 == max;
97 int tokenStart = s.indexOf(sep, searchStart);
99 while (-1 != tokenStart && (ignoreMax || l.count() < max - 1))
101 if (!s.mid(searchStart, tokenStart - searchStart).isEmpty())
102 l << s.mid(searchStart, tokenStart - searchStart);
104 searchStart = tokenStart + sep.length();
105 tokenStart = s.indexOf(sep, searchStart);
108 if (!s.mid(searchStart, s.length() - searchStart).isEmpty())
109 l << s.mid(searchStart, s.length() - searchStart);
116 bool ignoreMax = 0 == max;
122 int tokenStart = s.indexOf(sep, searchStart);
124 while (-1 != tokenStart && (ignoreMax || l.count() < max - 1))
126 if (!s.mid(searchStart, tokenStart - searchStart).isEmpty())
127 l << s.mid(searchStart, tokenStart - searchStart);
129 searchStart = tokenStart + 1;
130 tokenStart = s.indexOf(sep, searchStart);
133 if (!s.mid(searchStart, s.length() - searchStart).isEmpty())
134 l << s.mid(searchStart, s.length() - searchStart);
141 bool ignoreMax = 0 == max;
146 int tokenStart = sep.indexIn(s, searchStart);
147 int len = sep.matchedLength();
149 while (-1 != tokenStart && (ignoreMax || l.count() < max - 1))
151 if (!s.mid(searchStart, tokenStart - searchStart).isEmpty())
152 l << s.mid(searchStart, tokenStart - searchStart);
154 searchStart = tokenStart + len;
155 tokenStart = sep.indexIn(s, searchStart);
156 len = sep.matchedLength();
159 if (!s.mid(searchStart, s.length() - searchStart).isEmpty())
160 l << s.mid(searchStart, s.length() - searchStart);
167 QRegExp urlEx(QLatin1String(
"(www\\.(?!\\.)|(fish|(f|ht)tp(|s))://)[\\d\\w\\./,:_~\\?=&;#@\\-\\+\\%\\$]+[\\d\\w/]"));
170 int urlPos = 0, urlLen;
171 while ((urlPos = urlEx.indexIn(richText, urlPos)) >= 0)
173 urlLen = urlEx.matchedLength();
174 QString href = richText.mid( urlPos, urlLen );
176 if((urlPos > 0) && richText[urlPos-1].isLetterOrNumber()){
181 QString anchor = QString::fromLatin1(
"<a href=\"") + href + QLatin1String(
"\">") + href + QLatin1String(
"</a>");
182 richText.replace( urlPos, urlLen, anchor );
185 urlPos += anchor.length();
193 const QChar *unicode = str.unicode();
194 for (
int i = 0; i < str.length(); ++i )
197 result += ( unicode[ i ].unicode() <= 0x21 ) ? unicode[ i ] :
198 QChar( 0x1001F - unicode[ i ].unicode() );
207 register unsigned char c;
218 static const unsigned char text_chars[256] = {
220 F,
F,
F,
F,
F,
F,
F,
T,
T,
T,
T,
F,
T,
T,
F,
F,
222 F,
F,
F,
F,
F,
F,
F,
F,
F,
F,
F,
T,
F,
F,
F,
F,
223 T,
T,
T,
T,
T,
T,
T,
T,
T,
T,
T,
T,
T,
T,
T,
T,
224 T,
T,
T,
T,
T,
T,
T,
T,
T,
T,
T,
T,
T,
T,
T,
T,
225 T,
T,
T,
T,
T,
T,
T,
T,
T,
T,
T,
T,
T,
T,
T,
T,
226 T,
T,
T,
T,
T,
T,
T,
T,
T,
T,
T,
T,
T,
T,
T,
T,
227 T,
T,
T,
T,
T,
T,
T,
T,
T,
T,
T,
T,
T,
T,
T,
T,
228 T,
T,
T,
T,
T,
T,
T,
T,
T,
T,
T,
T,
T,
T,
T,
F,
230 X,
X,
X,
X,
X,
T,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
231 X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
232 I,
I,
I,
I,
I,
I,
I,
I,
I,
I,
I,
I,
I,
I,
I,
I,
233 I,
I,
I,
I,
I,
I,
I,
I,
I,
I,
I,
I,
I,
I,
I,
I,
234 I,
I,
I,
I,
I,
I,
I,
I,
I,
I,
I,
I,
I,
I,
I,
I,
235 I,
I,
I,
I,
I,
I,
I,
I,
I,
I,
I,
I,
I,
I,
I,
I,
236 I,
I,
I,
I,
I,
I,
I,
I,
I,
I,
I,
I,
I,
I,
I,
I,
237 I,
I,
I,
I,
I,
I,
I,
I,
I,
I,
I,
I,
I,
I,
I, I
241 for (i = 0; (c = buf[i]); ++i) {
242 if ((c & 0x80) == 0) {
248 if (text_chars[c] !=
T)
251 }
else if ((c & 0x40) == 0) {
256 if ((c & 0x20) == 0) {
258 }
else if ((c & 0x10) == 0) {
260 }
else if ((c & 0x08) == 0) {
262 }
else if ((c & 0x04) == 0) {
264 }
else if ((c & 0x02) == 0) {
269 for (n = 0; n < following; ++n) {
274 if ((c & 0x80) == 0 || (c & 0x40))
298 QString::fromUtf8( str ) :
299 QString::fromLocal8Bit( str );
320 if (caseSensitivity == Qt::CaseSensitive) {
328 const QChar* currA = a.unicode();
329 const QChar* currB = b.unicode();
331 if (currA == currB) {
335 while (!currA->isNull() && !currB->isNull()) {
336 const QChar* begSeqA = currA;
337 const QChar* begSeqB = currB;
338 if (currA->unicode() == QChar::ObjectReplacementCharacter) {
342 if (currB->unicode() == QChar::ObjectReplacementCharacter) {
346 if (currA->unicode() == QChar::ReplacementCharacter) {
350 if (currB->unicode() == QChar::ReplacementCharacter) {
355 while (!currA->isNull() && !currA->isDigit() && !currA->isPunct() && !currA->isSpace()) {
359 while (!currB->isNull() && !currB->isDigit() && !currB->isPunct() && !currB->isSpace()) {
364 const QStringRef& subA(a.midRef(begSeqA - a.unicode(), currA - begSeqA));
365 const QStringRef& subB(b.midRef(begSeqB - b.unicode(), currB - begSeqB));
366 const int cmp = QStringRef::localeAwareCompare(subA, subB);
368 return cmp < 0 ? -1 : +1;
371 if (currA->isNull() || currB->isNull()) {
376 while ((currA->isPunct() || currA->isSpace()) && (currB->isPunct() || currB->isSpace())) {
377 if (*currA != *currB) {
378 return (*currA < *currB) ? -1 : +1;
382 if (currA->isNull() || currB->isNull()) {
388 if ((*currA == QLatin1Char(
'0')) || (*currB == QLatin1Char(
'0'))) {
392 if (!currA->isDigit() && !currB->isDigit()) {
394 }
else if (!currA->isDigit()) {
396 }
else if (!currB->isDigit()) {
398 }
else if (*currA < *currB) {
400 }
else if (*currA > *currB) {
414 bool isFirstRun =
true;
417 if (!currA->isDigit() && !currB->isDigit()) {
422 }
else if (!currA->isDigit()) {
424 return *currA < *currB ? -1 : +1;
428 }
else if (!currB->isDigit()) {
430 return *currA < *currB ? -1 : +1;
434 }
else if ((*currA < *currB) && (weight == 0)) {
436 }
else if ((*currA > *currB) && (weight == 0)) {
446 if (currA->isNull() && currB->isNull()) {
450 return currA->isNull() ? -1 : + 1;
455 const QChar zwsp(0x200b);
458 result.reserve(text.length());
460 for (
int i = 0; i < text.length(); i++) {
461 const QChar c = text[i];
462 bool openingParens = (c == QLatin1Char(
'(') || c == QLatin1Char(
'{') || c == QLatin1Char(
'['));
463 bool singleQuote = (c == QLatin1Char(
'\'') );
464 bool closingParens = (c == QLatin1Char(
')') || c == QLatin1Char(
'}') || c == QLatin1Char(
']'));
465 bool breakAfter = (closingParens || c.isPunct() || c.isSymbol());
466 bool nextIsSpace = (i == (text.length() - 1) || text[i + 1].isSpace());
467 bool prevIsSpace = (i == 0 || text[i - 1].isSpace() || result[result.length() - 1] == zwsp);
470 if (openingParens && !prevIsSpace)
474 if (singleQuote && !prevIsSpace)
475 result += QChar(0x2060);
479 if (breakAfter && !openingParens && !nextIsSpace && !singleQuote)
QString from8Bit(const char *str)
Construct QString from a c string, guessing whether it is UTF8- or Local8Bit-encoded.
int naturalCompare(const QString &a, const QString &b, Qt::CaseSensitivity caseSensitivity=Qt::CaseSensitive)
Does a natural comparing of the strings.
QString rsqueeze(const QString &str, int maxlen=40)
Substitute characters at the end of a string by "...".
QString preProcessWrap(const QString &text)
Preprocesses the given string in order to provide additional line breaking opportunities for QTextLay...
bool isUtf8(const char *str)
Guess whether a string is UTF8 encoded.
QString csqueeze(const QString &str, int maxlen=40)
Substitute characters at the middle of a string by "...".
QStringList perlSplit(const QString &sep, const QString &s, int max=0)
Split a QString into a QStringList in a similar fashion to the static QStringList function in Qt...
QString capwords(const QString &text)
Capitalizes each word in the string "hello there" becomes "Hello There" (string)
const QString & staticQString(const char *str)
Creates a static QString.
QString lsqueeze(const QString &str, int maxlen=40)
Substitute characters at the beginning of a string by "...".
QString obscure(const QString &str)
Obscure string by using a simple symmetric encryption.
QString tagUrls(const QString &text)
This method auto-detects URLs in strings, and adds HTML markup to them so that richtext or HTML-enabl...