Changeset 6981


Ignore:
Timestamp:
Oct 29, 2008, 11:56:55 AM (12 years ago)
Author:
charles
Message:

(libT) one more low-hanging fruit from softwareelves' Shark reports: simplify tr_buildPath() to shave off another few %

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/utils.c

    r6961 r6981  
    603603tr_buildPath( const char *first_element, ... )
    604604{
    605     char            * ret;
    606     struct evbuffer * evbuf;
    607     const char      * element = first_element;
    608     va_list           vl;
    609 
    610     evbuf = evbuffer_new( );
     605    size_t bufLen = 0;
     606    const char * element;
     607    char * buf;
     608    char * pch;
     609    va_list vl;
     610
     611    /* pass 1: allocate enough space for the string */
    611612    va_start( vl, first_element );
    612 
    613     while( element )
    614     {
    615         if( EVBUFFER_LENGTH( evbuf ) )
    616             evbuffer_add_printf( evbuf, "%c", TR_PATH_DELIMITER );
    617         evbuffer_add_printf( evbuf, "%s", element );
     613    element = first_element;
     614    while( element ) {
     615        bufLen += strlen( element ) + 1;
    618616        element = (const char*) va_arg( vl, const char* );
    619617    }
    620 
     618    pch = buf = tr_new0( char, bufLen );
    621619    va_end( vl );
    622     ret = tr_strndup( EVBUFFER_DATA( evbuf ), EVBUFFER_LENGTH( evbuf ) );
    623     evbuffer_free( evbuf );
    624     return ret;
     620
     621    /* pass 2: build the string piece by piece */
     622    va_start( vl, first_element );
     623    element = first_element;
     624    while( element ) {
     625        const size_t elementLen = strlen( element );
     626        if( pch != buf )
     627            *pch++ = TR_PATH_DELIMITER;
     628        memcpy( pch, element, elementLen );
     629        pch += elementLen;
     630        element = (const char*) va_arg( vl, const char* );
     631    }
     632    va_end( vl );
     633
     634    /* sanity checks & return */
     635    *pch++ = '\0';
     636    assert( pch - buf == bufLen );
     637    return buf;
    625638}
    626639
Note: See TracChangeset for help on using the changeset viewer.