You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@apr.apache.org by Vaclav Ovsik <Va...@i.cz> on 2003/03/27 12:14:27 UTC

to add function apr_chomp into string functions?

Hello,
I have need to strip the line separator from a string now. Something
like perl's chomp could be useful, when a text file is read by
e.g. apr_file_gets().
In perl ...

zito@bobek zito $ perl -e '$_="BLA\n"; chomp $_; print "X${_}X\n";'
XBLAX

What about to include code like below to APR under ./strings  ?

/**
 * Strip trailing EOL from a string (in place)
 * @param s The string to chop EOL in place.
 */
APR_DECLARE(char *) apr_chomp(char *s);


APR_DECLARE(char *) apr_chomp(char *s)
{
    int s_len, eol_len;
    char *p;

    s_len = strlen(s);
    eol_len = strlen(APR_EOL_STR);
    if ( s_len < eol_len )
        return s;
    p = s + s_len - eol_len;
    if ( strcmp(p, APR_EOL_STR) == 0 )
        *p = (char)0;
    return s;
}

Maybe I missed another simple way to do this, please tell mi if.
-- 
Zito

Re: to add function apr_chomp into string functions?

Posted by Vaclav Ovsik <Va...@i.cz>.
On Thu, Mar 27, 2003 at 12:14:27PM +0100, Vaclav Ovsik wrote:
> APR_DECLARE(char *) apr_chomp(char *s)
> {
>     int s_len, eol_len;
>     char *p;
> 
>     s_len = strlen(s);
>     eol_len = ststrlenrlen(APR_EOL_STR);

     eol_len = sizeof(APR_EOL_STR) -1;

>     if ( s_len < eol_len )
>         return s;
>     p = s + s_len - eol_len;
>     if ( strcmp(p, APR_EOL_STR) == 0 )
>         *p = (char)0;
>     return s;
> }

strlen() from const string is superfluous, sizeof -1 at compile time
may be better.
-- 
Zito