Skip to content

Commit 32baf1c

Browse files
committed
Add a module to getopts to support verbose option definition
This is built on top of the existing functionality, but adds a `groups` module which defines functions allowing the user to specify whole short/long/description groups at once and provides a usage message.
1 parent 57b4d10 commit 32baf1c

File tree

4 files changed

+546
-68
lines changed

4 files changed

+546
-68
lines changed

src/libcore/str.rs

Lines changed: 62 additions & 0 deletions
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,
@@ -2465,6 +2506,18 @@ mod tests {
24652506
assert ~[] == words(~"");
24662507
}
24672508

2509+
#[test]
2510+
fn test_split_within() {
2511+
assert split_within(~"", 0) == ~[];
2512+
assert split_within(~"", 15) == ~[];
2513+
assert split_within(~"hello", 15) == ~[~"hello"];
2514+
2515+
let data = ~"\nMary had a little lamb\nLittle lamb\n";
2516+
assert split_within(data, 15) == ~[~"Mary had a little",
2517+
~"lamb Little",
2518+
~"lamb"];
2519+
}
2520+
24682521
#[test]
24692522
fn test_find_str() {
24702523
// byte positions
@@ -2540,6 +2593,15 @@ mod tests {
25402593
t(~[~"hi"], ~" ", ~"hi");
25412594
}
25422595

2596+
#[test]
2597+
fn test_repeat() {
2598+
assert repeat(~"x", 4) == ~"xxxx";
2599+
assert repeat(~"hi", 4) == ~"hihihihi";
2600+
assert repeat(~"ไท华", 3) == ~"ไท华ไท华ไท华";
2601+
assert repeat(~"", 4) == ~"";
2602+
assert repeat(~"hi", 0) == ~"";
2603+
}
2604+
25432605
#[test]
25442606
fn test_to_upper() {
25452607
// libc::toupper, and hence str::to_upper

0 commit comments

Comments
 (0)