msvcrt[1/2]: test and fix _ismbslead and _ismbstrail
Mikolaj Zalewski
mikolajz at google.com
Thu Aug 23 20:13:57 CDT 2007
-------------- next part --------------
From 093c304645c40c4057da51991a517dcba04e421c Mon Sep 17 00:00:00 2001
From: Mikolaj Zalewski <mikolaj at zalewski.pl>
Date: Thu, 23 Aug 2007 17:26:14 -0700
Subject: [PATCH] msvcrt: test and fix _ismbslead and _ismbstrail
---
dlls/msvcrt/mbcs.c | 29 ++++++++++++++++++-----------
dlls/msvcrt/tests/string.c | 36 +++++++++++++++++++++++++++++++++++-
2 files changed, 53 insertions(+), 12 deletions(-)
diff --git a/dlls/msvcrt/mbcs.c b/dlls/msvcrt/mbcs.c
index 718e387..91efadd 100644
--- a/dlls/msvcrt/mbcs.c
+++ b/dlls/msvcrt/mbcs.c
@@ -1058,18 +1058,22 @@ int CDECL _ismbbtrail(unsigned int c)
*/
int CDECL _ismbslead(const unsigned char* start, const unsigned char* str)
{
- /* Lead bytes can also be trail bytes if caller messed up
- * iterating through the string...
+ int lead = 0;
+
+ if(!g_mbcp_is_multibyte)
+ return 0;
+
+ /* Lead bytes can also be trail bytes so we need to analise the string
*/
- if(MSVCRT___mb_cur_max > 1)
+ while (start <= str)
{
- while(start < str)
- start += MSVCRT_isleadbyte(*str) ? 2 : 1;
-
- if(start == str)
- return MSVCRT_isleadbyte(*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
*/
int CDECL _ismbstrail(const unsigned char* start, const unsigned char* str)
{
- /* Must not be a lead, and must be preceded by one */
- return !_ismbslead(start, str) && MSVCRT_isleadbyte(str[-1]);
+ /* Note: this function doesn't check _ismbbtrail */
+ if ((str > start) && _ismbslead(start, str-1))
+ return -1;
+ else
+ return 0;
}
/*********************************************************************
diff --git a/dlls/msvcrt/tests/string.c b/dlls/msvcrt/tests/string.c
index 004d059..9199641 100644
--- a/dlls/msvcrt/tests/string.c
+++ b/dlls/msvcrt/tests/string.c
@@ -183,7 +183,7 @@ static void test_mbcp(void)
int curr_mbcp = _getmbcp();
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 *mbsonlylead = (unsigned char *)"\xb0\0\xb1\xb2";
+ unsigned char *mbsonlylead = (unsigned char *)"\xb0\0\xb1\xb2 \xb3";
unsigned char buf[16];
int step;
@@ -205,6 +205,40 @@ static void test_mbcp(void)
ok(_ismbbtrail('\xb0'), "\xa0 should 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 */
expect_eq(_mbsnextc(mbstring), 0xb0b1, int, "%x");
expect_eq(_mbsnextc(&mbstring[2]), 0xb220, int, "%x"); /* lead + invalid tail */
--
1.4.1
More information about the wine-patches
mailing list