add pa_truncate_utf8() function for truncating a string and guaranteeing it stays valid UTF8 afterwards

git-svn-id: file:///home/lennart/svn/public/pulseaudio/branches/lennart@1673 fefdeb5f-60dc-0310-8127-8f9354f1896f
This commit is contained in:
Lennart Poettering 2007-08-16 13:46:32 +00:00
parent 81cdb3798c
commit 03b0b1db7b
2 changed files with 20 additions and 0 deletions

View file

@ -78,6 +78,7 @@
#include <pulse/xmalloc.h> #include <pulse/xmalloc.h>
#include <pulse/util.h> #include <pulse/util.h>
#include <pulse/utf8.h>
#include <pulsecore/core-error.h> #include <pulsecore/core-error.h>
#include <pulsecore/winsock.h> #include <pulsecore/winsock.h>
@ -1223,3 +1224,20 @@ int pa_snprintf(char *str, size_t size, const char *format, ...) {
return ret; return ret;
} }
/* Truncate the specified string, but guarantee that the string
* returned still validates as UTF8 */
char *pa_truncate_utf8(char *c, size_t l) {
pa_assert(c);
pa_assert(pa_utf8_valid(c));
if (strlen(c) <= l)
return c;
c[l] = 0;
while (l > 0 && !pa_utf8_valid(c))
c[--l] = 0;
return c;
}

View file

@ -94,4 +94,6 @@ int pa_atou(const char *s, uint32_t *ret_u);
int pa_snprintf(char *str, size_t size, const char *format, ...); int pa_snprintf(char *str, size_t size, const char *format, ...);
char *pa_truncate_utf8(char *c, size_t l);
#endif #endif