sources/mystring.cpp

changeset 5
146825d63b9a
parent 1
4dd5bde4e777
child 10
3874575d924d
equal deleted inserted replaced
4:ec5387e121fa 5:146825d63b9a
32 #include "main.h" 32 #include "main.h"
33 #include "mystring.h" 33 #include "mystring.h"
34 #include "format.h" 34 #include "format.h"
35 35
36 // ------------------------------------------------------------------------------------------------- 36 // -------------------------------------------------------------------------------------------------
37 37 //
38 METHOD 38 METHOD
39 String::compare (const String& other) const -> int 39 String::compare (const String& other) const -> int
40 { 40 {
41 return m_string.compare (other.std_string()); 41 return m_string.compare (other.std_string());
42 } 42 }
43 43
44 // ------------------------------------------------------------------------------------------------- 44 // -------------------------------------------------------------------------------------------------
45 45 //
46 METHOD 46 METHOD
47 String::trim (int n) -> void 47 String::trim (int n) -> void
48 { 48 {
49 if (n > 0) 49 if (n > 0)
50 m_string = mid (0, length() - n).std_string(); 50 m_string = mid (0, length() - n).std_string();
51 else 51 else
52 m_string = mid (n, -1).std_string(); 52 m_string = mid (n, -1).std_string();
53 } 53 }
54 54
55 // ------------------------------------------------------------------------------------------------- 55 // -------------------------------------------------------------------------------------------------
56 56 //
57 METHOD 57 METHOD
58 String::strip (const List<char>& unwanted) -> String 58 String::strip (const List<char>& unwanted) -> String
59 { 59 {
60 String copy (m_string); 60 String copy (m_string);
61 61
67 67
68 return copy; 68 return copy;
69 } 69 }
70 70
71 // ------------------------------------------------------------------------------------------------- 71 // -------------------------------------------------------------------------------------------------
72 72 //
73 METHOD 73 METHOD
74 String::to_uppercase() const -> String 74 String::to_uppercase() const -> String
75 { 75 {
76 String newstr (m_string); 76 String newstr (m_string);
77 77
83 83
84 return newstr; 84 return newstr;
85 } 85 }
86 86
87 // ------------------------------------------------------------------------------------------------- 87 // -------------------------------------------------------------------------------------------------
88 88 //
89 METHOD 89 METHOD
90 String::to_lowercase() const -> String 90 String::to_lowercase() const -> String
91 { 91 {
92 String newstr (m_string); 92 String newstr (m_string);
93 93
99 99
100 return newstr; 100 return newstr;
101 } 101 }
102 102
103 // ------------------------------------------------------------------------------------------------- 103 // -------------------------------------------------------------------------------------------------
104 104 //
105 METHOD 105 METHOD
106 String::split (char del) const -> StringList 106 String::split (char del) const -> StringList
107 { 107 {
108 String delimstr; 108 String delimstr;
109 delimstr += del; 109 delimstr += del;
110 return split (delimstr); 110 return split (delimstr);
111 } 111 }
112 112
113 // ------------------------------------------------------------------------------------------------- 113 // -------------------------------------------------------------------------------------------------
114 114 //
115 METHOD 115 METHOD
116 String::split (const String& del) const -> StringList 116 String::split (const String& del) const -> StringList
117 { 117 {
118 StringList res; 118 StringList res;
119 int a = 0; 119 int a = 0;
136 136
137 return res; 137 return res;
138 } 138 }
139 139
140 // ------------------------------------------------------------------------------------------------- 140 // -------------------------------------------------------------------------------------------------
141 141 //
142 METHOD 142 METHOD
143 String::replace (const char* a, const char* b) -> void 143 String::replace (const char* a, const char* b) -> void
144 { 144 {
145 long pos; 145 long pos;
146 146
147 while ((pos = find (a)) != -1) 147 while ((pos = find (a)) != -1)
148 m_string = m_string.replace (pos, strlen (a), b); 148 m_string = m_string.replace (pos, strlen (a), b);
149 } 149 }
150 150
151 // ------------------------------------------------------------------------------------------------- 151 // -------------------------------------------------------------------------------------------------
152 152 //
153 METHOD 153 METHOD
154 String::count (char needle) const -> int 154 String::count (char needle) const -> int
155 { 155 {
156 int needles = 0; 156 int needles = 0;
157 157
161 161
162 return needles; 162 return needles;
163 } 163 }
164 164
165 // ------------------------------------------------------------------------------------------------- 165 // -------------------------------------------------------------------------------------------------
166 166 //
167 METHOD 167 METHOD
168 String::mid (long a, long b) const -> String 168 String::mid (long a, long b) const -> String
169 { 169 {
170 if (b == -1 or b > length()) 170 if (b == -1 or b > length())
171 b = length(); 171 b = length();
186 delete[] newstr; 186 delete[] newstr;
187 return other; 187 return other;
188 } 188 }
189 189
190 // ------------------------------------------------------------------------------------------------- 190 // -------------------------------------------------------------------------------------------------
191 191 //
192 METHOD 192 METHOD
193 String::word_position (int n) const -> int 193 String::word_position (int n) const -> int
194 { 194 {
195 int count = 0; 195 int count = 0;
196 196
197 for (int i = 0; i < length(); ++i) 197 for (int i = 0; i < length(); ++i)
198 { 198 {
199 if (m_string[i] != ' ') 199 if (not isspace (m_string[i]) or ++count < n)
200 continue; 200 continue;
201 201
202 if (++count < n)
203 continue;
204
205 return i; 202 return i;
206 } 203 }
207 204
208 return -1; 205 return -1;
209 } 206 }
210 207
211 // ------------------------------------------------------------------------------------------------- 208 // -------------------------------------------------------------------------------------------------
212 209 //
213 METHOD 210 METHOD
214 String::find (const char* c, int a) const -> int 211 String::find (const char* c, int a) const -> int
215 { 212 {
216 int pos = m_string.find (c, a); 213 int pos = m_string.find (c, a);
217 214
220 217
221 return pos; 218 return pos;
222 } 219 }
223 220
224 // ------------------------------------------------------------------------------------------------- 221 // -------------------------------------------------------------------------------------------------
225 222 //
226 METHOD 223 METHOD
227 String::find_last (const char* c, int a) const -> int 224 String::find_last (const char* c, int a) const -> int
228 { 225 {
229 if (a == -1 or a >= length()) 226 if (a == -1 or a >= length())
230 a = length() - 1; 227 a = length() - 1;
235 232
236 return -1; 233 return -1;
237 } 234 }
238 235
239 // ------------------------------------------------------------------------------------------------- 236 // -------------------------------------------------------------------------------------------------
240 237 //
241 METHOD 238 METHOD
242 String::to_int (bool* ok, int base) const -> long 239 String::to_int (bool* ok, int base) const -> long
243 { 240 {
244 errno = 0; 241 errno = 0;
245 char* endptr; 242 char* endptr;
250 247
251 return i; 248 return i;
252 } 249 }
253 250
254 // ------------------------------------------------------------------------------------------------- 251 // -------------------------------------------------------------------------------------------------
255 252 //
256 METHOD 253 METHOD
257 String::to_float (bool* ok) const -> float 254 String::to_float (bool* ok) const -> float
258 { 255 {
259 errno = 0; 256 errno = 0;
260 char* endptr; 257 char* endptr;
265 262
266 return i; 263 return i;
267 } 264 }
268 265
269 // ------------------------------------------------------------------------------------------------- 266 // -------------------------------------------------------------------------------------------------
270 267 //
271 METHOD 268 METHOD
272 String::to_double (bool* ok) const -> double 269 String::to_double (bool* ok) const -> double
273 { 270 {
274 errno = 0; 271 errno = 0;
275 char* endptr; 272 char* endptr;
280 277
281 return i; 278 return i;
282 } 279 }
283 280
284 // ------------------------------------------------------------------------------------------------- 281 // -------------------------------------------------------------------------------------------------
285 282 //
286 METHOD 283 METHOD
287 String::operator+ (const String& data) const -> String 284 String::operator+ (const String& data) const -> String
288 { 285 {
289 String newString = *this; 286 String newString = *this;
290 newString.append (data); 287 newString.append (data);
291 return newString; 288 return newString;
292 } 289 }
293 290
294 // ------------------------------------------------------------------------------------------------- 291 // -------------------------------------------------------------------------------------------------
295 292 //
296 METHOD 293 METHOD
297 String::operator+ (const char* data) const -> String 294 String::operator+ (const char* data) const -> String
298 { 295 {
299 String newstr = *this; 296 String newstr = *this;
300 newstr.append (data); 297 newstr.append (data);
301 return newstr; 298 return newstr;
302 } 299 }
303 300
304 // ------------------------------------------------------------------------------------------------- 301 // -------------------------------------------------------------------------------------------------
305 302 //
306 METHOD 303 METHOD
307 String::is_numeric() const -> bool 304 String::is_numeric() const -> bool
308 { 305 {
309 bool gotDot = false; 306 bool gotDot = false;
310 307
331 328
332 return true; 329 return true;
333 } 330 }
334 331
335 // ------------------------------------------------------------------------------------------------- 332 // -------------------------------------------------------------------------------------------------
336 333 //
337 METHOD 334 METHOD
338 String::ends_with (const String& other) -> bool 335 String::ends_with (const String& other) -> bool
339 { 336 {
340 if (length() < other.length()) 337 if (length() < other.length())
341 return false; 338 return false;
343 const int ofs = length() - other.length(); 340 const int ofs = length() - other.length();
344 return strncmp (chars() + ofs, other.chars(), other.length()) == 0; 341 return strncmp (chars() + ofs, other.chars(), other.length()) == 0;
345 } 342 }
346 343
347 // ------------------------------------------------------------------------------------------------- 344 // -------------------------------------------------------------------------------------------------
348 345 //
349 METHOD 346 METHOD
350 String::starts_with (const String& other) -> bool 347 String::starts_with (const String& other) -> bool
351 { 348 {
352 if (length() < other.length()) 349 if (length() < other.length())
353 return false; 350 return false;
354 351
355 return strncmp (chars(), other.chars(), other.length()) == 0; 352 return strncmp (chars(), other.chars(), other.length()) == 0;
356 } 353 }
357 354
358 // ------------------------------------------------------------------------------------------------- 355 // -------------------------------------------------------------------------------------------------
359 356 //
360 METHOD 357 METHOD
361 String::sprintf (const char* fmtstr, ...) -> void 358 String::sprintf (const char* fmtstr, ...) -> void
362 { 359 {
363 char* buf; 360 char* buf;
364 int bufsize = 256; 361 int bufsize = 256;
373 m_string = buf; 370 m_string = buf;
374 delete[] buf; 371 delete[] buf;
375 } 372 }
376 373
377 // ------------------------------------------------------------------------------------------------- 374 // -------------------------------------------------------------------------------------------------
378 375 //
379 METHOD 376 METHOD
380 StringList::join (const String& delim) -> String 377 StringList::join (const String& delim) -> String
381 { 378 {
382 String result; 379 String result;
383 380
391 388
392 return result; 389 return result;
393 } 390 }
394 391
395 // ------------------------------------------------------------------------------------------------- 392 // -------------------------------------------------------------------------------------------------
396 393 //
397 METHOD 394 METHOD
398 String::mask_against (const String& pattern) const -> bool 395 String::mask_against (const String& pattern) const -> bool
399 { 396 {
400 // Elevate to uppercase for case-insensitive matching 397 // Elevate to uppercase for case-insensitive matching
401 String pattern_upper = pattern.to_uppercase(); 398 String pattern_upper = pattern.to_uppercase();
440 437
441 return true; 438 return true;
442 } 439 }
443 440
444 // ------------------------------------------------------------------------------------------------- 441 // -------------------------------------------------------------------------------------------------
445 442 //
446 METHOD 443 METHOD
447 String::from_number (int a) -> String 444 String::from_number (int a) -> String
448 { 445 {
449 char buf[32]; 446 char buf[32];
450 ::sprintf (buf, "%d", a); 447 ::sprintf (buf, "%d", a);
451 return String (buf); 448 return String (buf);
452 } 449 }
453 450
454 // ------------------------------------------------------------------------------------------------- 451 // -------------------------------------------------------------------------------------------------
455 452 //
456 METHOD 453 METHOD
457 String::from_number (long a) -> String 454 String::from_number (long a) -> String
458 { 455 {
459 char buf[32]; 456 char buf[32];
460 ::sprintf (buf, "%ld", a); 457 ::sprintf (buf, "%ld", a);
461 return String (buf); 458 return String (buf);
462 } 459 }
463 460
464 // ------------------------------------------------------------------------------------------------- 461 // -------------------------------------------------------------------------------------------------
465 462 //
466 METHOD 463 METHOD
467 String::from_number (unsigned long a) -> String 464 String::from_number (unsigned long a) -> String
468 { 465 {
469 char buf[32]; 466 char buf[32];
470 ::sprintf (buf, "%lu", a); 467 ::sprintf (buf, "%lu", a);
471 return String (buf); 468 return String (buf);
472 } 469 }
473 470
474 // ------------------------------------------------------------------------------------------------- 471 // -------------------------------------------------------------------------------------------------
475 472 //
476 METHOD 473 METHOD
477 String::from_number (double a) -> String 474 String::from_number (double a) -> String
478 { 475 {
479 char buf[64]; 476 char buf[64];
480 ::sprintf (buf, "%f", a); 477 ::sprintf (buf, "%f", a);

mercurial