-
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.
- Loading branch information
1 parent
3ae2f1d
commit 057fc14
Showing
3 changed files
with
88 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
//! Rust implementation of C library function `strspn` | ||
//! | ||
//! Copyright (c) Ferrous Systems UK Ltd | ||
//! Licensed under the Blue Oak Model Licence 1.0.0 | ||
use crate::{CChar, CInt}; | ||
|
||
/// Rust implementation of C library function `strspn` | ||
#[cfg_attr(feature = "strspn", no_mangle)] | ||
pub unsafe extern "C" fn strspn(s: *const CChar, charset: *const CChar) -> usize { | ||
if s.is_null() { | ||
return 0; | ||
} | ||
if charset.is_null() { | ||
return 0; | ||
} | ||
|
||
let s = unsafe { core::ffi::CStr::from_ptr(s.cast()) }; | ||
|
||
let charset = unsafe { core::ffi::CStr::from_ptr(charset.cast()) }; | ||
|
||
for (idx, b) in s.to_bytes().iter().enumerate() { | ||
if !is_c_in_charset(*b, charset) { | ||
return idx; | ||
} | ||
} | ||
|
||
s.count_bytes() | ||
Check failure on line 28 in src/strspn.rs GitHub Actions / clippyuse of unstable library feature 'cstr_count_bytes'
|
||
} | ||
|
||
fn is_c_in_charset(c: u8, charset: &core::ffi::CStr) -> bool { | ||
for b in charset.to_bytes() { | ||
if c == *b { | ||
return true; | ||
} | ||
} | ||
false | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
#[test] | ||
fn complete() { | ||
let charset = c"0123456789"; | ||
let s = c"987654321"; | ||
assert_eq!( | ||
unsafe { super::strspn(s.as_ptr().cast(), charset.as_ptr().cast()) }, | ||
9 | ||
); | ||
} | ||
|
||
#[test] | ||
fn subset() { | ||
let charset = c"0123456789"; | ||
let s = c"98xx7654321"; | ||
assert_eq!( | ||
unsafe { super::strspn(s.as_ptr().cast(), charset.as_ptr().cast()) }, | ||
2 | ||
); | ||
} | ||
|
||
#[test] | ||
fn empty_charset() { | ||
let charset = c""; | ||
let s = c"AABBCCDD"; | ||
assert_eq!( | ||
unsafe { super::strspn(s.as_ptr().cast(), charset.as_ptr().cast()) }, | ||
0 | ||
); | ||
} | ||
|
||
#[test] | ||
fn empty_string() { | ||
let charset = c"0123456789"; | ||
let s = c""; | ||
assert_eq!( | ||
unsafe { super::strspn(s.as_ptr().cast(), charset.as_ptr().cast()) }, | ||
0 | ||
); | ||
} | ||
} | ||
|
||
// End of file |