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 |
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 |
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 |
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 |
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; |
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); |