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