sources/mystring.cpp

changeset 105
b4466472aecd
parent 88
08ccaf26cffd
child 106
7b156b764d11
child 107
ca10837a2a9e
equal deleted inserted replaced
102:3492f8f0ee7e 105:b4466472aecd
164 String String::mid (long a, long b) const 164 String String::mid (long a, long b) const
165 { 165 {
166 if (b == -1 or b > length()) 166 if (b == -1 or b > length())
167 b = length(); 167 b = length();
168 168
169 if (b == a) 169 if (b <= a)
170 return ""; 170 return "";
171
172 if (b < a)
173 std::swap (a, b);
174 171
175 if (a == 0 and b == length()) 172 if (a == 0 and b == length())
176 return *this; 173 return *this;
177 174
178 char* newstr = new char[b - a + 1]; 175 char* newstr = new char[b - a + 1];
179 strncpy (newstr, chars() + a, b - a); 176 strncpy (newstr, chars() + a, b - a);
180 newstr[b - a] = '\0'; 177 newstr[b - a] = '\0';
181 String other (newstr); 178 String other (newstr);
182 delete[] newstr; 179 delete[] newstr;
183 return other; 180 return other;
181 }
182
183 // -------------------------------------------------------------------------------------------------
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);
184 } 191 }
185 192
186 // ------------------------------------------------------------------------------------------------- 193 // -------------------------------------------------------------------------------------------------
187 // 194 //
188 int String::word_position (int n) const 195 int String::word_position (int n) const

mercurial