1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-12-04 14:46:46 -05:00

Implemented parse_args_with_freetext with unicode compatibility

This commit is contained in:
James Booth 2013-07-14 00:24:57 +01:00
parent 51786f67a6
commit 4d35031cb0

View File

@ -65,18 +65,12 @@ parse_args(const char * const inp, int min, int max)
int token_size = 0; int token_size = 0;
GSList *tokens = NULL; GSList *tokens = NULL;
// add tokens to GSList // add tokens to GSList
int i; int i;
for (i = 0; i < inp_size; i++) { for (i = 0; i < inp_size; i++) {
gchar *curr_ch = g_utf8_offset_to_pointer(copy, i); gchar *curr_ch = g_utf8_offset_to_pointer(copy, i);
gunichar curr_uni = g_utf8_get_char(curr_ch); gunichar curr_uni = g_utf8_get_char(curr_ch);
gchar *character = malloc(7);
gint num_written = 0;
num_written = g_unichar_to_utf8(curr_uni, character);
character[num_written] = '\0';
if (!in_token) { if (!in_token) {
if (curr_uni == ' ') { if (curr_uni == ' ') {
continue; continue;
@ -195,7 +189,7 @@ parse_args_with_freetext(const char * const inp, int min, int max)
char *copy = strdup(inp); char *copy = strdup(inp);
g_strstrip(copy); g_strstrip(copy);
int inp_size = strlen(copy); int inp_size = g_utf8_strlen(copy, -1);
gboolean in_token = FALSE; gboolean in_token = FALSE;
gboolean in_freetext = FALSE; gboolean in_freetext = FALSE;
gboolean in_quotes = FALSE; gboolean in_quotes = FALSE;
@ -206,54 +200,66 @@ parse_args_with_freetext(const char * const inp, int min, int max)
// add tokens to GSList // add tokens to GSList
int i; int i;
for (i = 0; i <= inp_size; i++) { for (i = 0; i < inp_size; i++) {
gchar *curr_ch = g_utf8_offset_to_pointer(copy, i);
gunichar curr_uni = g_utf8_get_char(curr_ch);
if (!in_token) { if (!in_token) {
if (copy[i] == ' ') { if (curr_uni == ' ') {
continue; continue;
} else { } else {
in_token = TRUE; in_token = TRUE;
num_tokens++; num_tokens++;
if (num_tokens == max + 1) { if (num_tokens == max + 1) {
in_freetext = TRUE; in_freetext = TRUE;
} else if (copy[i] == '"') { } else if (curr_uni == '"') {
in_quotes = TRUE; in_quotes = TRUE;
i++; i++;
gchar *next_ch = g_utf8_next_char(curr_ch);
gunichar next_uni = g_utf8_get_char(next_ch);
token_start = next_ch;
token_size += g_unichar_to_utf8(next_uni, NULL);
} }
if (copy[i] == '"') { if (curr_uni == '"') {
token_start = &copy[i+1]; gchar *next_ch = g_utf8_next_char(curr_ch);
token_start = next_ch;
} else { } else {
token_start = &copy[i]; token_start = curr_ch;
token_size++; token_size += g_unichar_to_utf8(curr_uni, NULL);
} }
} }
} else { } else {
if (in_quotes) { if (in_quotes) {
if ((copy[i] == '\0') || (copy[i] == '"')) { if (curr_uni == '"') {
tokens = g_slist_append(tokens, g_strndup(token_start, tokens = g_slist_append(tokens, g_strndup(token_start,
token_size)); token_size));
token_size = 0; token_size = 0;
in_token = FALSE; in_token = FALSE;
in_quotes = FALSE; in_quotes = FALSE;
} else { } else {
if (copy[i] != '"') { if (curr_uni != '"') {
token_size++; token_size += g_unichar_to_utf8(curr_uni, NULL);
} }
} }
} else { } else {
if ((!in_freetext && copy[i] == ' ') || copy[i] == '\0') { if (!in_freetext && curr_uni == ' ') {
tokens = g_slist_append(tokens, g_strndup(token_start, tokens = g_slist_append(tokens, g_strndup(token_start,
token_size)); token_size));
token_size = 0; token_size = 0;
in_token = FALSE; in_token = FALSE;
} else { } else {
if (copy[i] != '"') { if (curr_uni != '"') {
token_size++; token_size += g_unichar_to_utf8(curr_uni, NULL);
} }
} }
} }
} }
} }
if (in_token) {
tokens = g_slist_append(tokens, g_strndup(token_start, token_size));
}
int num = g_slist_length(tokens) - 1; int num = g_slist_length(tokens) - 1;
// if num args not valid return NULL // if num args not valid return NULL