Skip to content
Snippets Groups Projects
Commit 9325a29a authored by Alex Lyon's avatar Alex Lyon
Browse files

stdlib: make rustfmt happy with strtol()

parent f5b1f872
No related branches found
No related tags found
No related merge requests found
...@@ -416,7 +416,11 @@ pub unsafe extern "C" fn strtod(s: *const c_char, endptr: *mut *mut c_char) -> c ...@@ -416,7 +416,11 @@ pub unsafe extern "C" fn strtod(s: *const c_char, endptr: *mut *mut c_char) -> c
} }
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn strtol(s: *const c_char, endptr: *mut *const c_char, base: c_int) -> c_long { pub unsafe extern "C" fn strtol(
s: *const c_char,
endptr: *mut *const c_char,
base: c_int,
) -> c_long {
let set_endptr = |idx: isize| { let set_endptr = |idx: isize| {
if !endptr.is_null() { if !endptr.is_null() {
*endptr = s.offset(idx); *endptr = s.offset(idx);
...@@ -449,21 +453,17 @@ pub unsafe extern "C" fn strtol(s: *const c_char, endptr: *mut *const c_char, ba ...@@ -449,21 +453,17 @@ pub unsafe extern "C" fn strtol(s: *const c_char, endptr: *mut *const c_char, ba
} }
None => { None => {
invalid_input(); invalid_input();
return 0 return 0;
} }
}; };
// convert the string to a number // convert the string to a number
let num_str = s.offset(idx); let num_str = s.offset(idx);
let res = match base { let res = match base {
0 => { 0 => detect_base(num_str).and_then(|(base, i)| convert_integer(num_str.offset(i), base)),
detect_base(num_str).and_then(|(base, i)| {
convert_integer(num_str.offset(i), base)
})
}
8 => convert_octal(num_str), 8 => convert_octal(num_str),
16 => convert_hex(num_str), 16 => convert_hex(num_str),
_ => convert_integer(num_str, base) _ => convert_integer(num_str, base),
}; };
// check for error parsing octal/hex prefix // check for error parsing octal/hex prefix
...@@ -504,7 +504,7 @@ fn is_positive(ch: c_char) -> Option<(bool, isize)> { ...@@ -504,7 +504,7 @@ fn is_positive(ch: c_char) -> Option<(bool, isize)> {
0 => None, 0 => None,
ch if ch == b'+' as c_char => Some((true, 1)), ch if ch == b'+' as c_char => Some((true, 1)),
ch if ch == b'-' as c_char => Some((false, 1)), ch if ch == b'-' as c_char => Some((false, 1)),
_ => Some((true, 0)) _ => Some((true, 0)),
} }
} }
...@@ -523,9 +523,7 @@ fn detect_base(s: *const c_char) -> Option<(c_int, isize)> { ...@@ -523,9 +523,7 @@ fn detect_base(s: *const c_char) -> Option<(c_int, isize)> {
Some((8, 0)) Some((8, 0))
} }
} }
_ => { _ => Some((10, 0)),
Some((10, 0))
}
} }
} }
...@@ -544,12 +542,9 @@ unsafe fn convert_octal(s: *const c_char) -> Option<(c_ulong, isize, bool)> { ...@@ -544,12 +542,9 @@ unsafe fn convert_octal(s: *const c_char) -> Option<(c_ulong, isize, bool)> {
unsafe fn convert_hex(s: *const c_char) -> Option<(c_ulong, isize, bool)> { unsafe fn convert_hex(s: *const c_char) -> Option<(c_ulong, isize, bool)> {
if (*s != 0 && *s == b'0' as c_char) if (*s != 0 && *s == b'0' as c_char)
&& (*s.offset(1) != 0 && (*s.offset(1) == b'x' as c_char && (*s.offset(1) != 0 && (*s.offset(1) == b'x' as c_char || *s.offset(1) == b'X' as c_char))
|| *s.offset(1) == b'X' as c_char))
{ {
convert_integer(s.offset(2), 16).map(|(val, idx, overflow)| { convert_integer(s.offset(2), 16).map(|(val, idx, overflow)| (val, idx + 2, overflow))
(val, idx + 2, overflow)
})
} else { } else {
None None
} }
...@@ -557,6 +552,7 @@ unsafe fn convert_hex(s: *const c_char) -> Option<(c_ulong, isize, bool)> { ...@@ -557,6 +552,7 @@ unsafe fn convert_hex(s: *const c_char) -> Option<(c_ulong, isize, bool)> {
fn convert_integer(s: *const c_char, base: c_int) -> Option<(c_ulong, isize, bool)> { fn convert_integer(s: *const c_char, base: c_int) -> Option<(c_ulong, isize, bool)> {
// -1 means the character is invalid // -1 means the character is invalid
#[cfg_attr(rustfmt, rustfmt_skip)]
const LOOKUP_TABLE: [c_long; 256] = [ const LOOKUP_TABLE: [c_long; 256] = [
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
...@@ -581,14 +577,12 @@ fn convert_integer(s: *const c_char, base: c_int) -> Option<(c_ulong, isize, boo ...@@ -581,14 +577,12 @@ fn convert_integer(s: *const c_char, base: c_int) -> Option<(c_ulong, isize, boo
let mut overflowed = false; let mut overflowed = false;
loop { loop {
let val = unsafe { let val = unsafe { LOOKUP_TABLE[*s.offset(idx) as usize] };
LOOKUP_TABLE[*s.offset(idx) as usize]
};
if val == -1 || val as c_int >= base { if val == -1 || val as c_int >= base {
break; break;
} else { } else {
if let Some(res) = num.checked_mul(base as c_ulong) if let Some(res) = num.checked_mul(base as c_ulong)
.and_then(|num| num.checked_add(val as c_ulong)) .and_then(|num| num.checked_add(val as c_ulong))
{ {
num = res; num = res;
} else { } else {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment