lib/strutils: improve normalize_whitespace()

Let's make it possible to use the function to normalize the string
between two buffers (from source to destination).

Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
Karel Zak 2021-06-14 16:24:59 +02:00
parent 38b9be784b
commit c862d0e177
2 changed files with 34 additions and 14 deletions

View File

@ -315,16 +315,20 @@ static inline size_t ltrim_whitespace(unsigned char *str)
/* Removes left-hand, right-hand and repeating whitespaces. /* Removes left-hand, right-hand and repeating whitespaces.
*/ */
static inline size_t normalize_whitespace(unsigned char *str) static inline size_t __normalize_whitespace(
const unsigned char *src,
size_t sz,
unsigned char *dst,
size_t len)
{ {
size_t i, x, sz = strlen((char *) str); size_t i, x = 0;
int nsp = 0, intext = 0; int nsp = 0, intext = 0;
if (!sz) if (!sz)
return 0; goto done;
for (i = 0, x = 0; i < sz; ) { for (i = 0, x = 0; i < sz && x < len - 1; ) {
if (isspace(str[i])) if (isspace(src[i]))
nsp++; nsp++;
else else
nsp = 0, intext = 1; nsp = 0, intext = 1;
@ -332,14 +336,21 @@ static inline size_t normalize_whitespace(unsigned char *str)
if (nsp > 1 || (nsp && !intext)) if (nsp > 1 || (nsp && !intext))
i++; i++;
else else
str[x++] = str[i++]; dst[x++] = src[i++];
} }
if (nsp) /* tailing space */ if (nsp && x > 0) /* tailing space */
x--; x--;
str[x] = '\0'; done:
dst[x] = '\0';
return x; return x;
} }
static inline size_t normalize_whitespace(unsigned char *str)
{
size_t sz = strlen((char *) str);
return __normalize_whitespace(str, sz, str, sz + 1);
}
static inline void strrep(char *s, int find, int replace) static inline void strrep(char *s, int find, int replace)
{ {
while (s && *s && (s = strchr(s, find)) != NULL) while (s && *s && (s = strchr(s, find)) != NULL)

View File

@ -1175,17 +1175,26 @@ static int test_strutils_cmp_paths(int argc, char *argv[])
static int test_strutils_normalize(int argc, char *argv[]) static int test_strutils_normalize(int argc, char *argv[])
{ {
unsigned char *str; unsigned char *src, *dst;
size_t sz; size_t sz, len;
if (argc < 2) if (argc < 2)
return EXIT_FAILURE; return EXIT_FAILURE;
str = (unsigned char *) strdup(argv[1]); src = (unsigned char *) strdup(argv[1]);
sz = normalize_whitespace(str); len = strlen((char *) src);
dst = malloc(len + 1);
printf("'%s' --> '%s' [sz=%zu]\n", argv[1], str, sz); /* two buffers */
free(str); sz = __normalize_whitespace(src, len, dst, len + 1);
printf("1: '%s' --> '%s' [sz=%zu]\n", src, dst, sz);
/* one buffer */
sz = normalize_whitespace(src);
printf("2: '%s' --> '%s' [sz=%zu]\n", argv[1], src, sz);
free(src);
free(dst);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }