mirror of
https://github.com/transmission/transmission
synced 2024-12-26 01:27:28 +00:00
(trunk libT) avoid unnecessary malloc()s & free()s in tr_utf8clean()
This commit is contained in:
parent
cdd779bb2a
commit
54e2e8ca2f
1 changed files with 26 additions and 17 deletions
|
@ -1352,16 +1352,23 @@ tr_lowerBound( const void * key,
|
||||||
char*
|
char*
|
||||||
tr_utf8clean( const char * str, int max_len, tr_bool * err )
|
tr_utf8clean( const char * str, int max_len, tr_bool * err )
|
||||||
{
|
{
|
||||||
const char zero = '\0';
|
|
||||||
char * ret;
|
char * ret;
|
||||||
struct evbuffer * buf = evbuffer_new( );
|
|
||||||
const char * end;
|
const char * end;
|
||||||
|
|
||||||
|
if( max_len < 0 )
|
||||||
|
max_len = (int) strlen( str );
|
||||||
|
|
||||||
if( err != NULL )
|
if( err != NULL )
|
||||||
*err = FALSE;
|
*err = FALSE;
|
||||||
|
|
||||||
if( max_len < 0 )
|
if( tr_utf8_validate( str, max_len, &end ) )
|
||||||
max_len = (int) strlen( str );
|
{
|
||||||
|
ret = tr_strndup( str, max_len );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
const char zero = '\0';
|
||||||
|
struct evbuffer * buf = evbuffer_new( );
|
||||||
|
|
||||||
while( !tr_utf8_validate ( str, max_len, &end ) )
|
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, str, max_len );
|
||||||
evbuffer_add( buf, &zero, 1 );
|
evbuffer_add( buf, &zero, 1 );
|
||||||
ret = tr_memdup( EVBUFFER_DATA( buf ), EVBUFFER_LENGTH( buf ) );
|
ret = tr_memdup( EVBUFFER_DATA( buf ), EVBUFFER_LENGTH( buf ) );
|
||||||
assert( tr_utf8_validate( ret, -1, NULL ) );
|
|
||||||
evbuffer_free( buf );
|
evbuffer_free( buf );
|
||||||
|
}
|
||||||
|
|
||||||
|
assert( tr_utf8_validate( ret, -1, NULL ) );
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue