How do I do the in-place equivalent of strstr()
for a counted string (i.e. not null-terminated) in C?
相关问题
- Multiple sockets for clients to connect to
- how to split a list into a given number of sub-lis
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- Generate string from integer with arbitrary base i
I just came across this and I'd like to share my implementation. It think it quite fast a I don't have any subcalls.
It returns the index in the haystack where the needle is found or -1 if it was not found.
I used this method
If you're afraid of O(m*n) behaviour - basically, you needn't, such cases don't occur naturally - here's a KMP implementation I had lying around which I've modified to take the length of the haystack. Also a wrapper. If you want to do repeated searches, write your own and reuse the
borders
array.No guarantees for bug-freeness, but it seems to still work.
See if the function below works for you. I haven't tested it thoroughly, so I would suggest you do so.