[libc] Adds memchr implementation with corresponding unit tests. Uses a simple loop to iterate through the string.
Details
Diff Detail
- Repository
- rG LLVM Github Monorepo
Event Timeline
libc/src/string/memchr.cpp | ||
---|---|---|
19 | Do you need the explicit static cast? Without it, wouldn't the compile time implicit cast do the same thing? | |
23 | I think you don't need need the cast to void*. | |
25 | Return nullptr? | |
libc/test/src/string/memchr_test.cpp | ||
19 | To avoid the noisy casts to const char *, may be use a helper function call call_memchr here and in other tests. | |
20 | I don't think you need the casts to void* here and in the rest of the tests. | |
23 | Why not ASSERT_STREQ? | |
33 | Ditto. |
libc/src/string/memchr.cpp | ||
---|---|---|
19 | Semantically they're the same thing. I just assumed that when narrowing or widening, its always better to be explicit. | |
libc/test/src/string/memchr_test.cpp | ||
33 | Since src is not null-terminated, ASSERT_STREQ is going to look beyond the end of the array. I could add a null terminator to the end of ret, but don't know if that's much better than this approach. Is there an approach you were thinking of? |
libc/src/string/memchr.cpp | ||
---|---|---|
19 | You still need a cast. I was asking about the explicit static cast, but at least the implicit cast is requried. That is: unsigned char ch = c; for (; n && *str != ch; --n, ++str) ... Without this, and if char type were signed char, then this will happen: char c = -1; char mem[5] = {c, c, c, c, c}; memchr(mem, c, 5); The unsigned char values in mem are all 255. But, they will get promoted to an int value of 255 and then compared with an int value of -1. So memchr will return nullptr. Sorry for not catching this earlier. |
Add it to the aarch64 list as well?