Skip to content

Commit 573056b

Browse files
committed
---
yaml --- r: 4315 b: refs/heads/master c: 1ad68ea h: refs/heads/master i: 4313: 3f3ac72 4311: 54772c9 v: v3
1 parent d826a08 commit 573056b

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: 598e25e09194f3ff89d413d9bb2a4246ce5cfeee
2+
refs/heads/master: 1ad68eafd2505e39d76c0082470e37ab8869ed48

trunk/src/lib/str.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ export is_empty;
5454
export is_not_empty;
5555
export replace;
5656
export char_slice;
57+
export trim_left;
58+
export trim_right;
59+
export trim;
5760

5861
native "rust" mod rustrt {
5962
type sbuf;
@@ -524,6 +527,42 @@ fn char_slice(s: &str, begin: uint, end: uint) -> str {
524527
from_chars(vec::slice(to_chars(s), begin, end))
525528
}
526529

530+
fn trim_left(s: &str) -> str {
531+
fn count_whities(s: &vec[char]) -> uint {
532+
let i = 0u;
533+
while i < vec::len(s) {
534+
if !char::is_whitespace(s.(i)) {
535+
break;
536+
}
537+
i += 1u;
538+
}
539+
ret i;
540+
}
541+
let chars = to_chars(s);
542+
let whities = count_whities(chars);
543+
ret from_chars(vec::slice(chars, whities, vec::len(chars)));
544+
}
545+
546+
fn trim_right(s: &str) -> str {
547+
fn count_whities(s: &vec[char]) -> uint {
548+
let i = vec::len(s);
549+
while 0u < i {
550+
if !char::is_whitespace(s.(i - 1u)) {
551+
break;
552+
}
553+
i -= 1u;
554+
}
555+
ret i;
556+
}
557+
let chars = to_chars(s);
558+
let whities = count_whities(chars);
559+
ret from_chars(vec::slice(chars, 0u, whities));
560+
}
561+
562+
fn trim(s: &str) -> str {
563+
trim_left(trim_right(s))
564+
}
565+
527566
// Local Variables:
528567
// mode: rust;
529568
// fill-column: 78;

trunk/src/test/stdtest/str.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,36 @@ fn test_char_slice() {
162162
assert (str::eq("\u65e5", str::char_slice("\u65e5\u672c", 0u, 1u)));
163163
}
164164

165+
#[test]
166+
fn trim_left() {
167+
assert str::trim_left("") == "";
168+
assert str::trim_left("a") == "a";
169+
assert str::trim_left(" ") == "";
170+
assert str::trim_left(" blah") == "blah";
171+
assert str::trim_left(" \u3000 wut") == "wut";
172+
assert str::trim_left("hey ") == "hey ";
173+
}
174+
175+
#[test]
176+
fn trim_right() {
177+
assert str::trim_right("") == "";
178+
assert str::trim_right("a") == "a";
179+
assert str::trim_right(" ") == "";
180+
assert str::trim_right("blah ") == "blah";
181+
assert str::trim_right("wut \u3000 ") == "wut";
182+
assert str::trim_right(" hey") == " hey";
183+
}
184+
185+
#[test]
186+
fn trim() {
187+
assert str::trim("") == "";
188+
assert str::trim("a") == "a";
189+
assert str::trim(" ") == "";
190+
assert str::trim(" blah ") == "blah";
191+
assert str::trim("\nwut \u3000 ") == "wut";
192+
assert str::trim(" hey dude ") == "hey dude";
193+
}
194+
165195
// Local Variables:
166196
// mode: rust;
167197
// fill-column: 78;

0 commit comments

Comments
 (0)