33//! Copyright (c) Jonathan 'theJPster' Pallant 2019
44//! Licensed under the Blue Oak Model Licence 1.0.0
55
6- use crate :: { CChar , CULong , CStringIter } ;
6+ use crate :: { CChar , CStringIter , CULong } ;
77
88/// Rust implementation of C library function `strtoul`.
99///
@@ -13,56 +13,55 @@ use crate::{CChar, CULong, CStringIter};
1313/// function returns zero.
1414#[ no_mangle]
1515pub unsafe extern "C" fn strtoul ( s : * const CChar ) -> CULong {
16- let mut result: CULong = 0 ;
17- for c in CStringIter :: new ( s) {
18- if c >= b'0' && c <= b'9' {
19- result *= 10 ;
20- result += ( c - b'0' ) as CULong ;
21- } else {
22- break ;
23- }
24- }
25- result
16+ let mut result: CULong = 0 ;
17+ for c in CStringIter :: new ( s) {
18+ if c >= b'0' && c <= b'9' {
19+ result *= 10 ;
20+ result += ( c - b'0' ) as CULong ;
21+ } else {
22+ break ;
23+ }
24+ }
25+ result
2626}
2727
2828#[ cfg( test) ]
2929mod test {
30- use super :: strtoul;
30+ use super :: strtoul;
3131
32- #[ test]
33- fn empty ( ) {
34- let result = unsafe { strtoul ( b"\0 " . as_ptr ( ) ) } ;
35- assert_eq ! ( result, 0 ) ;
36- }
32+ #[ test]
33+ fn empty ( ) {
34+ let result = unsafe { strtoul ( b"\0 " . as_ptr ( ) ) } ;
35+ assert_eq ! ( result, 0 ) ;
36+ }
3737
38- #[ test]
39- fn non_digit ( ) {
40- let result = unsafe { strtoul ( b"1234x\0 " . as_ptr ( ) ) } ;
41- assert_eq ! ( result, 1234 ) ;
42- }
38+ #[ test]
39+ fn non_digit ( ) {
40+ let result = unsafe { strtoul ( b"1234x\0 " . as_ptr ( ) ) } ;
41+ assert_eq ! ( result, 1234 ) ;
42+ }
4343
44- #[ test]
45- fn bad_number ( ) {
46- let result = unsafe { strtoul ( b"x\0 " . as_ptr ( ) ) } ;
47- assert_eq ! ( result, 0 ) ;
48- }
44+ #[ test]
45+ fn bad_number ( ) {
46+ let result = unsafe { strtoul ( b"x\0 " . as_ptr ( ) ) } ;
47+ assert_eq ! ( result, 0 ) ;
48+ }
4949
50- #[ test]
51- fn one ( ) {
52- let result = unsafe { strtoul ( b"1\0 " . as_ptr ( ) ) } ;
53- assert_eq ! ( result, 1 ) ;
54- }
50+ #[ test]
51+ fn one ( ) {
52+ let result = unsafe { strtoul ( b"1\0 " . as_ptr ( ) ) } ;
53+ assert_eq ! ( result, 1 ) ;
54+ }
5555
56- #[ test]
57- fn hundredish ( ) {
58- let result = unsafe { strtoul ( b"123\0 " . as_ptr ( ) ) } ;
59- assert_eq ! ( result, 123 ) ;
60- }
61-
62- #[ test]
63- fn big_long ( ) {
64- let result = unsafe { strtoul ( b"2147483647\0 " . as_ptr ( ) ) } ;
65- assert_eq ! ( result, 2147483647 ) ;
66- }
56+ #[ test]
57+ fn hundredish ( ) {
58+ let result = unsafe { strtoul ( b"123\0 " . as_ptr ( ) ) } ;
59+ assert_eq ! ( result, 123 ) ;
60+ }
6761
62+ #[ test]
63+ fn big_long ( ) {
64+ let result = unsafe { strtoul ( b"2147483647\0 " . as_ptr ( ) ) } ;
65+ assert_eq ! ( result, 2147483647 ) ;
66+ }
6867}
0 commit comments