summaryrefslogtreecommitdiff
path: root/noncore/applets/keyhelper/keyhelperapplet/misc/StringParser.cpp
Unidiff
Diffstat (limited to 'noncore/applets/keyhelper/keyhelperapplet/misc/StringParser.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/applets/keyhelper/keyhelperapplet/misc/StringParser.cpp72
1 files changed, 72 insertions, 0 deletions
diff --git a/noncore/applets/keyhelper/keyhelperapplet/misc/StringParser.cpp b/noncore/applets/keyhelper/keyhelperapplet/misc/StringParser.cpp
new file mode 100644
index 0000000..72c15f1
--- a/dev/null
+++ b/noncore/applets/keyhelper/keyhelperapplet/misc/StringParser.cpp
@@ -0,0 +1,72 @@
1#include "StringParser.h"
2
3#include <qregexp.h>
4
5QStringList StringParser::split(const QChar& sep, const QString& str,
6 bool allowEmptyEntries)
7{
8 QString line = str + sep;
9 QString quote;
10 QRegExp rxend;
11 QRegExp rxdbl;
12 int pos=0, len, idx=0;
13 QStringList list;
14 while(idx < (int)line.length()-1){
15 if(!quote.isEmpty()){
16 QString s;
17 while((pos = rxend.match(line, idx, &len)) != -1){
18 s += line.mid(idx, len+pos-idx-1);
19 idx = pos+len-1;
20 if(len % 2 == 0){
21 s.replace(rxdbl, quote);
22 list.append(s.left(s.length()-1));
23 idx++;
24 break;
25 }
26 }
27 quote = "";
28 } else if(line[idx] == '\"'){
29 rxend.setPattern(QString("\"+") + sep);
30 rxdbl.setPattern("\"\"");
31 quote = "\"";
32 idx++;
33 } else if(line[idx] == '\''){
34 rxend.setPattern(QString("\'+") + sep);
35 rxdbl.setPattern("\'\'");
36 quote = "\'";
37 idx++;
38 } else if(!allowEmptyEntries && line[idx] == sep){
39 idx++;
40 } else {
41 pos = line.find(sep, idx);
42 if(pos != -1){
43 const QString& s = line.mid(idx, pos-idx);
44 list.append(s);
45 idx = pos+1;
46 }
47 }
48 if(pos == -1) break;
49 }
50 return list;
51}
52
53QString StringParser::join(const QChar& sep, const QStringList& list)
54{
55 QString str;
56 QString s;
57 QStringList tmp;
58 QRegExp quote("\"");
59 for(QStringList::ConstIterator it=list.begin();
60 it!=list.end(); ++it){
61 s = *it;
62 if(s.find(sep) != -1
63 || s[0] == '\"'
64 || s[0] == '\''){
65 s.replace(quote, "\"\"");
66 tmp.append("\"" + s + "\"");
67 } else {
68 tmp.append(s);
69 }
70 }
71 return tmp.join(sep);
72}