[PATCH] libbb: shrink is_suffixed_with()
Isaac Dunham
ibid.ag at gmail.com
Wed Aug 26 18:01:47 UTC 2015
On Wed, Aug 26, 2015 at 10:18:33AM -0700, Isaac Dunham wrote:
> By my test (Debian Jessie, GCC 4.9.2, glibc shared build), the results are:
> function old new delta
> is_suffixed_with 61 45 -16
> ------------------------------------------------------------------------------
> (add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-16) Total: -16 bytes
> text data bss dec hex filename
> 760460 2092 9080 771632 bc630 busybox_old
> 760444 2092 9080 771616 bc620 busybox_unstripped
> ---
> libbb/compare_string_array.c | 15 +++++----------
> 1 file changed, 5 insertions(+), 10 deletions(-)
>
> diff --git a/libbb/compare_string_array.c b/libbb/compare_string_array.c
> @@ -35,17 +35,12 @@ char* FAST_FUNC is_prefixed_with(const char *string, const char *key)
> */
> char* FAST_FUNC is_suffixed_with(const char *string, const char *key)
> {
> + char *ret;
>
> + ret = strstr(string, key);
> + if (ret && strcmp(ret, key))
> + ret = NULL;
> + return ret;
> }
I forgot to check/update the documentation; the behavior is slightly
different in the case of an empty key.
As a side effect of the implementation, it returns NULL if key is "",
unless string is also "".
It's probably not going to be smaller if this change is not kept.
Regardless which behavior is used, is_suffixed_with() can be used to check
that config files in a directory are named *.conf or for similar purposes;
I think standard modprobe has this behavior, so for example the file
/etc/modprobe.d/file.conf.ignore
will be ignored. If there's anywhere that we check this, I can't see it.
To prevent the function from being optimized out, I wrote a small applet
which I'm attaching.
Thanks,
Isaac Dunham
-------------- next part --------------
A non-text attachment was scrubbed...
Name: suftest.c
Type: text/x-csrc
Size: 838 bytes
Desc: not available
URL: <http://lists.busybox.net/pipermail/busybox/attachments/20150826/0d838a57/attachment.c>
More information about the busybox
mailing list