Skip to content
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

Replaced the numeric string conversion functions, and made the api for them consistent #4656

Closed
wants to merge 14 commits into from
Closed
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
8 changes: 4 additions & 4 deletions doc/rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ mod quux {
pub fn bar() { }
pub fn baz() { }
}

pub use quux::foo::*;
}
~~~~
Expand Down Expand Up @@ -1242,7 +1242,7 @@ trait Num {
impl float: Num {
static pure fn from_int(n: int) -> float { n as float }
}
let x: float = Num::from_int(42);
let x: float = Num::from_int(42);
~~~~

Traits may inherit from other traits. For example, in
Expand Down Expand Up @@ -1615,7 +1615,7 @@ The following are examples of structure expressions:
~~~~
# struct Point { x: float, y: float }
# struct TuplePoint(float, float);
# mod game { pub struct User { name: &str, age: uint, mut score: uint } }
# mod game { pub struct User { name: &str, age: uint, mut score: uint } }
# use game;
Point {x: 10f, y: 20f};
TuplePoint(10f, 20f);
Expand Down Expand Up @@ -2812,7 +2812,7 @@ trait Printable {
}

impl int: Printable {
fn to_str() -> ~str { int::to_str(self, 10) }
fn to_str() -> ~str { int::to_str(self) }
}

fn print(a: @Printable) {
Expand Down
4 changes: 2 additions & 2 deletions doc/tutorial-tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ fn stringifier(channel: &DuplexStream<~str, uint>) {
let mut value: uint;
loop {
value = channel.recv();
channel.send(uint::to_str(value, 10));
channel.send(uint::to_str(value));
if value == 0 { break; }
}
}
Expand All @@ -497,7 +497,7 @@ Here is the code for the parent task:
# let mut value: uint;
# loop {
# value = channel.recv();
# channel.send(uint::to_str(value, 10u));
# channel.send(uint::to_str(value));
# if value == 0u { break; }
# }
# }
Expand Down
52 changes: 51 additions & 1 deletion src/libcore/char.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,26 @@ pub pure fn is_digit(c: char) -> bool {
unicode::general_category::No(c);
}

/**
* Checks if a character parses as a numeric digit in the given radix.
* Compared to `is_digit()`, this function only recognizes the ascii
* characters `0-9`, `a-z` and `A-Z`.
*
* Returns `true` if `c` is a valid digit under `radix`, and `false`
* otherwise.
*
* Fails if given a `radix` > 36.
*
* Note: This just wraps `to_digit()`.
*/
#[inline(always)]
pub pure fn is_digit_radix(c: char, radix: uint) -> bool {
match to_digit(c, radix) {
Some(_) => true,
None => false
}
}

/**
* Convert a char to the corresponding digit.
*
Expand All @@ -127,9 +147,15 @@ pub pure fn is_digit(c: char) -> bool {
* between 0 and 9. If `c` is 'a' or 'A', 10. If `c` is
* 'b' or 'B', 11, etc. Returns none if the char does not
* refer to a digit in the given radix.
*
* # Failure
* Fails if given a `radix` outside the range `[0..36]`.
*/
#[inline]
pub pure fn to_digit(c: char, radix: uint) -> Option<uint> {
if radix > 36 {
die!(fmt!("to_digit: radix %? is to high (maximum 36)", radix));
}
let val = match c {
'0' .. '9' => c as uint - ('0' as uint),
'a' .. 'z' => c as uint + 10u - ('a' as uint),
Expand All @@ -140,6 +166,30 @@ pub pure fn to_digit(c: char, radix: uint) -> Option<uint> {
else { None }
}

/**
* Converts a number to the ascii character representing it.
*
* Returns `Some(char)` if `num` represents one digit under `radix`,
* using one character of `0-9` or `a-z`, or `None` if it doesn't.
*
* Fails if given an `radix` > 36.
*/
#[inline]
pub pure fn from_digit(num: uint, radix: uint) -> Option<char> {
if radix > 36 {
die!(fmt!("from_digit: radix %? is to high (maximum 36)", num));
}
if num < radix {
if num < 10 {
Some(('0' as uint + num) as char)
} else {
Some(('a' as uint + num - 10u) as char)
}
} else {
None
}
}

/**
* Return the hexadecimal unicode escape of a char.
*
Expand All @@ -150,7 +200,7 @@ pub pure fn to_digit(c: char, radix: uint) -> Option<uint> {
* - chars above 0x10000 get 8-digit escapes: `\\UNNNNNNNN`
*/
pub pure fn escape_unicode(c: char) -> ~str {
let s = u32::to_str(c as u32, 16u);
let s = u32::to_str_radix(c as u32, 16u);
let (c, pad) = (if c <= '\xff' { ('x', 2u) }
else if c <= '\uffff' { ('u', 4u) }
else { ('U', 8u) });
Expand Down
25 changes: 15 additions & 10 deletions src/libcore/core.rc
Original file line number Diff line number Diff line change
Expand Up @@ -60,30 +60,33 @@ pub mod prelude;

/* Primitive types */

#[path = "int-template.rs"] #[merge = "int-template/int.rs"]
#[path = "num/int-template.rs"] #[merge = "num/int-template/int.rs"]
pub mod int;
#[path = "int-template.rs"] #[merge = "int-template/i8.rs"]
#[path = "num/int-template.rs"] #[merge = "num/int-template/i8.rs"]
pub mod i8;
#[path = "int-template.rs"] #[merge = "int-template/i16.rs"]
#[path = "num/int-template.rs"] #[merge = "num/int-template/i16.rs"]
pub mod i16;
#[path = "int-template.rs"] #[merge = "int-template/i32.rs"]
#[path = "num/int-template.rs"] #[merge = "num/int-template/i32.rs"]
pub mod i32;
#[path = "int-template.rs"] #[merge = "int-template/i64.rs"]
#[path = "num/int-template.rs"] #[merge = "num/int-template/i64.rs"]
pub mod i64;
#[path = "uint-template.rs"] #[merge = "uint-template/uint.rs"]
#[path = "num/uint-template.rs"] #[merge = "num/uint-template/uint.rs"]
pub mod uint;

#[path = "uint-template.rs"] #[merge = "uint-template/u8.rs"]
#[path = "num/uint-template.rs"] #[merge = "num/uint-template/u8.rs"]
pub mod u8;
#[path = "uint-template.rs"] #[merge = "uint-template/u16.rs"]
#[path = "num/uint-template.rs"] #[merge = "num/uint-template/u16.rs"]
pub mod u16;
#[path = "uint-template.rs"] #[merge = "uint-template/u32.rs"]
#[path = "num/uint-template.rs"] #[merge = "num/uint-template/u32.rs"]
pub mod u32;
#[path = "uint-template.rs"] #[merge = "uint-template/u64.rs"]
#[path = "num/uint-template.rs"] #[merge = "num/uint-template/u64.rs"]
pub mod u64;

#[path = "num/float.rs"]
pub mod float;
#[path = "num/f32.rs"]
pub mod f32;
#[path = "num/f64.rs"]
pub mod f64;

pub mod nil;
Expand Down Expand Up @@ -116,6 +119,7 @@ pub mod managed;
/* Common traits */

pub mod from_str;
#[path = "num/num.rs"]
pub mod num;
pub mod iter;
pub mod to_str;
Expand Down Expand Up @@ -232,6 +236,7 @@ pub mod private;
/* For internal use, not exported */

mod unicode;
#[path = "num/cmath.rs"]
mod cmath;
mod stackwalk;

Expand Down
4 changes: 2 additions & 2 deletions src/libcore/extfmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ pub mod rt {
pub pure fn conv_float(cv: Conv, f: float) -> ~str {
let (to_str, digits) = match cv.precision {
CountIs(c) => (float::to_str_exact, c as uint),
CountImplied => (float::to_str, 6u)
CountImplied => (float::to_str_digits, 6u)
};
let mut s = unsafe { to_str(f, digits) };
if 0.0 <= f {
Expand Down Expand Up @@ -596,7 +596,7 @@ pub mod rt {
return if prec == 0u && num == 0u {
~""
} else {
let s = uint::to_str(num, radix);
let s = uint::to_str_radix(num, radix);
let len = str::char_len(s);
if len < prec {
let diff = prec - len;
Expand Down
4 changes: 2 additions & 2 deletions src/libcore/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ impl &SipState : Streaming {
let r = self.result_bytes();
let mut s = ~"";
for vec::each(r) |b| {
s += uint::to_str(*b as uint, 16u);
s += uint::to_str_radix(*b as uint, 16u);
}
move s
}
Expand Down Expand Up @@ -449,7 +449,7 @@ pub fn test_siphash() {
fn to_hex_str(r: &[u8 * 8]) -> ~str {
let mut s = ~"";
for vec::each(*r) |b| {
s += uint::to_str(*b as uint, 16u);
s += uint::to_str_radix(*b as uint, 16u);
}
move s
}
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,7 @@ impl<T: Writer> T : WriterUtil {
int::to_str_bytes(n, 10u, |bytes| self.write(bytes))
}
fn write_uint(&self, n: uint) {
uint::to_str_bytes(false, n, 10u, |bytes| self.write(bytes))
uint::to_str_bytes(n, 10u, |bytes| self.write(bytes))
}
fn write_le_uint(&self, n: uint) {
u64_to_le_bytes(n as u64, uint::bytes, |v| self.write(v))
Expand Down
37 changes: 0 additions & 37 deletions src/libcore/num.rs

This file was deleted.

File renamed without changes.
Loading