step over unicode characters with autocomplete (correctly this time).

This commit is contained in:
Campbell Barton
2013-02-19 15:56:49 +00:00
parent de26f59220
commit cd3b98c4fa
3 changed files with 39 additions and 10 deletions

View File

@@ -41,6 +41,7 @@ int BLI_str_utf8_size_safe(const char *p);
/* copied from glib */
unsigned int BLI_str_utf8_as_unicode(const char *p);
unsigned int BLI_str_utf8_as_unicode_and_size(const char *__restrict p, size_t *__restrict index);
unsigned int BLI_str_utf8_as_unicode_and_size_safe(const char *__restrict p, size_t *__restrict index);
unsigned int BLI_str_utf8_as_unicode_step(const char *__restrict p, size_t *__restrict index);
size_t BLI_str_utf8_from_unicode(unsigned int c, char *outbuf);

View File

@@ -433,6 +433,22 @@ unsigned int BLI_str_utf8_as_unicode_and_size(const char *__restrict p, size_t *
return result;
}
unsigned int BLI_str_utf8_as_unicode_and_size_safe(const char *__restrict p, size_t *__restrict index)
{
int i, mask = 0, len;
unsigned int result;
const unsigned char c = (unsigned char) *p;
UTF8_COMPUTE (c, mask, len, -1);
if (len == -1) {
*index += 1;
return c;
}
UTF8_GET (result, p, i, mask, len, BLI_UTF8_ERR);
*index += len;
return result;
}
/* another variant that steps over the index,
* note, currently this also falls back to latin1 for text drawing. */
unsigned int BLI_str_utf8_as_unicode_step(const char *__restrict p, size_t *__restrict index)

View File

@@ -162,23 +162,29 @@ static GHash *text_autocomplete_build(Text *text)
gh = BLI_ghash_str_new(__func__);
for (linep = text->lines.first; linep; linep = linep->next) {
int i_start = 0;
int i_end = 0;
size_t i_start = 0;
size_t i_end = 0;
size_t i_pos = 0;
while (i_start < linep->len) {
/* seek identifier beginning */
while (i_start < linep->len && !text_check_identifier_nodigit(linep->line[i_start])) {
i_start++;
i_pos = i_start;
while ((i_start < linep->len) &&
(!text_check_identifier_nodigit(BLI_str_utf8_as_unicode_and_size_safe(&linep->line[i_start], &i_pos))))
{
i_start = i_pos;
}
i_end = i_start;
while (i_end < linep->len && text_check_identifier(linep->line[i_end])) {
i_end++;
i_pos = i_end = i_start;
while ((i_end < linep->len) &&
(text_check_identifier(BLI_str_utf8_as_unicode_and_size_safe(&linep->line[i_end], &i_pos))))
{
i_end = i_pos;
}
if ((i_start != i_end) &&
/* check we're at the beginning of a line or that the previous char is not an identifier
* this prevents digits from being added */
((i_start < 1) || !text_check_identifier(linep->line[i_start - 1])))
* this prevents digits from being added */
((i_start < 1) || !text_check_identifier(BLI_str_utf8_as_unicode(&linep->line[i_start - 1]))))
{
char *str_sub = &linep->line[i_start];
const int choice_len = i_end - i_start;
@@ -197,7 +203,13 @@ static GHash *text_autocomplete_build(Text *text)
str_sub[choice_len] = str_sub_last;
}
}
i_start = i_end;
if (i_end != i_start) {
i_start = i_end;
}
else {
/* highly unlikely, but prevent eternal loop */
i_start++;
}
}
}