On 08/25/2015 09:49 PM, Denys Vlasenko wrote:
> Fixed in git
> 
> On Tue, Aug 25, 2015 at 8:16 PM, Bartosz Gołaszewski
> <[email protected]> wrote:
>> 2015-08-25 19:29 GMT+02:00 Xabier Oneca  --  xOneca <[email protected]>:
>>> Hello Bartosz,
>>>
>>> 2015-08-25 13:10 GMT+02:00 Bartosz Golaszewski <[email protected]>:
>>>> +/*
>>>> + * Return NULL if string is not suffixed with key. Return pointer to the
>>>> + * beginning of prefix key in string. If key is an empty string return 
>>>> pointer
>>>> + * to the end of string.
>>>> + */
>>>> +char* FAST_FUNC is_suffixed_with(const char *string, const char *key)
>>>> +{
>>>> +       size_t str_len = strlen(string), key_len = strlen(key);
>>>> +
>>>> +       if (str_len >= key_len) {
>>>> +               if (strcmp(string + str_len - key_len, key) == 0) {
>>>> +                       return (char*)key;
>>>> +               }
>>>> +       }
>>>> +
>>>> +       return NULL;
>>>> +}
>>>
>>> Sorry to bug you again with this function, and I know it is too late,
>>> but it doesn't do what the comment before the function says it does.
>>>
>>> It says it returns a pointer to the beginning of prefix key *in
>>> string*, but what returns is a pointer to *key*. It's a subtle change,
>>> but as it is now the return value may not be as useful... (it's like
>>> returning 1/0 but with key/NULL.)
>>>
>>> Cheers,
>>>
>>> Xabier Oneca_,,_
>>
>> You're right, I missed that one. Will send a patch tomorrow.
>>
>> --
>> Best regards,
>> Bartosz Golaszewski

Hi,
I just discovered that libbb has a strrstr function,
so we can do:

char* FAST_FUNC is_suffixed_with(const char *string, const char *key)
{
        char *s = strrstr(string, key);
        if (s && strcmp(s, key) == 0)
                return s;
        return NULL;
}

this could save some space:

 ./scripts/bloat-o-meter busybox_old busybox_unstripped
function                                             old     new   delta
is_suffixed_with                                      83      47    -36
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-36)            Total: -36 bytes

Briefly tested and seems to work the same as before.

Ciao,
Tito

--- libbb/compare_string_array.c.orig   2015-08-27 00:39:27.694416966 +0200
+++ libbb/compare_string_array.c        2015-08-27 00:40:17.606664467 +0200
@@ -35,16 +35,9 @@ char* FAST_FUNC is_prefixed_with(const c
  */
 char* FAST_FUNC is_suffixed_with(const char *string, const char *key)
 {
-       size_t key_len = strlen(key);
-       ssize_t len_diff = strlen(string) - key_len;
-
-       if (len_diff >= 0) {
-               string += len_diff;
-               if (strcmp(string, key) == 0) {
-                       return (char*)string;
-               }
-       }
-
+       char *s = strrstr(string, key);
+       if (s && strcmp(s, key) == 0)
+               return s;
        return NULL;
 }

_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to