[FFmpeg-devel] [PATCH] avutil/avstring: improve av_strreplace implement

Steven Liu lingjiujianke at gmail.com
Sun Apr 2 19:06:31 EEST 2017


2017-04-02 23:18 GMT+08:00 Nicolas George <george at nsup.org>:

> Le tridi 13 germinal, an CCXXV, Steven Liu a écrit :
> > I have implementation the av_strreplace use AVBprint, the API context now
> > is:
>
> No need to Cc mo. On the other hand, sending the function itself instead
> of the diff makes it easier in this case, thanks.
>
> Nice. Just one issue:
>
> > char *av_strreplace(const char *str, const char *from, const char *to)
> > {
> >     char *ret = NULL;
> >     const char *pstr2, *pstr = str;
> >     size_t tolen = strlen(to), fromlen = strlen(from);
> >     AVBPrint pbuf;
> >
> >     av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
> >     while ((pstr2 = av_stristr(pstr, from))) {
> >         av_bprint_append_data(&pbuf, pstr, pstr2 - pstr);
> >         pstr = pstr2 + fromlen;
> >         av_bprint_append_data(&pbuf, to, tolen);
> >     }
> >     av_bprint_append_data(&pbuf, pstr, strlen(pstr));
>
> >     av_bprint_finalize(&pbuf, &ret);
>
> Just before that, a check with av_bprint_is_complete() is needed because
> some malloc may have failed: if it is not complete, av_bprint_finalize()
> to NULL to free the buffer and return NULL.
>
> >
> >     return ret;
> > }
>
> Also, did you consider extending libavutil/tests/avstring.c to cover
> this new function?
>
> Regards,
>
> --
>   Nicolas George
>
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel at ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
>
What about this one:


char *av_strreplace(const char *str, const char *from, const char *to)
{
    char *ret = NULL;
    const char *pstr2, *pstr = str;
    size_t tolen = strlen(to), fromlen = strlen(from);
    AVBPrint pbuf;

    av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
    while ((pstr2 = av_stristr(pstr, from))) {
        av_bprint_append_data(&pbuf, pstr, pstr2 - pstr);
        pstr = pstr2 + fromlen;
        av_bprint_append_data(&pbuf, to, tolen);
    }
    av_bprint_append_data(&pbuf, pstr, strlen(pstr));
    if (!av_bprint_is_complete(&pbuf)) {
        av_bprint_finalize(&pbuf, NULL);
    } else {
        av_bprint_finalize(&pbuf, &ret);
    }

    return ret;
}





and i will update this patch, add TEST case.
and update patch for patchwork.


More information about the ffmpeg-devel mailing list