Skip to content

Commit 8786bca

Browse files
committed
std: convert str::repeat to a method.
1 parent 3c23a0a commit 8786bca

File tree

3 files changed

+34
-32
lines changed

3 files changed

+34
-32
lines changed

src/libextra/getopts.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ pub mod groups {
593593
*/
594594
pub fn usage(brief: &str, opts: &[OptGroup]) -> ~str {
595595

596-
let desc_sep = ~"\n" + str::repeat(" ", 24);
596+
let desc_sep = ~"\n" + " ".repeat(24);
597597

598598
let rows = vec::map(opts, |optref| {
599599
let OptGroup{short_name: short_name,
@@ -603,7 +603,7 @@ pub mod groups {
603603
hasarg: hasarg,
604604
_} = copy *optref;
605605

606-
let mut row = str::repeat(" ", 4);
606+
let mut row = " ".repeat(4);
607607

608608
// short option
609609
row += match short_name.len() {
@@ -629,7 +629,7 @@ pub mod groups {
629629
// here we just need to indent the start of the description
630630
let rowlen = row.len();
631631
row += if rowlen < 24 {
632-
str::repeat(" ", 24 - rowlen)
632+
" ".repeat(24 - rowlen)
633633
} else {
634634
copy desc_sep
635635
};

src/librust/rust.rc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ fn usage() {
225225
);
226226

227227
for commands.each |command| {
228-
let padding = str::repeat(" ", indent - command.cmd.len());
228+
let padding = " ".repeat(indent - command.cmd.len());
229229
io::println(fmt!(" %s%s%s",
230230
command.cmd, padding, command.usage_line));
231231
}

src/libstd/str.rs

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -304,29 +304,6 @@ impl<'self> StrVector for &'self [&'self str] {
304304
}
305305
}
306306

307-
/// Given a string, make a new string with repeated copies of it
308-
pub fn repeat(ss: &str, nn: uint) -> ~str {
309-
do as_buf(ss) |buf, len| {
310-
let mut ret = ~"";
311-
// ignore the NULL terminator
312-
let len = len - 1;
313-
ret.reserve(nn * len);
314-
315-
unsafe {
316-
do as_buf(ret) |rbuf, _len| {
317-
let mut rbuf = ::cast::transmute_mut_unsafe(rbuf);
318-
319-
for nn.times {
320-
ptr::copy_memory(rbuf, buf, len);
321-
rbuf = rbuf.offset(len);
322-
}
323-
}
324-
raw::set_len(&mut ret, nn * len);
325-
}
326-
ret
327-
}
328-
}
329-
330307
/*
331308
Section: Adding to and removing from a string
332309
*/
@@ -1567,6 +1544,8 @@ pub trait StrSlice<'self> {
15671544
fn find<C: CharEq>(&self, search: C) -> Option<uint>;
15681545
fn rfind<C: CharEq>(&self, search: C) -> Option<uint>;
15691546
fn find_str(&self, &str) -> Option<uint>;
1547+
1548+
fn repeat(&self, nn: uint) -> ~str;
15701549
}
15711550
15721551
/// Extension methods for strings
@@ -2083,6 +2062,29 @@ impl<'self> StrSlice<'self> for &'self str {
20832062
.map_consume(|(start, _end)| start)
20842063
}
20852064
}
2065+
2066+
/// Given a string, make a new string with repeated copies of it.
2067+
fn repeat(&self, nn: uint) -> ~str {
2068+
do as_buf(*self) |buf, len| {
2069+
let mut ret = ~"";
2070+
// ignore the NULL terminator
2071+
let len = len - 1;
2072+
ret.reserve(nn * len);
2073+
2074+
unsafe {
2075+
do as_buf(ret) |rbuf, _len| {
2076+
let mut rbuf = ::cast::transmute_mut_unsafe(rbuf);
2077+
2078+
for nn.times {
2079+
ptr::copy_memory(rbuf, buf, len);
2080+
rbuf = rbuf.offset(len);
2081+
}
2082+
}
2083+
raw::set_len(&mut ret, nn * len);
2084+
}
2085+
ret
2086+
}
2087+
}
20862088
}
20872089
20882090
#[allow(missing_doc)]
@@ -2541,11 +2543,11 @@ mod tests {
25412543
25422544
#[test]
25432545
fn test_repeat() {
2544-
assert_eq!(repeat("x", 4), ~"xxxx");
2545-
assert_eq!(repeat("hi", 4), ~"hihihihi");
2546-
assert_eq!(repeat("ไท华", 3), ~"ไท华ไท华ไท华");
2547-
assert_eq!(repeat("", 4), ~"");
2548-
assert_eq!(repeat("hi", 0), ~"");
2546+
assert_eq!("x".repeat(4), ~"xxxx");
2547+
assert_eq!("hi".repeat(4), ~"hihihihi");
2548+
assert_eq!("ไท华".repeat(3), ~"ไท华ไท华ไท华");
2549+
assert_eq!("".repeat(4), ~"");
2550+
assert_eq!("hi".repeat(0), ~"");
25492551
}
25502552
25512553
#[test]

0 commit comments

Comments
 (0)