ReactOS 0.4.16-dev-1028-g8602629
mbslen_s.cpp
Go to the documentation of this file.
1/***
2*mbslen_s.c - Find length of MBCS string
3*
4* Copyright (c) Microsoft Corporation. All rights reserved.
5*
6*Purpose:
7* Find length of MBCS string
8*
9*******************************************************************************/
10#ifndef _MBCS
11 #error This file should only be compiled with _MBCS defined
12#endif
13
14#include <corecrt_internal.h>
16#include <locale.h>
17#include <string.h>
18
19/***
20* _mbsnlen - Find length of MBCS string
21*
22*Purpose:
23* Find the length of the MBCS string (in characters).
24*
25*Entry:
26* unsigned char *s = string
27* size_t maxsize
28*
29*Exit:
30* Returns the number of MBCS chars in the string.
31* Only the first sizeInBytes bytes of the string are inspected: if the null
32* terminator is not found, sizeInBytes is returned.
33* If the string is null terminated in sizeInBytes bytes, the return value
34* will always be less than sizeInBytes.
35* Returns (size_t)-1 if something went wrong.
36*
37*Exceptions:
38* Input parameters are validated. Refer to the validation section of the function.
39*
40*******************************************************************************/
41
43 const unsigned char *s,
44 size_t sizeInBytes,
46 )
47{
48 size_t n, size;
49 _LocaleUpdate _loc_update(plocinfo);
50
51 if (_loc_update.GetLocaleT()->mbcinfo->ismbcodepage == 0)
52 return strnlen((const char *)s, sizeInBytes);
53
54 /* Note that we do not check if s == nullptr, because we do not
55 * return errno_t...
56 */
57
58 /* Note that sizeInBytes here is the number of bytes, not mb characters! */
59 for (n = 0, size = 0; size < sizeInBytes && *s; n++, s++, size++)
60 {
61 if ( _ismbblead_l(*s, _loc_update.GetLocaleT()) )
62 {
63 size++;
64 if (size >= sizeInBytes)
65 {
66 break;
67 }
68 if (*++s == '\0')
69 {
70 break;
71 }
72 }
73 }
74
75 return (size >= sizeInBytes ? sizeInBytes : n);
76}
77
79 const unsigned char *s,
80 size_t maxsize
81 )
82{
83 return _mbsnlen_l(s,maxsize, nullptr);
84}
#define __cdecl
Definition: accygwin.h:79
#define _ismbblead_l(_c, p)
GLdouble s
Definition: gl.h:2039
GLdouble n
Definition: glext.h:7729
GLsizeiptr size
Definition: glext.h:5919
_locale_t plocinfo
Definition: ismbbyte.cpp:75
size_t __cdecl _mbsnlen(const unsigned char *s, size_t maxsize)
Definition: mbslen_s.cpp:78
size_t __cdecl _mbsnlen_l(const unsigned char *s, size_t sizeInBytes, _locale_t plocinfo)
Definition: mbslen_s.cpp:42
size_t sizeInBytes
Definition: mbslwr.cpp:119
_In_ size_t const maxsize
Definition: strftime.cpp:120
size_t __cdecl strnlen(char const *const string, size_t const maximum_count)
Definition: strnlen.cpp:202