summaryrefslogtreecommitdiff
path: root/src/pal/src/cruntime/mbstring.cpp
blob: ace2aa56c6d19d1e3e9370ad9674e36acf70edac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

/*++



Module Name:

    mbstring.c

Abstract:

    Implementation of the multi-byte string functions in the C runtime library that 
    are Windows specific.

Implementation Notes:

    Assuming it is not possible to change multi-byte code page using
    the PAL (_setmbcp does not seem to be required), these functions
    should have a trivial implementation (treat as single-byte). If it
    is possible, then support for multi-byte code pages will have to
    be implemented before these functions can behave correctly for
    multi-byte strings.



--*/

#include "pal/palinternal.h"
#include "pal/dbgmsg.h"


SET_DEFAULT_DEBUG_CHANNEL(CRT);


/*++
Function:
  _mbsinc

Return Value

Returns a pointer to the character that immediately follows string.

Parameter

string  Character pointer

Remarks

The _mbsinc function returns a pointer to the first byte of the
multibyte character that immediately follows string.

--*/
unsigned char *
__cdecl
_mbsinc(
        const unsigned char *string)
{
    unsigned char *ret;

    PERF_ENTRY(_mbsinc);
    ENTRY("_mbsinc (string=%p)\n", string);

    if (string == NULL)
    {
        ret = NULL;
    }
    else
    {
        ret = (unsigned char *) string;
        if (IsDBCSLeadByteEx(CP_ACP, *ret))
        {
            ++ret;
        }
        ++ret;
    }

    LOGEXIT("_mbsinc returning unsigned char* %p (%s)\n", ret, ret);
    PERF_EXIT(_mbsinc);
    return ret;
}


/*++
Function:
  _mbsninc

Return Value

Returns a pointer to string after string has been incremented by count
characters, or NULL if the supplied pointer is NULL. If count is
greater than or equal to the number of characters in string, the
result is undefined.

Parameters

string  Source string
count   Number of characters to increment string pointer

Remarks

The _mbsninc function increments string by count multibyte
characters. _mbsninc recognizes multibyte-character sequences
according to the multibyte code page currently in use.

--*/
unsigned char *
__cdecl
_mbsninc(
         const unsigned char *string, size_t count)
{
    unsigned char *ret;
    CPINFO cpinfo;

    PERF_ENTRY(_mbsninc);
    ENTRY("_mbsninc (string=%p, count=%lu)\n", string, count);
    if (string == NULL)
    {
        ret = NULL;
    }
    else
    {
        ret = (unsigned char *) string;
        if (GetCPInfo(CP_ACP, &cpinfo) && cpinfo.MaxCharSize == 1)
        {
            ret += min(count, strlen((const char*)string));
        }
        else
        {
            while (count-- && (*ret != 0))
            {
                if (IsDBCSLeadByteEx(CP_ACP, *ret))
                {
                    ++ret;
                }
                ++ret;
            }
        }
    }
    LOGEXIT("_mbsninc returning unsigned char* %p (%s)\n", ret, ret);
    PERF_EXIT(_mbsninc);
    return ret;
}

/*++
Function:
  _mbsdec

Return Value

_mbsdec returns a pointer to the character that immediately precedes
current; _mbsdec returns NULL if the value of start is greater than or
equal to that of current.

Parameters

start    Pointer to first byte of any multibyte character in the source
         string; start must precede current in the source string

current  Pointer to first byte of any multibyte character in the source
         string; current must follow start in the source string

--*/
unsigned char * 
__cdecl
_mbsdec(
        const unsigned char *start, 
        const unsigned char *current)
{
    unsigned char *ret;
    unsigned char *strPtr;
    CPINFO cpinfo;

    PERF_ENTRY(_mbsdec);
    ENTRY("_mbsdec (start=%p, current=%p)\n", start, current);

    if (current <= start)
    {
        ret = NULL;
    }
    else if (GetCPInfo(CP_ACP, &cpinfo) && cpinfo.MaxCharSize == 1)
    {
        ret = (unsigned char *) current - 1;
    }
    else
    {
        ret = strPtr = (unsigned char *) start;
        while (strPtr < current)
        {
            ret = strPtr;
            if (IsDBCSLeadByteEx(CP_ACP, *strPtr))
            {
                ++strPtr;
            }
            ++strPtr;
        }
    }
    LOGEXIT("_mbsdec returning unsigned int %p (%s)\n", ret, ret);
    PERF_EXIT(_mbsdec);
    return ret;
}