File tree Expand file tree Collapse file tree 5 files changed +61
-1
lines changed Expand file tree Collapse file tree 5 files changed +61
-1
lines changed Original file line number Diff line number Diff line change 22
33## Unreleased
44
5- * None
5+ * [ # 23 ] - Add ` strncasecmp `
66
77## v0.3.0 (2022-10-18)
88
Original file line number Diff line number Diff line change @@ -22,6 +22,7 @@ all = [
2222 " abs" ,
2323 " strcmp" ,
2424 " strncmp" ,
25+ " strncasecmp" ,
2526 " strcpy" ,
2627 " strncpy" ,
2728 " strlen" ,
@@ -46,6 +47,7 @@ all = [
4647abs = []
4748strcmp = []
4849strncmp = []
50+ strncasecmp = []
4951strcpy = []
5052strncpy = []
5153strlen = []
Original file line number Diff line number Diff line change @@ -17,6 +17,7 @@ This crate basically came about so that the [nrfxlib](https://github.com/NordicP
1717* isupper
1818* strcmp
1919* strncmp
20+ * strncasecmp
2021* strcpy
2122* strncpy
2223* strlen
Original file line number Diff line number Diff line change @@ -43,6 +43,10 @@ mod strncmp;
4343#[ cfg( feature = "strncmp" ) ]
4444pub use self :: strncmp:: strncmp;
4545
46+ mod strncasecmp;
47+ #[ cfg( feature = "strncasecmp" ) ]
48+ pub use self :: strncasecmp:: strncasecmp;
49+
4650mod strcpy;
4751#[ cfg( feature = "strcpy" ) ]
4852pub use self :: strcpy:: strcpy;
Original file line number Diff line number Diff line change 1+ //! Rust implementation of C library function `strncasecmp`
2+ //!
3+ //! Licensed under the Blue Oak Model Licence 1.0.0
4+
5+ use crate :: { CChar , CInt } ;
6+
7+ /// Rust implementation of C library function `strncasecmp`. Passing NULL
8+ /// (core::ptr::null()) gives undefined behaviour.
9+ #[ cfg_attr( feature = "strncasecmp" , no_mangle) ]
10+ pub unsafe extern "C" fn strncasecmp ( s1 : * const CChar , s2 : * const CChar , n : usize ) -> CInt {
11+ for i in 0 ..n {
12+ let s1_i = s1. add ( i) ;
13+ let s2_i = s2. add ( i) ;
14+
15+ let val = ( * s1_i) . to_ascii_lowercase ( ) as CInt - ( * s2_i) . to_ascii_lowercase ( ) as CInt ;
16+ if val != 0 || * s1_i == 0 {
17+ return val;
18+ }
19+ }
20+ 0
21+ }
22+
23+ #[ cfg( test) ]
24+ mod test {
25+ use super :: * ;
26+
27+ #[ test]
28+ fn matches ( ) {
29+ let a = b"abc\0 " ;
30+ let b = b"AbCDEF\0 " ;
31+ let result = unsafe { strncasecmp ( a. as_ptr ( ) , b. as_ptr ( ) , 3 ) } ;
32+ // Match!
33+ assert_eq ! ( result, 0 ) ;
34+ }
35+
36+ #[ test]
37+ fn no_match ( ) {
38+ let a = b"123\0 " ;
39+ let b = b"x1234\0 " ;
40+ let result = unsafe { strncasecmp ( a. as_ptr ( ) , b. as_ptr ( ) , 3 ) } ;
41+ // No match, first string first
42+ assert ! ( result < 0 ) ;
43+ }
44+
45+ #[ test]
46+ fn no_match2 ( ) {
47+ let a = b"bbbbb\0 " ;
48+ let b = b"aaaaa\0 " ;
49+ let result = unsafe { strncasecmp ( a. as_ptr ( ) , b. as_ptr ( ) , 3 ) } ;
50+ // No match, second string first
51+ assert ! ( result > 0 ) ;
52+ }
53+ }
You can’t perform that action at this time.
0 commit comments