[vlc-commits] config_StringEscape: iterate through string twice rather than 3 times

Rémi Denis-Courmont git at videolan.org
Sun May 1 16:37:50 CEST 2011


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Sun May  1 17:37:16 2011 +0300| [dcdf7a64a2d9e20d9b03c3ef478e2a1add1cb155] | committer: Rémi Denis-Courmont

config_StringEscape: iterate through string twice rather than 3 times

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=dcdf7a64a2d9e20d9b03c3ef478e2a1add1cb155
---

 src/config/chain.c |   31 +++++++++++--------------------
 1 files changed, 11 insertions(+), 20 deletions(-)

diff --git a/src/config/chain.c b/src/config/chain.c
index d7f0633..4ef60be 100644
--- a/src/config/chain.c
+++ b/src/config/chain.c
@@ -451,32 +451,23 @@ char *config_StringUnescape( char *psz_string )
     return psz_string;
 }
 
-char *config_StringEscape( const char *psz_string )
+char *config_StringEscape( const char *str )
 {
-    char *psz_return;
-    char *psz_dst;
-    int i_escape;
+    size_t length = 0;
 
-    if( !psz_string )
+    if( str == NULL )
         return NULL;
 
-    i_escape = 0;
-    for( const char *p = psz_string; *p; p++ )
-    {
-        if( IsEscapeNeeded( *p ) )
-            i_escape++;
-    }
+    for( const char *p = str; *p; p++ )
+        length += IsEscapeNeeded( *p ) ? 2 : 1;
 
-    psz_return = psz_dst = malloc( strlen( psz_string ) + i_escape + 1 );
-    for( const char *p = psz_string; *p; p++ )
+    char *ret = xmalloc( length + 1 ), *dst = ret;
+    for( const char *p = str; *p; p++ )
     {
         if( IsEscapeNeeded( *p ) )
-            *psz_dst++ = '\\';
-        *psz_dst++ = *p;
+            *dst++ = '\\';
+        *dst++ = *p;
     }
-    *psz_dst = '\0';
-
-    return psz_return;
+    *dst = '\0';;
+    return ret;
 }
-
-



More information about the vlc-commits mailing list