diff options
| author | James Booth <boothj5@gmail.com> | 2017-01-26 00:52:13 +0000 |
|---|---|---|
| committer | James Booth <boothj5@gmail.com> | 2017-01-26 00:52:13 +0000 |
| commit | 6679b890a052ccda92e086d94a9f253403b24a13 (patch) | |
| tree | 644b131dd78c3afaf9d45f4899e7e2bbb72c5034 /src | |
| parent | b712820c999e824771c683175ed41b392328ffa0 (diff) | |
| download | profani-tty-6679b890a052ccda92e086d94a9f253403b24a13.tar.gz | |
Fix multibyte chars in prof_occurrences
issue #901
Diffstat (limited to 'src')
| -rw-r--r-- | src/common.c | 22 |
1 files changed, 14 insertions, 8 deletions
diff --git a/src/common.c b/src/common.c index d3f09a56..9544ab73 100644 --- a/src/common.c +++ b/src/common.c @@ -509,13 +509,18 @@ prof_occurrences(const char *const needle, const char *const haystack, int offse return *result; } - if (g_str_has_prefix(&haystack[offset], needle)) { + gchar *haystack_curr = g_utf8_offset_to_pointer(haystack, offset); + if (g_str_has_prefix(haystack_curr, needle)) { if (whole_word) { - char *prev = g_utf8_prev_char(&haystack[offset]); - char *next = g_utf8_next_char(&haystack[offset] + strlen(needle) - 1); - gunichar prevu = g_utf8_get_char(prev); - gunichar nextu = g_utf8_get_char(next); - if (!g_unichar_isalnum(prevu) && !g_unichar_isalnum(nextu)) { + gchar *needle_last_ch = g_utf8_offset_to_pointer(needle, g_utf8_strlen(needle, -1)- 1); + int needle_last_ch_len = mblen(needle_last_ch, MB_CUR_MAX); + + gchar *haystack_before_ch = g_utf8_prev_char(haystack_curr); + gchar *haystack_after_ch = g_utf8_next_char(haystack_curr + strlen(needle) - needle_last_ch_len); + + gunichar before = g_utf8_get_char(haystack_before_ch); + gunichar after = g_utf8_get_char(haystack_after_ch); + if (!g_unichar_isalnum(before) && !g_unichar_isalnum(after)) { *result = g_slist_append(*result, GINT_TO_POINTER(offset)); } } else { @@ -523,8 +528,9 @@ prof_occurrences(const char *const needle, const char *const haystack, int offse } } - if (haystack[offset+1] != '\0') { - *result = prof_occurrences(needle, haystack, offset+1, whole_word, result); + offset++; + if (g_strcmp0(g_utf8_offset_to_pointer(haystack, offset), "\0") != 0) { + *result = prof_occurrences(needle, haystack, offset, whole_word, result); } return *result; |