diff options
author | robertabcd <robertabcd@63ad8ddf-47c3-0310-b6dd-a9e9d9715204> | 2012-04-14 20:13:15 +0800 |
---|---|---|
committer | robertabcd <robertabcd@63ad8ddf-47c3-0310-b6dd-a9e9d9715204> | 2012-04-14 20:13:15 +0800 |
commit | f0a16c418f0b87cbb8e72030484187c44801022c (patch) | |
tree | 072fbc44757f8ab7950bd67dcd084d383ac1de7e | |
parent | 5c2ff892ebe0428ec5bf5415dab35bedda204727 (diff) | |
download | pttbbs-f0a16c418f0b87cbb8e72030484187c44801022c.tar pttbbs-f0a16c418f0b87cbb8e72030484187c44801022c.tar.gz pttbbs-f0a16c418f0b87cbb8e72030484187c44801022c.tar.bz2 pttbbs-f0a16c418f0b87cbb8e72030484187c44801022c.tar.lz pttbbs-f0a16c418f0b87cbb8e72030484187c44801022c.tar.xz pttbbs-f0a16c418f0b87cbb8e72030484187c44801022c.tar.zst pttbbs-f0a16c418f0b87cbb8e72030484187c44801022c.zip |
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
-rw-r--r-- | pttbbs/mbbsd/edit.c | 32 |
1 files 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 |