From fdf58e887b5191fba4ffa704195feb931868bbb1 Mon Sep 17 00:00:00 2001 From: Ben S Date: Sat, 24 May 2014 01:14:40 +0100 Subject: [PATCH] Upgrade to latest Rust (s/~str/StrBuf/) See https://github.com/mozilla/rust/pull/14310 --- colours.rs | 20 +++++++++++--------- exa.rs | 4 ++-- file.rs | 20 ++++++++++---------- format.rs | 6 +++--- unix.rs | 4 ++-- 5 files changed, 28 insertions(+), 26 deletions(-) diff --git a/colours.rs b/colours.rs index 8ba9e65..15a3f23 100644 --- a/colours.rs +++ b/colours.rs @@ -16,19 +16,20 @@ pub struct StyleStruct { } impl Style { - pub fn paint(&self, input: ~str) -> ~str { + pub fn paint(&self, input: &str) -> StrBuf { match *self { - Plain => input, + Plain => input.to_strbuf(), Foreground(c) => c.paint(input), Style(s) => match s { StyleStruct { foreground, background, bold, underline } => { - let bg: ~str = match background { + let bg = match background { Some(c) => format!("{};", c as int + 10), - None => "".to_owned(), + None => "".to_strbuf() }; let bo = if bold { "1;" } else { "" }; let un = if underline { "4;" } else { "" }; - format!("\x1B[{}{}{}{}m{}\x1B[0m", bo, un, bg, foreground as int, input) + let re = format!("\x1B[{}{}{}{}m{}\x1B[0m", bo, un, bg, foreground as int, input.to_strbuf()); + return re.to_owned(); } } } @@ -62,8 +63,9 @@ impl Style { } impl Colour { - pub fn paint(&self, input: &str) -> ~str { - format!("\x1B[{}m{}\x1B[0m", *self as int, input) + pub fn paint(&self, input: &str) -> StrBuf { + let re = format!("\x1B[{}m{}\x1B[0m", *self as int, input); + return re.to_owned(); } pub fn underline(&self) -> Style { @@ -83,7 +85,7 @@ impl Colour { } } -pub fn strip_formatting(input: &~str) -> ~str { +pub fn strip_formatting(input: &StrBuf) -> StrBuf { let re = regex!("\x1B\\[.+?m"); - re.replace_all(*input, "").to_owned() + re.replace_all(input.as_slice(), "").to_owned() } diff --git a/exa.rs b/exa.rs index 9dda3f6..f4374d2 100644 --- a/exa.rs +++ b/exa.rs @@ -58,7 +58,7 @@ fn list(opts: Options, path: Path) { let columns = defaultColumns(); - let table: Vec> = files.iter() + let table: Vec> = files.iter() .map(|p| File::from_path(p)) .filter(|f| !f.is_dotfile() || opts.showInvisibles ) .map(|f| columns.iter().map(|c| f.display(c)).collect()) @@ -76,7 +76,7 @@ fn list(opts: Options, path: Path) { } else { print!(" "); } - print!("{}", cell); + print!("{}", cell.as_slice()); for _ in range(cell.len(), *length) { print!(" "); } diff --git a/file.rs b/file.rs index 12ea552..0e0fc4c 100644 --- a/file.rs +++ b/file.rs @@ -35,17 +35,17 @@ impl<'a> File<'a> { self.name.starts_with(".") } - pub fn display(&self, column: &Column) -> ~str { + pub fn display(&self, column: &Column) -> StrBuf { match *column { Permissions => self.permissions(), - FileName => self.file_colour().paint(self.name.to_owned()), + FileName => self.file_colour().paint(self.name.as_slice()), FileSize(si) => self.file_size(si), User => get_user_name(self.stat.unstable.uid as i32).unwrap_or(self.stat.unstable.uid.to_str()), Group => get_group_name(self.stat.unstable.gid as u32).unwrap_or(self.stat.unstable.gid.to_str()), } } - fn file_size(&self, si: bool) -> ~str { + fn file_size(&self, si: bool) -> StrBuf { let sizeStr = if si { formatBinaryBytes(self.stat.size) } else { @@ -56,12 +56,12 @@ impl<'a> File<'a> { Green.normal() } else { Green.bold() - }.paint(sizeStr); + }.paint(sizeStr.as_slice()); } - fn type_char(&self) -> ~str { + fn type_char(&self) -> StrBuf { return match self.stat.kind { - io::TypeFile => ".".to_owned(), + io::TypeFile => ".".to_strbuf(), io::TypeDirectory => Blue.paint("d"), io::TypeNamedPipe => Yellow.paint("|"), io::TypeBlockSpecial => Purple.paint("s"), @@ -83,7 +83,7 @@ impl<'a> File<'a> { } } - fn permissions(&self) -> ~str { + fn permissions(&self) -> StrBuf { let bits = self.stat.perm; return format!("{}{}{}{}{}{}{}{}{}{}", self.type_char(), @@ -100,10 +100,10 @@ impl<'a> File<'a> { } } -fn bit(bits: io::FilePermission, bit: io::FilePermission, other: &'static str, style: Style) -> ~str { +fn bit(bits: io::FilePermission, bit: io::FilePermission, other: &'static str, style: Style) -> StrBuf { if bits.contains(bit) { - style.paint(other.to_owned()) + style.paint(other.as_slice()) } else { - Black.bold().paint("-".to_owned()) + Black.bold().paint("-".as_slice()) } } diff --git a/format.rs b/format.rs index 6099bcf..0c921ac 100644 --- a/format.rs +++ b/format.rs @@ -1,4 +1,4 @@ -fn formatBytes(mut amount: u64, kilo: u64, prefixes: ~[&str]) -> ~str { +fn formatBytes(mut amount: u64, kilo: u64, prefixes: ~[&str]) -> StrBuf { let mut prefix = 0; while amount > kilo { amount /= kilo; @@ -7,10 +7,10 @@ fn formatBytes(mut amount: u64, kilo: u64, prefixes: ~[&str]) -> ~str { return format!("{:4}{}", amount, prefixes[prefix]); } -pub fn formatBinaryBytes(amount: u64) -> ~str { +pub fn formatBinaryBytes(amount: u64) -> StrBuf { formatBytes(amount, 1024, ~[ "B ", "KiB", "MiB", "GiB", "TiB" ]) } -pub fn formatDecimalBytes(amount: u64) -> ~str { +pub fn formatDecimalBytes(amount: u64) -> StrBuf { formatBytes(amount, 1000, ~[ "B ", "KB", "MB", "GB", "TB" ]) } diff --git a/unix.rs b/unix.rs index 3ae28e6..5ce9d8d 100644 --- a/unix.rs +++ b/unix.rs @@ -34,7 +34,7 @@ mod c { } } -pub fn get_user_name(uid: i32) -> Option<~str> { +pub fn get_user_name(uid: i32) -> Option { let pw = unsafe { c::getpwuid(uid) }; if pw.is_not_null() { return unsafe { Some(from_c_str(read(pw).pw_name)) }; @@ -44,7 +44,7 @@ pub fn get_user_name(uid: i32) -> Option<~str> { } } -pub fn get_group_name(gid: u32) -> Option<~str> { +pub fn get_group_name(gid: u32) -> Option { let gr = unsafe { c::getgrgid(gid) }; if gr.is_not_null() { return unsafe { Some(from_c_str(read(gr).gr_name)) };