PageRenderTime 32ms CodeModel.GetById 14ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/quassel-0.7.3/src/common/util.cpp

#
C++ | 171 lines | 121 code | 24 blank | 26 comment | 34 complexity | 8960093834123b947f882546b5086f3f MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.0
  1/***************************************************************************
  2 *   Copyright (C) 2005-2010 by the Quassel Project                        *
  3 *   devel@quassel-irc.org                                                 *
  4 *                                                                         *
  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.                                           *
  9 *                                                                         *
 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.                          *
 14 *                                                                         *
 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 ***************************************************************************/
 20
 21#include "util.h"
 22
 23#include <QCoreApplication>
 24#include <QDebug>
 25#include <QFile>
 26#include <QTextCodec>
 27
 28#include "quassel.h"
 29
 30class QMetaMethod;
 31
 32QString nickFromMask(QString mask) {
 33  return mask.section('!', 0, 0);
 34}
 35
 36QString userFromMask(QString mask) {
 37  QString userhost = mask.section('!', 1);
 38  if(userhost.isEmpty()) return QString();
 39  return userhost.section('@', 0, 0);
 40}
 41
 42QString hostFromMask(QString mask) {
 43  QString userhost = mask.section('!', 1);
 44  if(userhost.isEmpty()) return QString();
 45  return userhost.section('@', 1);
 46}
 47
 48bool isChannelName(QString str) {
 49  return QString("#&!+").contains(str[0]);
 50}
 51
 52QString stripFormatCodes(QString str) {
 53  str.remove(QRegExp("\x03(\\d\\d?(,\\d\\d?)?)?"));
 54  str.remove('\x02');
 55  str.remove('\x0f');
 56  str.remove('\x12');
 57  str.remove('\x16');
 58  str.remove('\x1d');
 59  str.remove('\x1f');
 60  return str;
 61}
 62
 63QString stripAcceleratorMarkers(const QString &label_) {
 64  QString label = label_;
 65  int p = 0;
 66  forever {
 67    p = label.indexOf('&', p);
 68    if(p < 0 || p + 1 >= label.length())
 69      break;
 70
 71    if(label.at(p + 1).isLetterOrNumber() || label.at(p + 1) == '&')
 72      label.remove(p, 1);
 73
 74    ++p;
 75  }
 76  return label;
 77}
 78
 79QString decodeString(const QByteArray &input, QTextCodec *codec) {
 80  // First, we check if it's utf8. It is very improbable to encounter a string that looks like
 81  // valid utf8, but in fact is not. This means that if the input string passes as valid utf8, it
 82  // is safe to assume that it is.
 83  // Q_ASSERT(sizeof(const char) == sizeof(quint8));  // In God we trust...
 84  bool isUtf8 = true;
 85  int cnt = 0;
 86  for(int i = 0; i < input.size(); i++) {
 87    if(cnt) {
 88      // We check a part of a multibyte char. These need to be of the form 10yyyyyy.
 89      if((input[i] & 0xc0) != 0x80) { isUtf8 = false; break; }
 90      cnt--;
 91      continue;
 92    }
 93    if((input[i] & 0x80) == 0x00) continue; // 7 bit is always ok
 94    if((input[i] & 0xf8) == 0xf0) { cnt = 3; continue; }  // 4-byte char 11110xxx 10yyyyyy 10zzzzzz 10vvvvvv
 95    if((input[i] & 0xf0) == 0xe0) { cnt = 2; continue; }  // 3-byte char 1110xxxx 10yyyyyy 10zzzzzz
 96    if((input[i] & 0xe0) == 0xc0) { cnt = 1; continue; }  // 2-byte char 110xxxxx 10yyyyyy
 97    isUtf8 = false; break;  // 8 bit char, but not utf8!
 98  }
 99  if(isUtf8 && cnt == 0) {
100    QString s = QString::fromUtf8(input);
101    //qDebug() << "Detected utf8:" << s;
102    return s;
103  }
104  //QTextCodec *codec = QTextCodec::codecForName(encoding.toAscii());
105  if(!codec) return QString::fromAscii(input);
106  return codec->toUnicode(input);
107}
108
109uint editingDistance(const QString &s1, const QString &s2) {
110  uint n = s1.size()+1;
111  uint m = s2.size()+1;
112  QVector< QVector< uint > >matrix(n,QVector<uint>(m,0));
113
114  for(uint i = 0; i < n; i++)
115    matrix[i][0] = i;
116
117  for(uint i = 0; i < m; i++)
118    matrix[0][i] = i;
119
120  uint min;
121  for(uint i = 1; i < n; i++) {
122    for(uint j = 1; j < m; j++) {
123      uint deleteChar = matrix[i-1][j] + 1;
124      uint insertChar = matrix[i][j-1] + 1;
125
126      if(deleteChar < insertChar)
127        min = deleteChar;
128      else
129        min = insertChar;
130
131      if(s1[i-1] == s2[j-1]) {
132        uint inheritChar = matrix[i-1][j-1];
133        if(inheritChar < min)
134          min = inheritChar;
135      }
136
137      matrix[i][j] = min;
138    }
139  }
140  return matrix[n-1][m-1];
141}
142
143QString secondsToString(int timeInSeconds) {
144    QList< QPair<int, QString> > timeUnit;
145    timeUnit.append(qMakePair(365*24*60*60, QCoreApplication::translate("Quassel::secondsToString()", "year")));
146    timeUnit.append(qMakePair(24*60*60, QCoreApplication::translate("Quassel::secondsToString()", "day")));
147    timeUnit.append(qMakePair(60*60, QCoreApplication::translate("Quassel::secondsToString()", "h")));
148    timeUnit.append(qMakePair(60, QCoreApplication::translate("Quassel::secondsToString()", "min")));
149    timeUnit.append(qMakePair(1, QCoreApplication::translate("Quassel::secondsToString()", "sec")));
150
151    QStringList returnString;
152    for(int i=0; i < timeUnit.size(); i++) {
153      int n = timeInSeconds / timeUnit[i].first;
154      if(n > 0) {
155        returnString += QString("%1 %2").arg(QString::number(n), timeUnit[i].second);
156      }
157      timeInSeconds = timeInSeconds % timeUnit[i].first;
158    }
159    return returnString.join(", ");
160}
161
162QByteArray prettyDigest(const QByteArray &digest) {
163  QByteArray hexDigest = digest.toHex().toUpper();
164  QByteArray prettyDigest;
165  prettyDigest.fill(':', hexDigest.count() + (hexDigest.count() / 2) - 1);
166
167  for(int i = 0; i * 2 < hexDigest.count(); i++) {
168    prettyDigest.replace(i * 3, 2, hexDigest.mid(i * 2, 2));
169  }
170  return prettyDigest;
171}