Skip to content

Commit 33adb7a

Browse files
committed
Merge pull request #3739 from killerswan/usagemsg
Add a module to getopts for verbose option group declaration (and use it in rustc)
2 parents bbc90b6 + 32baf1c commit 33adb7a

File tree

4 files changed

+546
-68
lines changed

4 files changed

+546
-68
lines changed

src/libcore/str.rs

+62
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,13 @@ pub pure fn connect(v: &[~str], sep: &str) -> ~str {
203203
move s
204204
}
205205

206+
/// Given a string, make a new string with repeated copies of it
207+
pub fn repeat(ss: &str, nn: uint) -> ~str {
208+
let mut acc = ~"";
209+
for nn.times { acc += ss; }
210+
return acc;
211+
}
212+
206213
/*
207214
Section: Adding to and removing from a string
208215
*/
@@ -573,6 +580,40 @@ pub pure fn words(s: &str) -> ~[~str] {
573580
split_nonempty(s, |c| char::is_whitespace(c))
574581
}
575582

583+
/** Split a string into a vector of substrings,
584+
* each of which is less than a limit
585+
*/
586+
pub fn split_within(ss: &str, lim: uint) -> ~[~str] {
587+
let words = str::words(ss);
588+
589+
// empty?
590+
if words == ~[] { return ~[]; }
591+
592+
let mut rows : ~[~str] = ~[];
593+
let mut row : ~str = ~"";
594+
595+
for words.each |wptr| {
596+
let word = *wptr;
597+
598+
// if adding this word to the row would go over the limit,
599+
// then start a new row
600+
if str::len(row) + str::len(word) + 1 > lim {
601+
rows += [row]; // save previous row
602+
row = word; // start a new one
603+
} else {
604+
if str::len(row) > 0 { row += ~" " } // separate words
605+
row += word; // append to this row
606+
}
607+
}
608+
609+
// save the last row
610+
if row != ~"" { rows += [row]; }
611+
612+
return rows;
613+
}
614+
615+
616+
576617
/// Convert a string to lowercase. ASCII only
577618
pub pure fn to_lower(s: &str) -> ~str {
578619
map(s,
@@ -2479,6 +2520,18 @@ mod tests {
24792520
assert ~[] == words(~"");
24802521
}
24812522

2523+
#[test]
2524+
fn test_split_within() {
2525+
assert split_within(~"", 0) == ~[];
2526+
assert split_within(~"", 15) == ~[];
2527+
assert split_within(~"hello", 15) == ~[~"hello"];
2528+
2529+
let data = ~"\nMary had a little lamb\nLittle lamb\n";
2530+
assert split_within(data, 15) == ~[~"Mary had a little",
2531+
~"lamb Little",
2532+
~"lamb"];
2533+
}
2534+
24822535
#[test]
24832536
fn test_find_str() {
24842537
// byte positions
@@ -2554,6 +2607,15 @@ mod tests {
25542607
t(~[~"hi"], ~" ", ~"hi");
25552608
}
25562609

2610+
#[test]
2611+
fn test_repeat() {
2612+
assert repeat(~"x", 4) == ~"xxxx";
2613+
assert repeat(~"hi", 4) == ~"hihihihi";
2614+
assert repeat(~"ไท华", 3) == ~"ไท华ไท华ไท华";
2615+
assert repeat(~"", 4) == ~"";
2616+
assert repeat(~"hi", 0) == ~"";
2617+
}
2618+
25572619
#[test]
25582620
fn test_to_upper() {
25592621
// libc::toupper, and hence str::to_upper

0 commit comments

Comments
 (0)