[FFmpeg-devel] [PATCH] ffprobe: redesign json_escape_str(), to make use of a global buffer

Clément Bœsch ubitux at gmail.com
Sat Oct 15 22:31:18 CEST 2011


On Fri, Oct 14, 2011 at 05:50:59PM +0200, Stefano Sabatini wrote:
> The new code avoids to call av_malloc/av_free when escaping is needed
> (possibly faster), and avoids an integer overflow in case of a huge

Not "possibly" faster, it is: 1.5sec while 1.9sec previously with my test
case.

> string and provides feedback when a string cannot be escaped.
> 
> When a string cannot be escaped, a special string is printed instead.
> ---
>  ffprobe.c |  113 +++++++++++++++++++++++++++++++++++++++++-------------------
>  1 files changed, 77 insertions(+), 36 deletions(-)
> 
> diff --git a/ffprobe.c b/ffprobe.c
[...]
> +static const char *json_escape_str(char **dst, size_t *dst_size, const char *src,
> +                                   void *log_ctx)
>  {
>      static const char json_escape[] = {'"', '\\', '\b', '\f', '\n', '\r', '\t', 0};
>      static const char json_subst[]  = {'"', '\\',  'b',  'f',  'n',  'r',  't', 0};
> -    char *ret, *p;
> -    int i, len = 0;
> +    const char *p;
> +    char *q;
> +    size_t size = 1;
>  
>      // compute the length of the escaped string
> -    for (i = 0; s[i]; i++) {
> -        if (strchr(json_escape, s[i]))     len += 2; // simple escape
> -        else if ((unsigned char)s[i] < 32) len += 6; // handle non-printable chars
> -        else                               len += 1; // char copy
> +    for (p = src; *p; p++) {
> +        ESCAPE_CHECK_SIZE(src, size, SIZE_MAX-6);
> +        if (strchr(json_escape, *p))     size += 2; // simple escape
> +        else if ((unsigned char)*p < 32) size += 6; // handle non-printable chars
> +        else                             size += 1; // char copy
>      }

Not that I care much, but why did you change the i = 0 ... into a pointer?

Otherwise looks good to me, thanks.

-- 
Clément B.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 490 bytes
Desc: not available
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20111015/6557995d/attachment.asc>


More information about the ffmpeg-devel mailing list