1 /***************************************************************************
2 * Copyright (C) 2005/06 by the Quassel IRC Team *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) version 3. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
27 QString nickFromMask(QString mask) {
28 return mask.section('!', 0, 0);
31 QString userFromMask(QString mask) {
32 QString userhost = mask.section('!', 1);
33 if(userhost.isEmpty()) return QString();
34 return userhost.section('@', 0, 0);
37 QString hostFromMask(QString mask) {
38 QString userhost = mask.section('!', 1);
39 if(userhost.isEmpty()) return QString();
40 return userhost.section('@', 1);
43 bool isChannelName(QString str) {
44 return QString("#&!+").contains(str[0]);
47 QString decodeString(QByteArray input, QString encoding) {
48 // First, we check if it's utf8. It is very improbable to encounter a string that looks like
49 // valid utf8, but in fact is not. This means that if the input string passes as valid utf8, it
50 // is safe to assume that it is.
51 Q_ASSERT(sizeof(const char) == sizeof(quint8)); // just to make sure
54 for(int i = 0; i < input.size(); i++) {
56 // We check a part of a multibyte char. These need to be of the form 10yyyyyy.
57 if((input[i] & 0xc0) != 0x80) { isUtf8 = false; break; }
61 if((input[i] & 0x80) == 0x00) continue; // 7 bit is always ok
62 if((input[i] & 0xf8) == 0xf0) { cnt = 3; continue; } // 4-byte char 11110xxx 10yyyyyy 10zzzzzz 10vvvvvv
63 if((input[i] & 0xf0) == 0xe0) { cnt = 2; continue; } // 3-byte char 1110xxxx 10yyyyyy 10zzzzzz
64 if((input[i] & 0xe0) == 0xc0) { cnt = 1; continue; } // 2-byte char 110xxxxx 10yyyyyy
65 isUtf8 = false; break; // 8 bit char, but not utf8!
67 if(isUtf8 && cnt == 0) {
68 QString s = QString::fromUtf8(input);
69 //qDebug() << "Detected utf8:" << s;
72 QTextCodec *codec = QTextCodec::codecForName(encoding.toAscii());
74 qWarning() << QString("Invalid encoding: %1").arg(encoding);
75 return QString::fromAscii(input);
77 return codec->toUnicode(input);
80 void writeDataToDevice(QIODevice *dev, const QVariant &item) {
82 QDataStream out(&block, QIODevice::WriteOnly);
83 out.setVersion(QDataStream::Qt_4_2);
84 out << (quint32)0 << item;
85 out.device()->seek(0);
86 out << (quint32)(block.size() - sizeof(quint32));
90 bool readDataFromDevice(QIODevice *dev, quint32 &blockSize, QVariant &item) {
92 in.setVersion(QDataStream::Qt_4_2);
95 if(dev->bytesAvailable() < (int)sizeof(quint32)) return false;
98 if(dev->bytesAvailable() < blockSize) return false;
104 uint editingDistance(const QString &s1, const QString &s2) {
105 uint n = s1.size()+1;
106 uint m = s2.size()+1;
108 QVector< QVector< uint > >matrix(n,QVector<uint>(m,0));
110 for(uint i = 0; i < n; i++)
113 for(uint i = 0; i < m; i++)
117 for(uint i = 1; i < n; i++) {
118 for(uint j = 1; j < m; j++) {
119 uint deleteChar = matrix[i-1][j] + 1;
120 uint insertChar = matrix[i][j-1] + 1;
122 if(deleteChar < insertChar)
127 if(s1[i-1] == s2[j-1]) {
128 uint inheritChar = matrix[i-1][j-1];
129 if(inheritChar < min)
136 return matrix[n-1][m-1];
139 QByteArray methodName(const QMetaMethod &method) {
140 QByteArray sig(method.signature());
141 return sig.left(sig.indexOf("("));