msvcrt: Test and fix _ismbslead and _ismbstrail.

This commit is contained in:
Mikolaj Zalewski 2007-08-23 17:26:14 -07:00 committed by Alexandre Julliard
parent b068ce5c2c
commit 884d718c49
2 changed files with 54 additions and 13 deletions

View File

@ -1058,18 +1058,22 @@ int CDECL _ismbbtrail(unsigned int c)
*/ */
int CDECL _ismbslead(const unsigned char* start, const unsigned char* str) int CDECL _ismbslead(const unsigned char* start, const unsigned char* str)
{ {
/* Lead bytes can also be trail bytes if caller messed up int lead = 0;
* iterating through the string...
*/
if(MSVCRT___mb_cur_max > 1)
{
while(start < str)
start += MSVCRT_isleadbyte(*str) ? 2 : 1;
if(start == str) if(!g_mbcp_is_multibyte)
return MSVCRT_isleadbyte(*str); return 0;
/* Lead bytes can also be trail bytes so we need to analise the string
*/
while (start <= str)
{
if (!*start)
return 0;
lead = !lead && _ismbblead(*start);
start++;
} }
return 0; /* Must have been a trail, we skipped it */
return lead ? -1 : 0;
} }
/********************************************************************* /*********************************************************************
@ -1077,8 +1081,11 @@ int CDECL _ismbslead(const unsigned char* start, const unsigned char* str)
*/ */
int CDECL _ismbstrail(const unsigned char* start, const unsigned char* str) int CDECL _ismbstrail(const unsigned char* start, const unsigned char* str)
{ {
/* Must not be a lead, and must be preceded by one */ /* Note: this function doesn't check _ismbbtrail */
return !_ismbslead(start, str) && MSVCRT_isleadbyte(str[-1]); if ((str > start) && _ismbslead(start, str-1))
return -1;
else
return 0;
} }
/********************************************************************* /*********************************************************************

View File

@ -183,7 +183,7 @@ static void test_mbcp(void)
int curr_mbcp = _getmbcp(); int curr_mbcp = _getmbcp();
unsigned char *mbstring = (unsigned char *)"\xb0\xb1\xb2 \xb3\xb4 \xb5"; /* incorrect string */ unsigned char *mbstring = (unsigned char *)"\xb0\xb1\xb2 \xb3\xb4 \xb5"; /* incorrect string */
unsigned char *mbstring2 = (unsigned char *)"\xb0\xb1\xb2\xb3Q\xb4\xb5"; /* correct string */ unsigned char *mbstring2 = (unsigned char *)"\xb0\xb1\xb2\xb3Q\xb4\xb5"; /* correct string */
unsigned char *mbsonlylead = (unsigned char *)"\xb0\0\xb1\xb2"; unsigned char *mbsonlylead = (unsigned char *)"\xb0\0\xb1\xb2 \xb3";
unsigned char buf[16]; unsigned char buf[16];
int step; int step;
@ -205,6 +205,40 @@ static void test_mbcp(void)
ok(_ismbbtrail('\xb0'), "\xa0 should be a trail byte\n"); ok(_ismbbtrail('\xb0'), "\xa0 should be a trail byte\n");
ok(_ismbbtrail(' ') == FALSE, "' ' should not be a trail byte\n"); ok(_ismbbtrail(' ') == FALSE, "' ' should not be a trail byte\n");
/* _ismbslead */
expect_eq(_ismbslead(mbstring, &mbstring[0]), -1, int, "%d");
expect_eq(_ismbslead(mbstring, &mbstring[1]), FALSE, int, "%d");
expect_eq(_ismbslead(mbstring, &mbstring[2]), -1, int, "%d");
expect_eq(_ismbslead(mbstring, &mbstring[3]), FALSE, int, "%d");
expect_eq(_ismbslead(mbstring, &mbstring[4]), -1, int, "%d");
expect_eq(_ismbslead(mbstring, &mbstring[5]), FALSE, int, "%d");
expect_eq(_ismbslead(mbstring, &mbstring[6]), FALSE, int, "%d");
expect_eq(_ismbslead(mbstring, &mbstring[7]), -1, int, "%d");
expect_eq(_ismbslead(mbstring, &mbstring[8]), FALSE, int, "%d");
expect_eq(_ismbslead(mbsonlylead, &mbsonlylead[0]), -1, int, "%d");
expect_eq(_ismbslead(mbsonlylead, &mbsonlylead[1]), FALSE, int, "%d");
expect_eq(_ismbslead(mbsonlylead, &mbsonlylead[2]), FALSE, int, "%d");
expect_eq(_ismbslead(mbsonlylead, &mbsonlylead[5]), FALSE, int, "%d");
/* _ismbstrail */
expect_eq(_ismbstrail(mbstring, &mbstring[0]), FALSE, int, "%d");
expect_eq(_ismbstrail(mbstring, &mbstring[1]), -1, int, "%d");
expect_eq(_ismbstrail(mbstring, &mbstring[2]), FALSE, int, "%d");
expect_eq(_ismbstrail(mbstring, &mbstring[3]), -1, int, "%d");
expect_eq(_ismbstrail(mbstring, &mbstring[4]), FALSE, int, "%d");
expect_eq(_ismbstrail(mbstring, &mbstring[5]), -1, int, "%d");
expect_eq(_ismbstrail(mbstring, &mbstring[6]), FALSE, int, "%d");
expect_eq(_ismbstrail(mbstring, &mbstring[7]), FALSE, int, "%d");
expect_eq(_ismbstrail(mbstring, &mbstring[8]), -1, int, "%d");
expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[0]), FALSE, int, "%d");
expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[1]), -1, int, "%d");
expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[2]), FALSE, int, "%d");
expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[3]), FALSE, int, "%d");
expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[4]), FALSE, int, "%d");
expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[5]), FALSE, int, "%d");
/* _mbsnextc */ /* _mbsnextc */
expect_eq(_mbsnextc(mbstring), 0xb0b1, int, "%x"); expect_eq(_mbsnextc(mbstring), 0xb0b1, int, "%x");
expect_eq(_mbsnextc(&mbstring[2]), 0xb220, int, "%x"); /* lead + invalid tail */ expect_eq(_mbsnextc(&mbstring[2]), 0xb220, int, "%x"); /* lead + invalid tail */