From 6415d06616a5203c76d9885b11b42fa95a639b41 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Mon, 12 May 2014 22:24:20 -0700 Subject: [PATCH] libtime: Remove all uses of `~str` from `libtime` --- src/libtime/lib.rs | 528 +++++++++++++++++++++++---------------------- 1 file changed, 267 insertions(+), 261 deletions(-) diff --git a/src/libtime/lib.rs b/src/libtime/lib.rs index 3cfc611f34c..1ffe4d3cbad 100644 --- a/src/libtime/lib.rs +++ b/src/libtime/lib.rs @@ -315,10 +315,10 @@ pub fn to_utc(&self) -> Tm { * Return a string of the current time in the form * "Thu Jan 1 00:00:00 1970". */ - pub fn ctime(&self) -> ~str { self.strftime("%c") } + pub fn ctime(&self) -> StrBuf { self.strftime("%c") } /// Formats the time according to the format string. - pub fn strftime(&self, format: &str) -> ~str { + pub fn strftime(&self, format: &str) -> StrBuf { strftime(format, self) } @@ -328,7 +328,7 @@ pub fn strftime(&self, format: &str) -> ~str { * local: "Thu, 22 Mar 2012 07:53:18 PST" * utc: "Thu, 22 Mar 2012 14:53:18 UTC" */ - pub fn rfc822(&self) -> ~str { + pub fn rfc822(&self) -> StrBuf { if self.tm_gmtoff == 0_i32 { self.strftime("%a, %d %b %Y %T GMT") } else { @@ -342,7 +342,7 @@ pub fn rfc822(&self) -> ~str { * local: "Thu, 22 Mar 2012 07:53:18 -0700" * utc: "Thu, 22 Mar 2012 14:53:18 -0000" */ - pub fn rfc822z(&self) -> ~str { + pub fn rfc822z(&self) -> StrBuf { self.strftime("%a, %d %b %Y %T %z") } @@ -352,7 +352,7 @@ pub fn rfc822z(&self) -> ~str { * local: "2012-02-22T07:53:18-07:00" * utc: "2012-02-22T14:53:18Z" */ - pub fn rfc3339(&self) -> ~str { + pub fn rfc3339(&self) -> StrBuf { if self.tm_gmtoff == 0_i32 { self.strftime("%Y-%m-%dT%H:%M:%SZ") } else { @@ -361,13 +361,13 @@ pub fn rfc3339(&self) -> ~str { let mut m = num::abs(self.tm_gmtoff) / 60_i32; let h = m / 60_i32; m -= h * 60_i32; - s + format!("{}{:02d}:{:02d}", sign, h as int, m as int) + format_strbuf!("{}{}{:02d}:{:02d}", s, sign, h as int, m as int) } } } /// Parses the time from the string according to the format string. -pub fn strptime(s: &str, format: &str) -> Result { +pub fn strptime(s: &str, format: &str) -> Result { fn match_str(s: &str, pos: uint, needle: &str) -> bool { let mut i = pos; for ch in needle.bytes() { @@ -379,14 +379,14 @@ fn match_str(s: &str, pos: uint, needle: &str) -> bool { return true; } - fn match_strs(ss: &str, pos: uint, strs: &[(~str, i32)]) + fn match_strs(ss: &str, pos: uint, strs: &[(StrBuf, i32)]) -> Option<(i32, uint)> { let mut i = 0u; let len = strs.len(); while i < len { match strs[i] { // can't use let due to let-pattern bugs (ref needle, value) => { - if match_str(ss, pos, *needle) { + if match_str(ss, pos, needle.as_slice()) { return Some((value, pos + needle.len())); } } @@ -461,78 +461,78 @@ fn match_digits_in_range(ss: &str, pos: uint, digits: uint, ws: bool, } } - fn parse_char(s: &str, pos: uint, c: char) -> Result { + fn parse_char(s: &str, pos: uint, c: char) -> Result { let range = s.char_range_at(pos); if c == range.ch { Ok(range.next) } else { - Err(format!("Expected {}, found {}", + Err(format_strbuf!("Expected {}, found {}", str::from_char(c), str::from_char(range.ch))) } } fn parse_type(s: &str, pos: uint, ch: char, tm: &mut Tm) - -> Result { + -> Result { match ch { 'A' => match match_strs(s, pos, [ - ("Sunday".to_owned(), 0_i32), - ("Monday".to_owned(), 1_i32), - ("Tuesday".to_owned(), 2_i32), - ("Wednesday".to_owned(), 3_i32), - ("Thursday".to_owned(), 4_i32), - ("Friday".to_owned(), 5_i32), - ("Saturday".to_owned(), 6_i32) + ("Sunday".to_strbuf(), 0_i32), + ("Monday".to_strbuf(), 1_i32), + ("Tuesday".to_strbuf(), 2_i32), + ("Wednesday".to_strbuf(), 3_i32), + ("Thursday".to_strbuf(), 4_i32), + ("Friday".to_strbuf(), 5_i32), + ("Saturday".to_strbuf(), 6_i32) ]) { Some(item) => { let (v, pos) = item; tm.tm_wday = v; Ok(pos) } - None => Err("Invalid day".to_owned()) + None => Err("Invalid day".to_strbuf()) }, 'a' => match match_strs(s, pos, [ - ("Sun".to_owned(), 0_i32), - ("Mon".to_owned(), 1_i32), - ("Tue".to_owned(), 2_i32), - ("Wed".to_owned(), 3_i32), - ("Thu".to_owned(), 4_i32), - ("Fri".to_owned(), 5_i32), - ("Sat".to_owned(), 6_i32) + ("Sun".to_strbuf(), 0_i32), + ("Mon".to_strbuf(), 1_i32), + ("Tue".to_strbuf(), 2_i32), + ("Wed".to_strbuf(), 3_i32), + ("Thu".to_strbuf(), 4_i32), + ("Fri".to_strbuf(), 5_i32), + ("Sat".to_strbuf(), 6_i32) ]) { Some(item) => { let (v, pos) = item; tm.tm_wday = v; Ok(pos) } - None => Err("Invalid day".to_owned()) + None => Err("Invalid day".to_strbuf()) }, 'B' => match match_strs(s, pos, [ - ("January".to_owned(), 0_i32), - ("February".to_owned(), 1_i32), - ("March".to_owned(), 2_i32), - ("April".to_owned(), 3_i32), - ("May".to_owned(), 4_i32), - ("June".to_owned(), 5_i32), - ("July".to_owned(), 6_i32), - ("August".to_owned(), 7_i32), - ("September".to_owned(), 8_i32), - ("October".to_owned(), 9_i32), - ("November".to_owned(), 10_i32), - ("December".to_owned(), 11_i32) + ("January".to_strbuf(), 0_i32), + ("February".to_strbuf(), 1_i32), + ("March".to_strbuf(), 2_i32), + ("April".to_strbuf(), 3_i32), + ("May".to_strbuf(), 4_i32), + ("June".to_strbuf(), 5_i32), + ("July".to_strbuf(), 6_i32), + ("August".to_strbuf(), 7_i32), + ("September".to_strbuf(), 8_i32), + ("October".to_strbuf(), 9_i32), + ("November".to_strbuf(), 10_i32), + ("December".to_strbuf(), 11_i32) ]) { Some(item) => { let (v, pos) = item; tm.tm_mon = v; Ok(pos) } - None => Err("Invalid month".to_owned()) + None => Err("Invalid month".to_strbuf()) }, 'b' | 'h' => match match_strs(s, pos, [ - ("Jan".to_owned(), 0_i32), - ("Feb".to_owned(), 1_i32), - ("Mar".to_owned(), 2_i32), - ("Apr".to_owned(), 3_i32), - ("May".to_owned(), 4_i32), - ("Jun".to_owned(), 5_i32), - ("Jul".to_owned(), 6_i32), - ("Aug".to_owned(), 7_i32), - ("Sep".to_owned(), 8_i32), - ("Oct".to_owned(), 9_i32), - ("Nov".to_owned(), 10_i32), - ("Dec".to_owned(), 11_i32) + ("Jan".to_strbuf(), 0_i32), + ("Feb".to_strbuf(), 1_i32), + ("Mar".to_strbuf(), 2_i32), + ("Apr".to_strbuf(), 3_i32), + ("May".to_strbuf(), 4_i32), + ("Jun".to_strbuf(), 5_i32), + ("Jul".to_strbuf(), 6_i32), + ("Aug".to_strbuf(), 7_i32), + ("Sep".to_strbuf(), 8_i32), + ("Oct".to_strbuf(), 9_i32), + ("Nov".to_strbuf(), 10_i32), + ("Dec".to_strbuf(), 11_i32) ]) { Some(item) => { let (v, pos) = item; tm.tm_mon = v; Ok(pos) } - None => Err("Invalid month".to_owned()) + None => Err("Invalid month".to_strbuf()) }, 'C' => match match_digits_in_range(s, pos, 2u, false, 0_i32, 99_i32) { @@ -541,7 +541,7 @@ fn parse_type(s: &str, pos: uint, ch: char, tm: &mut Tm) tm.tm_year += (v * 100_i32) - 1900_i32; Ok(pos) } - None => Err("Invalid year".to_owned()) + None => Err("Invalid year".to_strbuf()) }, 'c' => { parse_type(s, pos, 'a', &mut *tm) @@ -564,12 +564,12 @@ fn parse_type(s: &str, pos: uint, ch: char, tm: &mut Tm) 'd' => match match_digits_in_range(s, pos, 2u, false, 1_i32, 31_i32) { Some(item) => { let (v, pos) = item; tm.tm_mday = v; Ok(pos) } - None => Err("Invalid day of the month".to_owned()) + None => Err("Invalid day of the month".to_strbuf()) }, 'e' => match match_digits_in_range(s, pos, 2u, true, 1_i32, 31_i32) { Some(item) => { let (v, pos) = item; tm.tm_mday = v; Ok(pos) } - None => Err("Invalid day of the month".to_owned()) + None => Err("Invalid day of the month".to_strbuf()) }, 'f' => { let (val, pos) = match_fractional_seconds(s, pos); @@ -586,7 +586,7 @@ fn parse_type(s: &str, pos: uint, ch: char, tm: &mut Tm) 'H' => { match match_digits_in_range(s, pos, 2u, false, 0_i32, 23_i32) { Some(item) => { let (v, pos) = item; tm.tm_hour = v; Ok(pos) } - None => Err("Invalid hour".to_owned()) + None => Err("Invalid hour".to_strbuf()) } } 'I' => { @@ -596,7 +596,7 @@ fn parse_type(s: &str, pos: uint, ch: char, tm: &mut Tm) tm.tm_hour = if v == 12_i32 { 0_i32 } else { v }; Ok(pos) } - None => Err("Invalid hour".to_owned()) + None => Err("Invalid hour".to_strbuf()) } } 'j' => { @@ -606,13 +606,13 @@ fn parse_type(s: &str, pos: uint, ch: char, tm: &mut Tm) tm.tm_yday = v - 1_i32; Ok(pos) } - None => Err("Invalid day of year".to_owned()) + None => Err("Invalid day of year".to_strbuf()) } } 'k' => { match match_digits_in_range(s, pos, 2u, true, 0_i32, 23_i32) { Some(item) => { let (v, pos) = item; tm.tm_hour = v; Ok(pos) } - None => Err("Invalid hour".to_owned()) + None => Err("Invalid hour".to_strbuf()) } } 'l' => { @@ -622,13 +622,13 @@ fn parse_type(s: &str, pos: uint, ch: char, tm: &mut Tm) tm.tm_hour = if v == 12_i32 { 0_i32 } else { v }; Ok(pos) } - None => Err("Invalid hour".to_owned()) + None => Err("Invalid hour".to_strbuf()) } } 'M' => { match match_digits_in_range(s, pos, 2u, false, 0_i32, 59_i32) { Some(item) => { let (v, pos) = item; tm.tm_min = v; Ok(pos) } - None => Err("Invalid minute".to_owned()) + None => Err("Invalid minute".to_strbuf()) } } 'm' => { @@ -638,21 +638,21 @@ fn parse_type(s: &str, pos: uint, ch: char, tm: &mut Tm) tm.tm_mon = v - 1_i32; Ok(pos) } - None => Err("Invalid month".to_owned()) + None => Err("Invalid month".to_strbuf()) } } 'n' => parse_char(s, pos, '\n'), 'P' => match match_strs(s, pos, - [("am".to_owned(), 0_i32), ("pm".to_owned(), 12_i32)]) { + [("am".to_strbuf(), 0_i32), ("pm".to_strbuf(), 12_i32)]) { Some(item) => { let (v, pos) = item; tm.tm_hour += v; Ok(pos) } - None => Err("Invalid hour".to_owned()) + None => Err("Invalid hour".to_strbuf()) }, 'p' => match match_strs(s, pos, - [("AM".to_owned(), 0_i32), ("PM".to_owned(), 12_i32)]) { + [("AM".to_strbuf(), 0_i32), ("PM".to_strbuf(), 12_i32)]) { Some(item) => { let (v, pos) = item; tm.tm_hour += v; Ok(pos) } - None => Err("Invalid hour".to_owned()) + None => Err("Invalid hour".to_strbuf()) }, 'R' => { parse_type(s, pos, 'H', &mut *tm) @@ -675,7 +675,7 @@ fn parse_type(s: &str, pos: uint, ch: char, tm: &mut Tm) tm.tm_sec = v; Ok(pos) } - None => Err("Invalid second".to_owned()) + None => Err("Invalid second".to_strbuf()) } } //'s' {} @@ -694,7 +694,7 @@ fn parse_type(s: &str, pos: uint, ch: char, tm: &mut Tm) tm.tm_wday = if v == 7 { 0 } else { v }; Ok(pos) } - None => Err("Invalid day of week".to_owned()) + None => Err("Invalid day of week".to_strbuf()) } } 'v' => { @@ -708,7 +708,7 @@ fn parse_type(s: &str, pos: uint, ch: char, tm: &mut Tm) 'w' => { match match_digits_in_range(s, pos, 1u, false, 0_i32, 6_i32) { Some(item) => { let (v, pos) = item; tm.tm_wday = v; Ok(pos) } - None => Err("Invalid day of week".to_owned()) + None => Err("Invalid day of week".to_strbuf()) } } 'Y' => { @@ -718,7 +718,7 @@ fn parse_type(s: &str, pos: uint, ch: char, tm: &mut Tm) tm.tm_year = v - 1900_i32; Ok(pos) } - None => Err("Invalid year".to_owned()) + None => Err("Invalid year".to_strbuf()) } } 'y' => { @@ -728,7 +728,7 @@ fn parse_type(s: &str, pos: uint, ch: char, tm: &mut Tm) tm.tm_year = v; Ok(pos) } - None => Err("Invalid year".to_owned()) + None => Err("Invalid year".to_strbuf()) } } 'Z' => { @@ -764,15 +764,16 @@ fn parse_type(s: &str, pos: uint, ch: char, tm: &mut Tm) Ok(pos) } - None => Err("Invalid zone offset".to_owned()) + None => Err("Invalid zone offset".to_strbuf()) } } else { - Err("Invalid zone offset".to_owned()) + Err("Invalid zone offset".to_strbuf()) } } '%' => parse_char(s, pos, '%'), ch => { - Err(format!("unknown formatting type: {}", str::from_char(ch))) + Err(format_strbuf!("unknown formatting type: {}", + str::from_char(ch))) } } } @@ -794,7 +795,7 @@ fn parse_type(s: &str, pos: uint, ch: char, tm: &mut Tm) }; let mut pos = 0u; let len = s.len(); - let mut result = Err("Invalid time".to_owned()); + let mut result = Err("Invalid time".to_strbuf()); while pos < len { let range = s.char_range_at(pos); @@ -843,7 +844,7 @@ fn parse_type(s: &str, pos: uint, ch: char, tm: &mut Tm) } /// Formats the time according to the format string. -pub fn strftime(format: &str, tm: &Tm) -> ~str { +pub fn strftime(format: &str, tm: &Tm) -> StrBuf { fn days_in_year(year: int) -> i32 { if (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0)) { 366 /* Days in a leap year */ @@ -871,7 +872,7 @@ fn iso_week_days(yday: i32, wday: i32) -> int { + iso_week1_wday - iso_week_start_wday } - fn iso_week(ch:char, tm: &Tm) -> ~str { + fn iso_week(ch:char, tm: &Tm) -> StrBuf { let mut year: int = tm.tm_year as int + 1900; let mut days: int = iso_week_days (tm.tm_yday, tm.tm_wday); @@ -890,69 +891,71 @@ fn iso_week(ch:char, tm: &Tm) -> ~str { } match ch { - 'G' => format!("{}", year), - 'g' => format!("{:02d}", (year % 100 + 100) % 100), - 'V' => format!("{:02d}", days / 7 + 1), - _ => "".to_owned() + 'G' => format_strbuf!("{}", year), + 'g' => format_strbuf!("{:02d}", (year % 100 + 100) % 100), + 'V' => format_strbuf!("{:02d}", days / 7 + 1), + _ => "".to_strbuf() } } - fn parse_type(ch: char, tm: &Tm) -> ~str { - let die = || format!("strftime: can't understand this format {} ", ch); + fn parse_type(ch: char, tm: &Tm) -> StrBuf { + let die = || { + format_strbuf!("strftime: can't understand this format {} ", ch) + }; match ch { 'A' => match tm.tm_wday as int { - 0 => "Sunday".to_owned(), - 1 => "Monday".to_owned(), - 2 => "Tuesday".to_owned(), - 3 => "Wednesday".to_owned(), - 4 => "Thursday".to_owned(), - 5 => "Friday".to_owned(), - 6 => "Saturday".to_owned(), + 0 => "Sunday".to_strbuf(), + 1 => "Monday".to_strbuf(), + 2 => "Tuesday".to_strbuf(), + 3 => "Wednesday".to_strbuf(), + 4 => "Thursday".to_strbuf(), + 5 => "Friday".to_strbuf(), + 6 => "Saturday".to_strbuf(), _ => die() }, 'a' => match tm.tm_wday as int { - 0 => "Sun".to_owned(), - 1 => "Mon".to_owned(), - 2 => "Tue".to_owned(), - 3 => "Wed".to_owned(), - 4 => "Thu".to_owned(), - 5 => "Fri".to_owned(), - 6 => "Sat".to_owned(), + 0 => "Sun".to_strbuf(), + 1 => "Mon".to_strbuf(), + 2 => "Tue".to_strbuf(), + 3 => "Wed".to_strbuf(), + 4 => "Thu".to_strbuf(), + 5 => "Fri".to_strbuf(), + 6 => "Sat".to_strbuf(), _ => die() }, 'B' => match tm.tm_mon as int { - 0 => "January".to_owned(), - 1 => "February".to_owned(), - 2 => "March".to_owned(), - 3 => "April".to_owned(), - 4 => "May".to_owned(), - 5 => "June".to_owned(), - 6 => "July".to_owned(), - 7 => "August".to_owned(), - 8 => "September".to_owned(), - 9 => "October".to_owned(), - 10 => "November".to_owned(), - 11 => "December".to_owned(), + 0 => "January".to_strbuf(), + 1 => "February".to_strbuf(), + 2 => "March".to_strbuf(), + 3 => "April".to_strbuf(), + 4 => "May".to_strbuf(), + 5 => "June".to_strbuf(), + 6 => "July".to_strbuf(), + 7 => "August".to_strbuf(), + 8 => "September".to_strbuf(), + 9 => "October".to_strbuf(), + 10 => "November".to_strbuf(), + 11 => "December".to_strbuf(), _ => die() }, 'b' | 'h' => match tm.tm_mon as int { - 0 => "Jan".to_owned(), - 1 => "Feb".to_owned(), - 2 => "Mar".to_owned(), - 3 => "Apr".to_owned(), - 4 => "May".to_owned(), - 5 => "Jun".to_owned(), - 6 => "Jul".to_owned(), - 7 => "Aug".to_owned(), - 8 => "Sep".to_owned(), - 9 => "Oct".to_owned(), - 10 => "Nov".to_owned(), - 11 => "Dec".to_owned(), + 0 => "Jan".to_strbuf(), + 1 => "Feb".to_strbuf(), + 2 => "Mar".to_strbuf(), + 3 => "Apr".to_strbuf(), + 4 => "May".to_strbuf(), + 5 => "Jun".to_strbuf(), + 6 => "Jul".to_strbuf(), + 7 => "Aug".to_strbuf(), + 8 => "Sep".to_strbuf(), + 9 => "Oct".to_strbuf(), + 10 => "Nov".to_strbuf(), + 11 => "Dec".to_strbuf(), _ => die() }, - 'C' => format!("{:02d}", (tm.tm_year as int + 1900) / 100), + 'C' => format_strbuf!("{:02d}", (tm.tm_year as int + 1900) / 100), 'c' => { - format!("{} {} {} {} {}", + format_strbuf!("{} {} {} {} {}", parse_type('a', tm), parse_type('b', tm), parse_type('e', tm), @@ -960,90 +963,92 @@ fn parse_type(ch: char, tm: &Tm) -> ~str { parse_type('Y', tm)) } 'D' | 'x' => { - format!("{}/{}/{}", + format_strbuf!("{}/{}/{}", parse_type('m', tm), parse_type('d', tm), parse_type('y', tm)) } - 'd' => format!("{:02d}", tm.tm_mday), - 'e' => format!("{:2d}", tm.tm_mday), - 'f' => format!("{:09d}", tm.tm_nsec), + 'd' => format_strbuf!("{:02d}", tm.tm_mday), + 'e' => format_strbuf!("{:2d}", tm.tm_mday), + 'f' => format_strbuf!("{:09d}", tm.tm_nsec), 'F' => { - format!("{}-{}-{}", + format_strbuf!("{}-{}-{}", parse_type('Y', tm), parse_type('m', tm), parse_type('d', tm)) } 'G' => iso_week('G', tm), 'g' => iso_week('g', tm), - 'H' => format!("{:02d}", tm.tm_hour), + 'H' => format_strbuf!("{:02d}", tm.tm_hour), 'I' => { let mut h = tm.tm_hour; if h == 0 { h = 12 } if h > 12 { h -= 12 } - format!("{:02d}", h) + format_strbuf!("{:02d}", h) } - 'j' => format!("{:03d}", tm.tm_yday + 1), - 'k' => format!("{:2d}", tm.tm_hour), + 'j' => format_strbuf!("{:03d}", tm.tm_yday + 1), + 'k' => format_strbuf!("{:2d}", tm.tm_hour), 'l' => { let mut h = tm.tm_hour; if h == 0 { h = 12 } if h > 12 { h -= 12 } - format!("{:2d}", h) + format_strbuf!("{:2d}", h) } - 'M' => format!("{:02d}", tm.tm_min), - 'm' => format!("{:02d}", tm.tm_mon + 1), - 'n' => "\n".to_owned(), - 'P' => if (tm.tm_hour as int) < 12 { "am".to_owned() } else { "pm".to_owned() }, - 'p' => if (tm.tm_hour as int) < 12 { "AM".to_owned() } else { "PM".to_owned() }, + 'M' => format_strbuf!("{:02d}", tm.tm_min), + 'm' => format_strbuf!("{:02d}", tm.tm_mon + 1), + 'n' => "\n".to_strbuf(), + 'P' => if (tm.tm_hour as int) < 12 { "am".to_strbuf() } else { "pm".to_strbuf() }, + 'p' => if (tm.tm_hour as int) < 12 { "AM".to_strbuf() } else { "PM".to_strbuf() }, 'R' => { - format!("{}:{}", + format_strbuf!("{}:{}", parse_type('H', tm), parse_type('M', tm)) } 'r' => { - format!("{}:{}:{} {}", + format_strbuf!("{}:{}:{} {}", parse_type('I', tm), parse_type('M', tm), parse_type('S', tm), parse_type('p', tm)) } - 'S' => format!("{:02d}", tm.tm_sec), - 's' => format!("{}", tm.to_timespec().sec), + 'S' => format_strbuf!("{:02d}", tm.tm_sec), + 's' => format_strbuf!("{}", tm.to_timespec().sec), 'T' | 'X' => { - format!("{}:{}:{}", + format_strbuf!("{}:{}:{}", parse_type('H', tm), parse_type('M', tm), parse_type('S', tm)) } - 't' => "\t".to_owned(), - 'U' => format!("{:02d}", (tm.tm_yday - tm.tm_wday + 7) / 7), + 't' => "\t".to_strbuf(), + 'U' => format_strbuf!("{:02d}", (tm.tm_yday - tm.tm_wday + 7) / 7), 'u' => { let i = tm.tm_wday as int; - (if i == 0 { 7 } else { i }).to_str() + (if i == 0 { 7 } else { i }).to_str().to_strbuf() } 'V' => iso_week('V', tm), 'v' => { - format!("{}-{}-{}", + format_strbuf!("{}-{}-{}", parse_type('e', tm), parse_type('b', tm), parse_type('Y', tm)) } - 'W' => format!("{:02d}", (tm.tm_yday - (tm.tm_wday - 1 + 7) % 7 + 7) - / 7), - 'w' => (tm.tm_wday as int).to_str(), - 'Y' => (tm.tm_year as int + 1900).to_str(), - 'y' => format!("{:02d}", (tm.tm_year as int + 1900) % 100), - 'Z' => tm.tm_zone.clone(), + 'W' => { + format_strbuf!("{:02d}", + (tm.tm_yday - (tm.tm_wday - 1 + 7) % 7 + 7) / 7) + } + 'w' => (tm.tm_wday as int).to_str().to_strbuf(), + 'Y' => (tm.tm_year as int + 1900).to_str().to_strbuf(), + 'y' => format_strbuf!("{:02d}", (tm.tm_year as int + 1900) % 100), + 'Z' => tm.tm_zone.to_strbuf(), 'z' => { let sign = if tm.tm_gmtoff > 0_i32 { '+' } else { '-' }; let mut m = num::abs(tm.tm_gmtoff) / 60_i32; let h = m / 60_i32; m -= h * 60_i32; - format!("{}{:02d}{:02d}", sign, h, m) + format_strbuf!("{}{:02d}{:02d}", sign, h, m) } '+' => tm.rfc3339(), - '%' => "%".to_owned(), + '%' => "%".to_strbuf(), _ => die() } } @@ -1067,7 +1072,7 @@ fn parse_type(ch: char, tm: &Tm) -> ~str { } } - str::from_utf8(buf.as_slice()).unwrap().to_str() + str::from_utf8(buf.as_slice()).unwrap().to_strbuf() } #[cfg(test)] @@ -1235,9 +1240,9 @@ fn test_strptime() { } let format = "%a %b %e %T.%f %Y"; - assert_eq!(strptime("", format), Err("Invalid time".to_owned())); + assert_eq!(strptime("", format), Err("Invalid time".to_strbuf())); assert!(strptime("Fri Feb 13 15:31:30", format) - == Err("Invalid time".to_owned())); + == Err("Invalid time".to_strbuf())); match strptime("Fri Feb 13 15:31:30.01234 2009", format) { Err(e) => fail!(e), @@ -1259,71 +1264,71 @@ fn test_strptime() { fn test(s: &str, format: &str) -> bool { match strptime(s, format) { - Ok(ref tm) => tm.strftime(format) == s.to_owned(), + Ok(ref tm) => tm.strftime(format) == s.to_strbuf(), Err(e) => fail!(e) } } let days = [ - "Sunday".to_owned(), - "Monday".to_owned(), - "Tuesday".to_owned(), - "Wednesday".to_owned(), - "Thursday".to_owned(), - "Friday".to_owned(), - "Saturday".to_owned() + "Sunday".to_strbuf(), + "Monday".to_strbuf(), + "Tuesday".to_strbuf(), + "Wednesday".to_strbuf(), + "Thursday".to_strbuf(), + "Friday".to_strbuf(), + "Saturday".to_strbuf() ]; for day in days.iter() { - assert!(test(*day, "%A")); + assert!(test(day.as_slice(), "%A")); } let days = [ - "Sun".to_owned(), - "Mon".to_owned(), - "Tue".to_owned(), - "Wed".to_owned(), - "Thu".to_owned(), - "Fri".to_owned(), - "Sat".to_owned() + "Sun".to_strbuf(), + "Mon".to_strbuf(), + "Tue".to_strbuf(), + "Wed".to_strbuf(), + "Thu".to_strbuf(), + "Fri".to_strbuf(), + "Sat".to_strbuf() ]; for day in days.iter() { - assert!(test(*day, "%a")); + assert!(test(day.as_slice(), "%a")); } let months = [ - "January".to_owned(), - "February".to_owned(), - "March".to_owned(), - "April".to_owned(), - "May".to_owned(), - "June".to_owned(), - "July".to_owned(), - "August".to_owned(), - "September".to_owned(), - "October".to_owned(), - "November".to_owned(), - "December".to_owned() + "January".to_strbuf(), + "February".to_strbuf(), + "March".to_strbuf(), + "April".to_strbuf(), + "May".to_strbuf(), + "June".to_strbuf(), + "July".to_strbuf(), + "August".to_strbuf(), + "September".to_strbuf(), + "October".to_strbuf(), + "November".to_strbuf(), + "December".to_strbuf() ]; for day in months.iter() { - assert!(test(*day, "%B")); + assert!(test(day.as_slice(), "%B")); } let months = [ - "Jan".to_owned(), - "Feb".to_owned(), - "Mar".to_owned(), - "Apr".to_owned(), - "May".to_owned(), - "Jun".to_owned(), - "Jul".to_owned(), - "Aug".to_owned(), - "Sep".to_owned(), - "Oct".to_owned(), - "Nov".to_owned(), - "Dec".to_owned() + "Jan".to_strbuf(), + "Feb".to_strbuf(), + "Mar".to_strbuf(), + "Apr".to_strbuf(), + "May".to_strbuf(), + "Jun".to_strbuf(), + "Jul".to_strbuf(), + "Aug".to_strbuf(), + "Sep".to_strbuf(), + "Oct".to_strbuf(), + "Nov".to_strbuf(), + "Dec".to_strbuf() ]; for day in months.iter() { - assert!(test(*day, "%b")); + assert!(test(day.as_slice(), "%b")); } assert!(test("19", "%C")); @@ -1375,7 +1380,7 @@ fn test(s: &str, format: &str) -> bool { assert!(test("%", "%%")); // Test for #7256 - assert_eq!(strptime("360", "%Y-%m-%d"), Err("Invalid year".to_owned())) + assert_eq!(strptime("360", "%Y-%m-%d"), Err("Invalid year".to_strbuf())) } fn test_ctime() { @@ -1387,8 +1392,8 @@ fn test_ctime() { debug!("test_ctime: {:?} {:?}", utc.ctime(), local.ctime()); - assert_eq!(utc.ctime(), "Fri Feb 13 23:31:30 2009".to_owned()); - assert_eq!(local.ctime(), "Fri Feb 13 15:31:30 2009".to_owned()); + assert_eq!(utc.ctime(), "Fri Feb 13 23:31:30 2009".to_strbuf()); + assert_eq!(local.ctime(), "Fri Feb 13 15:31:30 2009".to_strbuf()); } fn test_strftime() { @@ -1398,70 +1403,71 @@ fn test_strftime() { let utc = at_utc(time); let local = at(time); - assert_eq!(local.strftime(""), "".to_owned()); - assert_eq!(local.strftime("%A"), "Friday".to_owned()); - assert_eq!(local.strftime("%a"), "Fri".to_owned()); - assert_eq!(local.strftime("%B"), "February".to_owned()); - assert_eq!(local.strftime("%b"), "Feb".to_owned()); - assert_eq!(local.strftime("%C"), "20".to_owned()); - assert_eq!(local.strftime("%c"), "Fri Feb 13 15:31:30 2009".to_owned()); - assert_eq!(local.strftime("%D"), "02/13/09".to_owned()); - assert_eq!(local.strftime("%d"), "13".to_owned()); - assert_eq!(local.strftime("%e"), "13".to_owned()); - assert_eq!(local.strftime("%f"), "000054321".to_owned()); - assert_eq!(local.strftime("%F"), "2009-02-13".to_owned()); - assert_eq!(local.strftime("%G"), "2009".to_owned()); - assert_eq!(local.strftime("%g"), "09".to_owned()); - assert_eq!(local.strftime("%H"), "15".to_owned()); - assert_eq!(local.strftime("%I"), "03".to_owned()); - assert_eq!(local.strftime("%j"), "044".to_owned()); - assert_eq!(local.strftime("%k"), "15".to_owned()); - assert_eq!(local.strftime("%l"), " 3".to_owned()); - assert_eq!(local.strftime("%M"), "31".to_owned()); - assert_eq!(local.strftime("%m"), "02".to_owned()); - assert_eq!(local.strftime("%n"), "\n".to_owned()); - assert_eq!(local.strftime("%P"), "pm".to_owned()); - assert_eq!(local.strftime("%p"), "PM".to_owned()); - assert_eq!(local.strftime("%R"), "15:31".to_owned()); - assert_eq!(local.strftime("%r"), "03:31:30 PM".to_owned()); - assert_eq!(local.strftime("%S"), "30".to_owned()); - assert_eq!(local.strftime("%s"), "1234567890".to_owned()); - assert_eq!(local.strftime("%T"), "15:31:30".to_owned()); - assert_eq!(local.strftime("%t"), "\t".to_owned()); - assert_eq!(local.strftime("%U"), "06".to_owned()); - assert_eq!(local.strftime("%u"), "5".to_owned()); - assert_eq!(local.strftime("%V"), "07".to_owned()); - assert_eq!(local.strftime("%v"), "13-Feb-2009".to_owned()); - assert_eq!(local.strftime("%W"), "06".to_owned()); - assert_eq!(local.strftime("%w"), "5".to_owned()); - assert_eq!(local.strftime("%X"), "15:31:30".to_owned()); // FIXME (#2350): support locale - assert_eq!(local.strftime("%x"), "02/13/09".to_owned()); // FIXME (#2350): support locale - assert_eq!(local.strftime("%Y"), "2009".to_owned()); - assert_eq!(local.strftime("%y"), "09".to_owned()); - assert_eq!(local.strftime("%+"), "2009-02-13T15:31:30-08:00".to_owned()); + assert_eq!(local.strftime(""), "".to_strbuf()); + assert_eq!(local.strftime("%A"), "Friday".to_strbuf()); + assert_eq!(local.strftime("%a"), "Fri".to_strbuf()); + assert_eq!(local.strftime("%B"), "February".to_strbuf()); + assert_eq!(local.strftime("%b"), "Feb".to_strbuf()); + assert_eq!(local.strftime("%C"), "20".to_strbuf()); + assert_eq!(local.strftime("%c"), "Fri Feb 13 15:31:30 2009".to_strbuf()); + assert_eq!(local.strftime("%D"), "02/13/09".to_strbuf()); + assert_eq!(local.strftime("%d"), "13".to_strbuf()); + assert_eq!(local.strftime("%e"), "13".to_strbuf()); + assert_eq!(local.strftime("%f"), "000054321".to_strbuf()); + assert_eq!(local.strftime("%F"), "2009-02-13".to_strbuf()); + assert_eq!(local.strftime("%G"), "2009".to_strbuf()); + assert_eq!(local.strftime("%g"), "09".to_strbuf()); + assert_eq!(local.strftime("%H"), "15".to_strbuf()); + assert_eq!(local.strftime("%I"), "03".to_strbuf()); + assert_eq!(local.strftime("%j"), "044".to_strbuf()); + assert_eq!(local.strftime("%k"), "15".to_strbuf()); + assert_eq!(local.strftime("%l"), " 3".to_strbuf()); + assert_eq!(local.strftime("%M"), "31".to_strbuf()); + assert_eq!(local.strftime("%m"), "02".to_strbuf()); + assert_eq!(local.strftime("%n"), "\n".to_strbuf()); + assert_eq!(local.strftime("%P"), "pm".to_strbuf()); + assert_eq!(local.strftime("%p"), "PM".to_strbuf()); + assert_eq!(local.strftime("%R"), "15:31".to_strbuf()); + assert_eq!(local.strftime("%r"), "03:31:30 PM".to_strbuf()); + assert_eq!(local.strftime("%S"), "30".to_strbuf()); + assert_eq!(local.strftime("%s"), "1234567890".to_strbuf()); + assert_eq!(local.strftime("%T"), "15:31:30".to_strbuf()); + assert_eq!(local.strftime("%t"), "\t".to_strbuf()); + assert_eq!(local.strftime("%U"), "06".to_strbuf()); + assert_eq!(local.strftime("%u"), "5".to_strbuf()); + assert_eq!(local.strftime("%V"), "07".to_strbuf()); + assert_eq!(local.strftime("%v"), "13-Feb-2009".to_strbuf()); + assert_eq!(local.strftime("%W"), "06".to_strbuf()); + assert_eq!(local.strftime("%w"), "5".to_strbuf()); + assert_eq!(local.strftime("%X"), "15:31:30".to_strbuf()); // FIXME (#2350): support locale + assert_eq!(local.strftime("%x"), "02/13/09".to_strbuf()); // FIXME (#2350): support locale + assert_eq!(local.strftime("%Y"), "2009".to_strbuf()); + assert_eq!(local.strftime("%y"), "09".to_strbuf()); + assert_eq!(local.strftime("%+"), "2009-02-13T15:31:30-08:00".to_strbuf()); // FIXME (#2350): We should probably standardize on the timezone // abbreviation. let zone = local.strftime("%Z"); - assert!(zone == "PST".to_owned() || zone == "Pacific Standard Time".to_owned()); + assert!(zone == "PST".to_strbuf() || zone == "Pacific Standard Time".to_strbuf()); - assert_eq!(local.strftime("%z"), "-0800".to_owned()); - assert_eq!(local.strftime("%%"), "%".to_owned()); + assert_eq!(local.strftime("%z"), "-0800".to_strbuf()); + assert_eq!(local.strftime("%%"), "%".to_strbuf()); // FIXME (#2350): We should probably standardize on the timezone // abbreviation. let rfc822 = local.rfc822(); - let prefix = "Fri, 13 Feb 2009 15:31:30 ".to_owned(); - assert!(rfc822 == prefix + "PST" || rfc822 == prefix + "Pacific Standard Time"); - - assert_eq!(local.ctime(), "Fri Feb 13 15:31:30 2009".to_owned()); - assert_eq!(local.rfc822z(), "Fri, 13 Feb 2009 15:31:30 -0800".to_owned()); - assert_eq!(local.rfc3339(), "2009-02-13T15:31:30-08:00".to_owned()); - - assert_eq!(utc.ctime(), "Fri Feb 13 23:31:30 2009".to_owned()); - assert_eq!(utc.rfc822(), "Fri, 13 Feb 2009 23:31:30 GMT".to_owned()); - assert_eq!(utc.rfc822z(), "Fri, 13 Feb 2009 23:31:30 -0000".to_owned()); - assert_eq!(utc.rfc3339(), "2009-02-13T23:31:30Z".to_owned()); + let prefix = "Fri, 13 Feb 2009 15:31:30 ".to_strbuf(); + assert!(rfc822 == format_strbuf!("{}PST", prefix) || + rfc822 == format_strbuf!("{}Pacific Standard Time", prefix)); + + assert_eq!(local.ctime(), "Fri Feb 13 15:31:30 2009".to_strbuf()); + assert_eq!(local.rfc822z(), "Fri, 13 Feb 2009 15:31:30 -0800".to_strbuf()); + assert_eq!(local.rfc3339(), "2009-02-13T15:31:30-08:00".to_strbuf()); + + assert_eq!(utc.ctime(), "Fri Feb 13 23:31:30 2009".to_strbuf()); + assert_eq!(utc.rfc822(), "Fri, 13 Feb 2009 23:31:30 GMT".to_strbuf()); + assert_eq!(utc.rfc822z(), "Fri, 13 Feb 2009 23:31:30 -0000".to_strbuf()); + assert_eq!(utc.rfc3339(), "2009-02-13T23:31:30Z".to_strbuf()); } fn test_timespec_eq_ord() { -- 2.44.0