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