1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-09-22 19:45:54 -04:00

Added utf8_display_len

This commit is contained in:
James Booth 2015-01-17 21:09:40 +00:00
parent 4ac11ddcd6
commit ba89297382
6 changed files with 82 additions and 23 deletions

View File

@ -202,6 +202,28 @@ str_contains(const char str[], int size, char ch)
return 0;
}
int
utf8_display_len(const char * const str)
{
if (!str) {
return 0;
}
int len = 0;
gchar *curr = g_utf8_offset_to_pointer(str, 0);
while (*curr != '\0') {
gunichar curru = g_utf8_get_char(curr);
if (g_unichar_iswide(curru)) {
len += 2;
} else {
len ++;
}
curr = g_utf8_next_char(curr);
}
return len;
}
char *
prof_getline(FILE *stream)
{

View File

@ -104,6 +104,7 @@ gboolean mkdir_recursive(const char *dir);
char * str_replace(const char *string, const char *substr,
const char *replacement);
int str_contains(const char str[], int size, char ch);
int utf8_display_len(const char * const str);
char * prof_getline(FILE *stream);
char* release_get_latest(void);
gboolean release_is_new(char *found_version);

View File

@ -92,7 +92,6 @@ static int _printable(const wint_t ch);
static void _clear_input(void);
static void _go_to_end(void);
static void _delete_previous_word(void);
static int _get_display_length(void);
void
create_input_window(void)
@ -145,7 +144,7 @@ inp_block(void)
char *
inp_read(int *key_type, wint_t *ch)
{
int display_size = _get_display_length();
int display_size = utf8_display_len(input);
// echo off, and get some more input
noecho();
@ -260,24 +259,6 @@ inp_put_back(void)
_inp_win_update_virtual();
}
static int
_get_display_length(void)
{
int len = 0;
gchar *curr = g_utf8_offset_to_pointer(input, 0);
while (*curr != '\0') {
gunichar curru = g_utf8_get_char(curr);
if (g_unichar_iswide(curru)) {
len += 2;
} else {
len ++;
}
curr = g_utf8_next_char(curr);
}
return len;
}
void
inp_replace_input(const char * const new_input)
{
@ -322,7 +303,7 @@ _handle_edit(int key_type, const wint_t ch)
char *next = NULL;
int inp_x = getcurx(inp_win);
int next_ch;
int display_size = _get_display_length();
int display_size = utf8_display_len(input);
// CTRL-LEFT
if ((key_type == KEY_CODE_YES) && (ch == 547 || ch == 545 || ch == 544 || ch == 540 || ch == 539) && (inp_x > 0)) {
@ -608,7 +589,7 @@ static void
_handle_backspace(void)
{
int inp_x = getcurx(inp_win);
int display_size = _get_display_length();
int display_size = utf8_display_len(input);
roster_reset_search_attempts();
if (display_size > 0) {
@ -784,7 +765,7 @@ _delete_previous_word(void)
static void
_go_to_end(void)
{
int display_size = _get_display_length();
int display_size = utf8_display_len(input);
wmove(inp_win, 0, display_size);
if (display_size > cols-2) {
pad_start = display_size - cols + 1;

View File

@ -544,3 +544,46 @@ void test_p_sha1_hash7(void **state)
assert_string_equal(result, "bNfKVfqEOGmzlH8M+e8FYTB46SU=");
}
void utf8_display_len_null_str(void **state)
{
int result = utf8_display_len(NULL);
assert_int_equal(0, result);
}
void utf8_display_len_1_non_wide(void **state)
{
int result = utf8_display_len("1");
assert_int_equal(1, result);
}
void utf8_display_len_1_wide(void **state)
{
int result = utf8_display_len("");
assert_int_equal(2, result);
}
void utf8_display_len_non_wide(void **state)
{
int result = utf8_display_len("123456789abcdef");
assert_int_equal(15, result);
}
void utf8_display_len_wide(void **state)
{
int result = utf8_display_len("12三四56");
assert_int_equal(8, result);
}
void utf8_display_len_all_wide(void **state)
{
int result = utf8_display_len("ひらがな");
assert_int_equal(8, result);
}

View File

@ -46,3 +46,9 @@ void test_p_sha1_hash5(void **state);
void test_p_sha1_hash6(void **state);
void test_p_sha1_hash6(void **state);
void test_p_sha1_hash7(void **state);
void utf8_display_len_null_str(void **state);
void utf8_display_len_1_non_wide(void **state);
void utf8_display_len_1_wide(void **state);
void utf8_display_len_non_wide(void **state);
void utf8_display_len_wide(void **state);
void utf8_display_len_all_wide(void **state);

View File

@ -85,6 +85,12 @@ int main(int argc, char* argv[]) {
unit_test(test_p_sha1_hash5),
unit_test(test_p_sha1_hash6),
unit_test(test_p_sha1_hash7),
unit_test(utf8_display_len_null_str),
unit_test(utf8_display_len_1_non_wide),
unit_test(utf8_display_len_1_wide),
unit_test(utf8_display_len_non_wide),
unit_test(utf8_display_len_wide),
unit_test(utf8_display_len_all_wide),
unit_test(clear_empty),
unit_test(reset_after_create),