mirror of
https://github.com/profanity-im/profanity.git
synced 2024-12-04 14:46:46 -05:00
Remaining parser function unicode compatible
This commit is contained in:
parent
e7478d8cb8
commit
7f82dc42f5
@ -297,29 +297,23 @@ parse_args_with_freetext(const char * const inp, int min, int max)
|
|||||||
int
|
int
|
||||||
count_tokens(char *string)
|
count_tokens(char *string)
|
||||||
{
|
{
|
||||||
int num_tokens = 0;
|
int length = g_utf8_strlen(string, -1);
|
||||||
|
|
||||||
// if no quotes, use glib
|
|
||||||
if (g_strrstr(string, "\"") == NULL) {
|
|
||||||
gchar **tokens = g_strsplit(string, " ", 0);
|
|
||||||
num_tokens = g_strv_length(tokens);
|
|
||||||
g_strfreev(tokens);
|
|
||||||
|
|
||||||
// else count tokens including quoted
|
|
||||||
} else {
|
|
||||||
int length = strlen(string);
|
|
||||||
int i = 0;
|
|
||||||
gboolean in_quotes = FALSE;
|
gboolean in_quotes = FALSE;
|
||||||
|
int num_tokens = 0;
|
||||||
|
int i = 0;
|
||||||
|
|
||||||
// include first token
|
// include first token
|
||||||
num_tokens++;
|
num_tokens++;
|
||||||
|
|
||||||
for (i = 0; i < length; i++) {
|
for (i = 0; i < length; i++) {
|
||||||
if (string[i] == ' ') {
|
gchar *curr_ch = g_utf8_offset_to_pointer(string, i);
|
||||||
|
gunichar curr_uni = g_utf8_get_char(curr_ch);
|
||||||
|
|
||||||
|
if (curr_uni == ' ') {
|
||||||
if (!in_quotes) {
|
if (!in_quotes) {
|
||||||
num_tokens++;
|
num_tokens++;
|
||||||
}
|
}
|
||||||
} else if (string[i] == '"') {
|
} else if (curr_uni == '"') {
|
||||||
if (in_quotes) {
|
if (in_quotes) {
|
||||||
in_quotes = FALSE;
|
in_quotes = FALSE;
|
||||||
} else {
|
} else {
|
||||||
@ -327,7 +321,6 @@ count_tokens(char *string)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return num_tokens;
|
return num_tokens;
|
||||||
}
|
}
|
||||||
@ -335,25 +328,31 @@ count_tokens(char *string)
|
|||||||
char *
|
char *
|
||||||
get_start(char *string, int tokens)
|
get_start(char *string, int tokens)
|
||||||
{
|
{
|
||||||
|
GString *result = g_string_new("");
|
||||||
|
int length = g_utf8_strlen(string, -1);
|
||||||
|
gboolean in_quotes = FALSE;
|
||||||
char *result_str = NULL;
|
char *result_str = NULL;
|
||||||
int num_tokens = 0;
|
int num_tokens = 0;
|
||||||
int length = strlen(string);
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
gboolean in_quotes = FALSE;
|
|
||||||
GString *result = g_string_new("");
|
|
||||||
|
|
||||||
// include first token
|
// include first token
|
||||||
num_tokens++;
|
num_tokens++;
|
||||||
|
|
||||||
for (i = 0; i < length; i++) {
|
for (i = 0; i < length; i++) {
|
||||||
|
gchar *curr_ch = g_utf8_offset_to_pointer(string, i);
|
||||||
|
gunichar curr_uni = g_utf8_get_char(curr_ch);
|
||||||
|
|
||||||
if (num_tokens < tokens) {
|
if (num_tokens < tokens) {
|
||||||
g_string_append_c(result, string[i]);
|
gchar *uni_char = malloc(7);
|
||||||
|
int len = g_unichar_to_utf8(curr_uni, uni_char);
|
||||||
|
uni_char[len] = '\0';
|
||||||
|
g_string_append(result, uni_char);
|
||||||
}
|
}
|
||||||
if (string[i] == ' ') {
|
if (curr_uni == ' ') {
|
||||||
if (!in_quotes) {
|
if (!in_quotes) {
|
||||||
num_tokens++;
|
num_tokens++;
|
||||||
}
|
}
|
||||||
} else if (string[i] == '"') {
|
} else if (curr_uni == '"') {
|
||||||
if (in_quotes) {
|
if (in_quotes) {
|
||||||
in_quotes = FALSE;
|
in_quotes = FALSE;
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
Reference in New Issue
Block a user