Browse Source

Fix delete and backspace keys for UTF8 operations

git-svn-id: https://klish.googlecode.com/svn/trunk@486 0eaa4687-2ee9-07dd-09d9-bcdd2d2dd5fb
Serj Kalichev 13 years ago
parent
commit
77fc29496f
4 changed files with 22 additions and 11 deletions
  1. 8 1
      lub/string.h
  2. 8 1
      lub/string/string_nocasestr.c
  3. 6 5
      tinyrl/tinyrl.c
  4. 0 4
      tinyrl/tinyrl.h

+ 8 - 1
lub/string.h

@@ -34,6 +34,12 @@ If a "const char *" is returned then the client has no responsiblity for releasi
 #include <stddef.h>
 
 #include "lub/c_decl.h"
+#include "lub/types.h"
+
+#define UTF8_MASK 0xC0
+#define UTF8_11   0xC0 /* First UTF8 byte */
+#define UTF8_10   0x80 /* Next UTF8 bytes */
+
 _BEGIN_C_DECL
 /**
  * This operation duplicates the specified string.
@@ -261,7 +267,8 @@ char *lub_string_ndecode(const char *string, unsigned int len);
 char *lub_string_encode(const char *string, const char *escape_chars);
 
 char *lub_string_tolower(const char *str);
-unsigned int lub_string_equal_part(const char *str1, const char *str2);
+unsigned int lub_string_equal_part(const char *str1, const char *str2,
+	bool_t utf8);
 
 
 _END_C_DECL

+ 8 - 1
lub/string/string_nocasestr.c

@@ -42,7 +42,8 @@ const char *lub_string_nocasestr(const char *cs, const char *ct)
 }
 
 /*--------------------------------------------------------- */
-unsigned int lub_string_equal_part(const char *str1, const char *str2)
+unsigned int lub_string_equal_part(const char *str1, const char *str2,
+	bool_t utf8)
 {
 	unsigned int cnt = 0;
 
@@ -55,6 +56,12 @@ unsigned int lub_string_equal_part(const char *str1, const char *str2)
 		str1++;
 		str2++;
 	}
+	if (!utf8)
+		return cnt;
+
+	/* UTF8 features */
+	if (cnt && (UTF8_11 == (*(str1 - 1) & UTF8_MASK)))
+		cnt--;
 
 	return cnt;
 }

+ 6 - 5
tinyrl/tinyrl.c

@@ -276,7 +276,7 @@ static bool_t tinyrl_key_right(tinyrl_t * this, int key)
 }
 
 /*-------------------------------------------------------- */
-static bool_t tinyrl_key_backspace(tinyrl_t * this, int key)
+static bool_t tinyrl_key_backspace(tinyrl_t *this, int key)
 {
 	bool_t result = BOOL_FALSE;
 	if (this->point) {
@@ -295,9 +295,9 @@ static bool_t tinyrl_key_delete(tinyrl_t * this, int key)
 {
 	bool_t result = BOOL_FALSE;
 	if (this->point < this->end) {
-		unsigned end = this->point;
-		utf8_point_left(this);
-		tinyrl_delete_text(this, this->point, end);
+		unsigned begin = this->point++;
+		utf8_point_right(this);
+		tinyrl_delete_text(this, begin, this->point - 1);
 		result = BOOL_TRUE;
 	}
 	/* keep the compiler happy */
@@ -552,7 +552,8 @@ void tinyrl_redisplay(tinyrl_t * this)
 	if (this->last_buffer && (width == this->last_width)) {
 		unsigned int eq_len = 0;
 		/* If line and last line have the equal chars at begining */
-		eq_chars = lub_string_equal_part(this->line, this->last_buffer);
+		eq_chars = lub_string_equal_part(this->line, this->last_buffer,
+			this->utf8);
 		eq_len = utf8_nsyms(this, this->last_buffer, eq_chars);
 		count = utf8_nsyms(this, this->last_buffer, this->last_point);
 		tinyrl_internal_position(this, this->prompt_len + eq_len,

+ 0 - 4
tinyrl/tinyrl.h

@@ -15,10 +15,6 @@ from a CLI in a "readline" like fashion.
 #include "lub/c_decl.h"
 #include "tinyrl/history.h"
 
-#define UTF8_MASK 0xC0
-#define UTF8_11   0xC0
-#define UTF8_10   0x80
-
 _BEGIN_C_DECL typedef struct _tinyrl tinyrl_t;
 typedef enum {
     /**