forked from BlackIkeEagle/muttvcardsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stringutils.cpp
108 lines (84 loc) · 3.7 KB
/
stringutils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/***************************************************************************
* Copyright (C) 2013 by Torsten Flammiger *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "stringutils.h"
vector<string> StringUtils::split(const string &s, const string &token)
{
// return this
vector<string> result;
// at pos begins the first char of the token...
unsigned long pos = s.find(token, 0);
// return original string if the token was not found
if ( pos == std::string::npos ) {
result.push_back(s);
return result;
}
std::string tmp;
while(pos != std::string::npos) {
// first iteration
if(tmp.length() == 0)
tmp = s;
result.push_back(tmp.substr(0, pos));
tmp = tmp.substr(pos + token.length()); // fast forward to skip the token itself
pos = tmp.find(token, 0);
// only one match found?
if(pos == std::string::npos) {
result.push_back(tmp);
}
}
return result;
}
bool StringUtils::endsWith(const string &text, string suffix) {
if( text.length() == 0 || suffix.length() == 0 )
return false;
if( suffix.length() > text.length() )
return false;
string tmp = text.substr(text.length() - suffix.length());
// transform both to lowercase for testing, and thus, ignores case
std::transform(tmp.begin(), tmp.end(), tmp.begin(), ::tolower);
std::transform(suffix.begin(), suffix.end(), suffix.begin(), ::tolower);
if(tmp == suffix) return true;
return false;
}
bool StringUtils::startsWith(const string &text, const string prefix) {
if( text.length() == 0 || prefix.length() == 0 )
return false;
if( prefix.length() > text.length() )
return false;
string tmp = text.substr(0, prefix.length());
if(tmp == prefix) return true;
return false;
}
bool StringUtils::contains(const string &text, const string& pattern) {
if( text.length() == 0 || pattern.length() == 0 )
return false;
if( pattern.length() > text.length() )
return false;
if( text.find(pattern, 0) == std::string::npos )
return false;
return true;
}
void StringUtils::replace(string *text, const string &from, const string &to) {
unsigned long pos = text->find(from);
// text contains something to replace
while(pos != std::string::npos) {
*text = text->replace(pos, from.length(), to);
pos = text->find(from);
}
}