sources/mystring.cpp

changeset 88
08ccaf26cffd
parent 83
08bfc3d9d2ae
child 105
b4466472aecd
equal deleted inserted replaced
87:53c2aecb9704 88:08ccaf26cffd
31 #include <cstring> 31 #include <cstring>
32 #include "main.h" 32 #include "main.h"
33 #include "mystring.h" 33 #include "mystring.h"
34 #include "md5.h" 34 #include "md5.h"
35 35
36 BEGIN_ZFC_NAMESPACE
37
36 // ------------------------------------------------------------------------------------------------- 38 // -------------------------------------------------------------------------------------------------
37 // 39 //
38 int String::compare (const String& other) const 40 int String::compare (const String& other) const
39 { 41 {
40 return m_string.compare (other.std_string()); 42 return m_string.compare (other.std_string());
52 54
53 // ------------------------------------------------------------------------------------------------- 55 // -------------------------------------------------------------------------------------------------
54 // 56 //
55 String String::strip (const List<char>& unwanted) 57 String String::strip (const List<char>& unwanted)
56 { 58 {
57 String copy (m_string); 59 String result (m_string);
58 60
59 for (char c : unwanted) 61 for (int i = 0; i < unwanted.size(); ++i)
60 { 62 {
61 for (int pos = 0; (pos = copy.find (String (c))) != -1;) 63 String c = unwanted[i];
62 copy.remove_at (pos--); 64
63 } 65 for (int pos = 0; (pos = result.find (c)) != -1;)
64 66 result.remove_at (pos--);
65 return copy; 67 }
68
69 return result;
66 } 70 }
67 71
68 // ------------------------------------------------------------------------------------------------- 72 // -------------------------------------------------------------------------------------------------
69 // 73 //
70 String String::to_uppercase() const 74 String String::to_uppercase() const
71 { 75 {
72 String newstr (m_string); 76 String result (m_string);
73 77
74 for (char& c : newstr) 78 for (int i = 0; i < result.length(); ++i)
75 { 79 {
76 if (c >= 'a' and c <= 'z') 80 if (islower (result[i]))
77 c -= 'a' - 'A'; 81 result[i] -= 'a' - 'A';
78 } 82 }
79 83
80 return newstr; 84 return result;
81 } 85 }
82 86
83 // ------------------------------------------------------------------------------------------------- 87 // -------------------------------------------------------------------------------------------------
84 // 88 //
85 String String::to_lowercase() const 89 String String::to_lowercase() const
86 { 90 {
87 String newstr (m_string); 91 String result (m_string);
88 92
89 for (char& c : newstr) 93 for (int i = 0; i < result.length(); ++i)
90 { 94 {
91 if (c >= 'A' and c <= 'Z') 95 if (isupper (result[i]))
92 c += 'a' - 'A'; 96 result[i] += 'a' - 'A';
93 } 97 }
94 98
95 return newstr; 99 return result;
96 } 100 }
97 101
98 // ------------------------------------------------------------------------------------------------- 102 // -------------------------------------------------------------------------------------------------
99 // 103 //
100 StringList String::split (char del) const 104 StringList String::split (char del) const
142 146
143 // ------------------------------------------------------------------------------------------------- 147 // -------------------------------------------------------------------------------------------------
144 // 148 //
145 int String::count (char needle) const 149 int String::count (char needle) const
146 { 150 {
147 int needles = 0; 151 int result = 0;
148 152
149 for (const char & c : m_string) 153 for (int i = 0; i < length(); ++i)
150 if (c == needle) 154 {
151 needles++; 155 if (m_string[i] == needle)
152 156 result++;
153 return needles; 157 }
158
159 return result;
154 } 160 }
155 161
156 // ------------------------------------------------------------------------------------------------- 162 // -------------------------------------------------------------------------------------------------
157 // 163 //
158 String String::mid (long a, long b) const 164 String String::mid (long a, long b) const
162 168
163 if (b == a) 169 if (b == a)
164 return ""; 170 return "";
165 171
166 if (b < a) 172 if (b < a)
167 swap (a, b); 173 std::swap (a, b);
168 174
169 if (a == 0 and b == length()) 175 if (a == 0 and b == length())
170 return *this; 176 return *this;
171 177
172 char* newstr = new char[b - a + 1]; 178 char* newstr = new char[b - a + 1];
238 // 244 //
239 float String::to_float (bool* ok) const 245 float String::to_float (bool* ok) const
240 { 246 {
241 errno = 0; 247 errno = 0;
242 char* endptr; 248 char* endptr;
243 float i = strtof (chars(), &endptr); 249 float i = (float) strtod (chars(), &endptr);
244 250
245 if (ok != nullptr) 251 if (ok != nullptr)
246 *ok = (errno == 0 and *endptr == '\0'); 252 *ok = (errno == 0 and *endptr == '\0');
247 253
248 return i; 254 return i;
343 // 349 //
344 String StringList::join (const String& delim) 350 String StringList::join (const String& delim)
345 { 351 {
346 String result; 352 String result;
347 353
348 for (const String& it : container()) 354 for (int i = 0; i < size(); ++i)
349 { 355 {
350 if (result.is_empty() == false) 356 if (result.is_empty() == false)
351 result += delim; 357 result += delim;
352 358
353 result += it; 359 result += container()[i];
354 } 360 }
355 361
356 return result; 362 return result;
357 } 363 }
358 364
493 if (a == b) 499 if (a == b)
494 m_string = ""; 500 m_string = "";
495 else if (a != 0 or b != length() - 1) 501 else if (a != 0 or b != length() - 1)
496 m_string = m_string.substr (a, b - a + 1); 502 m_string = m_string.substr (a, b - a + 1);
497 } 503 }
504
505 END_ZFC_NAMESPACE

mercurial