mirror of
https://gitlab.freedesktop.org/libbsd/libbsd.git
synced 2025-10-15 15:16:53 +02:00
Add strnvis and strnunvis functions
Taken from OpenBSD.
This commit is contained in:
41
src/unvis.c
41
src/unvis.c
@@ -257,6 +257,47 @@ strunvis(char *dst, const char *src)
|
||||
return (dst - start);
|
||||
}
|
||||
|
||||
ssize_t
|
||||
strnunvis(char *dst, const char *src, size_t sz)
|
||||
{
|
||||
char c, p;
|
||||
char *start = dst, *end = dst + sz - 1;
|
||||
int state = 0;
|
||||
|
||||
if (sz > 0)
|
||||
*end = '\0';
|
||||
while ((c = *src++)) {
|
||||
again:
|
||||
switch (unvis(&p, c, &state, 0)) {
|
||||
case UNVIS_VALID:
|
||||
if (dst < end)
|
||||
*dst = p;
|
||||
dst++;
|
||||
break;
|
||||
case UNVIS_VALIDPUSH:
|
||||
if (dst < end)
|
||||
*dst = p;
|
||||
dst++;
|
||||
goto again;
|
||||
case 0:
|
||||
case UNVIS_NOCHAR:
|
||||
break;
|
||||
default:
|
||||
if (dst <= end)
|
||||
*dst = '\0';
|
||||
return (-1);
|
||||
}
|
||||
}
|
||||
if (unvis(&p, c, &state, UNVIS_END) == UNVIS_VALID) {
|
||||
if (dst < end)
|
||||
*dst = p;
|
||||
dst++;
|
||||
}
|
||||
if (dst <= end)
|
||||
*dst = '\0';
|
||||
return (dst - start);
|
||||
}
|
||||
|
||||
int
|
||||
strunvisx(char *dst, const char *src, int flag)
|
||||
{
|
||||
|
Reference in New Issue
Block a user