Skip to content

Commit 8c9be62

Browse files
committed
Code formatting
1 parent fa08d45 commit 8c9be62

2 files changed

Lines changed: 71 additions & 75 deletions

File tree

src/strcpy.rs

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,39 @@ use crate::CChar;
77
/// Rust implementation of C library function `strcpy`. Passing NULL
88
/// (core::ptr::null()) gives undefined behaviour.
99
#[no_mangle]
10-
pub unsafe extern "C" fn strcpy(
11-
dest: *mut CChar,
12-
src: *const CChar,
13-
) -> *const CChar {
14-
let mut i = 0;
15-
loop {
16-
*dest.offset(i) = *src.offset(i);
17-
let c = *dest.offset(i);
18-
if c == 0 {
19-
break;
20-
}
21-
i += 1;
22-
}
23-
dest
10+
pub unsafe extern "C" fn strcpy(dest: *mut CChar, src: *const CChar) -> *const CChar {
11+
let mut i = 0;
12+
loop {
13+
*dest.offset(i) = *src.offset(i);
14+
let c = *dest.offset(i);
15+
if c == 0 {
16+
break;
17+
}
18+
i += 1;
19+
}
20+
dest
2421
}
2522

2623
#[cfg(test)]
2724
mod test {
28-
use super::*;
25+
use super::*;
2926

30-
#[test]
31-
fn short() {
32-
let src = b"hi\0";
33-
let mut dest = *b"abcdef"; // no null terminator
34-
let result = unsafe { strcpy(dest.as_mut_ptr(), src.as_ptr()) };
35-
assert_eq!(
36-
unsafe { core::slice::from_raw_parts(result, 5) },
37-
*b"hi\0de"
38-
);
39-
}
27+
#[test]
28+
fn short() {
29+
let src = b"hi\0";
30+
let mut dest = *b"abcdef"; // no null terminator
31+
let result = unsafe { strcpy(dest.as_mut_ptr(), src.as_ptr()) };
32+
assert_eq!(
33+
unsafe { core::slice::from_raw_parts(result, 5) },
34+
*b"hi\0de"
35+
);
36+
}
4037

41-
#[test]
42-
fn two() {
43-
let src = b"hi\0";
44-
let mut dest = [0u8; 2]; // no space for null terminator
45-
let result = unsafe { strcpy(dest.as_mut_ptr(), src.as_ptr()) };
46-
assert_eq!(unsafe { core::slice::from_raw_parts(result, 2) }, b"hi");
47-
}
38+
#[test]
39+
fn two() {
40+
let src = b"hi\0";
41+
let mut dest = [0u8; 2]; // no space for null terminator
42+
let result = unsafe { strcpy(dest.as_mut_ptr(), src.as_ptr()) };
43+
assert_eq!(unsafe { core::slice::from_raw_parts(result, 2) }, b"hi");
44+
}
4845
}

src/strtoul.rs

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
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]
1515
pub 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)]
2929
mod 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

Comments
 (0)