Debian Codegraph @

Frameworks/OpenVanilla/Headers/OVStringHelper.h

raw symbols SCIP
1 //
2 // OVStringHelper.h
3 //
4 // Copyright (c) 2004-2008 The OpenVanilla Project (http://openvanilla.org)
5 // All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
11 // 1. Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // 2. Redistributions in binary form must reproduce the above copyright
14 // notice, this list of conditions and the following disclaimer in the
15 // documentation and/or other materials provided with the distribution.
16 // 3. Neither the name of OpenVanilla nor the names of its contributors
17 // may be used to endorse or promote products derived from this software
18 // without specific prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 // POSSIBILITY OF SUCH DAMAGE.
31
32 #ifndef OVStringHelper_h
33 #define OVStringHelper_h
34
35 #include <string>
36
37 namespace OpenVanilla {
38 using namespace std;
39
40 class OVStringHelper {
41 public:
42 static const vector<string> SplitBySpacesOrTabsWithDoubleQuoteSupport(const string& text)
43 {
44 vector<string> result;
45 size_t index = 0, last = 0, length = text.length();
46 while (index < length) {
47 if (text[index] == '\"') {
48 index++;
49 string tmp;
50 while (index < length) {
51 if (text[index] == '\"') {
52 index++;
53 break;
54 }
55
56 if (text[index] == '\\' && index + 1 < length) {
57 index++;
58 char c = text[index];
59 switch (c) {
60 case 'r':
61 tmp += '\r';
62 break;
63 case 'n':
64 tmp += '\n';
65 break;
66 case '\"':
67 tmp += '\"';
68 break;
69 case '\\':
70 tmp += '\\';
71 break;
72 }
73 }
74 else {
75 tmp += text[index];
76 }
77
78 index++;
79 }
80 result.push_back(tmp);
81 }
82
83 if (text[index] != ' ' && text[index] != '\t') {
84 last = index;
85 while (index < length) {
86 if (text[index] == ' ' || text[index] == '\t') {
87 if (index - last)
88 result.push_back(text.substr(last, index - last));
89 break;
90 }
91 index++;
92 }
93
94 if (index == length && index - last)
95 result.push_back(text.substr(last, index - last));
96 }
97
98 index++;
99 }
100
101 return result;
102 }
103
104 static const vector<string> SplitBySpacesOrTabs(const string& text)
105 {
106 vector<string> result;
107 size_t index = 0, last = 0, length = text.length();
108 while (index < length) {
109 if (text[index] != ' ' && text[index] != '\t') {
110 last = index;
111 while (index < length) {
112 if (text[index] == ' ' || text[index] == '\t') {
113 if (index - last)
114 result.push_back(text.substr(last, index - last));
115 break;
116 }
117 index++;
118 }
119
120 if (index == length && index - last)
121 result.push_back(text.substr(last, index - last));
122 }
123
124 index++;
125 }
126
127 return result;
128 }
129
130 static const vector<string> Split(const string& text, char c)
131 {
132 vector<string> result;
133 size_t index = 0, last = 0, length = text.length();
134 while (index < length) {
135 last = index;
136 while (index < length) {
137 if (text[index] == c) {
138 result.push_back(text.substr(last, index - last));
139 break;
140 }
141 index++;
142
143 if (index == length && index - last)
144 result.push_back(text.substr(last, index - last));
145 }
146
147 index++;
148 }
149
150 return result;
151 }
152
153 // named after Cocoa's NSString -stringByReplacingOccurrencesOfString:WithString:, horrible
154 static const string StringByReplacingOccurrencesOfStringWithString(const string& source, const string& substr, const string& replacement)
155 {
156 if (!substr.length())
157 return source;
158
159 size_t pos;
160 if ((pos = source.find(substr)) >= source.length())
161 return source;
162
163 return source.substr(0, pos) + replacement + StringByReplacingOccurrencesOfStringWithString(source.substr(pos + substr.length()), substr, replacement);
164 }
165
166 static const string Join(const vector<string>& vec, const string& separator)
167 {
168 return Join(vec.begin(), vec.end(), separator);
169 }
170
171 static const string Join(const vector<string>& vec, size_t from, size_t size, const string& separator)
172 {
173 return Join(vec.begin() + from, vec.begin() + from + size, separator);
174 }
175
176 static const string Join(vector<string>::const_iterator begin, vector<string>::const_iterator end, const string& separator)
177 {
178 string result;
179 for (vector<string>::const_iterator iter = begin ; iter != end ; ) {
180 result += *iter;
181 if (++iter != end)
182 result += separator;
183 }
184 return result;
185 }
186 };
187 };
188
189 #endif