Skip to content

Audit integer types in libunicode, libcore/(char, str) and libstd/ascii #22339

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
Feb 16, 2015
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
10 changes: 5 additions & 5 deletions src/etc/unicode.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ def emit_conversions_module(f, lowerupper, upperlower):
}
}

fn bsearch_case_table(c: char, table: &'static [(char, char)]) -> Option<uint> {
fn bsearch_case_table(c: char, table: &'static [(char, char)]) -> Option<usize> {
match table.binary_search(|&(key, _)| {
if c == key { Equal }
else if key < c { Less }
Expand Down Expand Up @@ -449,13 +449,13 @@ def emit_charwidth_module(f, width_table):
""")

f.write("""
pub fn width(c: char, is_cjk: bool) -> Option<uint> {
match c as uint {
pub fn width(c: char, is_cjk: bool) -> Option<usize> {
match c as usize {
_c @ 0 => Some(0), // null is zero width
cu if cu < 0x20 => None, // control sequences have no width
cu if cu < 0x7F => Some(1), // ASCII
cu if cu < 0xA0 => None, // more control sequences
_ => Some(bsearch_range_value_table(c, is_cjk, charwidth_table) as uint)
_ => Some(bsearch_range_value_table(c, is_cjk, charwidth_table) as usize)
}
}

Expand Down Expand Up @@ -610,7 +610,7 @@ def optimize_width_table(wtable):
rf.write("""
/// The version of [Unicode](http://www.unicode.org/)
/// that the `UnicodeChar` and `UnicodeStrPrelude` traits are based on.
pub const UNICODE_VERSION: (uint, uint, uint) = (%s, %s, %s);
pub const UNICODE_VERSION: (u64, u64, u64) = (%s, %s, %s);
""" % unicode_version)
(canon_decomp, compat_decomp, gencats, combines,
lowerupper, upperlower) = load_unicode_data("UnicodeData.txt")
Expand Down
42 changes: 21 additions & 21 deletions src/libcore/char.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,16 @@ pub fn from_u32(i: u32) -> Option<char> {
/// ```
#[inline]
#[unstable(feature = "core", reason = "pending integer conventions")]
pub fn from_digit(num: uint, radix: uint) -> Option<char> {
pub fn from_digit(num: u32, radix: u32) -> Option<char> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

radix could be as small as u8 here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the guideline here #22240

Actually u32 here is not as arbitrary as it may seem, radixes, for example, are directly tied to the textual representation of numbers, and u32 can be viewed as value of unicode code point. And numeric values of unicode digits (e.g. Roman numerals) can be larger than 256 (not in this example, but in others). Regardless, I think u8 would be as good as u32 and the choice doesn't really matter in practice :)

if radix > 36 {
panic!("from_digit: radix is too high (maximum 36)");
}
if num < radix {
unsafe {
if num < 10 {
Some(transmute(('0' as uint + num) as u32))
Some(transmute('0' as u32 + num))
} else {
Some(transmute(('a' as uint + num - 10) as u32))
Some(transmute('a' as u32 + num - 10))
}
}
} else {
Expand Down Expand Up @@ -164,7 +164,7 @@ pub trait CharExt {
/// ```
#[unstable(feature = "core",
reason = "pending integer conventions")]
fn is_digit(self, radix: uint) -> bool;
fn is_digit(self, radix: u32) -> bool;

/// Converts a character to the corresponding digit.
///
Expand All @@ -189,7 +189,7 @@ pub trait CharExt {
/// ```
#[unstable(feature = "core",
reason = "pending integer conventions")]
fn to_digit(self, radix: uint) -> Option<uint>;
fn to_digit(self, radix: u32) -> Option<u32>;

/// Returns an iterator that yields the hexadecimal Unicode escape of a character, as `char`s.
///
Expand Down Expand Up @@ -275,7 +275,7 @@ pub trait CharExt {
/// assert_eq!(n, 2);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
fn len_utf8(self) -> uint;
fn len_utf8(self) -> usize;

/// Returns the number of bytes this character would need if encoded in UTF-16.
///
Expand All @@ -287,7 +287,7 @@ pub trait CharExt {
/// assert_eq!(n, 1);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
fn len_utf16(self) -> uint;
fn len_utf16(self) -> usize;

/// Encodes this character as UTF-8 into the provided byte buffer, and then returns the number
/// of bytes written.
Expand Down Expand Up @@ -317,7 +317,7 @@ pub trait CharExt {
/// assert_eq!(result, None);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
fn encode_utf8(self, dst: &mut [u8]) -> Option<uint>;
fn encode_utf8(self, dst: &mut [u8]) -> Option<usize>;

/// Encodes this character as UTF-16 into the provided `u16` buffer, and then returns the
/// number of `u16`s written.
Expand Down Expand Up @@ -347,27 +347,27 @@ pub trait CharExt {
/// assert_eq!(result, None);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
fn encode_utf16(self, dst: &mut [u16]) -> Option<uint>;
fn encode_utf16(self, dst: &mut [u16]) -> Option<usize>;
}

#[stable(feature = "rust1", since = "1.0.0")]
impl CharExt for char {
#[unstable(feature = "core",
reason = "pending integer conventions")]
fn is_digit(self, radix: uint) -> bool {
fn is_digit(self, radix: u32) -> bool {
self.to_digit(radix).is_some()
}

#[unstable(feature = "core",
reason = "pending integer conventions")]
fn to_digit(self, radix: uint) -> Option<uint> {
fn to_digit(self, radix: u32) -> Option<u32> {
if radix > 36 {
panic!("to_digit: radix is too high (maximum 36)");
}
let val = match self {
'0' ... '9' => self as uint - ('0' as uint),
'a' ... 'z' => self as uint + 10 - ('a' as uint),
'A' ... 'Z' => self as uint + 10 - ('A' as uint),
'0' ... '9' => self as u32 - '0' as u32,
'a' ... 'z' => self as u32 - 'a' as u32 + 10,
'A' ... 'Z' => self as u32 - 'A' as u32 + 10,
_ => return None,
};
if val < radix { Some(val) }
Expand Down Expand Up @@ -396,7 +396,7 @@ impl CharExt for char {

#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
fn len_utf8(self) -> uint {
fn len_utf8(self) -> usize {
let code = self as u32;
match () {
_ if code < MAX_ONE_B => 1,
Expand All @@ -408,22 +408,22 @@ impl CharExt for char {

#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
fn len_utf16(self) -> uint {
fn len_utf16(self) -> usize {
let ch = self as u32;
if (ch & 0xFFFF_u32) == ch { 1 } else { 2 }
}

#[inline]
#[unstable(feature = "core",
reason = "pending decision about Iterator/Writer/Reader")]
fn encode_utf8(self, dst: &mut [u8]) -> Option<uint> {
fn encode_utf8(self, dst: &mut [u8]) -> Option<usize> {
encode_utf8_raw(self as u32, dst)
}

#[inline]
#[unstable(feature = "core",
reason = "pending decision about Iterator/Writer/Reader")]
fn encode_utf16(self, dst: &mut [u16]) -> Option<uint> {
fn encode_utf16(self, dst: &mut [u16]) -> Option<usize> {
encode_utf16_raw(self as u32, dst)
}
}
Expand All @@ -435,7 +435,7 @@ impl CharExt for char {
/// and a `None` will be returned.
#[inline]
#[unstable(feature = "core")]
pub fn encode_utf8_raw(code: u32, dst: &mut [u8]) -> Option<uint> {
pub fn encode_utf8_raw(code: u32, dst: &mut [u8]) -> Option<usize> {
// Marked #[inline] to allow llvm optimizing it away
if code < MAX_ONE_B && dst.len() >= 1 {
dst[0] = code as u8;
Expand Down Expand Up @@ -467,7 +467,7 @@ pub fn encode_utf8_raw(code: u32, dst: &mut [u8]) -> Option<uint> {
/// and a `None` will be returned.
#[inline]
#[unstable(feature = "core")]
pub fn encode_utf16_raw(mut ch: u32, dst: &mut [u16]) -> Option<uint> {
pub fn encode_utf16_raw(mut ch: u32, dst: &mut [u16]) -> Option<usize> {
// Marked #[inline] to allow llvm optimizing it away
if (ch & 0xFFFF_u32) == ch && dst.len() >= 1 {
// The BMP falls through (assuming non-surrogate, as it should)
Expand Down Expand Up @@ -499,7 +499,7 @@ enum EscapeUnicodeState {
Backslash,
Type,
LeftBrace,
Value(uint),
Value(usize),
RightBrace,
Done,
}
Expand Down
10 changes: 5 additions & 5 deletions src/libcore/fmt/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub enum SignFormat {
SignNeg
}

static DIGIT_E_RADIX: uint = ('e' as uint) - ('a' as uint) + 11;
static DIGIT_E_RADIX: u32 = ('e' as u32) - ('a' as u32) + 11;

/// Converts a number to its string representation as a byte vector.
/// This is meant to be a common base implementation for all numeric string
Expand Down Expand Up @@ -87,7 +87,7 @@ static DIGIT_E_RADIX: uint = ('e' as uint) - ('a' as uint) + 11;
/// between digit and exponent sign `'p'`.
pub fn float_to_str_bytes_common<T: Float, U, F>(
num: T,
radix: uint,
radix: u32,
negative_zero: bool,
sign: SignFormat,
digits: SignificantDigits,
Expand Down Expand Up @@ -156,7 +156,7 @@ pub fn float_to_str_bytes_common<T: Float, U, F>(
deccum = deccum / radix_gen;
deccum = deccum.trunc();

let c = char::from_digit(current_digit.to_int().unwrap() as uint, radix);
let c = char::from_digit(current_digit.to_int().unwrap() as u32, radix);
buf[end] = c.unwrap() as u8;
end += 1;

Expand Down Expand Up @@ -211,7 +211,7 @@ pub fn float_to_str_bytes_common<T: Float, U, F>(
// See note in first loop.
let current_digit = deccum.trunc().abs();

let c = char::from_digit(current_digit.to_int().unwrap() as uint,
let c = char::from_digit(current_digit.to_int().unwrap() as u32,
radix);
buf[end] = c.unwrap() as u8;
end += 1;
Expand All @@ -228,7 +228,7 @@ pub fn float_to_str_bytes_common<T: Float, U, F>(
let ascii2value = |chr: u8| {
(chr as char).to_digit(radix).unwrap()
};
let value2ascii = |val: uint| {
let value2ascii = |val: u32| {
char::from_digit(val, radix).unwrap() as u8
};

Expand Down
8 changes: 4 additions & 4 deletions src/libcore/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1432,12 +1432,12 @@ pub trait Float
#[unstable(feature = "core", reason = "needs reevaluation")]
pub trait FromStrRadix {
type Err;
fn from_str_radix(str: &str, radix: uint) -> Result<Self, Self::Err>;
fn from_str_radix(str: &str, radix: u32) -> Result<Self, Self::Err>;
}

/// A utility function that just calls FromStrRadix::from_str_radix.
#[unstable(feature = "core", reason = "needs reevaluation")]
pub fn from_str_radix<T: FromStrRadix>(str: &str, radix: uint)
pub fn from_str_radix<T: FromStrRadix>(str: &str, radix: u32)
-> Result<T, T::Err> {
FromStrRadix::from_str_radix(str, radix)
}
Expand Down Expand Up @@ -1501,7 +1501,7 @@ macro_rules! from_str_radix_float_impl {
/// `None` if the string did not represent a valid number.
/// Otherwise, `Some(n)` where `n` is the floating-point number
/// represented by `src`.
fn from_str_radix(src: &str, radix: uint)
fn from_str_radix(src: &str, radix: u32)
-> Result<$T, ParseFloatError> {
use self::FloatErrorKind::*;
use self::ParseFloatError as PFE;
Expand Down Expand Up @@ -1661,7 +1661,7 @@ macro_rules! from_str_radix_int_impl {
#[stable(feature = "rust1", since = "1.0.0")]
impl FromStrRadix for $T {
type Err = ParseIntError;
fn from_str_radix(src: &str, radix: uint)
fn from_str_radix(src: &str, radix: u32)
-> Result<$T, ParseIntError> {
use self::IntErrorKind::*;
use self::ParseIntError as PIE;
Expand Down
Loading