<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
You're obviously right. I didn't look at it as a diff. Sorry.<br>
<br>
I would like to offer this (somewhat tested) code instead:<br>
<br>
<br>
char* FAST_FUNC last_char_is(char *s, char c)<br>
{<br>
if (s && *s) {<br>
while (*s != '\0') s++;<br>
s--;<br>
if (*s == c) return s;<br>
}<br>
return NULL;<br>
}<br>
<br>
<br>
For gcc -O2 (strlen="old", strrchr version blank, above="new"):<br>
text data bss dec hex filename<br>
91 0 0 91 5b last_char_is.o<br>
97 0 0 97 61 last_char_is_new.o<br>
139 0 0 139 8b last_char_is_old.o<br>
<br>
for gcc -Os:<br>
text data bss dec hex filename<br>
78 0 0 78 4e last_char_is.o<br>
85 0 0 85 55 last_char_is_new.o<br>
105 0 0 105 69 last_char_is_old.o<br>
<br>
The above code should be more efficient than both the strlen and
strrchr versions while still being smaller than the current strlen
version. It only scans the string once, doesn't do any unnecessary
work, and rejects both null and empty string inputs. I am not
certain of the consequences of dropping 'const' from the function,
but it looks like the function would be making a new copy of a
pointer variable to work with anyway.<br>
<br>
I wrote a version that calls strlen() but it was larger. All my
tests were done on x86_64.<br>
<br>
<div class="moz-cite-prefix">On 2020-07-01 09:34, Martin Lewis
wrote:<br>
</div>
<blockquote type="cite"
cite="mid:CA+nwWBs8RuYmugotZeApCH=ywokiHu_SNF8=6dNWkS-wu90Fbg@mail.gmail.com">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<div dir="ltr">Notice that there's a "-" before the line with
strlen.<br>
Here's the final implementation to simplify reading:<br>
<br>
char* FAST_FUNC last_char_is(const char *s, int c)<br>
{<br>
if (s) {<br>
char *index = strrchr(s, c);<br>
if (index && *(index + 1) == '\0')<br>
return index;<br>
}<br>
return NULL;<br>
}<br>
</div>
<br>
<div class="gmail_quote">
<div dir="ltr" class="gmail_attr">On Tue, 30 Jun 2020 at 10:59,
Jody Bruchon <<a href="mailto:jody@jodybruchon.com"
moz-do-not-send="true">jody@jodybruchon.com</a>> wrote:<br>
</div>
<blockquote class="gmail_quote" style="margin:0px 0px 0px
0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">strlen
scans the string; strrchr also scans the string.<br>
<br>
On 2020-07-01 10:23, Martin Lewis wrote:<br>
> Hi,<br>
><br>
> I'm not sure what I'm missing, from a quick glance at
strrchr <br>
> (glibc's) it seems that it scans the string only once?<br>
><br>
> Thank you,<br>
> Martin<br>
><br>
> On Tue, 30 Jun 2020 at 01:34, Denys Vlasenko <<a
href="mailto:vda.linux@googlemail.com" target="_blank"
moz-do-not-send="true">vda.linux@googlemail.com</a> <br>
> <mailto:<a href="mailto:vda.linux@googlemail.com"
target="_blank" moz-do-not-send="true">vda.linux@googlemail.com</a>>>
wrote:<br>
><br>
> This scans the string twice, unnecessarily. Let's not
do that.<br>
><br>
> On Thu, Jun 11, 2020 at 3:45 PM Martin Lewis<br>
> <<a href="mailto:martin.lewis.x84@gmail.com"
target="_blank" moz-do-not-send="true">martin.lewis.x84@gmail.com</a>
<mailto:<a href="mailto:martin.lewis.x84@gmail.com"
target="_blank" moz-do-not-send="true">martin.lewis.x84@gmail.com</a>>><br>
> wrote:<br>
> ><br>
> > function
old new <br>
> delta<br>
> > last_char_is
53 30 <br>
> -23<br>
> ><br>
>
------------------------------------------------------------------------------<br>
> > (add/remove: 0/0 grow/shrink: 0/1 up/down:
0/-23) Total:<br>
> -23 bytes<br>
> > text data bss dec hex filename<br>
> > 981322 16915 1872 1000109 f42ad
busybox_old<br>
> > 981299 16915 1872 1000086 f4296
busybox_unstripped<br>
> ><br>
> > Signed-off-by: Martin Lewis <<a
href="mailto:martin.lewis.x84@gmail.com" target="_blank"
moz-do-not-send="true">martin.lewis.x84@gmail.com</a><br>
> <mailto:<a
href="mailto:martin.lewis.x84@gmail.com" target="_blank"
moz-do-not-send="true">martin.lewis.x84@gmail.com</a>>><br>
> > ---<br>
> > libbb/last_char_is.c | 9 ++++-----<br>
> > 1 file changed, 4 insertions(+), 5 deletions(-)<br>
> ><br>
> > diff --git a/libbb/last_char_is.c
b/libbb/last_char_is.c<br>
> > index 66f2e3635..1fff08f9f 100644<br>
> > --- a/libbb/last_char_is.c<br>
> > +++ b/libbb/last_char_is.c<br>
> > @@ -13,11 +13,10 @@<br>
> > */<br>
> > char* FAST_FUNC last_char_is(const char *s, int
c)<br>
> > {<br>
> > - if (s && *s) {<br>
> > - size_t sz = strlen(s) - 1;<br>
> > - s += sz;<br>
> > - if ( (unsigned char)*s == c)<br>
> > - return (char*)s;<br>
> > + if (s) {<br>
> > + char *index = strrchr(s, c);<br>
> > + if (index && *(index +
1) == '\0')<br>
> > + return index;<br>
> > }<br>
> > return NULL;<br>
> > }<br>
> > --<br>
> > 2.11.0<br>
> ><br>
> > _______________________________________________<br>
> > busybox mailing list<br>
> > <a href="mailto:busybox@busybox.net"
target="_blank" moz-do-not-send="true">busybox@busybox.net</a>
<mailto:<a href="mailto:busybox@busybox.net"
target="_blank" moz-do-not-send="true">busybox@busybox.net</a>><br>
> > <a
href="http://lists.busybox.net/mailman/listinfo/busybox"
rel="noreferrer" target="_blank" moz-do-not-send="true">http://lists.busybox.net/mailman/listinfo/busybox</a><br>
><br>
><br>
> _______________________________________________<br>
> busybox mailing list<br>
> <a href="mailto:busybox@busybox.net" target="_blank"
moz-do-not-send="true">busybox@busybox.net</a><br>
> <a
href="http://lists.busybox.net/mailman/listinfo/busybox"
rel="noreferrer" target="_blank" moz-do-not-send="true">http://lists.busybox.net/mailman/listinfo/busybox</a><br>
<br>
_______________________________________________<br>
busybox mailing list<br>
<a href="mailto:busybox@busybox.net" target="_blank"
moz-do-not-send="true">busybox@busybox.net</a><br>
<a href="http://lists.busybox.net/mailman/listinfo/busybox"
rel="noreferrer" target="_blank" moz-do-not-send="true">http://lists.busybox.net/mailman/listinfo/busybox</a><br>
</blockquote>
</div>
</blockquote>
<br>
</body>
</html>