Skip to content

Overhaul to_str_common and char::escape_* #7465

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 30, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/libextra/terminfo/parm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
//! Parameterized string expansion

use std::{char, vec, util};
use std::num::strconv::{SignNone,SignNeg,SignAll,DigAll,to_str_bytes_common};
use std::num::strconv::{SignNone,SignNeg,SignAll,int_to_str_bytes_common};
use std::iterator::IteratorUtil;

#[deriving(Eq)]
Expand Down Expand Up @@ -469,14 +469,20 @@ priv fn format(val: Param, op: FormatOp, flags: Flags) -> Result<~[u8],~str> {
FormatHex|FormatHEX => 16,
FormatString => util::unreachable()
};
let (s,_) = match op {
let mut s = ~[];
match op {
FormatDigit => {
let sign = if flags.sign { SignAll } else { SignNeg };
to_str_bytes_common(&d, radix, false, sign, DigAll)
do int_to_str_bytes_common(d, radix, sign) |c| {
s.push(c);
}
}
_ => {
do int_to_str_bytes_common(d as uint, radix, SignNone) |c| {
s.push(c);
}
}
_ => to_str_bytes_common(&(d as uint), radix, false, SignNone, DigAll)
};
let mut s = s;
if flags.precision > s.len() {
let mut s_ = vec::with_capacity(flags.precision);
let n = flags.precision - s.len();
Expand Down
109 changes: 59 additions & 50 deletions src/libstd/char.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@

//! Utilities for manipulating the char type

use container::Container;
use option::{None, Option, Some};
use str;
use str::{StrSlice, OwnedStr};
use u32;
use uint;
use int;
use str::StrSlice;
use unicode::{derived_property, general_category};

#[cfg(test)] use str::OwnedStr;

#[cfg(not(test))] use cmp::{Eq, Ord};
#[cfg(not(test))] use num::Zero;

Expand Down Expand Up @@ -202,21 +201,21 @@ pub fn from_digit(num: uint, radix: uint) -> Option<char> {
/// - chars in [0x100,0xffff] get 4-digit escapes: `\\uNNNN`
/// - chars above 0x10000 get 8-digit escapes: `\\UNNNNNNNN`
///
pub fn escape_unicode(c: char) -> ~str {
let s = u32::to_str_radix(c as u32, 16u);
let (c, pad) = cond!(
(c <= '\xff') { ('x', 2u) }
(c <= '\uffff') { ('u', 4u) }
_ { ('U', 8u) }
pub fn escape_unicode(c: char, f: &fn(char)) {
// avoid calling str::to_str_radix because we don't really need to allocate
// here.
f('\\');
let pad = cond!(
(c <= '\xff') { f('x'); 2 }
(c <= '\uffff') { f('u'); 4 }
_ { f('U'); 8 }
);
assert!(s.len() <= pad);
let mut out = ~"\\";
out.push_str(str::from_char(c));
for uint::range(s.len(), pad) |_| {
out.push_str("0");
for int::range_step(4 * (pad - 1), -1, -4) |offset| {
match ((c as u32) >> offset) & 0xf {
i @ 0 .. 9 => { f('0' + i as char); }
i => { f('a' + (i - 10) as char); }
}
}
out.push_str(s);
out
}

///
Expand All @@ -231,16 +230,16 @@ pub fn escape_unicode(c: char) -> ~str {
/// - Any other chars in the range [0x20,0x7e] are not escaped.
/// - Any other chars are given hex unicode escapes; see `escape_unicode`.
///
pub fn escape_default(c: char) -> ~str {
pub fn escape_default(c: char, f: &fn(char)) {
match c {
'\t' => ~"\\t",
'\r' => ~"\\r",
'\n' => ~"\\n",
'\\' => ~"\\\\",
'\'' => ~"\\'",
'"' => ~"\\\"",
'\x20' .. '\x7e' => str::from_char(c),
_ => c.escape_unicode(),
'\t' => { f('\\'); f('t'); }
'\r' => { f('\\'); f('r'); }
'\n' => { f('\\'); f('n'); }
'\\' => { f('\\'); f('\\'); }
'\'' => { f('\\'); f('\''); }
'"' => { f('\\'); f('"'); }
'\x20' .. '\x7e' => { f(c); }
_ => c.escape_unicode(f),
}
}

Expand Down Expand Up @@ -274,8 +273,8 @@ pub trait Char {
fn is_digit_radix(&self, radix: uint) -> bool;
fn to_digit(&self, radix: uint) -> Option<uint>;
fn from_digit(num: uint, radix: uint) -> Option<char>;
fn escape_unicode(&self) -> ~str;
fn escape_default(&self) -> ~str;
fn escape_unicode(&self, f: &fn(char));
fn escape_default(&self, f: &fn(char));
fn len_utf8_bytes(&self) -> uint;
}

Expand All @@ -302,9 +301,9 @@ impl Char for char {

fn from_digit(num: uint, radix: uint) -> Option<char> { from_digit(num, radix) }

fn escape_unicode(&self) -> ~str { escape_unicode(*self) }
fn escape_unicode(&self, f: &fn(char)) { escape_unicode(*self, f) }

fn escape_default(&self) -> ~str { escape_default(*self) }
fn escape_default(&self, f: &fn(char)) { escape_default(*self, f) }

fn len_utf8_bytes(&self) -> uint { len_utf8_bytes(*self) }
}
Expand Down Expand Up @@ -392,27 +391,37 @@ fn test_is_digit() {

#[test]
fn test_escape_default() {
assert_eq!('\n'.escape_default(), ~"\\n");
assert_eq!('\r'.escape_default(), ~"\\r");
assert_eq!('\''.escape_default(), ~"\\'");
assert_eq!('"'.escape_default(), ~"\\\"");
assert_eq!(' '.escape_default(), ~" ");
assert_eq!('a'.escape_default(), ~"a");
assert_eq!('~'.escape_default(), ~"~");
assert_eq!('\x00'.escape_default(), ~"\\x00");
assert_eq!('\x1f'.escape_default(), ~"\\x1f");
assert_eq!('\x7f'.escape_default(), ~"\\x7f");
assert_eq!('\xff'.escape_default(), ~"\\xff");
assert_eq!('\u011b'.escape_default(), ~"\\u011b");
assert_eq!('\U0001d4b6'.escape_default(), ~"\\U0001d4b6");
fn string(c: char) -> ~str {
let mut result = ~"";
do escape_default(c) |c| { result.push_char(c); }
return result;
}
assert_eq!(string('\n'), ~"\\n");
assert_eq!(string('\r'), ~"\\r");
assert_eq!(string('\''), ~"\\'");
assert_eq!(string('"'), ~"\\\"");
assert_eq!(string(' '), ~" ");
assert_eq!(string('a'), ~"a");
assert_eq!(string('~'), ~"~");
assert_eq!(string('\x00'), ~"\\x00");
assert_eq!(string('\x1f'), ~"\\x1f");
assert_eq!(string('\x7f'), ~"\\x7f");
assert_eq!(string('\xff'), ~"\\xff");
assert_eq!(string('\u011b'), ~"\\u011b");
assert_eq!(string('\U0001d4b6'), ~"\\U0001d4b6");
}

#[test]
fn test_escape_unicode() {
assert_eq!('\x00'.escape_unicode(), ~"\\x00");
assert_eq!('\n'.escape_unicode(), ~"\\x0a");
assert_eq!(' '.escape_unicode(), ~"\\x20");
assert_eq!('a'.escape_unicode(), ~"\\x61");
assert_eq!('\u011b'.escape_unicode(), ~"\\u011b");
assert_eq!('\U0001d4b6'.escape_unicode(), ~"\\U0001d4b6");
fn string(c: char) -> ~str {
let mut result = ~"";
do escape_unicode(c) |c| { result.push_char(c); }
return result;
}
assert_eq!(string('\x00'), ~"\\x00");
assert_eq!(string('\n'), ~"\\x0a");
assert_eq!(string(' '), ~"\\x20");
assert_eq!(string('a'), ~"\\x61");
assert_eq!(string('\u011b'), ~"\\u011b");
assert_eq!(string('\U0001d4b6'), ~"\\U0001d4b6");
}
22 changes: 11 additions & 11 deletions src/libstd/num/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -754,8 +754,8 @@ impl Float for f32 {
///
#[inline]
pub fn to_str(num: f32) -> ~str {
let (r, _) = strconv::to_str_common(
&num, 10u, true, strconv::SignNeg, strconv::DigAll);
let (r, _) = strconv::float_to_str_common(
num, 10u, true, strconv::SignNeg, strconv::DigAll);
r
}

Expand All @@ -768,8 +768,8 @@ pub fn to_str(num: f32) -> ~str {
///
#[inline]
pub fn to_str_hex(num: f32) -> ~str {
let (r, _) = strconv::to_str_common(
&num, 16u, true, strconv::SignNeg, strconv::DigAll);
let (r, _) = strconv::float_to_str_common(
num, 16u, true, strconv::SignNeg, strconv::DigAll);
r
}

Expand All @@ -789,8 +789,8 @@ pub fn to_str_hex(num: f32) -> ~str {
///
#[inline]
pub fn to_str_radix(num: f32, rdx: uint) -> ~str {
let (r, special) = strconv::to_str_common(
&num, rdx, true, strconv::SignNeg, strconv::DigAll);
let (r, special) = strconv::float_to_str_common(
num, rdx, true, strconv::SignNeg, strconv::DigAll);
if special { fail!("number has a special value, \
try to_str_radix_special() if those are expected") }
r
Expand All @@ -807,7 +807,7 @@ pub fn to_str_radix(num: f32, rdx: uint) -> ~str {
///
#[inline]
pub fn to_str_radix_special(num: f32, rdx: uint) -> (~str, bool) {
strconv::to_str_common(&num, rdx, true,
strconv::float_to_str_common(num, rdx, true,
strconv::SignNeg, strconv::DigAll)
}

Expand All @@ -822,8 +822,8 @@ pub fn to_str_radix_special(num: f32, rdx: uint) -> (~str, bool) {
///
#[inline]
pub fn to_str_exact(num: f32, dig: uint) -> ~str {
let (r, _) = strconv::to_str_common(
&num, 10u, true, strconv::SignNeg, strconv::DigExact(dig));
let (r, _) = strconv::float_to_str_common(
num, 10u, true, strconv::SignNeg, strconv::DigExact(dig));
r
}

Expand All @@ -838,8 +838,8 @@ pub fn to_str_exact(num: f32, dig: uint) -> ~str {
///
#[inline]
pub fn to_str_digits(num: f32, dig: uint) -> ~str {
let (r, _) = strconv::to_str_common(
&num, 10u, true, strconv::SignNeg, strconv::DigMax(dig));
let (r, _) = strconv::float_to_str_common(
num, 10u, true, strconv::SignNeg, strconv::DigMax(dig));
r
}

Expand Down
22 changes: 11 additions & 11 deletions src/libstd/num/f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -796,8 +796,8 @@ impl Float for f64 {
///
#[inline]
pub fn to_str(num: f64) -> ~str {
let (r, _) = strconv::to_str_common(
&num, 10u, true, strconv::SignNeg, strconv::DigAll);
let (r, _) = strconv::float_to_str_common(
num, 10u, true, strconv::SignNeg, strconv::DigAll);
r
}

Expand All @@ -810,8 +810,8 @@ pub fn to_str(num: f64) -> ~str {
///
#[inline]
pub fn to_str_hex(num: f64) -> ~str {
let (r, _) = strconv::to_str_common(
&num, 16u, true, strconv::SignNeg, strconv::DigAll);
let (r, _) = strconv::float_to_str_common(
num, 16u, true, strconv::SignNeg, strconv::DigAll);
r
}

Expand All @@ -831,8 +831,8 @@ pub fn to_str_hex(num: f64) -> ~str {
///
#[inline]
pub fn to_str_radix(num: f64, rdx: uint) -> ~str {
let (r, special) = strconv::to_str_common(
&num, rdx, true, strconv::SignNeg, strconv::DigAll);
let (r, special) = strconv::float_to_str_common(
num, rdx, true, strconv::SignNeg, strconv::DigAll);
if special { fail!("number has a special value, \
try to_str_radix_special() if those are expected") }
r
Expand All @@ -849,7 +849,7 @@ pub fn to_str_radix(num: f64, rdx: uint) -> ~str {
///
#[inline]
pub fn to_str_radix_special(num: f64, rdx: uint) -> (~str, bool) {
strconv::to_str_common(&num, rdx, true,
strconv::float_to_str_common(num, rdx, true,
strconv::SignNeg, strconv::DigAll)
}

Expand All @@ -864,8 +864,8 @@ pub fn to_str_radix_special(num: f64, rdx: uint) -> (~str, bool) {
///
#[inline]
pub fn to_str_exact(num: f64, dig: uint) -> ~str {
let (r, _) = strconv::to_str_common(
&num, 10u, true, strconv::SignNeg, strconv::DigExact(dig));
let (r, _) = strconv::float_to_str_common(
num, 10u, true, strconv::SignNeg, strconv::DigExact(dig));
r
}

Expand All @@ -880,8 +880,8 @@ pub fn to_str_exact(num: f64, dig: uint) -> ~str {
///
#[inline]
pub fn to_str_digits(num: f64, dig: uint) -> ~str {
let (r, _) = strconv::to_str_common(
&num, 10u, true, strconv::SignNeg, strconv::DigMax(dig));
let (r, _) = strconv::float_to_str_common(
num, 10u, true, strconv::SignNeg, strconv::DigMax(dig));
r
}

Expand Down
22 changes: 11 additions & 11 deletions src/libstd/num/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ pub mod consts {
///
#[inline]
pub fn to_str(num: float) -> ~str {
let (r, _) = strconv::to_str_common(
&num, 10u, true, strconv::SignNeg, strconv::DigAll);
let (r, _) = strconv::float_to_str_common(
num, 10u, true, strconv::SignNeg, strconv::DigAll);
r
}

Expand All @@ -115,8 +115,8 @@ pub fn to_str(num: float) -> ~str {
///
#[inline]
pub fn to_str_hex(num: float) -> ~str {
let (r, _) = strconv::to_str_common(
&num, 16u, true, strconv::SignNeg, strconv::DigAll);
let (r, _) = strconv::float_to_str_common(
num, 16u, true, strconv::SignNeg, strconv::DigAll);
r
}

Expand All @@ -136,8 +136,8 @@ pub fn to_str_hex(num: float) -> ~str {
///
#[inline]
pub fn to_str_radix(num: float, radix: uint) -> ~str {
let (r, special) = strconv::to_str_common(
&num, radix, true, strconv::SignNeg, strconv::DigAll);
let (r, special) = strconv::float_to_str_common(
num, radix, true, strconv::SignNeg, strconv::DigAll);
if special { fail!("number has a special value, \
try to_str_radix_special() if those are expected") }
r
Expand All @@ -154,7 +154,7 @@ pub fn to_str_radix(num: float, radix: uint) -> ~str {
///
#[inline]
pub fn to_str_radix_special(num: float, radix: uint) -> (~str, bool) {
strconv::to_str_common(&num, radix, true,
strconv::float_to_str_common(num, radix, true,
strconv::SignNeg, strconv::DigAll)
}

Expand All @@ -169,8 +169,8 @@ pub fn to_str_radix_special(num: float, radix: uint) -> (~str, bool) {
///
#[inline]
pub fn to_str_exact(num: float, digits: uint) -> ~str {
let (r, _) = strconv::to_str_common(
&num, 10u, true, strconv::SignNeg, strconv::DigExact(digits));
let (r, _) = strconv::float_to_str_common(
num, 10u, true, strconv::SignNeg, strconv::DigExact(digits));
r
}

Expand All @@ -185,8 +185,8 @@ pub fn to_str_exact(num: float, digits: uint) -> ~str {
///
#[inline]
pub fn to_str_digits(num: float, digits: uint) -> ~str {
let (r, _) = strconv::to_str_common(
&num, 10u, true, strconv::SignNeg, strconv::DigMax(digits));
let (r, _) = strconv::float_to_str_common(
num, 10u, true, strconv::SignNeg, strconv::DigMax(digits));
r
}

Expand Down
Loading