From f0a16c418f0b87cbb8e72030484187c44801022c Mon Sep 17 00:00:00 2001 From: robertabcd Date: Sat, 14 Apr 2012 12:13:15 +0000 Subject: Rewrote fix_cursor, the old one is impossible to understand. git-svn-id: http://opensvn.csie.org/pttbbs/trunk@5647 63ad8ddf-47c3-0310-b6dd-a9e9d9715204 --- pttbbs/mbbsd/edit.c | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/pttbbs/mbbsd/edit.c b/pttbbs/mbbsd/edit.c index 4e6c2547..622de5bc 100644 --- a/pttbbs/mbbsd/edit.c +++ b/pttbbs/mbbsd/edit.c @@ -266,19 +266,27 @@ int mchar_len(unsigned char *str) #define FC_RIGHT (0) #define FC_LEFT (1) -int fix_cursor(char *str, int pos, int dir) +/* Return the cursor position aligned to the beginning of a character. + * If `pos' is in the middle of a character, the alignment determines on `dir': + * FC_LEFT: aligned to the current character. + * FC_RIGHT: aligned to the next character. + */ +static int +fix_cursor(char *str, int pos, int dir) { - int newpos, w; - assert(dir == FC_RIGHT || dir == FC_LEFT); - - for(newpos = 0; - *str != '\0' && - (w = mchar_len((unsigned char*)str), - newpos + 1 + (dir == FC_RIGHT ? 0 : w - 1) <= pos); - str += w, newpos += w) - ; - - return newpos; + int newpos = 0, w = 0; + assert(dir == FC_RIGHT || dir == FC_LEFT); + assert(pos >= 0); + + while (*str != '\0' && newpos < pos) { + w = mchar_len((unsigned char *) str); + str += w; + newpos += w; + } + if (dir == FC_LEFT && newpos > pos) + newpos -= w; + + return newpos; } #endif -- cgit v1.2.3