Skip to content

Commit 001d7bf

Browse files
committed
Add _str.starts_with and ends_with.
1 parent 546f1e9 commit 001d7bf

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/lib/_str.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,35 @@ fn find(str haystack, str needle) -> int {
188188
ret -1;
189189
}
190190

191+
fn starts_with(str haystack, str needle) -> bool {
192+
let uint haystack_len = byte_len(haystack);
193+
let uint needle_len = byte_len(needle);
194+
if (needle_len == 0u) {
195+
ret true;
196+
}
197+
if (needle_len > haystack_len) {
198+
ret false;
199+
}
200+
ret eq(substr(haystack, 0u, needle_len), needle);
201+
}
202+
203+
204+
fn ends_with(str haystack, str needle) -> bool {
205+
let uint haystack_len = byte_len(haystack);
206+
let uint needle_len = byte_len(needle);
207+
if (needle_len == 0u) {
208+
ret true;
209+
}
210+
if (needle_len > haystack_len) {
211+
ret false;
212+
}
213+
ret eq(substr(haystack,
214+
haystack_len - needle_len,
215+
needle_len),
216+
needle);
217+
}
218+
219+
191220
fn substr(str s, uint begin, uint len) -> str {
192221
let str accum = "";
193222
let uint i = begin;

0 commit comments

Comments
 (0)