Skip to content

Commit 381a624

Browse files
authored
Add strncasecmp (#23)
* add strncasecmp
1 parent a62f24c commit 381a624

File tree

5 files changed

+61
-1
lines changed

5 files changed

+61
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Unreleased
44

5-
* None
5+
* [#23] - Add `strncasecmp`
66

77
## v0.3.0 (2022-10-18)
88

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff 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 = [
4647
abs = []
4748
strcmp = []
4849
strncmp = []
50+
strncasecmp = []
4951
strcpy = []
5052
strncpy = []
5153
strlen = []

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff 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

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ mod strncmp;
4343
#[cfg(feature = "strncmp")]
4444
pub use self::strncmp::strncmp;
4545

46+
mod strncasecmp;
47+
#[cfg(feature = "strncasecmp")]
48+
pub use self::strncasecmp::strncasecmp;
49+
4650
mod strcpy;
4751
#[cfg(feature = "strcpy")]
4852
pub use self::strcpy::strcpy;

src/strncasecmp.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
}

0 commit comments

Comments
 (0)