[gtk/im-context-work] composetable: Parse multi-char values
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtk/im-context-work] composetable: Parse multi-char values
- Date: Tue, 2 Feb 2021 16:54:15 +0000 (UTC)
commit 773ae0cd0f84b7d8e9aa0842656562713c3ceb93
Author: Matthias Clasen <mclasen redhat com>
Date: Tue Feb 2 00:21:11 2021 -0500
composetable: Parse multi-char values
Rewrite the value parsing function to accept strings
that hold more than a single Unicode character.
gtk/gtkcomposetable.c | 82 +++++++++++++++++++++++----------------------------
1 file changed, 37 insertions(+), 45 deletions(-)
---
diff --git a/gtk/gtkcomposetable.c b/gtk/gtkcomposetable.c
index 97821bbced..364027ffa2 100644
--- a/gtk/gtkcomposetable.c
+++ b/gtk/gtkcomposetable.c
@@ -37,7 +37,6 @@
typedef struct {
gunichar *sequence;
char *value;
- char *comment;
} GtkComposeData;
@@ -46,7 +45,6 @@ gtk_compose_data_free (GtkComposeData *compose_data)
{
g_free (compose_data->sequence);
g_free (compose_data->value);
- g_free (compose_data->comment);
g_slice_free (GtkComposeData, compose_data);
}
@@ -79,72 +77,66 @@ parse_compose_value (GtkComposeData *compose_data,
const char *val,
const char *line)
{
- char **words = g_strsplit (val, "\"", 3);
- gunichar uch;
+ char *word;
+ const char *p;
+ gsize len;
+ GString *value;
+ gunichar ch;
char *endp;
- char buf[8] = { 0, };
- if (g_strv_length (words) < 3)
+ len = strlen (val);
+ if (val[0] != '"' || val[len - 1] != '"')
{
g_warning ("Need to double-quote the value: %s: %s", val, line);
goto fail;
}
- uch = g_utf8_get_char (words[1]);
+ word = g_strndup (val + 1, len - 2);
- if (uch == 0)
- {
- g_warning ("Invalid value: %s: %s", val, line);
- goto fail;
- }
- else if (uch == '\\')
- {
- uch = words[1][1];
+ value = g_string_new ("");
- /* The escaped string "\"" is separated with '\\' and '"'. */
- if (uch == '\0' && words[2][0] == '"')
- {
- uch = '"';
- }
- /* The escaped octal */
- else if (uch >= '0' && uch < '8')
+ p = word;
+ while (*p)
+ {
+ if (*p == '\\')
{
- uch = g_ascii_strtoll (words[1] + 1, &endp, 8);
- if (*endp != '\0')
+ if (p[1] == '"')
+ {
+ g_string_append_c (value, '"');
+ p += 2;
+ }
+ else if (p[1] == '\\')
+ {
+ g_string_append_c (value, '\\');
+ p += 2;
+ }
+ else if (p[1] >= '0' && p[1] < '8')
{
- g_warning ("GTK supports to output one char only: %s: %s", val, line);
+ ch = g_ascii_strtoll (p + 1, &endp, 8);
+ g_string_append_unichar (value, ch);
+ p = endp;
+ }
+ else
+ {
+ g_warning ("Invalid escape sequence: %s: %s", val, line);
goto fail;
}
}
- /* If we need to handle other escape sequences. */
- else if (uch != '\\')
- {
- g_warning ("Invalid escape sequence: %s: %s", val, line);
- }
- }
- else
- {
- if (g_utf8_get_char (g_utf8_next_char (words[1])) > 0)
+ else
{
- g_warning ("GTK supports to output one char only: %s: %s", val, line);
- goto fail;
+ ch = g_utf8_get_char (p);
+ g_string_append_unichar (value, ch);
+ p = g_utf8_next_char (p);
}
}
- g_unichar_to_utf8 (uch, buf);
- compose_data->value = g_strdup (buf);
+ compose_data->value = g_string_free (value, FALSE);
- if (uch == '"')
- compose_data->comment = g_strdup (g_strstrip (words[2] + 1));
- else
- compose_data->comment = g_strdup (g_strstrip (words[2]));
-
- g_strfreev (words);
+ g_free (word);
return TRUE;
fail:
- g_strfreev (words);
return FALSE;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]