sources/mystring.cpp

branch
protocol5
changeset 131
4996c8684b93
parent 108
5900be70c619
parent 129
a556ce001e26
child 141
d9073c13dc98
equal deleted inserted replaced
130:9f54db6f9922 131:4996c8684b93
1 /* 1 /*
2 Copyright 2014, 2015 Teemu Piippo 2 Copyright 2014 - 2016 Teemu Piippo
3 All rights reserved. 3 All rights reserved.
4 4
5 Redistribution and use in source and binary forms, with or without 5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions 6 modification, are permitted provided that the following conditions
7 are met: 7 are met:
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 String String::strip (const List<char>& unwanted) 57 String String::strip (char unwanted) const
58 {
59 String result (m_string);
60
61 for (int pos = 0; (pos = result.find (unwanted)) != -1;)
62 result.remove_at (pos--);
63
64 return result;
65 }
66
67 // -------------------------------------------------------------------------------------------------
68 //
69 String String::strip (const List<char>& unwanted) const
58 { 70 {
59 String result (m_string); 71 String result (m_string);
60 72
61 for (int i = 0; i < unwanted.size(); ++i) 73 for (int i = 0; i < unwanted.size(); ++i)
62 { 74 {
159 return result; 171 return result;
160 } 172 }
161 173
162 // ------------------------------------------------------------------------------------------------- 174 // -------------------------------------------------------------------------------------------------
163 // 175 //
164 String String::mid (long a, long b) const 176 // Returns a substring from [a, b)
165 { 177 //
166 if (b == -1 or b > length()) 178 String String::mid (int a, int b) const
179 {
180 a = max(a, 0);
181 b = min(b, length());
182
183 if (b == -1)
167 b = length(); 184 b = length();
168 185
169 if (b <= a) 186 if (b <= a)
170 return ""; 187 return "";
171 188 else
172 if (a == 0 and b == length()) 189 return m_string.substr(a, b - a);
173 return *this;
174
175 char* newstr = new char[b - a + 1];
176 strncpy (newstr, chars() + a, b - a);
177 newstr[b - a] = '\0';
178 String other (newstr);
179 delete[] newstr;
180 return other;
181 } 190 }
182 191
183 // ------------------------------------------------------------------------------------------------- 192 // -------------------------------------------------------------------------------------------------
184 // 193 //
185 String String::right(int length) const 194 String String::right(int length) const
210 // ------------------------------------------------------------------------------------------------- 219 // -------------------------------------------------------------------------------------------------
211 // 220 //
212 int String::find (const char* c, int a) const 221 int String::find (const char* c, int a) const
213 { 222 {
214 int pos = m_string.find (c, a); 223 int pos = m_string.find (c, a);
224
225 if (pos == int (std::string::npos))
226 return -1;
227
228 return pos;
229 }
230
231 // -------------------------------------------------------------------------------------------------
232 //
233 int String::find (char ch, int a) const
234 {
235 int pos = m_string.find (ch, a);
215 236
216 if (pos == int (std::string::npos)) 237 if (pos == int (std::string::npos))
217 return -1; 238 return -1;
218 239
219 return pos; 240 return pos;

mercurial