This is an archive of the discontinued LLVM Phabricator instance.

[libc] Add memmem implementation
ClosedPublic

Authored by Caslyn on Apr 7 2023, 4:12 PM.

Details

Summary

Introduce the memmem libc string function.

memmem_implementation performs shared logic for strstr,
strcasestr, and memmem; essentially reconfiguring what was the
strstr_implementation to support length parameters.

Diff Detail

Event Timeline

Caslyn created this revision.Apr 7 2023, 4:12 PM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptApr 7 2023, 4:12 PM
Caslyn requested review of this revision.Apr 7 2023, 4:12 PM
Caslyn updated this revision to Diff 511821.Apr 7 2023, 4:17 PM

Fix typo

Code looks good to me, I've added @gchatelet for his opinion on memmem being in memory_utils.

libc/src/string/memmem.cpp
1

nit: typo

mcgrathr added inline comments.
libc/src/string/memory_utils/strstr_implementations.h
17

It might make sense to keep this around and still call it from strstr, even if strstr_implementation just calls memmem_implementation. An optimized strstr might roll the length determination into the search more efficiently than strlen+memmem so it doesn't seem ideal to have the public entrypoint code assume that strlen+memmem is the implementation you want.

Caslyn updated this revision to Diff 512258.Apr 10 2023, 2:05 PM

Fix typos

Caslyn marked an inline comment as done.Apr 10 2023, 2:06 PM
Caslyn updated this revision to Diff 512262.Apr 10 2023, 2:21 PM

Add back strstr_implementations to call memmmem_implementation.

Caslyn marked an inline comment as done.Apr 10 2023, 2:25 PM
Caslyn added inline comments.
libc/src/string/memory_utils/strstr_implementations.h
17

That makes sense - thanks for pointing out.

Caslyn marked an inline comment as done.Apr 11 2023, 11:44 AM

Code looks good to me, I've added @gchatelet for his opinion on memmem being in memory_utils.

There's a bit of urgency to have this merged so we can begin some testing on Fuchsia, @gchatelet would appreciate your review as soon as you can.

mcgrathr accepted this revision.Apr 11 2023, 12:20 PM

LGTM. We can look into any possible code-sharing with strstr_implementation later along with optimization.

libc/src/string/memmem.cpp
19

Can be constexpr.

This revision is now accepted and ready to land.Apr 11 2023, 12:20 PM
gchatelet accepted this revision.Apr 11 2023, 12:46 PM

Sorry for the lag, Monday was off in France and I missed the patch today.
I'm not blocking this patch but please consider the suggested changes.

libc/src/string/memory_utils/memmem_implementations.h
19

Not necessarily for this patch but it may be better to use the cpp::string_view abstraction for both needle and haystack.

We should also return a const char* to keep types as sane as possible.
This way we const_cast only where it actually matters, i.e. in memmem.

libc/test/src/string/memmem_test.cpp
82

put on a single line

98

same here

Sorry for the lag, Monday was off in France and I missed the patch today.
I'm not blocking this patch but please consider the suggested changes.

These are good suggestions and easy to apply right away before landing.

libc/test/src/string/memmem_test.cpp
98

Note that clang-format will use separate lines for each element when there is a trailing comma before the end-brace, and will compact things when there is no trailing comma.

mcgrathr added inline comments.Apr 11 2023, 12:51 PM
libc/src/string/memmem.cpp
19

If this is intended as an ordering comparison and not just equality, then generally unsigned char should be used.

Caslyn updated this revision to Diff 512570.Apr 11 2023, 1:46 PM
Caslyn marked 5 inline comments as done.

Updates to memmem.cpp/memmem_test.cpp per review feedback

Thanks for the quick response @gchatelet!

libc/src/string/memory_utils/memmem_implementations.h
19

I'll be sure to address this in a follow up patch.

libc/test/src/string/memmem_test.cpp
98

Ah - thanks. I noticed this behavior with clang-format but wasn't sure how to work around it. Done.

This revision was automatically updated to reflect the committed changes.