1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-25 17:17:31 +00:00

(trunk libT) avoid unnecessary malloc()s & free()s in tr_utf8clean()

This commit is contained in:
Charles Kerr 2009-04-16 21:00:48 +00:00
parent cdd779bb2a
commit 54e2e8ca2f

View file

@ -1352,16 +1352,23 @@ tr_lowerBound( const void * key,
char*
tr_utf8clean( const char * str, int max_len, tr_bool * err )
{
const char zero = '\0';
char * ret;
struct evbuffer * buf = evbuffer_new( );
const char * end;
if( max_len < 0 )
max_len = (int) strlen( str );
if( err != NULL )
*err = FALSE;
if( max_len < 0 )
max_len = (int) strlen( str );
if( tr_utf8_validate( str, max_len, &end ) )
{
ret = tr_strndup( str, max_len );
}
else
{
const char zero = '\0';
struct evbuffer * buf = evbuffer_new( );
while( !tr_utf8_validate ( str, max_len, &end ) )
{
@ -1379,8 +1386,10 @@ tr_utf8clean( const char * str, int max_len, tr_bool * err )
evbuffer_add( buf, str, max_len );
evbuffer_add( buf, &zero, 1 );
ret = tr_memdup( EVBUFFER_DATA( buf ), EVBUFFER_LENGTH( buf ) );
assert( tr_utf8_validate( ret, -1, NULL ) );
evbuffer_free( buf );
}
assert( tr_utf8_validate( ret, -1, NULL ) );
return ret;
}