[RFC/PATCH v3 6/8] libbb: add is_suffixed_with() function
Bartosz Gołaszewski
bartekgola at gmail.com
Thu Aug 27 09:18:58 UTC 2015
2015-08-27 0:45 GMT+02:00 Tito <farmatito at tiscali.it>:
>
> 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
>> <bartekgola at gmail.com> wrote:
>>> 2015-08-25 19:29 GMT+02:00 Xabier Oneca -- xOneca <xoneca at gmail.com>:
>>>> Hello Bartosz,
>>>>
>>>> 2015-08-25 13:10 GMT+02:00 Bartosz Golaszewski <bartekgola at gmail.com>:
>>>>> +/*
>>>>> + * 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.
>
Hi Tito,
seems to work fine. If you will be sending a patch, can you include
the following test case as well?
BBUNIT_ASSERT_STREQ("foo", is_suffixed_with("barfoofoo", "foo"));
--
Best regards,
Bartosz Golaszewski
More information about the busybox
mailing list