-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from rafaelbeckel/master
implements strcat and strrchr
- Loading branch information
Showing
6 changed files
with
117 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
//! Rust implementation of C library function `strcat` | ||
//! | ||
//! Licensed under the Blue Oak Model Licence 1.0.0 | ||
use crate::CChar; | ||
|
||
/// Rust implementation of C library function `strcat`. Passing NULL | ||
/// (core::ptr::null()) gives undefined behaviour. | ||
#[cfg_attr(feature = "strcat", no_mangle)] | ||
pub unsafe extern "C" fn strcat(dest: *mut CChar, src: *const CChar) -> *const CChar { | ||
crate::strcpy::strcpy(dest.add(crate::strlen::strlen(dest)), src); | ||
dest | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
#[test] | ||
fn simple() { | ||
let mut dest = *b"hello\0\0\0\0\0\0\0\0"; | ||
let src = *b" world\0"; | ||
let result = unsafe { strcat(dest.as_mut_ptr(), src.as_ptr()) }; | ||
assert_eq!( | ||
unsafe { core::slice::from_raw_parts(result, 12) }, | ||
b"hello world\0" | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
//! Rust implementation of C library function `strchr` | ||
//! | ||
//! Copyright (c) 42 Technology Ltd | ||
//! Licensed under the Blue Oak Model Licence 1.0.0 | ||
use crate::{CChar, CInt}; | ||
|
||
/// Rust implementation of C library function `strrchr` | ||
#[cfg_attr(feature = "strrchr", no_mangle)] | ||
pub unsafe extern "C" fn strrchr(haystack: *const CChar, needle: CInt) -> *const CChar { | ||
let mut last = core::ptr::null(); | ||
for idx in 0.. { | ||
let ptr = haystack.offset(idx); | ||
if needle == (*ptr) as CInt { | ||
last = ptr; | ||
} | ||
if (*ptr) == 0 { | ||
break; | ||
} | ||
} | ||
last | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
#[test] | ||
fn strrchr_no_match() { | ||
let haystack = b"hayyystack\0".as_ptr(); | ||
let result = unsafe { strrchr(haystack, b'X' as CInt) }; | ||
assert_eq!(result, core::ptr::null()); | ||
} | ||
|
||
#[test] | ||
fn strrchr_null() { | ||
let haystack = b"hayyystack\0".as_ptr(); | ||
let result = unsafe { strrchr(haystack, 0) }; | ||
assert_eq!(result, unsafe { haystack.offset(10) }); | ||
} | ||
|
||
#[test] | ||
fn strrchr_start() { | ||
let haystack = b"hayhay\0".as_ptr(); | ||
let result = unsafe { strrchr(haystack, b'h' as CInt) }; | ||
assert_eq!(result, unsafe { haystack.offset(3) }); | ||
} | ||
|
||
#[test] | ||
fn strrchr_middle() { | ||
let haystack = b"hayyystack\0".as_ptr(); | ||
let result = unsafe { strrchr(haystack, b'y' as CInt) }; | ||
assert_eq!(result, unsafe { haystack.offset(4) }); | ||
} | ||
|
||
#[test] | ||
fn strrchr_end() { | ||
let haystack = b"hayyystack\0".as_ptr(); | ||
let result = unsafe { strrchr(haystack, b'k' as CInt) }; | ||
assert_eq!(result, unsafe { haystack.offset(9) }); | ||
} | ||
} |