sources/mystring.cpp

branch
protocol5
changeset 106
7b156b764d11
parent 103
b78c0ca832a9
parent 105
b4466472aecd
child 108
5900be70c619
equal deleted inserted replaced
104:a76af67a3a4b 106:7b156b764d11
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
159 { 165 {
160 if (b == -1 or b > length()) 166 if (b == -1 or b > length())
161 b = length(); 167 b = length();
162 168
163 if (b == a) 169 if (b <= a)
164 return ""; 170 return "";
165
166 if (b < a)
167 swap (a, b);
168 171
169 if (a == 0 and b == length()) 172 if (a == 0 and b == length())
170 return *this; 173 return *this;
171 174
172 char* newstr = new char[b - a + 1]; 175 char* newstr = new char[b - a + 1];
177 return other; 180 return other;
178 } 181 }
179 182
180 // ------------------------------------------------------------------------------------------------- 183 // -------------------------------------------------------------------------------------------------
181 // 184 //
185 String String::right(int length) const
186 {
187 if (length >= this->length())
188 return *this;
189 else
190 return String(chars() + this->length() - length);
191 }
192
193 // -------------------------------------------------------------------------------------------------
194 //
182 int String::word_position (int n) const 195 int String::word_position (int n) const
183 { 196 {
184 int count = 0; 197 int count = 0;
185 198
186 for (int i = 0; i < length(); ++i) 199 for (int i = 0; i < length(); ++i)
238 // 251 //
239 float String::to_float (bool* ok) const 252 float String::to_float (bool* ok) const
240 { 253 {
241 errno = 0; 254 errno = 0;
242 char* endptr; 255 char* endptr;
243 float i = strtof (chars(), &endptr); 256 float i = (float) strtod (chars(), &endptr);
244 257
245 if (ok != nullptr) 258 if (ok != nullptr)
246 *ok = (errno == 0 and *endptr == '\0'); 259 *ok = (errno == 0 and *endptr == '\0');
247 260
248 return i; 261 return i;
343 // 356 //
344 String StringList::join (const String& delim) 357 String StringList::join (const String& delim)
345 { 358 {
346 String result; 359 String result;
347 360
348 for (const String& it : container()) 361 for (int i = 0; i < size(); ++i)
349 { 362 {
350 if (result.is_empty() == false) 363 if (result.is_empty() == false)
351 result += delim; 364 result += delim;
352 365
353 result += it; 366 result += container()[i];
354 } 367 }
355 368
356 return result; 369 return result;
357 } 370 }
358 371
502 { 515 {
503 String result = *this; 516 String result = *this;
504 result.normalize(filter); 517 result.normalize(filter);
505 return result; 518 return result;
506 } 519 }
520
521 END_ZFC_NAMESPACE

mercurial