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)?

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"

> Try to have more descriptive names. This helps in understanding code,
> especially _at _callsites_.
> 
> > {
> >     char *t = NULL;
> > 
> >     if (!s) return NULL;
> > 
> >     if (c)
> >             t = strchr(s, c);
> >     else
> >             t = s;
> 
> 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()

> >     memmove(s, t, strlen(t));
> >     memset(s+strlen(t), 0, strlen(s)-strlen(t));
> >     return s;
> 
> Maybe just return strcpy(s,t) ?

In all cases the s is modified and memmove() authorizes the overlapping.

trim.c ================================================================
#include "libbb.h"

/*
 * Beware: these functions modify the buffer
 * The return value is there to simplify the tests:
 * if (bb_strtrim(s, '#'))
 *      ...
 */

char *bb_strtrim(char *s, int c)
{
        char *t;

        if (!s) return NULL;

        t = c ? strchrnul(s, c) : s;

        if (t) {
                t = skip_whitespace(t);
                memmove(s, t, strlen(t));
                memset(s+strlen(t), 0, strlen(s)-strlen(t));
        }

        return s;
}

char *bb_strrtrim(char *s, int c)
{
        char *t;

        if (!s) return NULL;

        t = c ? strrchr(s, c) : s;

        while (t-- > s) {
                if (!isspace(*t)) {
                        t[1] = '\0';
                        break;
                }
        }

        return s;
}

char *bb_strtrim_index(char *s, unsigned int i)
{
        if (!s || i > strlen(s)) {
                return (s = NULL);
        }

        return bb_strtrim(s, s[i]);
}

char *bb_strrtrim_index(char *s, unsigned int i)
{
        if (!s || i > strlen(s)) {
                return (s = NULL);
        }

        return bb_strrtrim(s, s[i]);
}

char *bb_trimmed_slice(char *s, int start_c, int end_c)
{
        bb_strtrim(s, start_c);
        bb_strrtrim(s, end_c);

        return s;
}

char *bb_trim(char *s)
{
        return bb_trimmed_slice(s, 0, 0);
}




_______________________________________________
busybox mailing list
[email protected]
http://busybox.net/cgi-bin/mailman/listinfo/busybox

Reply via email to