RESOLVED FIXED Bug 28901
strnstr in wtf/StringExtras.h
https://bugs.webkit.org/show_bug.cgi?id=28901
Summary strnstr in wtf/StringExtras.h
Fumitoshi Ukai
Reported 2009-09-01 20:20:54 PDT
Add strnstr in wtf/StringExtras.h MacOSX has strnstr(), but at least Linux and Windows don't have it.
Attachments
Add strnstr for Linux and Windows in StringExtras.h (1.22 KB, patch)
2009-09-01 22:28 PDT, Fumitoshi Ukai
no flags
Add strnstr for Linux and Windows in StringExtras.h (1.31 KB, patch)
2009-09-02 02:21 PDT, Fumitoshi Ukai
no flags
Add strnstr for Linux and Windows in StringExtras.h (1.35 KB, patch)
2009-09-02 18:28 PDT, Fumitoshi Ukai
no flags
Fumitoshi Ukai
Comment 1 2009-09-01 22:28:02 PDT
Created attachment 38912 [details] Add strnstr for Linux and Windows in StringExtras.h --- 2 files changed, 26 insertions(+), 0 deletions(-)
Alexey Proskuryakov
Comment 2 2009-09-01 23:46:18 PDT
Comment on attachment 38912 [details] Add strnstr for Linux and Windows in StringExtras.h > + return NULL; Should be 0, not NULL. Is this new code, or did you take it from somewhere? The implementation in <http://www.opensource.apple.com/source/Libc/Libc-583/string/FreeBSD/strnstr.c> looks quite different, but I do not know if any of the differences are practical.
Eric Seidel (no email)
Comment 3 2009-09-02 00:30:08 PDT
Comment on attachment 38912 [details] Add strnstr for Linux and Windows in StringExtras.h I would have used longer names for the variables. Just because this is a POSIX method doesn't mean we have to use old c-style naming conventions. ;) r-, but not for the naming nit, rather for the nits above from ap. :) Thanks for the patch!
Fumitoshi Ukai
Comment 4 2009-09-02 00:34:21 PDT
Thanks for the review (In reply to comment #2) > (From update of attachment 38912 [details]) > > + return NULL; > > Should be 0, not NULL. > > Is this new code, or did you take it from somewhere? The implementation in > <http://www.opensource.apple.com/source/Libc/Libc-583/string/FreeBSD/strnstr.c> > looks quite different, but I do not know if any of the differences are > practical. This is new code. Should we use FreeBSD implementation? I'm slightly worried about copyright issue (FreeBSD one has advertise clause).
Fumitoshi Ukai
Comment 5 2009-09-02 02:21:50 PDT
Created attachment 38917 [details] Add strnstr for Linux and Windows in StringExtras.h --- 2 files changed, 26 insertions(+), 0 deletions(-)
Darin Adler
Comment 6 2009-09-02 09:18:55 PDT
Comment on attachment 38917 [details] Add strnstr for Linux and Windows in StringExtras.h This is a fairly long function. Do we really want it as an inline? I suppose we don't have a StringExtras.cpp so this is OK for now. The name "find" is not really good as a name for the second argument, because it's a verb, not a noun. The second argument is the string to find, not "a find". So I suggest calling it something like "target". > + int findLength = strlen(find); The result of strlen is a size_t, not an int. There's no reason to use int instead of size_t here. > + for (const char* start = buffer; *start && start + findLength <= buffer + bufferLength; start++) { > + if (*start == *find && strncmp(start, find, findLength) == 0) > + return const_cast<char*>(start); > + } I'd think you'd want to call strncmp on start + 1 and find + 1 and findLength - 1 instead. r=me but I think it's worth fixing some of those things I mentioned. So you could clear the review flag if you intend to post a new patch.
Fumitoshi Ukai
Comment 7 2009-09-02 18:28:14 PDT
Created attachment 38958 [details] Add strnstr for Linux and Windows in StringExtras.h --- 2 files changed, 26 insertions(+), 0 deletions(-)
Fumitoshi Ukai
Comment 8 2009-09-02 18:29:24 PDT
(In reply to comment #6) > (From update of attachment 38917 [details]) > This is a fairly long function. Do we really want it as an inline? I suppose we > don't have a StringExtras.cpp so this is OK for now. > > The name "find" is not really good as a name for the second argument, because > it's a verb, not a noun. The second argument is the string to find, not "a > find". So I suggest calling it something like "target". > > > + int findLength = strlen(find); > > The result of strlen is a size_t, not an int. There's no reason to use int > instead of size_t here. > > > + for (const char* start = buffer; *start && start + findLength <= buffer + bufferLength; start++) { > > + if (*start == *find && strncmp(start, find, findLength) == 0) > > + return const_cast<char*>(start); > > + } > > I'd think you'd want to call strncmp on start + 1 and find + 1 and findLength - > 1 instead. > > r=me but I think it's worth fixing some of those things I mentioned. So you > could clear the review flag if you intend to post a new patch. Thanks for review. Fixed those things.
Eric Seidel (no email)
Comment 9 2009-09-03 01:49:31 PDT
Comment on attachment 38958 [details] Add strnstr for Linux and Windows in StringExtras.h Based partially on Darin's previous r+, this still looks good. Thanks for the patch!
Eric Seidel (no email)
Comment 10 2009-09-03 02:38:07 PDT
Comment on attachment 38958 [details] Add strnstr for Linux and Windows in StringExtras.h Clearing flags on attachment: 38958 Committed r48012: <http://trac.webkit.org/changeset/48012>
Eric Seidel (no email)
Comment 11 2009-09-03 02:38:11 PDT
All reviewed patches have been landed. Closing bug.
Noah Friedman
Comment 12 2010-02-10 18:24:34 PST
This seems to be needed for PLATFORM(SOLARIS) also, at least with Solaris 10 sparc.
Note You need to log in before you can comment on or make changes to this bug.