libbb/trim.c

Bernhard Fischer rep.dot.nop at gmail.com
Fri Jun 15 07:48:53 UTC 2007


On Thu, Jun 14, 2007 at 11:42:57PM +0200, souf wrote:
>Le jeudi 14 juin 2007 à 10:14 +0200, Denis Vlasenko a écrit :
>> From name I can infer that this function trims something from a string.
>> What does it trim? where (from the start, end, both)?

In my mind trim() strips off trailing whitespace
(http://gcc.gnu.org/onlinedocs/gfortran/TRIM.html fwiw).

perhaps this would be intuitive to understand:
trim() /* strip both leading and trailing whitespace */
ltrim() /* strip whitespace from the start of the string (usually
leading) */
rtrim() /* strip whitespace from the end (aka trailing) */
We are not concerned about bidi, imho
>
>if we can understand strchr() and strrchr(), we can naturally understand bb_strtrim() and bb_strrtrim()
>example:
>char s[] = "    BusyBox: The Swiss Army Knife of Embedded Linux      ";
>
>bb_strtrim(s, ':') = ": The Swiss Army Knife of Embedded Linux      "
>bb_strtrim(s, 0) = "BusyBox: The Swiss Army Knife of Embedded Linux      "
>we can rename it : bb_trim_leading()
>
>bb_strrtrim(s, ':') = "    BusyBox"
>bb_strrtrim(s, 0) = "    BusyBox: The Swiss Army Knife of Embedded Linux"
>we can rename it : bb_trim_trailing()
>
>bb_strtrim_index(s, 11) = ": The Swiss Army Knife of Embedded Linux      "
>bb_trim_leading_index()
>
>bb_strrtrim_index(s, 11) = "    BusyBox"
>bb_trim_trailing_index()
>
>bb_trimmed_slice(s, ':', 'K') = ": The Swiss Army"
>
>bb_trim(s) = "BusyBox: The Swiss Army Knife of Embedded Linux"
>

>> First t = NULL is superfluous.
>> > 
>> > 	t = skip_whitespace(t);
>> 
>> If c!=0 and it is not found, we're toast (t==NULL).
>
>Ok, I replaced strchr() by strchrnul()

strchrnul is a GNU extension. You need to provide a fallback. (It's a
bad idea that we use -std=gnu99. We should really only use -std=c99).




More information about the busybox mailing list