(trunk libT) Use tr_strndup() instead of tr_strdup() in tr_urlParse() to avoid a couple of strlen() calls

This commit is contained in:
Jordan Lee 2011-04-05 00:55:09 +00:00
parent ac3ec2db39
commit 1e0f92ba78
1 changed files with 7 additions and 3 deletions

View File

@ -1010,8 +1010,10 @@ tr_urlParse( const char * url_in,
int n; int n;
char * tmp; char * tmp;
char * pch; char * pch;
const char * protocol = NULL; size_t host_len;
size_t protocol_len;
const char * host = NULL; const char * host = NULL;
const char * protocol = NULL;
const char * path = NULL; const char * path = NULL;
tmp = tr_strndup( url_in, len ); tmp = tr_strndup( url_in, len );
@ -1019,12 +1021,14 @@ tr_urlParse( const char * url_in,
{ {
*pch = '\0'; *pch = '\0';
protocol = tmp; protocol = tmp;
protocol_len = pch - protocol;
pch += 3; pch += 3;
/*fprintf( stderr, "protocol is [%s]... what's left is [%s]\n", protocol, pch);*/ /*fprintf( stderr, "protocol is [%s]... what's left is [%s]\n", protocol, pch);*/
if( ( n = strcspn( pch, ":/" ) ) ) if( ( n = strcspn( pch, ":/" ) ) )
{ {
const int havePort = pch[n] == ':'; const int havePort = pch[n] == ':';
host = pch; host = pch;
host_len = n;
pch += n; pch += n;
*pch++ = '\0'; *pch++ = '\0';
/*fprintf( stderr, "host is [%s]... what's left is [%s]\n", host, pch );*/ /*fprintf( stderr, "host is [%s]... what's left is [%s]\n", host, pch );*/
@ -1053,10 +1057,10 @@ tr_urlParse( const char * url_in,
if( !err ) if( !err )
{ {
if( setme_protocol ) *setme_protocol = tr_strdup( protocol ); if( setme_protocol ) *setme_protocol = tr_strndup( protocol, protocol_len );
if( setme_host ){ ( (char*)host )[-3] = ':'; *setme_host = if( setme_host ){ ( (char*)host )[-3] = ':'; *setme_host =
tr_strdup( host ); } tr_strndup( host, host_len ); }
if( setme_path ){ if( path[0] == '/' ) *setme_path = tr_strdup( path ); if( setme_path ){ if( path[0] == '/' ) *setme_path = tr_strdup( path );
else { ( (char*)path )[-1] = '/'; *setme_path = tr_strdup( path - 1 ); } } else { ( (char*)path )[-1] = '/'; *setme_path = tr_strdup( path - 1 ); } }
if( setme_port ) *setme_port = port; if( setme_port ) *setme_port = port;