public inbox for newlib@sourceware.org
 help / color / mirror / Atom feed
From: Sichen Zhao <1473996754@qq.com>
To: Eric Blake <eblake@redhat.com>, newlib@sourceware.org
Cc: gedare@rtems.org, joel@rtems.org, christian.mauderer@embedded-brains.de
Subject: Re: [PATCH 1/2] Import strnstr.c from FreeBSD.
Date: Sat, 26 Aug 2017 12:31:00 -0000	[thread overview]
Message-ID: <3881c2e8-59e9-4b5d-e058-125a05ae6ba1@qq.com> (raw)
In-Reply-To: <cee05563-df20-64d5-d552-5c1e8e5c930c@redhat.com>

> On 08/25/2017 01:07 PM, Eric Blake wrote:
>> This is a poor algorithm (O(n^2) because it is is calling an O(n)
>> strncmp() over every byte of haystack).  It also calls strlen() instead
>> of strnlen(), which (when the needle is bigger than the haystack, and
>> therefore the function must return NULL) is wasted effort.
>>
>> Instead of copying the poor BSD implementation, why not just open-code
>> it yourself to take advantage of our O(n) memmem(), which has already
>> been optimized to take advantage of a better algorithm that is not
>> quadratic?
>>
>> char *
>> strnstr(const char *haystack, const char *needle, size_t haystack_len)
>> {
>>    size_t needle_len = strnlen(needle, slen);
>>    if (needle_len < slen || !needle[needle_len])
>>      return memmem(haystack, haystack_len, needle, needle_len);
>>    return NULL;
>> }
> Hmm, I guess you also have to double-check that there is no NUL byte in
> haystack prior to the result returned by memmem(). But that is still O(n):
>
> char *
> strnstr(const char *haystack, const char *needle, size_t haystack_len)
> {
>    size_t needle_len = strnlen(needle, slen);
>    if (needle_len < slen || !needle[needle_len]) {
>      char *x = memmem(haystack, haystack_len, needle, needle_len);
>      if (x && !memchr(haystack, 0, x - haystack))
>        return x;
>    }
>    return NULL;
> }
Ok, So just modify the strnstr like that, right?



  reply	other threads:[~2017-08-26  2:19 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-08-24  5:47 Sichen Zhao
2017-08-24  2:14 ` [PATCH 2/2] Port strnstr.c to newlib Sichen Zhao
2017-08-24  8:50 ` [PATCH 1/2] Import strnstr.c from FreeBSD Corinna Vinschen
2017-08-24 14:34   ` Sichen Zhao
2017-08-29 14:56   ` Craig Howland
2017-08-25 18:12 ` Eric Blake
2017-08-25 19:18   ` Eric Blake
2017-08-26 12:31     ` Sichen Zhao [this message]
2017-08-26 13:03       ` Gedare Bloom
2017-08-26 13:28         ` Sichen Zhao

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3881c2e8-59e9-4b5d-e058-125a05ae6ba1@qq.com \
    --to=1473996754@qq.com \
    --cc=christian.mauderer@embedded-brains.de \
    --cc=eblake@redhat.com \
    --cc=gedare@rtems.org \
    --cc=joel@rtems.org \
    --cc=newlib@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).