msvcrt[2/2]: implement and test _mbsbtype

Mikolaj Zalewski mikolajz at google.com
Thu Aug 23 20:17:37 CDT 2007


We can't scan the string backward as I wanted in the first patch as we
have to detect if we past the end of the string. The function
duplicates some code from _ismbslead but I wanted to do checking if it
is lead and checking if we passed the end in one pass.
-------------- next part --------------
From ebdd76e3e12e4ff3421d317e3aa862e1dc02f37e Mon Sep 17 00:00:00 2001
From: Mikolaj Zalewski <mikolaj at zalewski.pl>
Date: Thu, 23 Aug 2007 18:11:43 -0700
Subject: [PATCH] msvcrt: implement and test _mbsbtype
---
 dlls/msvcrt/mbcs.c         |   32 ++++++++++++++++++++++++++++++++
 dlls/msvcrt/msvcrt.spec    |    2 +-
 dlls/msvcrt/tests/string.c |   18 ++++++++++++++++++
 3 files changed, 51 insertions(+), 1 deletions(-)

diff --git a/dlls/msvcrt/mbcs.c b/dlls/msvcrt/mbcs.c
index 91efadd..9269a00 100644
--- a/dlls/msvcrt/mbcs.c
+++ b/dlls/msvcrt/mbcs.c
@@ -1089,6 +1089,38 @@ int CDECL _ismbstrail(const unsigned cha
 }
 
 /*********************************************************************
+ *		_mbsbtype (MSVCRT.@)
+ */
+int CDECL _mbsbtype(const unsigned char *str, MSVCRT_size_t count)
+{
+  int lead = 0;
+  const unsigned char *end = str + count;
+  int mbcp = g_mbcp_is_multibyte;
+
+  /* Lead bytes can also be trail bytes so we need to analise the string.
+   * Also we must return _MBC_ILLEGAL for chars past the end of the string
+   */
+  while (str < end) /* Note: we skip the last byte - will check after the loop */
+  {
+    if (!*str)
+      return _MBC_ILLEGAL;
+    lead = mbcp && !lead && _ismbblead(*str);
+    str++;
+  }
+
+  if (lead)
+    if (_ismbbtrail(*str))
+      return _MBC_TRAIL;
+    else
+      return _MBC_ILLEGAL;
+  else
+    if (_ismbblead(*str))
+      return _MBC_LEAD;
+    else
+      return _MBC_SINGLE;
+}
+
+/*********************************************************************
  *		_mbsset(MSVCRT.@)
  */
 unsigned char* CDECL _mbsset(unsigned char* str, unsigned int c)
diff --git a/dlls/msvcrt/msvcrt.spec b/dlls/msvcrt/msvcrt.spec
index fc81898..1a516f0 100644
--- a/dlls/msvcrt/msvcrt.spec
+++ b/dlls/msvcrt/msvcrt.spec
@@ -355,7 +355,7 @@ # extern _mbcasemap
 @ stub _mbctombb #(long)
 @ cdecl _mbctoupper(long)
 @ extern _mbctype MSVCRT_mbctype
-@ stub _mbsbtype #(str long)
+@ cdecl _mbsbtype(str long)
 @ cdecl _mbscat(str str)
 @ cdecl _mbschr(str long)
 @ cdecl _mbscmp(str str)
diff --git a/dlls/msvcrt/tests/string.c b/dlls/msvcrt/tests/string.c
index 9199641..6dabc61 100644
--- a/dlls/msvcrt/tests/string.c
+++ b/dlls/msvcrt/tests/string.c
@@ -239,6 +239,24 @@ static void test_mbcp(void)
     expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[4]), FALSE, int, "%d");
     expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[5]), FALSE, int, "%d");
 
+    /* _mbsbtype */
+    expect_eq(_mbsbtype(mbstring, 0), _MBC_LEAD, int, "%d");
+    expect_eq(_mbsbtype(mbstring, 1), _MBC_TRAIL, int, "%d");
+    expect_eq(_mbsbtype(mbstring, 2), _MBC_LEAD, int, "%d");
+    expect_eq(_mbsbtype(mbstring, 3), _MBC_ILLEGAL, int, "%d");
+    expect_eq(_mbsbtype(mbstring, 4), _MBC_LEAD, int, "%d");
+    expect_eq(_mbsbtype(mbstring, 5), _MBC_TRAIL, int, "%d");
+    expect_eq(_mbsbtype(mbstring, 6), _MBC_SINGLE, int, "%d");
+    expect_eq(_mbsbtype(mbstring, 7), _MBC_LEAD, int, "%d");
+    expect_eq(_mbsbtype(mbstring, 8), _MBC_ILLEGAL, int, "%d");
+
+    expect_eq(_mbsbtype(mbsonlylead, 0), _MBC_LEAD, int, "%d");
+    expect_eq(_mbsbtype(mbsonlylead, 1), _MBC_ILLEGAL, int, "%d");
+    expect_eq(_mbsbtype(mbsonlylead, 2), _MBC_ILLEGAL, int, "%d");
+    expect_eq(_mbsbtype(mbsonlylead, 3), _MBC_ILLEGAL, int, "%d");
+    expect_eq(_mbsbtype(mbsonlylead, 4), _MBC_ILLEGAL, int, "%d");
+    expect_eq(_mbsbtype(mbsonlylead, 5), _MBC_ILLEGAL, 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