[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: [libvirt] [PATCHv2 1/2] sysinfo: add virSkipSpacesBackwards()



On 06/28/2011 10:42 PM, Minoru Usui wrote:
> sysinfo: add virSkipSpacesBackwards()
> 
>   * Add virSkipSpacesBackwards() to src/util/util.[ch]
> 
> Signed-off-by: Minoru Usui <usui mxm nes nec co jp>
> ---
>  src/util/util.c |   25 +++++++++++++++++++++++++
>  src/util/util.h |    1 +
>  2 files changed, 26 insertions(+), 0 deletions(-)

> + * virSkipSpacesBackwards:
> + * @str : pointer to the target strings
> + * @endp: pointer to the end of @str
> + *
> + * Skip potential blanks backwards.
> + */
> +void
> +virSkipSpacesBackwards(const char *str, char **endp)
> +{
> +    char *cur;
> +
> +    if (!endp || !*endp)
> +        return;

This interface is rather limited, because it cannot modify str in-place.
 I would find it more flexible if we could allow modifications or a way
to let the interface do strlen() for me instead of me having to always
find the string end first, so I wonder if we should instead add two
interfaces:

/**
 * virTrimSpaces:
 * @str: string to modify to remove all trailing spaces
 * @endp: track the end of the string
 *
 * If @endp is NULL on entry, then all spaces prior to the trailing
 * NUL in @str are removed, by writing NUL into the appropriate
 * location.  If @endp is non-NULL but points to a NULL pointer,
 * then all spaces prior to the trailing NUL in @str are removed,
 * NUL is written to the new string end, and endp is set to the
 * location of the (new) string end.  If @endp is non-NULL and
 * points to a non-NULL pointer, then that pointer is used as
 * the end of the string, endp is set to the (new) location, but
 * no NUL pointer is written into the string.
 */
void
virTrimSpaces(char *str, char **endp)
{
    char *end;

    if (!endp || !*endp)
        end = str + strlen(str);
    while (end > str && c_isspace(end[-1]))
        end--;
    if (endp) {
        if (!*endp)
            *end = '\0';
        *endp = end;
    } else {
        *end = '\0';
    }
}
/**
 * virSkipSpacesBackwards:
 * @str: start of string
 * @endp: on entry, must point to a location with @str, on exit,
 * will be adjusted to skip trailing spaces
 *
 * Returns 0 on success, -1 on error
 */
int
virSkipSpacesBackwards(const char *str, char **endp)
{
    if (!endp || !*endp)
        return -1;
    /* Casting away const is safe, since virTrimSpaces does not
     * modify string with this particular usage.  */
    virTrimSpaces((char *)str), endp);
    return 0;
}

-- 
Eric Blake   eblake redhat com    +1-801-349-2682
Libvirt virtualization library http://libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]