]>
git.proxmox.com Git - mirror_edk2.git/blob - Tools/CCode/Source/String/String.c
3 Copyright (c) 2004-2006 Intel Corporation. All rights reserved
4 This program and the accompanying materials are licensed and made available
5 under the terms and conditions of the BSD License which accompanies this
6 distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 Unicode and ASCII string primatives.
25 #include <Common/UefiBaseTypes.h>
29 #include "CommonLib.h"
32 Returns the length of a Null-terminated Unicode string.
34 This function returns the number of Unicode characters in the Null-terminated
35 Unicode string specified by String.
37 If String is NULL, then ASSERT().
39 @param String Pointer to a Null-terminated Unicode string.
41 @return The length of String.
47 IN CONST CHAR16
*String
52 ASSERT (String
!= NULL
);
54 for (Length
= 0; *String
!= L
'\0'; String
++, Length
++) {
61 Returns the length of a Null-terminated ASCII string.
63 This function returns the number of ASCII characters in the Null-terminated
64 ASCII string specified by String.
66 If String is NULL, then ASSERT().
68 @param String Pointer to a Null-terminated ASCII string.
70 @return The length of String.
76 IN CONST CHAR8
*String
81 ASSERT (String
!= NULL
);
83 for (Length
= 0; *String
!= '\0'; String
++, Length
++) {
90 Copies one Null-terminated Unicode string to another Null-terminated Unicode
91 string and returns the new Unicode string.
93 This function copies the contents of the Unicode string Source to the Unicode
94 string Destination, and returns Destination. If Source and Destination
95 overlap, then the results are undefined.
97 If Destination is NULL, then ASSERT().
98 If Source is NULL, then ASSERT().
99 If Source and Destination overlap, then ASSERT().
101 @param Destination Pointer to a Null-terminated Unicode string.
102 @param Source Pointer to a Null-terminated Unicode string.
110 OUT CHAR16
*Destination
,
111 IN CONST CHAR16
*Source
117 // Destination cannot be NULL
119 ASSERT (Destination
!= NULL
);
122 // Destination and source cannot overlap
124 ASSERT ((UINTN
)(Destination
- Source
) > StrLen (Source
));
125 ASSERT ((UINTN
)(Source
- Destination
) > StrLen (Source
));
127 ReturnValue
= Destination
;
129 *(Destination
++) = *(Source
++);
136 Copies one Null-terminated Unicode string with a maximum length to another
137 Null-terminated Unicode string with a maximum length and returns the new
140 This function copies the contents of the Unicode string Source to the Unicode
141 string Destination, and returns Destination. At most, Length Unicode
142 characters are copied from Source to Destination. If Length is 0, then
143 Destination is returned unmodified. If Length is greater that the number of
144 Unicode characters in Source, then Destination is padded with Null Unicode
145 characters. If Source and Destination overlap, then the results are
148 If Destination is NULL, then ASSERT().
149 If Source is NULL, then ASSERT().
150 If Source and Destination overlap, then ASSERT().
152 @param Destination Pointer to a Null-terminated Unicode string.
153 @param Source Pointer to a Null-terminated Unicode string.
154 @param Length Maximum number of Unicode characters to copy.
162 OUT CHAR16
*Destination
,
163 IN CONST CHAR16
*Source
,
174 // Destination cannot be NULL if Length is not zero
176 ASSERT (Destination
!= NULL
);
179 // Destination and source cannot overlap
180 // Q: Does Source have to be NULL-terminated?
182 ASSERT ((UINTN
)(Destination
- Source
) > StrLen (Source
));
183 ASSERT ((UINTN
)(Source
- Destination
) >= Length
);
185 ReturnValue
= Destination
;
187 while ((*Source
!= L
'\0') && (Length
> 0)) {
188 *(Destination
++) = *(Source
++);
192 memset (Destination
, 0, Length
* sizeof (*Destination
));
197 Returns the size of a Null-terminated Unicode string in bytes, including the
200 This function returns the size, in bytes, of the Null-terminated Unicode
201 string specified by String.
203 If String is NULL, then ASSERT().
205 @param String Pointer to a Null-terminated Unicode string.
207 @return The size of String.
213 IN CONST CHAR16
*String
216 return (StrLen (String
) + 1) * sizeof (*String
);
220 Compares two Null-terminated Unicode strings, and returns the difference
221 between the first mismatched Unicode characters.
223 This function compares the Null-terminated Unicode string FirstString to the
224 Null-terminated Unicode string SecondString. If FirstString is identical to
225 SecondString, then 0 is returned. Otherwise, the value returned is the first
226 mismatched Unicode character in SecondString subtracted from the first
227 mismatched Unicode character in FirstString.
229 If FirstString is NULL, then ASSERT().
230 If SecondString is NULL, then ASSERT().
232 @param FirstString Pointer to a Null-terminated Unicode string.
233 @param SecondString Pointer to a Null-terminated Unicode string.
235 @retval 0 FirstString is identical to SecondString.
236 @retval !=0 FirstString is not identical to SecondString.
242 IN CONST CHAR16
*FirstString
,
243 IN CONST CHAR16
*SecondString
247 // ASSERT both strings should never be zero
249 ASSERT (StrSize (FirstString
) != 0);
250 ASSERT (StrSize (SecondString
) != 0);
252 while ((*FirstString
!= L
'\0') && (*FirstString
== *SecondString
)) {
256 return *FirstString
- *SecondString
;
260 Compares two Null-terminated Unicode strings with maximum lengths, and
261 returns the difference between the first mismatched Unicode characters.
263 This function compares the Null-terminated Unicode string FirstString to the
264 Null-terminated Unicode string SecondString. At most, Length Unicode
265 characters will be compared. If Length is 0, then 0 is returned. If
266 FirstString is identical to SecondString, then 0 is returned. Otherwise, the
267 value returned is the first mismatched Unicode character in SecondString
268 subtracted from the first mismatched Unicode character in FirstString.
270 If FirstString is NULL, then ASSERT().
271 If SecondString is NULL, then ASSERT().
273 @param FirstString Pointer to a Null-terminated Unicode string.
274 @param SecondString Pointer to a Null-terminated Unicode string.
275 @param Length Maximum number of Unicode characters to compare.
277 @retval 0 FirstString is identical to SecondString.
278 @retval !=0 FirstString is not identical to SecondString.
284 IN CONST CHAR16
*FirstString
,
285 IN CONST CHAR16
*SecondString
,
294 // ASSERT both strings should never be zero
296 ASSERT (StrSize (FirstString
) != 0);
297 ASSERT (StrSize (SecondString
) != 0);
299 while ((*FirstString
!= L
'\0') &&
300 (*FirstString
== *SecondString
) &&
307 return *FirstString
- *SecondString
;
311 Concatenates one Null-terminated Unicode string to another Null-terminated
312 Unicode string, and returns the concatenated Unicode string.
314 This function concatenates two Null-terminated Unicode strings. The contents
315 of Null-terminated Unicode string Source are concatenated to the end of
316 Null-terminated Unicode string Destination. The Null-terminated concatenated
317 Unicode String is returned. If Source and Destination overlap, then the
318 results are undefined.
320 If Destination is NULL, then ASSERT().
321 If Source is NULL, then ASSERT().
322 If Source and Destination overlap, then ASSERT().
324 @param Destination Pointer to a Null-terminated Unicode string.
325 @param Source Pointer to a Null-terminated Unicode string.
333 IN OUT CHAR16
*Destination
,
334 IN CONST CHAR16
*Source
337 StrCpy (Destination
+ StrLen (Destination
), Source
);
340 // Size of the resulting string should never be zero.
342 ASSERT (StrSize (Destination
) != 0);
347 Concatenates one Null-terminated Unicode string with a maximum length to the
348 end of another Null-terminated Unicode string, and returns the concatenated
351 This function concatenates two Null-terminated Unicode strings. The contents
352 of Null-terminated Unicode string Source are concatenated to the end of
353 Null-terminated Unicode string Destination, and Destination is returned. At
354 most, Length Unicode characters are concatenated from Source to the end of
355 Destination, and Destination is always Null-terminated. If Length is 0, then
356 Destination is returned unmodified. If Source and Destination overlap, then
357 the results are undefined.
359 If Destination is NULL, then ASSERT().
360 If Source is NULL, then ASSERT().
361 If Source and Destination overlap, then ASSERT().
363 @param Destination Pointer to a Null-terminated Unicode string.
364 @param Source Pointer to a Null-terminated Unicode string.
365 @param Length Maximum number of Unicode characters to concatenate from
374 IN OUT CHAR16
*Destination
,
375 IN CONST CHAR16
*Source
,
379 StrnCpy (Destination
+ StrLen (Destination
), Source
, Length
);
382 // Size of the resulting string should never be zero.
384 ASSERT (StrSize (Destination
) != 0);
389 Copies one Null-terminated ASCII string to another Null-terminated ASCII
390 string and returns the new ASCII string.
392 This function copies the contents of the ASCII string Source to the ASCII
393 string Destination, and returns Destination. If Source and Destination
394 overlap, then the results are undefined.
396 If Destination is NULL, then ASSERT().
397 If Source is NULL, then ASSERT().
398 If Source and Destination overlap, then ASSERT().
400 @param Destination Pointer to a Null-terminated ASCII string.
401 @param Source Pointer to a Null-terminated ASCII string.
409 OUT CHAR8
*Destination
,
410 IN CONST CHAR8
*Source
416 // Destination cannot be NULL
418 ASSERT (Destination
!= NULL
);
421 // Destination and source cannot overlap
423 ASSERT ((UINTN
)(Destination
- Source
) > AsciiStrLen (Source
));
424 ASSERT ((UINTN
)(Source
- Destination
) > AsciiStrLen (Source
));
426 ReturnValue
= Destination
;
428 *(Destination
++) = *(Source
++);
435 Copies one Null-terminated ASCII string with a maximum length to another
436 Null-terminated ASCII string with a maximum length and returns the new ASCII
439 This function copies the contents of the ASCII string Source to the ASCII
440 string Destination, and returns Destination. At most, Length ASCII characters
441 are copied from Source to Destination. If Length is 0, then Destination is
442 returned unmodified. If Length is greater that the number of ASCII characters
443 in Source, then Destination is padded with Null ASCII characters. If Source
444 and Destination overlap, then the results are undefined.
446 If Destination is NULL, then ASSERT().
447 If Source is NULL, then ASSERT().
448 If Source and Destination overlap, then ASSERT().
450 @param Destination Pointer to a Null-terminated ASCII string.
451 @param Source Pointer to a Null-terminated ASCII string.
452 @param Length Maximum number of ASCII characters to copy.
460 OUT CHAR8
*Destination
,
461 IN CONST CHAR8
*Source
,
472 // Destination cannot be NULL
474 ASSERT (Destination
!= NULL
);
477 // Destination and source cannot overlap
479 ASSERT ((UINTN
)(Destination
- Source
) > AsciiStrLen (Source
));
480 ASSERT ((UINTN
)(Source
- Destination
) >= Length
);
482 ReturnValue
= Destination
;
484 while (*Source
&& Length
> 0) {
485 *(Destination
++) = *(Source
++);
489 // ZeroMem (Destination, Length * sizeof (*Destination));
490 memset (Destination
, 0, Length
* sizeof (*Destination
));
495 Returns the size of a Null-terminated ASCII string in bytes, including the
498 This function returns the size, in bytes, of the Null-terminated ASCII string
501 If String is NULL, then ASSERT().
503 @param String Pointer to a Null-terminated ASCII string.
505 @return The size of String.
511 IN CONST CHAR8
*String
514 return (AsciiStrLen (String
) + 1) * sizeof (*String
);
518 Compares two Null-terminated ASCII strings, and returns the difference
519 between the first mismatched ASCII characters.
521 This function compares the Null-terminated ASCII string FirstString to the
522 Null-terminated ASCII string SecondString. If FirstString is identical to
523 SecondString, then 0 is returned. Otherwise, the value returned is the first
524 mismatched ASCII character in SecondString subtracted from the first
525 mismatched ASCII character in FirstString.
527 If FirstString is NULL, then ASSERT().
528 If SecondString is NULL, then ASSERT().
530 @param FirstString Pointer to a Null-terminated ASCII string.
531 @param SecondString Pointer to a Null-terminated ASCII string.
533 @retval 0 FirstString is identical to SecondString.
534 @retval !=0 FirstString is not identical to SecondString.
540 IN CONST CHAR8
*FirstString
,
541 IN CONST CHAR8
*SecondString
545 // ASSERT both strings should never be zero
547 ASSERT (AsciiStrSize (FirstString
));
548 ASSERT (AsciiStrSize (SecondString
));
550 while ((*FirstString
!= '\0') && (*FirstString
== *SecondString
)) {
555 return *FirstString
- *SecondString
;
565 return (Chr
>= 'a' && Chr
<= 'z') ? Chr
- ('a' - 'A') : Chr
;
569 Performs a case insensitive comparison of two Null-terminated ASCII strings,
570 and returns the difference between the first mismatched ASCII characters.
572 This function performs a case insensitive comparison of the Null-terminated
573 ASCII string FirstString to the Null-terminated ASCII string SecondString. If
574 FirstString is identical to SecondString, then 0 is returned. Otherwise, the
575 value returned is the first mismatched lower case ASCII character in
576 SecondString subtracted from the first mismatched lower case ASCII character
579 If FirstString is NULL, then ASSERT().
580 If SecondString is NULL, then ASSERT().
582 @param FirstString Pointer to a Null-terminated ASCII string.
583 @param SecondString Pointer to a Null-terminated ASCII string.
585 @retval 0 FirstString is identical to SecondString using case insensitive
587 @retval !=0 FirstString is not identical to SecondString using case
588 insensitive comparisons.
594 IN CONST CHAR8
*FirstString
,
595 IN CONST CHAR8
*SecondString
599 // ASSERT both strings should never be zero
601 ASSERT (AsciiStrSize (FirstString
));
602 ASSERT (AsciiStrSize (SecondString
));
604 while ((*FirstString
!= '\0') &&
605 (AsciiToUpper (*FirstString
) == AsciiToUpper (*SecondString
))) {
610 return AsciiToUpper (*FirstString
) - AsciiToUpper (*SecondString
);
614 Compares two Null-terminated ASCII strings with maximum lengths, and returns
615 the difference between the first mismatched ASCII characters.
617 This function compares the Null-terminated ASCII string FirstString to the
618 Null-terminated ASCII string SecondString. At most, Length ASCII characters
619 will be compared. If Length is 0, then 0 is returned. If FirstString is
620 identical to SecondString, then 0 is returned. Otherwise, the value returned
621 is the first mismatched ASCII character in SecondString subtracted from the
622 first mismatched ASCII character in FirstString.
624 If FirstString is NULL, then ASSERT().
625 If SecondString is NULL, then ASSERT().
627 @param FirstString Pointer to a Null-terminated ASCII string.
628 @param SecondString Pointer to a Null-terminated ASCII string.
630 @retval 0 FirstString is identical to SecondString.
631 @retval !=0 FirstString is not identical to SecondString.
637 IN CONST CHAR8
*FirstString
,
638 IN CONST CHAR8
*SecondString
,
643 // ASSERT both strings should never be zero
645 ASSERT (AsciiStrSize (FirstString
));
646 ASSERT (AsciiStrSize (SecondString
));
648 while ((*FirstString
!= '\0') &&
649 (*FirstString
== *SecondString
) &&
655 return *FirstString
- *SecondString
;
659 Concatenates one Null-terminated ASCII string to another Null-terminated
660 ASCII string, and returns the concatenated ASCII string.
662 This function concatenates two Null-terminated ASCII strings. The contents of
663 Null-terminated ASCII string Source are concatenated to the end of Null-
664 terminated ASCII string Destination. The Null-terminated concatenated ASCII
667 If Destination is NULL, then ASSERT().
668 If Source is NULL, then ASSERT().
670 @param Destination Pointer to a Null-terminated ASCII string.
671 @param Source Pointer to a Null-terminated ASCII string.
679 IN OUT CHAR8
*Destination
,
680 IN CONST CHAR8
*Source
683 AsciiStrCpy (Destination
+ AsciiStrLen (Destination
), Source
);
686 // Size of the resulting string should never be zero.
688 ASSERT (AsciiStrSize (Destination
) != 0);
693 Concatenates one Null-terminated ASCII string with a maximum length to the
694 end of another Null-terminated ASCII string, and returns the concatenated
697 This function concatenates two Null-terminated ASCII strings. The contents
698 of Null-terminated ASCII string Source are concatenated to the end of Null-
699 terminated ASCII string Destination, and Destination is returned. At most,
700 Length ASCII characters are concatenated from Source to the end of
701 Destination, and Destination is always Null-terminated. If Length is 0, then
702 Destination is returned unmodified. If Source and Destination overlap, then
703 the results are undefined.
705 If Destination is NULL, then ASSERT().
706 If Source is NULL, then ASSERT().
707 If Source and Destination overlap, then ASSERT().
709 @param Destination Pointer to a Null-terminated ASCII string.
710 @param Source Pointer to a Null-terminated ASCII string.
711 @param Length Maximum number of ASCII characters to concatenate from
720 IN OUT CHAR8
*Destination
,
721 IN CONST CHAR8
*Source
,
725 AsciiStrnCpy (Destination
+ AsciiStrLen (Destination
), Source
, Length
);
728 // Size of the resulting string should never be zero.
730 ASSERT (AsciiStrSize (Destination
) != 0);