Add strncmp as a function to strings.h. Also adds unit tests, and adds
strncmp as an entrypoint for all current platforms.
Details
- Reviewers
sivachandra - Commits
- rGc6d03b583b48: [libc] add strncmp to strings
Diff Detail
- Repository
- rG LLVM Github Monorepo
Event Timeline
libc/src/string/strncmp.cpp | ||
---|---|---|
19 | Nitty comment: Why can it not be just n == 0? | |
22 | Another nitty comment - This for statement is hard to read. Can we make it simpler like this: for (; n > 0; --n, ++left, ++right) { char lc = *left; if (lc == '\0' || lc != *right) break; } | |
24 | Just pointing out as I am not sure what is right here: casting to unsigned char can remove the potential sign and lead to -1 > 0. Even strcmp is set up this way so may be clean up both of them in a follow up patch. | |
libc/test/src/string/strncmp_test.cpp | ||
159 | Fix. |
libc/src/string/strncmp.cpp | ||
---|---|---|
19 | nit: We could omit the braces since the body of if is simple. |
address comments
libc/src/string/strncmp.cpp | ||
---|---|---|
24 | given that these are supposed to be strings, I would assume that unsigned char is correct. Testing with my system libc, it appears that this is what it does as well. I ran strncmp on the following char array: [-1, 'a', '\0'] compared to "abc" with length 3 and got the value 158, which is 255 - 97 ('a' = 97). |
libc/src/string/strncmp.cpp | ||
---|---|---|
24 | Thanks for checking! |
Nitty comment: Why can it not be just n == 0?