mirror of
https://github.com/rust-lang/rust.git
synced 2024-12-28 00:15:23 +00:00
std: convert str::repeat to a method.
This commit is contained in:
parent
3c23a0a836
commit
8786bca7e2
@ -593,7 +593,7 @@ pub mod groups {
|
|||||||
*/
|
*/
|
||||||
pub fn usage(brief: &str, opts: &[OptGroup]) -> ~str {
|
pub fn usage(brief: &str, opts: &[OptGroup]) -> ~str {
|
||||||
|
|
||||||
let desc_sep = ~"\n" + str::repeat(" ", 24);
|
let desc_sep = ~"\n" + " ".repeat(24);
|
||||||
|
|
||||||
let rows = vec::map(opts, |optref| {
|
let rows = vec::map(opts, |optref| {
|
||||||
let OptGroup{short_name: short_name,
|
let OptGroup{short_name: short_name,
|
||||||
@ -603,7 +603,7 @@ pub mod groups {
|
|||||||
hasarg: hasarg,
|
hasarg: hasarg,
|
||||||
_} = copy *optref;
|
_} = copy *optref;
|
||||||
|
|
||||||
let mut row = str::repeat(" ", 4);
|
let mut row = " ".repeat(4);
|
||||||
|
|
||||||
// short option
|
// short option
|
||||||
row += match short_name.len() {
|
row += match short_name.len() {
|
||||||
@ -629,7 +629,7 @@ pub mod groups {
|
|||||||
// here we just need to indent the start of the description
|
// here we just need to indent the start of the description
|
||||||
let rowlen = row.len();
|
let rowlen = row.len();
|
||||||
row += if rowlen < 24 {
|
row += if rowlen < 24 {
|
||||||
str::repeat(" ", 24 - rowlen)
|
" ".repeat(24 - rowlen)
|
||||||
} else {
|
} else {
|
||||||
copy desc_sep
|
copy desc_sep
|
||||||
};
|
};
|
||||||
|
@ -225,7 +225,7 @@ fn usage() {
|
|||||||
);
|
);
|
||||||
|
|
||||||
for commands.each |command| {
|
for commands.each |command| {
|
||||||
let padding = str::repeat(" ", indent - command.cmd.len());
|
let padding = " ".repeat(indent - command.cmd.len());
|
||||||
io::println(fmt!(" %s%s%s",
|
io::println(fmt!(" %s%s%s",
|
||||||
command.cmd, padding, command.usage_line));
|
command.cmd, padding, command.usage_line));
|
||||||
}
|
}
|
||||||
|
@ -304,29 +304,6 @@ impl<'self> StrVector for &'self [&'self str] {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Given a string, make a new string with repeated copies of it
|
|
||||||
pub fn repeat(ss: &str, nn: uint) -> ~str {
|
|
||||||
do as_buf(ss) |buf, len| {
|
|
||||||
let mut ret = ~"";
|
|
||||||
// ignore the NULL terminator
|
|
||||||
let len = len - 1;
|
|
||||||
ret.reserve(nn * len);
|
|
||||||
|
|
||||||
unsafe {
|
|
||||||
do as_buf(ret) |rbuf, _len| {
|
|
||||||
let mut rbuf = ::cast::transmute_mut_unsafe(rbuf);
|
|
||||||
|
|
||||||
for nn.times {
|
|
||||||
ptr::copy_memory(rbuf, buf, len);
|
|
||||||
rbuf = rbuf.offset(len);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
raw::set_len(&mut ret, nn * len);
|
|
||||||
}
|
|
||||||
ret
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Section: Adding to and removing from a string
|
Section: Adding to and removing from a string
|
||||||
*/
|
*/
|
||||||
@ -1567,6 +1544,8 @@ pub trait StrSlice<'self> {
|
|||||||
fn find<C: CharEq>(&self, search: C) -> Option<uint>;
|
fn find<C: CharEq>(&self, search: C) -> Option<uint>;
|
||||||
fn rfind<C: CharEq>(&self, search: C) -> Option<uint>;
|
fn rfind<C: CharEq>(&self, search: C) -> Option<uint>;
|
||||||
fn find_str(&self, &str) -> Option<uint>;
|
fn find_str(&self, &str) -> Option<uint>;
|
||||||
|
|
||||||
|
fn repeat(&self, nn: uint) -> ~str;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Extension methods for strings
|
/// Extension methods for strings
|
||||||
@ -2083,6 +2062,29 @@ impl<'self> StrSlice<'self> for &'self str {
|
|||||||
.map_consume(|(start, _end)| start)
|
.map_consume(|(start, _end)| start)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Given a string, make a new string with repeated copies of it.
|
||||||
|
fn repeat(&self, nn: uint) -> ~str {
|
||||||
|
do as_buf(*self) |buf, len| {
|
||||||
|
let mut ret = ~"";
|
||||||
|
// ignore the NULL terminator
|
||||||
|
let len = len - 1;
|
||||||
|
ret.reserve(nn * len);
|
||||||
|
|
||||||
|
unsafe {
|
||||||
|
do as_buf(ret) |rbuf, _len| {
|
||||||
|
let mut rbuf = ::cast::transmute_mut_unsafe(rbuf);
|
||||||
|
|
||||||
|
for nn.times {
|
||||||
|
ptr::copy_memory(rbuf, buf, len);
|
||||||
|
rbuf = rbuf.offset(len);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
raw::set_len(&mut ret, nn * len);
|
||||||
|
}
|
||||||
|
ret
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(missing_doc)]
|
#[allow(missing_doc)]
|
||||||
@ -2541,11 +2543,11 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_repeat() {
|
fn test_repeat() {
|
||||||
assert_eq!(repeat("x", 4), ~"xxxx");
|
assert_eq!("x".repeat(4), ~"xxxx");
|
||||||
assert_eq!(repeat("hi", 4), ~"hihihihi");
|
assert_eq!("hi".repeat(4), ~"hihihihi");
|
||||||
assert_eq!(repeat("ไท华", 3), ~"ไท华ไท华ไท华");
|
assert_eq!("ไท华".repeat(3), ~"ไท华ไท华ไท华");
|
||||||
assert_eq!(repeat("", 4), ~"");
|
assert_eq!("".repeat(4), ~"");
|
||||||
assert_eq!(repeat("hi", 0), ~"");
|
assert_eq!("hi".repeat(0), ~"");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
Loading…
Reference in New Issue
Block a user