Commit | Line | Data |
---|---|---|
970ed795 EL |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Copyright (c) 2000-2014 Ericsson Telecom AB | |
3 | // All rights reserved. This program and the accompanying materials | |
4 | // are made available under the terms of the Eclipse Public License v1.0 | |
5 | // which accompanies this distribution, and is available at | |
6 | // http://www.eclipse.org/legal/epl-v10.html | |
7 | /////////////////////////////////////////////////////////////////////////////// | |
8 | #include "../common/dbgnew.hh" | |
9 | // dbgnew must come before Mstring.hh which includes common/memory.hh | |
10 | #include "Mstring.hh" | |
11 | ||
12 | #include <string.h> | |
13 | #include <ctype.h> | |
14 | #include <stdio.h> | |
15 | #include <stdlib.h> // only for abort | |
16 | ||
17 | const Mstring empty_string; | |
18 | ||
19 | Mstring::Mstring() | |
20 | : text(memptystr()) | |
21 | {} | |
22 | ||
23 | Mstring::Mstring(const Mstring & other) | |
24 | : text(mcopystr(other.text)) | |
25 | {} | |
26 | ||
27 | Mstring::Mstring(const char * s) | |
28 | : text(mcopystr(s)) | |
29 | {} | |
30 | ||
31 | Mstring::Mstring(size_t len, const char *s) | |
32 | : text((expstring_t)Malloc(len+1)) | |
33 | { | |
34 | memcpy(text, s, len); | |
35 | text[len] = 0; | |
36 | } | |
37 | ||
38 | Mstring::Mstring(char c) | |
39 | : text(memptystr()) | |
40 | { | |
41 | text = mputc(text, c); | |
42 | } | |
43 | ||
44 | Mstring::~Mstring() | |
45 | { | |
46 | Free(text); | |
47 | } | |
48 | ||
49 | bool Mstring::empty() const | |
50 | { | |
51 | return mstrlen(text) == 0; | |
52 | } | |
53 | ||
54 | size_t Mstring::size() const | |
55 | { | |
56 | return mstrlen(text); | |
57 | } | |
58 | ||
59 | void Mstring::clear() | |
60 | { | |
61 | Free(text); | |
62 | text = memptystr(); | |
63 | } | |
64 | ||
65 | const char * Mstring::c_str() const | |
66 | { | |
67 | return text; | |
68 | } | |
69 | ||
70 | void Mstring::eraseChar(size_t pos) | |
71 | { | |
72 | Mstring temp; | |
73 | for (size_t i = 0; i != size(); ++i) | |
74 | if (i != pos) temp += text[i]; | |
75 | Free(text); | |
76 | text = mcopystr(temp.text); | |
77 | } | |
78 | ||
79 | void Mstring::insertChar(size_t pos, char c) | |
80 | { | |
81 | Mstring temp; | |
82 | for (size_t i = 0; i != size(); ++i) | |
83 | if (i == pos) { | |
84 | temp += c; | |
85 | temp += text[i]; | |
86 | } | |
87 | else temp += text[i]; | |
88 | Free(text); | |
89 | text = mcopystr(temp.text); | |
90 | } | |
91 | ||
92 | bool Mstring::isFound(const Mstring & s) | |
93 | { | |
94 | return strstr(text, s.text); | |
95 | } | |
96 | ||
97 | bool Mstring::isFound(const char * s) | |
98 | { | |
99 | return strstr(text, s); | |
100 | } | |
101 | ||
102 | bool Mstring::isFound(char c) | |
103 | { | |
104 | return strchr(text, c); | |
105 | } | |
106 | ||
107 | void Mstring::setCapitalized() | |
108 | { | |
109 | text[0] = toupper(text[0]); | |
110 | } | |
111 | ||
112 | void Mstring::setUncapitalized() | |
113 | { | |
114 | text[0] = tolower(text[0]); | |
115 | } | |
116 | ||
117 | Mstring Mstring::getPrefix(const char delimiter) const | |
118 | { | |
119 | Mstring result; | |
120 | char * pos = strchr(text, delimiter); | |
121 | if (pos != NULL) for (int i = 0; text + i != pos; ++i) result += text[i]; | |
122 | return result; | |
123 | } | |
124 | ||
125 | Mstring Mstring::getValueWithoutPrefix(const char delimiter) const | |
126 | { | |
127 | char * pos = strrchr(text, delimiter); | |
128 | if (pos != NULL) return Mstring(pos+1); | |
129 | else return *this; | |
130 | } | |
131 | ||
132 | void Mstring::removeWSfromBegin() | |
133 | { | |
134 | size_t i = 0; | |
135 | size_t s = mstrlen(text); | |
136 | for ( ; i < s; ++i) | |
137 | if (!isspace((const unsigned char)text[i])) break; | |
138 | memmove(text, text+i, s-i); | |
139 | text = mtruncstr(text, s-i); | |
140 | } | |
141 | ||
142 | void Mstring::removeWSfromEnd() | |
143 | { | |
144 | int i = mstrlen(text); | |
145 | for ( ; i > 0; --i) | |
146 | if (!isspace((const unsigned char)text[i-1])) break; | |
147 | text = mtruncstr(text, i); | |
148 | } | |
149 | ||
150 | char & Mstring::operator [](size_t pos) | |
151 | { | |
152 | size_t s = mstrlen(text); | |
153 | if (pos < s) | |
154 | return text[pos]; | |
155 | else { | |
156 | fputs("String index overflow\n", stderr); | |
157 | abort(); | |
158 | } | |
159 | } | |
160 | ||
161 | const char & Mstring::operator [](size_t pos) const | |
162 | { | |
163 | size_t s = mstrlen(text); | |
164 | if (pos < s) | |
165 | return text[pos]; | |
166 | else { | |
167 | fputs("String index overflow\n", stderr); | |
168 | abort(); | |
169 | } | |
170 | } | |
171 | ||
172 | Mstring & Mstring::operator =(const Mstring & other) | |
173 | { | |
174 | if (&other != this) { | |
175 | Free(text); | |
176 | text = mcopystr(other.text); | |
177 | } | |
178 | return *this; | |
179 | } | |
180 | ||
181 | Mstring & Mstring::operator =(const char * s) | |
182 | { | |
183 | Free(text); | |
184 | text = mcopystr(s); | |
185 | return *this; | |
186 | } | |
187 | ||
188 | Mstring & Mstring::operator =(char c) | |
189 | { | |
190 | Free(text); | |
191 | text = memptystr(); | |
192 | text = mputc(text, c); | |
193 | return *this; | |
194 | } | |
195 | ||
196 | Mstring & Mstring::operator +=(const Mstring & other) | |
197 | { | |
198 | text = mputstr(text, other.text); | |
199 | return *this; | |
200 | } | |
201 | ||
202 | Mstring & Mstring::operator +=(const char * s) | |
203 | { | |
204 | text = mputstr(text, s); | |
205 | return *this; | |
206 | } | |
207 | ||
208 | Mstring & Mstring::operator +=(char c) | |
209 | { | |
210 | text = mputc(text, c); | |
211 | return *this; | |
212 | } | |
213 | ||
214 | const Mstring operator +(const Mstring & lhs, const Mstring & rhs) | |
215 | { | |
216 | return Mstring(lhs) += rhs; | |
217 | } | |
218 | ||
219 | const Mstring operator +(const char * lhs, const Mstring & rhs) | |
220 | { | |
221 | return Mstring(lhs) += rhs; | |
222 | } | |
223 | ||
224 | const Mstring operator +(char lhs, const Mstring & rhs) | |
225 | { | |
226 | return Mstring(lhs) += rhs; | |
227 | } | |
228 | ||
229 | const Mstring operator +(const Mstring & lhs, const char * rhs) | |
230 | { | |
231 | return Mstring(lhs) += rhs; | |
232 | } | |
233 | ||
234 | const Mstring operator +(const Mstring & lhs, char rhs) | |
235 | { | |
236 | return Mstring(lhs) += rhs; | |
237 | } | |
238 | ||
239 | bool operator ==(const Mstring & lhs, const Mstring & rhs) | |
240 | { | |
241 | if (strcmp(lhs.text, rhs.text) == 0) // they are equal | |
242 | return true; | |
243 | else | |
244 | return false; | |
245 | } | |
246 | ||
247 | bool operator ==(const char * lhs, const Mstring & rhs) | |
248 | { | |
249 | if (strcmp(lhs, rhs.text) == 0) // they are equal | |
250 | return true; | |
251 | else | |
252 | return false; | |
253 | } | |
254 | ||
255 | bool operator ==(const Mstring & lhs, const char * rhs) | |
256 | { | |
257 | if (strcmp(lhs.text, rhs) == 0) // they are equal | |
258 | return true; | |
259 | else | |
260 | return false; | |
261 | } | |
262 | ||
263 | bool operator !=(const Mstring & lhs, const Mstring & rhs) | |
264 | { | |
265 | if (strcmp(lhs.text, rhs.text) != 0) // they are NOT equal | |
266 | return true; | |
267 | else | |
268 | return false; | |
269 | } | |
270 | ||
271 | bool operator !=(const char * lhs, const Mstring & rhs) | |
272 | { | |
273 | if (strcmp(lhs, rhs.text) != 0) // they are NOT equal | |
274 | return true; | |
275 | else | |
276 | return false; | |
277 | } | |
278 | ||
279 | bool operator !=(const Mstring & lhs, const char * rhs) | |
280 | { | |
281 | if (strcmp(lhs.text, rhs) != 0) // they are NOT equal | |
282 | return true; | |
283 | else | |
284 | return false; | |
285 | } | |
286 | ||
287 | bool operator <(const Mstring & lhs, const Mstring & rhs) | |
288 | { | |
289 | if (strcmp(lhs.text, rhs.text) < 0) | |
290 | return true; | |
291 | else | |
292 | return false; | |
293 | } | |
294 | ||
295 | bool operator <(const char * lhs, const Mstring & rhs) | |
296 | { | |
297 | if (strcmp(lhs, rhs.text) < 0) | |
298 | return true; | |
299 | else | |
300 | return false; | |
301 | } | |
302 | ||
303 | bool operator <(const Mstring & lhs, const char * rhs) | |
304 | { | |
305 | if (strcmp(lhs.text, rhs) < 0) | |
306 | return true; | |
307 | else | |
308 | return false; | |
309 | } | |
310 | ||
311 | bool operator >(const Mstring & lhs, const Mstring & rhs) | |
312 | { | |
313 | if (strcmp(lhs.text, rhs.text) > 0) | |
314 | return true; | |
315 | else | |
316 | return false; | |
317 | } | |
318 | ||
319 | bool operator >(const char * lhs, const Mstring & rhs) | |
320 | { | |
321 | if (strcmp(lhs, rhs.text) > 0) | |
322 | return true; | |
323 | else | |
324 | return false; | |
325 | } | |
326 | ||
327 | bool operator >(const Mstring & lhs, const char * rhs) | |
328 | { | |
329 | if (strcmp(lhs.text, rhs) > 0) | |
330 | return true; | |
331 | else | |
332 | return false; | |
333 | } | |
334 | ||
335 | bool operator <=(const Mstring & lhs, const Mstring & rhs) | |
336 | { | |
337 | if (strcmp(lhs.text, rhs.text) <= 0) | |
338 | return true; | |
339 | else | |
340 | return false; | |
341 | } | |
342 | ||
343 | bool operator <=(const char * lhs, const Mstring & rhs) | |
344 | { | |
345 | if (strcmp(lhs, rhs.text) <= 0) | |
346 | return true; | |
347 | else | |
348 | return false; | |
349 | } | |
350 | ||
351 | bool operator <=(const Mstring & lhs, const char * rhs) | |
352 | { | |
353 | if (strcmp(lhs.text, rhs) <= 0) | |
354 | return true; | |
355 | else | |
356 | return false; | |
357 | } | |
358 | ||
359 | bool operator >=(const Mstring & lhs, const Mstring & rhs) | |
360 | { | |
361 | if (strcmp(lhs.text, rhs.text) >= 0) | |
362 | return true; | |
363 | else | |
364 | return false; | |
365 | } | |
366 | ||
367 | bool operator >=(const char * lhs, const Mstring & rhs) | |
368 | { | |
369 | if (strcmp(lhs, rhs.text) >= 0) | |
370 | return true; | |
371 | else | |
372 | return false; | |
373 | } | |
374 | ||
375 | bool operator >=(const Mstring & lhs, const char * rhs) | |
376 | { | |
377 | if (strcmp(lhs.text, rhs) >= 0) | |
378 | return true; | |
379 | else | |
380 | return false; | |
381 | } |