[RFC/PATCH v2 3/5] libbb: add ends_with() function

walter harms wharms at bfs.de
Sat Aug 22 09:21:54 UTC 2015



Am 21.08.2015 22:34, schrieb Xabier Oneca -- xOneca:
> Hello Walter,
> 
> 2015-08-21 18:26 GMT+02:00 walter harms <wharms at bfs.de>:
>>
>> maybe its a bit late but ...
>>
>> the function name is a bit unfortunate whats about has_suffix() ?
>>
>> you can improve readability by returning strcmp directly and calculating
>> the len immediately.
>>
>> int has_suffix(const char *str, const char *key)
>> {
>>         size_t len = strlen(str)-strlen(key);
>>
>>         if (len < 0 )
>>                 return -1;
>>
>>         return strcmp(str + len, key);
>> }
>>
>> re,
>>  wh
> 
> strcmp returns 0 when they match. I think it makes more sense if
> has_suffix returns 1 if match, 0 otherwise:
> 
>     return !strcmp(str + str_len - key_len, key);
> 
> Also, can len be < 0? Isn't size_t unsigned?


yep, you are right to much c&p, i should have stayed with int.
my idea was make something compatible to strcmp().

my original also had tests for !key etc. the question is: Is that needed ?

re,
 wh



More information about the busybox mailing list