diff options
| author | Huon Wilson <dbau.pp+github@gmail.com> | 2013-06-11 12:05:42 +1000 |
|---|---|---|
| committer | Huon Wilson <dbau.pp+github@gmail.com> | 2013-06-12 12:21:03 +1000 |
| commit | 8786bca7e2978e6c1d6eed7e61680b0f25db1f18 (patch) | |
| tree | 275bea77da23c3e7a6872a5786f619d00370397e /src | |
| parent | 3c23a0a836164ed3ac1b94b526ff8f4e71571e8e (diff) | |
| download | rust-8786bca7e2978e6c1d6eed7e61680b0f25db1f18.tar.gz rust-8786bca7e2978e6c1d6eed7e61680b0f25db1f18.zip | |
std: convert str::repeat to a method.
Diffstat (limited to 'src')
| -rw-r--r-- | src/libextra/getopts.rs | 6 | ||||
| -rw-r--r-- | src/librust/rust.rc | 2 | ||||
| -rw-r--r-- | src/libstd/str.rs | 58 |
3 files changed, 34 insertions, 32 deletions
diff --git a/src/libextra/getopts.rs b/src/libextra/getopts.rs index 9fe9ad64010..9fe81804bd2 100644 --- a/src/libextra/getopts.rs +++ b/src/libextra/getopts.rs @@ -593,7 +593,7 @@ pub mod groups { */ 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 OptGroup{short_name: short_name, @@ -603,7 +603,7 @@ pub mod groups { hasarg: hasarg, _} = copy *optref; - let mut row = str::repeat(" ", 4); + let mut row = " ".repeat(4); // short option row += match short_name.len() { @@ -629,7 +629,7 @@ pub mod groups { // here we just need to indent the start of the description let rowlen = row.len(); row += if rowlen < 24 { - str::repeat(" ", 24 - rowlen) + " ".repeat(24 - rowlen) } else { copy desc_sep }; diff --git a/src/librust/rust.rc b/src/librust/rust.rc index 5bc490937b6..21f944d2af1 100644 --- a/src/librust/rust.rc +++ b/src/librust/rust.rc @@ -225,7 +225,7 @@ fn usage() { ); 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", command.cmd, padding, command.usage_line)); } diff --git a/src/libstd/str.rs b/src/libstd/str.rs index e8145b37114..3929356723d 100644 --- a/src/libstd/str.rs +++ b/src/libstd/str.rs @@ -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 */ @@ -1567,6 +1544,8 @@ pub trait StrSlice<'self> { fn find<C: CharEq>(&self, search: C) -> Option<uint>; fn rfind<C: CharEq>(&self, search: C) -> Option<uint>; fn find_str(&self, &str) -> Option<uint>; + + fn repeat(&self, nn: uint) -> ~str; } /// Extension methods for strings @@ -2083,6 +2062,29 @@ impl<'self> StrSlice<'self> for &'self str { .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)] @@ -2541,11 +2543,11 @@ mod tests { #[test] fn test_repeat() { - assert_eq!(repeat("x", 4), ~"xxxx"); - assert_eq!(repeat("hi", 4), ~"hihihihi"); - assert_eq!(repeat("ไท华", 3), ~"ไท华ไท华ไท华"); - assert_eq!(repeat("", 4), ~""); - assert_eq!(repeat("hi", 0), ~""); + assert_eq!("x".repeat(4), ~"xxxx"); + assert_eq!("hi".repeat(4), ~"hihihihi"); + assert_eq!("ไท华".repeat(3), ~"ไท华ไท华ไท华"); + assert_eq!("".repeat(4), ~""); + assert_eq!("hi".repeat(0), ~""); } #[test] |
