Skip to content

Add a module to getopts for verbose option group declaration (and use it in rustc) #3739

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 17, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions src/libcore/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,13 @@ pub pure fn connect(v: &[~str], sep: &str) -> ~str {
move s
}

/// Given a string, make a new string with repeated copies of it
pub fn repeat(ss: &str, nn: uint) -> ~str {
let mut acc = ~"";
for nn.times { acc += ss; }
return acc;
}

/*
Section: Adding to and removing from a string
*/
Expand Down Expand Up @@ -573,6 +580,40 @@ pub pure fn words(s: &str) -> ~[~str] {
split_nonempty(s, |c| char::is_whitespace(c))
}

/** Split a string into a vector of substrings,
* each of which is less than a limit
*/
pub fn split_within(ss: &str, lim: uint) -> ~[~str] {
let words = str::words(ss);

// empty?
if words == ~[] { return ~[]; }

let mut rows : ~[~str] = ~[];
let mut row : ~str = ~"";

for words.each |wptr| {
let word = *wptr;

// if adding this word to the row would go over the limit,
// then start a new row
if str::len(row) + str::len(word) + 1 > lim {
rows += [row]; // save previous row
row = word; // start a new one
} else {
if str::len(row) > 0 { row += ~" " } // separate words
row += word; // append to this row
}
}

// save the last row
if row != ~"" { rows += [row]; }

return rows;
}



/// Convert a string to lowercase. ASCII only
pub pure fn to_lower(s: &str) -> ~str {
map(s,
Expand Down Expand Up @@ -2465,6 +2506,18 @@ mod tests {
assert ~[] == words(~"");
}

#[test]
fn test_split_within() {
assert split_within(~"", 0) == ~[];
assert split_within(~"", 15) == ~[];
assert split_within(~"hello", 15) == ~[~"hello"];

let data = ~"\nMary had a little lamb\nLittle lamb\n";
assert split_within(data, 15) == ~[~"Mary had a little",
~"lamb Little",
~"lamb"];
}

#[test]
fn test_find_str() {
// byte positions
Expand Down Expand Up @@ -2540,6 +2593,15 @@ mod tests {
t(~[~"hi"], ~" ", ~"hi");
}

#[test]
fn test_repeat() {
assert repeat(~"x", 4) == ~"xxxx";
assert repeat(~"hi", 4) == ~"hihihihi";
assert repeat(~"ไท华", 3) == ~"ไท华ไท华ไท华";
assert repeat(~"", 4) == ~"";
assert repeat(~"hi", 0) == ~"";
}

#[test]
fn test_to_upper() {
// libc::toupper, and hence str::to_upper
Expand Down
Loading