1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2024-11-26 19:01:44 +02:00

avutil/avstring: improve av_strreplace implement

Use AVBprint to implement av_strreplace
add av_strreplace test case TEST_STRREPLACE

Reviewed-by: Nicolas George <george@nsup.org>
Signed-off-by: Steven Liu <lq@chinaffmpeg.org>
This commit is contained in:
Steven Liu 2017-04-11 10:54:35 +08:00
parent a44b3abb4c
commit 44cd7502c7
3 changed files with 28 additions and 66 deletions

View File

@ -231,80 +231,26 @@ int av_strncasecmp(const char *a, const char *b, size_t n)
return c1 - c2; return c1 - c2;
} }
char *av_strreplace(const char *str, const char *from, const char *to) char *av_strireplace(const char *str, const char *from, const char *to)
{ {
/* Adjust each of the below values to suit your needs. */ char *ret = NULL;
/* Increment positions cache size initially by this number. */
size_t cache_sz_inc = 16;
/* Thereafter, each time capacity needs to be increased,
* multiply the increment by this factor. */
const size_t cache_sz_inc_factor = 3;
/* But never increment capacity by more than this number. */
const size_t cache_sz_inc_max = 1048576;
char *pret, *ret = NULL;
const char *pstr2, *pstr = str; const char *pstr2, *pstr = str;
size_t i, count = 0; size_t tolen = strlen(to), fromlen = strlen(from);
uintptr_t *pos_cache_tmp, *pos_cache = NULL; AVBPrint pbuf;
size_t cache_sz = 0;
size_t cpylen, orglen, retlen, tolen, fromlen = strlen(from);
/* Find all matches and cache their positions. */ av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
while ((pstr2 = av_stristr(pstr, from))) { while ((pstr2 = av_stristr(pstr, from))) {
count++; av_bprint_append_data(&pbuf, pstr, pstr2 - pstr);
/* Increase the cache size when necessary. */
if (cache_sz < count) {
cache_sz += cache_sz_inc;
pos_cache_tmp = av_realloc(pos_cache, sizeof(*pos_cache) * cache_sz);
if (!pos_cache_tmp) {
goto end_strreplace;
} else pos_cache = pos_cache_tmp;
cache_sz_inc *= cache_sz_inc_factor;
if (cache_sz_inc > cache_sz_inc_max) {
cache_sz_inc = cache_sz_inc_max;
}
}
pos_cache[count-1] = pstr2 - str;
pstr = pstr2 + fromlen; pstr = pstr2 + fromlen;
av_bprint_append_data(&pbuf, to, tolen);
} }
orglen = pstr - str + strlen(pstr); av_bprint_append_data(&pbuf, pstr, strlen(pstr));
/* Allocate memory for the post-replacement string. */ if (!av_bprint_is_complete(&pbuf)) {
if (count > 0) { av_bprint_finalize(&pbuf, NULL);
tolen = strlen(to);
retlen = orglen + (tolen - fromlen) * count;
} else { } else {
retlen = orglen; av_bprint_finalize(&pbuf, &ret);
}
ret = av_malloc(retlen + 1);
if (!ret) {
goto end_strreplace;
} }
if (!count) {
/* If no matches, then just duplicate the string. */
av_strlcpy(ret, str, retlen + 1);
} else {
/* Otherwise, duplicate the string whilst performing
* the replacements using the position cache. */
pret = ret;
memcpy(pret, str, pos_cache[0]);
pret += pos_cache[0];
for (i = 0; i < count; i++) {
memcpy(pret, to, tolen);
pret += tolen;
pstr = str + pos_cache[i] + fromlen;
cpylen = (i == count-1 ? orglen : pos_cache[i+1]) - pos_cache[i] - fromlen;
memcpy(pret, pstr, cpylen);
pret += cpylen;
}
ret[retlen] = '\0';
}
end_strreplace:
/* Free the cache and return the post-replacement string,
* which will be NULL in the event of an error. */
av_free(pos_cache);
return ret; return ret;
} }

View File

@ -270,7 +270,7 @@ int av_strncasecmp(const char *a, const char *b, size_t n);
* Locale-independent strings replace. * Locale-independent strings replace.
* @note This means only ASCII-range characters are replace * @note This means only ASCII-range characters are replace
*/ */
char *av_strreplace(const char *str, const char *from, const char *to); char *av_strireplace(const char *str, const char *from, const char *to);
/** /**
* Thread safe basename. * Thread safe basename.

View File

@ -93,6 +93,22 @@ int main(void)
TEST_STRNSTR(haystack, needle [2], strlen(haystack), NULL ); TEST_STRNSTR(haystack, needle [2], strlen(haystack), NULL );
TEST_STRNSTR(haystack, strings[1], strlen(haystack), haystack ); TEST_STRNSTR(haystack, strings[1], strlen(haystack), haystack );
/*Testing av_strireplace()*/
#define TEST_STRIREPLACE(haystack, needle, expected) \
ptr = av_strireplace(haystack, needle, "instead"); \
if (ptr == NULL) { \
printf("error, received null pointer!\n"); \
} else { \
if (strcmp(ptr, expected) != 0) \
printf( "expected: %s, received: %s\n", expected, ptr); \
av_free(ptr); \
}
TEST_STRIREPLACE(haystack, needle [0], "Education consists mainly in what we have uninstead");
TEST_STRIREPLACE(haystack, needle [1], "Education consists mainly in what we have instead");
TEST_STRIREPLACE(haystack, needle [2], "Education consists mainly in what we have instead.");
TEST_STRIREPLACE(haystack, needle [1], "Education consists mainly in what we have instead");
/*Testing av_d2str()*/ /*Testing av_d2str()*/
#define TEST_D2STR(value, expected) \ #define TEST_D2STR(value, expected) \
if((ptr = av_d2str(value)) == NULL){ \ if((ptr = av_d2str(value)) == NULL){ \