From 5fe4b4b4250a4bf15610b8bad863c8ac0a17de52 Mon Sep 17 00:00:00 2001 From: Gregor Schneider Date: Thu, 18 Dec 2008 21:17:22 +0000 Subject: [PATCH] - Update _mbclen, mblen, _mbslen, _mbsnextc and _mbstrlen which should rely on the leadbyte mechanism - Fixes 9 msvcrt string tests, 7 failures tbd - Remove leftover function declaration from ismblead.c svn path=/trunk/; revision=38181 --- reactos/lib/sdk/crt/mbstring/ismblead.c | 2 -- reactos/lib/sdk/crt/mbstring/mbclen.c | 31 ++++++++++++++-------- reactos/lib/sdk/crt/mbstring/mbslen.c | 34 +++++++++++++++++------- reactos/lib/sdk/crt/mbstring/mbsnextc.c | 27 ++++++++++--------- reactos/lib/sdk/crt/mbstring/mbstrlen.c | 35 +++++++++++++++++-------- 5 files changed, 83 insertions(+), 46 deletions(-) diff --git a/reactos/lib/sdk/crt/mbstring/ismblead.c b/reactos/lib/sdk/crt/mbstring/ismblead.c index 80c535c2ac9..9b315ae93db 100644 --- a/reactos/lib/sdk/crt/mbstring/ismblead.c +++ b/reactos/lib/sdk/crt/mbstring/ismblead.c @@ -14,8 +14,6 @@ #include #include -size_t _mbclen2(const unsigned int s); - /* * @implemented */ diff --git a/reactos/lib/sdk/crt/mbstring/mbclen.c b/reactos/lib/sdk/crt/mbstring/mbclen.c index 414852d04b4..e186674242d 100644 --- a/reactos/lib/sdk/crt/mbstring/mbclen.c +++ b/reactos/lib/sdk/crt/mbstring/mbclen.c @@ -1,18 +1,30 @@ +/* + * COPYRIGHT: See COPYING in the top level directory + * PROJECT: ReactOS system libraries + * FILE: lib/sdk/crt/mbstring/mbclen.c + * PURPOSE: Determines the length of a multi byte character + * PROGRAMERS: + * Copyright 1999 Alexandre Julliard + * Copyright 2000 Jon Griffths + * + */ + #include #include +int isleadbyte(int byte); /* * @implemented */ size_t _mbclen(const unsigned char *s) { - return (_ismbblead(*s>>8) && _ismbbtrail(*s&0x00FF)) ? 2 : 1; + return _ismbblead(*s) ? 2 : 1; } size_t _mbclen2(const unsigned int s) { - return (_ismbblead(s>>8) && _ismbbtrail(s&0x00FF)) ? 2 : 1; + return (_ismbblead(s>>8) && _ismbbtrail(s&0x00FF)) ? 2 : 1; } /* @@ -20,14 +32,11 @@ size_t _mbclen2(const unsigned int s) * * @implemented */ -int mblen( const char *s, size_t count ) +int mblen( const char *str, size_t size ) { - size_t l; - if ( s == NULL ) - return 0; - - l = _mbclen((const unsigned char *)s); - if ( l < count ) - return -1; - return l; + if (str && *str && size) + { + return !isleadbyte(*str) ? 1 : (size>1 ? 2 : -1); + } + return 0; } diff --git a/reactos/lib/sdk/crt/mbstring/mbslen.c b/reactos/lib/sdk/crt/mbstring/mbslen.c index 67733298e2c..5093796ba53 100644 --- a/reactos/lib/sdk/crt/mbstring/mbslen.c +++ b/reactos/lib/sdk/crt/mbstring/mbslen.c @@ -1,18 +1,32 @@ -#include +/* + * COPYRIGHT: See COPYING in the top level directory + * PROJECT: ReactOS system libraries + * FILE: lib/sdk/crt/mbstring/mbslen.c + * PURPOSE: Determines the length of a multi byte string + * PROGRAMERS: + * Copyright 1999 Alexandre Julliard + * Copyright 2000 Jon Griffths + * + */ -size_t _mbclen2(const unsigned int s); +#include /* * @implemented */ size_t _mbslen(const unsigned char *str) { - int i = 0; - unsigned char *s; - - if (str == 0) - return 0; - - for (s = (unsigned char *)str; *s; s+=_mbclen2(*s),i++); - return i; + size_t len = 0; + while(*str) + { + if (_ismbblead(*str)) + { + str++; + if (!*str) /* count only full chars */ + break; + } + str++; + len++; + } + return len; } diff --git a/reactos/lib/sdk/crt/mbstring/mbsnextc.c b/reactos/lib/sdk/crt/mbstring/mbsnextc.c index 667024ae078..f5bd724e35c 100644 --- a/reactos/lib/sdk/crt/mbstring/mbsnextc.c +++ b/reactos/lib/sdk/crt/mbstring/mbsnextc.c @@ -1,20 +1,23 @@ +/* + * COPYRIGHT: See COPYING in the top level directory + * PROJECT: ReactOS system libraries + * FILE: lib/sdk/crt/mbstring/mbsnextc.c + * PURPOSE: Finds the next character in a string + * PROGRAMERS: +* Copyright 1999 Alexandre Julliard + * Copyright 2000 Jon Griffths + * + */ + #include #include /* * @implemented */ -unsigned int _mbsnextc (const unsigned char *src) +unsigned int _mbsnextc (const unsigned char *str) { - unsigned char *char_src = (unsigned char *)src; - unsigned short *short_src = (unsigned short *)src; - - if (src == NULL) - return 0; - - if (!_ismbblead(*src)) - return *char_src; - else - return *short_src; - return 0; + if(_ismbblead(*str)) + return *str << 8 | str[1]; + return *str; } diff --git a/reactos/lib/sdk/crt/mbstring/mbstrlen.c b/reactos/lib/sdk/crt/mbstring/mbstrlen.c index e4411f1f708..c600bd1029b 100644 --- a/reactos/lib/sdk/crt/mbstring/mbstrlen.c +++ b/reactos/lib/sdk/crt/mbstring/mbstrlen.c @@ -1,19 +1,32 @@ +/* + * COPYRIGHT: See COPYING in the top level directory + * PROJECT: ReactOS system libraries + * FILE: lib/sdk/crt/mbstring/mbstrlen.c + * PURPOSE: Determines the length of a multi byte string, current locale + * PROGRAMERS: + * Copyright 1999 Alexandre Julliard + * Copyright 2000 Jon Griffths + * + */ + #include #include +int isleadbyte(int byte); + /* * @implemented */ -size_t _mbstrlen( const char *string ) +size_t _mbstrlen( const char *str ) { - char *s = (char *)string; - size_t i = 0; - - while ( *s != 0 ) { - if ( _ismbblead(*s) ) - s++; - s++; - i++; - } - return i; + size_t len = 0; + while(*str) + { + /* FIXME: According to the documentation we are supposed to test for + * multi-byte character validity. Whatever that means + */ + str += isleadbyte(*str) ? 2 : 1; + len++; + } + return len; }