sources/mystring.cpp

changeset 129
a556ce001e26
parent 110
cad1163333b9
child 137
485cb6d6b98c
child 131
4996c8684b93
equal deleted inserted replaced
128:e5d185b62b7f 129:a556ce001e26
171 return result; 171 return result;
172 } 172 }
173 173
174 // ------------------------------------------------------------------------------------------------- 174 // -------------------------------------------------------------------------------------------------
175 // 175 //
176 String String::mid (long a, long b) const 176 // Returns a substring from [a, b)
177 { 177 //
178 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)
179 b = length(); 184 b = length();
180 185
181 if (b <= a) 186 if (b <= a)
182 return ""; 187 return "";
183 188 else
184 if (a == 0 and b == length()) 189 return m_string.substr(a, b - a);
185 return *this;
186
187 char* newstr = new char[b - a + 1];
188 strncpy (newstr, chars() + a, b - a);
189 newstr[b - a] = '\0';
190 String other (newstr);
191 delete[] newstr;
192 return other;
193 } 190 }
194 191
195 // ------------------------------------------------------------------------------------------------- 192 // -------------------------------------------------------------------------------------------------
196 // 193 //
197 String String::right(int length) const 194 String String::right(int length) const

mercurial