]> git.lizzy.rs Git - rust.git/commitdiff
std: convert str::repeat to a method.
authorHuon Wilson <dbau.pp+github@gmail.com>
Tue, 11 Jun 2013 02:05:42 +0000 (12:05 +1000)
committerHuon Wilson <dbau.pp+github@gmail.com>
Wed, 12 Jun 2013 02:21:03 +0000 (12:21 +1000)
src/libextra/getopts.rs
src/librust/rust.rc
src/libstd/str.rs

index 9fe9ad6401045b646e931a6f831689f0a2eb2301..9fe81804bd2f4a2e912b77fd395630ce48d4234d 100644 (file)
@@ -593,7 +593,7 @@ pub fn getopts(args: &[~str], opts: &[OptGroup]) -> ::getopts::Result {
      */
     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 fn usage(brief: &str, opts: &[OptGroup]) -> ~str {
                          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 fn usage(brief: &str, opts: &[OptGroup]) -> ~str {
             // 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
             };
index 5bc490937b615742db434dadf9579e36583368bf..21f944d2af18d0b8cbc45937a75f3b88aff4d22a 100644 (file)
@@ -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));
     }
index e8145b37114ef06965e58bcf661142d3ac8d1dcf..3929356723d2fbffab7b08b7b0d6bbfa0ac7f0c7 100644 (file)
@@ -304,29 +304,6 @@ pub fn connect(&self, sep: &str) -> ~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 @@ fn split_options_iter<Sep: CharEq>(&self, sep: Sep, count: uint, allow_trailing_
     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 @@ fn find_str(&self, needle: &str) -> Option<uint> {
                 .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 @@ fn t(v: &[&str], sep: &str, s: &str) {
 
     #[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]