Added a UTF-8 sanitization routine. This will be needed for titles on uiWindow. Reintroduced our copy of the utf library. This should really be part of utf; I'll have to decide whether to test this or not. Made that a TODO for now. I'll be testing bad UTF-8 inputs on each function that takes UTF-8 strings anyway, just to be safe. Now we can finally add uiWindow!
This commit is contained in:
parent
7aeb937b93
commit
6fe7c1ef66
|
@ -5,4 +5,7 @@ libui_sources += [
|
|||
'common/controls.c',
|
||||
'common/errors.c',
|
||||
'common/main.c',
|
||||
'common/utf8.c',
|
||||
|
||||
'common/third_party/utf.c',
|
||||
]
|
||||
|
|
|
@ -75,6 +75,10 @@ extern void uiprivReportError(const char *prefix, const char *msg, const char *s
|
|||
extern bool uiprivOSVtableValid(const uiControlOSVtable *osVtable, const char *func);
|
||||
extern uiControlOSVtable *uiprivCloneOSVtable(const uiControlOSVtable *osVtable);
|
||||
|
||||
// utf8.c
|
||||
extern char *uiprivSanitizeUTF8(const char *str);
|
||||
extern void uiprivFreeUTF8(char *sanitized);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -0,0 +1,41 @@
|
|||
// 17 may 2020
|
||||
#include "uipriv.h"
|
||||
#include "third_party/utf.h"
|
||||
|
||||
// TODO write separate tests for this file?
|
||||
// TODO ideally this functionality should really be part of utf itself, in some form or another (for instance, via utf8SanitizedLen() + the requisite loop)
|
||||
|
||||
#define nGrow 32
|
||||
|
||||
char *uiprivSanitizeUTF8(const char *str)
|
||||
{
|
||||
size_t len;
|
||||
char *out;
|
||||
const char *s;
|
||||
size_t i;
|
||||
uint32_t rune;
|
||||
char encoded[4];
|
||||
size_t n;
|
||||
|
||||
// TODO can we even use strlen() with UTF-8 strings? or is '\0' == 0 == actual memory zero just a source code connection (and thus the last one isn't necessarily true)?
|
||||
len = strlen(str);
|
||||
out = (char *) uiprivAlloc((len + 1) * sizeof (char), "sanitized UTF-8 string");
|
||||
s = str;
|
||||
i = 0;
|
||||
while (*s != '\0') {
|
||||
s = uiprivUTF8DecodeRune(s, 0, &rune);
|
||||
n = uiprivUTF8EncodeRune(rune, encoded);
|
||||
if ((i + n) >= len) {
|
||||
out = (char *) uiprivRealloc(out, (len + 1) * sizeof (char), (len + nGrow + 1) * sizeof (char), "sanitized UTF-8 string");
|
||||
len += nGrow;
|
||||
}
|
||||
memcpy(out + i, encoded, n);
|
||||
i += n;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
void uiprivFreeUTF8(char *sanitized)
|
||||
{
|
||||
uiprivFree(sanitized);
|
||||
}
|
Loading…
Reference in New Issue