From 959d7d76aec53f03463223ee64afb805e1865464 Mon Sep 17 00:00:00 2001 From: erdgeist <> Date: Mon, 15 Jul 2013 19:05:54 +0000 Subject: Work around mbrlen return code stupidity. --- vchat-ui.c | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) (limited to 'vchat-ui.c') diff --git a/vchat-ui.c b/vchat-ui.c index 7873268..f3e5af5 100755 --- a/vchat-ui.c +++ b/vchat-ui.c @@ -142,14 +142,22 @@ togglequery() { } const char * skip_to_character( const char * string, size_t offset ) { + size_t ch_size; mbstate_t mbs; memset(&mbs, 0, sizeof(mbs)); while( offset-- > 0 ) { - size_t ch_size = mbrlen( string, MB_CUR_MAX, &mbs ); - if( ch_size < 0 ) return NULL; - if( !ch_size ) break; - string += ch_size; + switch( ch_size = mbrlen( string, MB_CUR_MAX, &mbs ) ) + { + case (size_t)-1: + case (size_t)-2: + return NULL; + case 0: + return string; + default: + string += ch_size; + break; + } } return string; } @@ -158,13 +166,19 @@ size_t offset_to_character( const char * string, size_t offset ) { mbstate_t mbs; memset(&mbs, 0, sizeof(mbs)); const char * string_offset = string + offset; - size_t nchars = 0; + size_t ch_size, nchars = 0; while( string < string_offset ) { - size_t ch_size = mbrlen( string, MB_CUR_MAX, &mbs ); - if( ch_size < 0 ) return -1; - if( !ch_size ) break; - string += ch_size; + switch( ch_size = mbrlen( string, MB_CUR_MAX, &mbs ) ) + { + case (size_t)-1: + case (size_t)-2: + return -1; + case 0: + return nchars; + default: + string += ch_size; + } nchars++; } return nchars; -- cgit v1.2.3