Add (fake) g_unichar_to_utf8

This commit is contained in:
Evan Miller
2021-01-22 07:45:47 -05:00
parent da80fb3430
commit 4e969425ac
2 changed files with 19 additions and 11 deletions

View File

@@ -45,6 +45,7 @@ typedef void * gpointer;
typedef const void * gconstpointer;
typedef uint8_t guint8;
typedef guint32 GQuark;
typedef guint32 gunichar;
typedef guint (*GHashFunc)(gconstpointer);
typedef int (*GCompareFunc)(gconstpointer, gconstpointer);
@@ -153,6 +154,7 @@ gchar *g_strdelimit(gchar *string, const gchar *delimiters, gchar new_delimiter)
void g_printerr(const gchar *format, ...);
/* conversion */
gint g_unichar_to_utf8(gunichar c, gchar *dst);
gchar *g_locale_to_utf8(const gchar *opsysstring, size_t len,
size_t *bytes_read, size_t *bytes_written, GError **error);

View File

@@ -223,6 +223,22 @@ gchar *g_string_free (GString *string, gboolean free_segment) {
}
/* conversion */
gint g_unichar_to_utf8(gunichar u, gchar *dst) {
if (u >= 0x0800) {
*dst++ = 0xE0 | ((u & 0xF000) >> 12);
*dst++ = 0x80 | ((u & 0x0FC0) >> 6);
*dst++ = 0x80 | ((u & 0x003F) >> 0);
return 3;
}
if (u >= 0x0080) {
*dst++ = 0xC0 | ((u & 0x07C0) >> 6);
*dst++ = 0x80 | ((u & 0x003F) >> 0);
return 2;
}
*dst++ = (u & 0x7F);
return 1;
}
gchar *g_locale_to_utf8(const gchar *opsysstring, size_t len,
size_t *bytes_read, size_t *bytes_written, GError **error) {
if (len == (size_t)-1)
@@ -235,18 +251,8 @@ gchar *g_locale_to_utf8(const gchar *opsysstring, size_t len,
gchar *utf8 = malloc(3*len+1);
gchar *dst = utf8;
for (size_t i=0; i<len; i++) {
wchar_t u = utf16[i];
// u >= 0x10000 requires surrogate pairs, ignore
if (u >= 0x0800) {
*dst++ = 0xE0 | ((u & 0xF000) >> 12);
*dst++ = 0x80 | ((u & 0x0FC0) >> 6);
*dst++ = 0x80 | ((u & 0x003F) >> 0);
} else if (u >= 0x0080) {
*dst++ = 0xC0 | ((u & 0x07C0) >> 6);
*dst++ = 0x80 | ((u & 0x003F) >> 0);
} else {
*dst++ = (u & 0x7F);
}
dst += g_unichar_to_utf8(utf16[i], dst);
}
*dst++ = '\0';
free(utf16);