Commit dcdf7a64 authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

config_StringEscape: iterate through string twice rather than 3 times

parent 87ea4084
...@@ -451,32 +451,23 @@ char *config_StringUnescape( char *psz_string ) ...@@ -451,32 +451,23 @@ char *config_StringUnescape( char *psz_string )
return psz_string; return psz_string;
} }
char *config_StringEscape( const char *psz_string ) char *config_StringEscape( const char *str )
{ {
char *psz_return; size_t length = 0;
char *psz_dst;
int i_escape;
if( !psz_string ) if( str == NULL )
return NULL; return NULL;
i_escape = 0; for( const char *p = str; *p; p++ )
for( const char *p = psz_string; *p; p++ ) length += IsEscapeNeeded( *p ) ? 2 : 1;
{
if( IsEscapeNeeded( *p ) )
i_escape++;
}
psz_return = psz_dst = malloc( strlen( psz_string ) + i_escape + 1 ); char *ret = xmalloc( length + 1 ), *dst = ret;
for( const char *p = psz_string; *p; p++ ) for( const char *p = str; *p; p++ )
{ {
if( IsEscapeNeeded( *p ) ) if( IsEscapeNeeded( *p ) )
*psz_dst++ = '\\'; *dst++ = '\\';
*psz_dst++ = *p; *dst++ = *p;
} }
*psz_dst = '\0'; *dst = '\0';;
return ret;
return psz_return;
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment