summaryrefslogtreecommitdiff
path: root/src/pal/src/cruntime/lstr.cpp
blob: 2267d8491b41e7fd57c79edae2664b7d6d2850d5 (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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// 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:

    lstr.c

Abstract:

    Implementation of functions manipulating unicode/ansi strings. (lstr*A/W)



--*/

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

SET_DEFAULT_DEBUG_CHANNEL(CRT);



/*++
Function:
  lstrcatW

The lstrcat function appends one string to another. 

Parameters

lpString1        [in/out] Pointer to a null-terminated string. The buffer must be large
                          enough to contain both strings. 
lpString2        [in]     Pointer to the null-terminated string to be appended to the
                          string specified in the lpString1 parameter. 

Return Values

If the function succeeds, the return value is a pointer to the buffer.
If the function fails, the return value is NULL. 

--*/
LPWSTR
PALAPI
lstrcatW(
	 IN OUT LPWSTR lpString1,
	 IN LPCWSTR lpString2)
{
    LPWSTR lpStart = lpString1;

    PERF_ENTRY(lstrcatW);
    ENTRY("lstrcatW (lpString1=%p (%S), lpString2=%p (%S))\n",
          lpString1?lpString1:W16_NULLSTRING,
          lpString1?lpString1:W16_NULLSTRING, lpString2?lpString2:W16_NULLSTRING, lpString2?lpString2:W16_NULLSTRING);

    if (lpString1 == NULL)
    {
        ERROR("invalid lpString1 argument\n");
        LOGEXIT("lstrcatW returning LPWSTR NULL\n");
        PERF_EXIT(lstrcatW);
        return NULL;
    }

    if (lpString2 == NULL)
    {
        ERROR("invalid lpString2 argument\n");
        LOGEXIT("lstrcatW returning LPWSTR NULL\n");
        PERF_EXIT(lstrcatW);
        return NULL;
    }

    /* find end of source string */
    while (*lpString1)
    {
        lpString1++;
    }

    /* concatenate new string */
    while(*lpString2)
    {
        *lpString1++ = *lpString2++;
    }

    /* add terminating null */
    *lpString1 = '\0';

    LOGEXIT("lstrcatW returning LPWSTR %p (%S)\n", lpStart, lpStart);
    PERF_EXIT(lstrcatW);
    return lpStart;
}


/*++
Function:
  lstrcpyW

The lstrcpy function copies a string to a buffer. 

To copy a specified number of characters, use the lstrcpyn function.

Parameters

lpString1        [out] Pointer to a buffer to receive the contents of the string pointed
                       to by the lpString2 parameter. The buffer must be large enough to
                       contain the string, including the terminating null character. 

lpString2        [in] Pointer to the null-terminated string to be copied. 

Return Values

If the function succeeds, the return value is a pointer to the buffer.
If the function fails, the return value is NULL. 

--*/
LPWSTR
PALAPI
lstrcpyW(
	 OUT LPWSTR lpString1,
	 IN LPCWSTR lpString2)
{
    LPWSTR lpStart = lpString1;

    PERF_ENTRY(lstrcpyW);
    ENTRY("lstrcpyW (lpString1=%p, lpString2=%p (%S))\n",
          lpString1?lpString1:W16_NULLSTRING, lpString2?lpString2:W16_NULLSTRING, lpString2?lpString2:W16_NULLSTRING);

    if (lpString1 == NULL)
    {
        ERROR("invalid lpString1 argument\n");
        LOGEXIT("lstrcpyW returning LPWSTR NULL\n");
        PERF_EXIT(lstrcpyW);
        return NULL;
    }

    if (lpString2 == NULL)
    {
        ERROR("invalid lpString2 argument\n");
        LOGEXIT("lstrcpyW returning LPWSTR NULL\n");
        PERF_EXIT(lstrcpyW);
        return NULL;
    }

    /* copy source string to destination string */
    while(*lpString2)
    {
        *lpString1++ = *lpString2++;
    }

    /* add terminating null */
    *lpString1 = '\0';

    LOGEXIT("lstrcpyW returning LPWSTR %p (%S)\n", lpStart, lpStart);
    PERF_EXIT(lstrcpyW);
    return lpStart;
}


/*++
Function:
  lstrlenA


The lstrlen function returns the length in bytes (ANSI version) or
characters (Unicode version) of the specified string (not including
the terminating null character).

Parameters

lpString        [in] Pointer to a null-terminated string. 

Return Values

The return value specifies the length of the string, in TCHARs. This
refers to bytes for ANSI versions of the function or characters for
Unicode versions.

--*/
int
PALAPI
lstrlenA( IN LPCSTR lpString)
{
    int nChar = 0;

    PERF_ENTRY(lstrlenA);
    ENTRY("lstrlenA (lpString=%p (%s))\n", lpString?lpString:"NULL", lpString?lpString:"NULL");
    if (lpString)
    {
        while (*lpString++)
        {
            nChar++;      
        }
    }
    LOGEXIT("lstrlenA returning int %d\n", nChar);
    PERF_EXIT(lstrlenA);
    return nChar;
}


/*++
Function:
  lstrlenW

The lstrlen function returns the length in bytes (ANSI version) or
characters (Unicode version) of the specified string (not including
the terminating null character).

Parameters

lpString        [in] Pointer to a null-terminated string. 

Return Values

The return value specifies the length of the string, in TCHARs. This
refers to bytes for ANSI versions of the function or characters for
Unicode versions.

--*/
int
PALAPI
lstrlenW(
	 IN LPCWSTR lpString)
{
    int nChar = 0;

    PERF_ENTRY(lstrlenW);
    ENTRY("lstrlenW (lpString=%p (%S))\n", lpString?lpString:W16_NULLSTRING, lpString?lpString:W16_NULLSTRING);
    if (lpString != NULL)
    {
        while (*lpString++)
        {
            nChar++;      
        }
    }
    LOGEXIT("lstrlenW returning int %d\n", nChar);
    PERF_EXIT(lstrlenW);
    return nChar;
}


/*++
Function:
  lstrcpynW

The lstrcpyn function copies a specified number of characters from a
source string into a buffer.

Parameters

lpString1        [out] Pointer to a buffer into which the function copies characters.
                       The buffer must be large enough to contain the number of TCHARs
                       specified by iMaxLength, including room for a terminating null character. 
lpString2        [in]  Pointer to a null-terminated string from which the function copies
                       characters. 
iMaxLength       [in]  Specifies the number of TCHARs to be copied from the string pointed
                       to by lpString2 into the buffer pointed to by lpString1, including a
                       terminating null character.

Return Values

If the function succeeds, the return value is a pointer to the buffer.
If the function fails, the return value is NULL. 

--*/
LPWSTR
PALAPI
lstrcpynW(
	  OUT LPWSTR lpString1,
	  IN LPCWSTR lpString2,
	  IN int iMaxLength)
{
    LPWSTR lpStart = lpString1;

    PERF_ENTRY(lstrcpynW);
    ENTRY("lstrcpynW (lpString1=%p, lpString2=%p (%S), iMaxLength=%d)\n",
              lpString1?lpString1:W16_NULLSTRING, lpString2?lpString2:W16_NULLSTRING, lpString2?lpString2:W16_NULLSTRING, iMaxLength);

    if (lpString1 == NULL)
    {
        ERROR("invalid lpString1 argument\n");
        LOGEXIT("lstrcpynW returning LPWSTR NULL\n");
        PERF_EXIT(lstrcpynW);
        return NULL;
    }

    if (lpString2 == NULL)
    {
        ERROR("invalid lpString2 argument\n");
        LOGEXIT("lstrcpynW returning LPWSTR NULL\n");
        PERF_EXIT(lstrcpynW);
        return NULL;
    }

    /* copy source string to destination string */
    while(iMaxLength > 1 && *lpString2)
    {
        *lpString1++ = *lpString2++;
        iMaxLength--;
    }

    /* add terminating null */
    if (iMaxLength > 0)
    {
        *lpString1 = '\0';
    }

    LOGEXIT("lstrcpynW returning LPWSTR %p (%S)\n", lpStart, lpStart);
    PERF_EXIT(lstrcpynW);
    return lpStart;

}