[vlc-devel] commit: convert_xml_special_chars: factor ( Rémi Denis-Courmont )

git version control git at videolan.org
Wed Jul 15 20:37:12 CEST 2009


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Wed Jul 15 21:36:41 2009 +0300| [6f34b357029d83ad40d6d9972ab1880e56fd9a8a] | committer: Rémi Denis-Courmont 

convert_xml_special_chars: factor

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

 src/text/strings.c |   57 +++++++++++++++++++++-------------------------------
 1 files changed, 23 insertions(+), 34 deletions(-)

diff --git a/src/text/strings.c b/src/text/strings.c
index c2ab87b..3f21500 100644
--- a/src/text/strings.c
+++ b/src/text/strings.c
@@ -402,47 +402,36 @@ void resolve_xml_special_chars( char *psz_value )
  */
 char *convert_xml_special_chars( const char *psz_content )
 {
-    char *psz_temp = malloc( 6 * strlen( psz_content ) + 1 );
-    const char *p_from = psz_content;
+    assert( psz_content );
+
+    const size_t len = strlen( psz_content );
+    char *const psz_temp = malloc( 6 * len + 1 );
     char *p_to   = psz_temp;
 
-    while ( *p_from )
+    if( psz_temp == NULL )
+        return NULL;
+    for( size_t i = 0; i < len; i++ )
     {
-        if ( *p_from == '<' )
-        {
-            strcpy( p_to, "<" );
-            p_to += 4;
-        }
-        else if ( *p_from == '>' )
-        {
-            strcpy( p_to, ">" );
-            p_to += 4;
-        }
-        else if ( *p_from == '&' )
-        {
-            strcpy( p_to, "&" );
-            p_to += 5;
-        }
-        else if( *p_from == '\"' )
-        {
-            strcpy( p_to, """ );
-            p_to += 6;
-        }
-        else if( *p_from == '\'' )
-        {
-            strcpy( p_to, "'" );
-            p_to += 6;
-        }
-        else
+        const char *str;
+        char c = psz_content[i];
+
+        switch ( c )
         {
-            *p_to = *p_from;
-            p_to++;
+            case '\"': str = "quot"; break;
+            case '&':  str = "amp";  break;
+            case '\'': str = "#39";  break;
+            case '<':  str = "lt";   break;
+            case '>':  str = "gt";   break;
+            default:
+                *(p_to++) = c;
+                continue;
         }
-        p_from++;
+        p_to += sprintf( p_to, "&%s;", str );
     }
-    *p_to = '\0';
+    *(p_to++) = '\0';
 
-    return psz_temp;
+    p_to = realloc( psz_temp, p_to - psz_temp );
+    return p_to ? p_to : psz_temp; /* cannot fail */
 }
 
 /* Base64 encoding */




More information about the vlc-devel mailing list