|
| 1 | +//! Rust implementation of C library function `strcspn` |
| 2 | +//! |
| 3 | +//! Copyright (c) Ferrous Systems UK Ltd |
| 4 | +//! Licensed under the Blue Oak Model Licence 1.0.0 |
| 5 | +
|
| 6 | +use crate::{CChar, CInt}; |
| 7 | + |
| 8 | +/// Rust implementation of C library function `strcspn` |
| 9 | +#[cfg_attr(feature = "strcspn", no_mangle)] |
| 10 | +pub unsafe extern "C" fn strcspn(s: *const CChar, charset: *const CChar) -> usize { |
| 11 | + if s.is_null() { |
| 12 | + return 0; |
| 13 | + } |
| 14 | + if charset.is_null() { |
| 15 | + return 0; |
| 16 | + } |
| 17 | + |
| 18 | + let s = unsafe { core::ffi::CStr::from_ptr(s.cast()) }; |
| 19 | + |
| 20 | + let charset = unsafe { core::ffi::CStr::from_ptr(charset.cast()) }; |
| 21 | + |
| 22 | + let bytes = s.to_bytes(); |
| 23 | + for (idx, b) in bytes.iter().enumerate() { |
| 24 | + if is_c_in_charset(*b, charset) { |
| 25 | + return idx; |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + bytes.len() |
| 30 | +} |
| 31 | + |
| 32 | +fn is_c_in_charset(c: u8, charset: &core::ffi::CStr) -> bool { |
| 33 | + for b in charset.to_bytes() { |
| 34 | + if c == *b { |
| 35 | + return true; |
| 36 | + } |
| 37 | + } |
| 38 | + false |
| 39 | +} |
| 40 | + |
| 41 | +#[cfg(test)] |
| 42 | +mod test { |
| 43 | + #[test] |
| 44 | + fn complete() { |
| 45 | + let charset = c"0123456789"; |
| 46 | + let s = c"abcdef"; |
| 47 | + assert_eq!( |
| 48 | + unsafe { super::strcspn(s.as_ptr().cast(), charset.as_ptr().cast()) }, |
| 49 | + 6 |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + #[test] |
| 54 | + fn subset() { |
| 55 | + let charset = c"0123456789"; |
| 56 | + let s = c"xyz1"; |
| 57 | + assert_eq!( |
| 58 | + unsafe { super::strcspn(s.as_ptr().cast(), charset.as_ptr().cast()) }, |
| 59 | + 3 |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + #[test] |
| 64 | + fn none() { |
| 65 | + let charset = c"0123456789"; |
| 66 | + let s = c"567"; |
| 67 | + assert_eq!( |
| 68 | + unsafe { super::strcspn(s.as_ptr().cast(), charset.as_ptr().cast()) }, |
| 69 | + 0 |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + #[test] |
| 74 | + fn empty_charset() { |
| 75 | + let charset = c""; |
| 76 | + let s = c"AABBCCDD"; |
| 77 | + assert_eq!( |
| 78 | + unsafe { super::strcspn(s.as_ptr().cast(), charset.as_ptr().cast()) }, |
| 79 | + 8 |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + #[test] |
| 84 | + fn empty_string() { |
| 85 | + let charset = c"0123456789"; |
| 86 | + let s = c""; |
| 87 | + assert_eq!( |
| 88 | + unsafe { super::strcspn(s.as_ptr().cast(), charset.as_ptr().cast()) }, |
| 89 | + 0 |
| 90 | + ); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +// End of file |
0 commit comments