[xiph-commits] r16147 - trunk/ffmpeg2theora/src

j at svn.xiph.org j at svn.xiph.org
Thu Jun 18 16:19:33 PDT 2009


Author: j
Date: 2009-06-18 16:19:32 -0700 (Thu, 18 Jun 2009)
New Revision: 16147

Modified:
   trunk/ffmpeg2theora/src/avinfo.c
Log:
better str replace

Modified: trunk/ffmpeg2theora/src/avinfo.c
===================================================================
--- trunk/ffmpeg2theora/src/avinfo.c	2009-06-18 06:10:25 UTC (rev 16146)
+++ trunk/ffmpeg2theora/src/avinfo.c	2009-06-18 23:19:32 UTC (rev 16147)
@@ -98,22 +98,23 @@
    return codec_name;
 }
 
-char *replace_str(char *str, char *orig, char *rep) {
-  char buffer[4096];
-  char *p, *rest, *ret;
+char *replace_str_all(char *str, char *orig, char *rep) {
+  const char buffer[4096];
+  char *p, *p_str = str, *p_buffer = (char *)buffer;
+  int len = strlen(str);
 
-  if(!(p = strstr(str, orig)))
-    return str;
-
-  strncpy(buffer, str, p-str);
-  buffer[p-str] = '\0';
-  rest = replace_str(p+strlen(orig), orig, rep);
-  sprintf(buffer+(p-str), "%s%s", rep, rest);
-
-  ret = malloc(strlen(buffer)+1); // not zero terminated, so make it so
-  strncpy(ret, buffer, strlen(buffer));
-  ret[strlen(buffer)] = '\0';
-  return ret;
+  strncpy(p_buffer, str, len);
+  while (p = strstr(p_str, orig)) {
+    strncpy(p_buffer, p_str, p-p_str);
+    p_buffer += (p-p_str);
+    len = len - strlen(orig) + strlen(rep);    
+    sprintf(p_buffer, "%s%s", rep, p+strlen(orig));
+    p_str = p + strlen(orig);
+    p_buffer += strlen(rep);
+  }
+  p = (char *)buffer;
+  p[len] = '\0';
+  return p;
 }
 
 enum {
@@ -128,8 +129,8 @@
     switch(type) {
         case JSON_STRING:
             p = (char *)value;
-            p = replace_str(p, "\\", "\\\\");
-            p = replace_str(p, "\"", "\\\"");
+            p = replace_str_all(p, "\\", "\\\\");
+            p = replace_str_all(p, "\"", "\\\"");
             fprintf(output, "  \"%s\": \"%s\"", key, p);
             break;
         case JSON_INT:



More information about the commits mailing list