]>
git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/TianoTools/String/String.c
2 Unicode string primatives.
4 Copyright (c) 2006, Intel Corporation<BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 #include <Common/UefiBaseTypes.h>
21 #include "CommonLib.h"
24 Copies one Null-terminated Unicode string to another Null-terminated Unicode
25 string and returns the new Unicode string.
27 This function copies the contents of the Unicode string Source to the Unicode
28 string Destination, and returns Destination. If Source and Destination
29 overlap, then the results are undefined.
31 If Destination is NULL, then ASSERT().
32 If Source is NULL, then ASSERT().
33 If Source and Destination overlap, then ASSERT().
35 @param Destination Pointer to a Null-terminated Unicode string.
36 @param Source Pointer to a Null-terminated Unicode string.
44 OUT CHAR16
*Destination
,
45 IN CONST CHAR16
*Source
51 // Destination cannot be NULL
53 ASSERT (Destination
!= NULL
);
56 // Destination and source cannot overlap
58 ASSERT ((UINTN
)(Destination
- Source
) > StrLen (Source
));
59 ASSERT ((UINTN
)(Source
- Destination
) > StrLen (Source
));
61 ReturnValue
= Destination
;
63 *(Destination
++) = *(Source
++);
70 Copies one Null-terminated Unicode string with a maximum length to another
71 Null-terminated Unicode string with a maximum length and returns the new
74 This function copies the contents of the Unicode string Source to the Unicode
75 string Destination, and returns Destination. At most, Length Unicode
76 characters are copied from Source to Destination. If Length is 0, then
77 Destination is returned unmodified. If Length is greater that the number of
78 Unicode characters in Source, then Destination is padded with Null Unicode
79 characters. If Source and Destination overlap, then the results are
82 If Destination is NULL, then ASSERT().
83 If Source is NULL, then ASSERT().
84 If Source and Destination overlap, then ASSERT().
86 @param Destination Pointer to a Null-terminated Unicode string.
87 @param Source Pointer to a Null-terminated Unicode string.
88 @param Length Maximum number of Unicode characters to copy.
96 OUT CHAR16
*Destination
,
97 IN CONST CHAR16
*Source
,
108 // Destination cannot be NULL if Length is not zero
110 ASSERT (Destination
!= NULL
);
113 // Destination and source cannot overlap
114 // Q: Does Source have to be NULL-terminated?
116 ASSERT ((UINTN
)(Destination
- Source
) > StrLen (Source
));
117 ASSERT ((UINTN
)(Source
- Destination
) >= Length
);
119 ReturnValue
= Destination
;
121 while ((*Source
!= L
'\0') && (Length
> 0)) {
122 *(Destination
++) = *(Source
++);
126 memset (Destination
, 0, Length
* sizeof (*Destination
));
131 Returns the length of a Null-terminated Unicode string.
133 This function returns the number of Unicode characters in the Null-terminated
134 Unicode string specified by String.
136 If String is NULL, then ASSERT().
138 @param String Pointer to a Null-terminated Unicode string.
140 @return The length of String.
146 IN CONST CHAR16
*String
151 ASSERT (String
!= NULL
);
153 for (Length
= 0; *String
!= L
'\0'; String
++, Length
++) {
160 Returns the size of a Null-terminated Unicode string in bytes, including the
163 This function returns the size, in bytes, of the Null-terminated Unicode
164 string specified by String.
166 If String is NULL, then ASSERT().
168 @param String Pointer to a Null-terminated Unicode string.
170 @return The size of String.
176 IN CONST CHAR16
*String
179 return (StrLen (String
) + 1) * sizeof (*String
);
183 Compares two Null-terminated Unicode strings, and returns the difference
184 between the first mismatched Unicode characters.
186 This function compares the Null-terminated Unicode string FirstString to the
187 Null-terminated Unicode string SecondString. If FirstString is identical to
188 SecondString, then 0 is returned. Otherwise, the value returned is the first
189 mismatched Unicode character in SecondString subtracted from the first
190 mismatched Unicode character in FirstString.
192 If FirstString is NULL, then ASSERT().
193 If SecondString is NULL, then ASSERT().
195 @param FirstString Pointer to a Null-terminated Unicode string.
196 @param SecondString Pointer to a Null-terminated Unicode string.
198 @retval 0 FirstString is identical to SecondString.
199 @retval !=0 FirstString is not identical to SecondString.
205 IN CONST CHAR16
*FirstString
,
206 IN CONST CHAR16
*SecondString
210 // ASSERT both strings should never be zero
212 ASSERT (StrSize (FirstString
) != 0);
213 ASSERT (StrSize (SecondString
) != 0);
215 while ((*FirstString
!= L
'\0') && (*FirstString
== *SecondString
)) {
219 return *FirstString
- *SecondString
;
223 Compares two Null-terminated Unicode strings with maximum lengths, and
224 returns the difference between the first mismatched Unicode characters.
226 This function compares the Null-terminated Unicode string FirstString to the
227 Null-terminated Unicode string SecondString. At most, Length Unicode
228 characters will be compared. If Length is 0, then 0 is returned. If
229 FirstString is identical to SecondString, then 0 is returned. Otherwise, the
230 value returned is the first mismatched Unicode character in SecondString
231 subtracted from the first mismatched Unicode character in FirstString.
233 If FirstString is NULL, then ASSERT().
234 If SecondString is NULL, then ASSERT().
236 @param FirstString Pointer to a Null-terminated Unicode string.
237 @param SecondString Pointer to a Null-terminated Unicode string.
238 @param Length Maximum number of Unicode characters to compare.
240 @retval 0 FirstString is identical to SecondString.
241 @retval !=0 FirstString is not identical to SecondString.
247 IN CONST CHAR16
*FirstString
,
248 IN CONST CHAR16
*SecondString
,
257 // ASSERT both strings should never be zero
259 ASSERT (StrSize (FirstString
) != 0);
260 ASSERT (StrSize (SecondString
) != 0);
262 while ((*FirstString
!= L
'\0') &&
263 (*FirstString
== *SecondString
) &&
270 return *FirstString
- *SecondString
;
274 Concatenates one Null-terminated Unicode string to another Null-terminated
275 Unicode string, and returns the concatenated Unicode string.
277 This function concatenates two Null-terminated Unicode strings. The contents
278 of Null-terminated Unicode string Source are concatenated to the end of
279 Null-terminated Unicode string Destination. The Null-terminated concatenated
280 Unicode String is returned. If Source and Destination overlap, then the
281 results are undefined.
283 If Destination is NULL, then ASSERT().
284 If Source is NULL, then ASSERT().
285 If Source and Destination overlap, then ASSERT().
287 @param Destination Pointer to a Null-terminated Unicode string.
288 @param Source Pointer to a Null-terminated Unicode string.
296 IN OUT CHAR16
*Destination
,
297 IN CONST CHAR16
*Source
300 StrCpy (Destination
+ StrLen (Destination
), Source
);
303 // Size of the resulting string should never be zero.
305 ASSERT (StrSize (Destination
) != 0);
310 Concatenates one Null-terminated Unicode string with a maximum length to the
311 end of another Null-terminated Unicode string, and returns the concatenated
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, and Destination is returned. At
317 most, Length Unicode characters are concatenated from Source to the end of
318 Destination, and Destination is always Null-terminated. If Length is 0, then
319 Destination is returned unmodified. If Source and Destination overlap, then
320 the results are undefined.
322 If Destination is NULL, then ASSERT().
323 If Source is NULL, then ASSERT().
324 If Source and Destination overlap, then ASSERT().
326 @param Destination Pointer to a Null-terminated Unicode string.
327 @param Source Pointer to a Null-terminated Unicode string.
328 @param Length Maximum number of Unicode characters to concatenate from
337 IN OUT CHAR16
*Destination
,
338 IN CONST CHAR16
*Source
,
342 StrnCpy (Destination
+ StrLen (Destination
), Source
, Length
);
345 // Size of the resulting string should never be zero.
347 ASSERT (StrSize (Destination
) != 0);
352 Copies one Null-terminated ASCII string to another Null-terminated ASCII
353 string and returns the new ASCII string.
355 This function copies the contents of the ASCII string Source to the ASCII
356 string Destination, and returns Destination. If Source and Destination
357 overlap, then 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 ASCII string.
364 @param Source Pointer to a Null-terminated ASCII string.
372 OUT CHAR8
*Destination
,
373 IN CONST CHAR8
*Source
379 // Destination cannot be NULL
381 ASSERT (Destination
!= NULL
);
384 // Destination and source cannot overlap
386 ASSERT ((UINTN
)(Destination
- Source
) > AsciiStrLen (Source
));
387 ASSERT ((UINTN
)(Source
- Destination
) > AsciiStrLen (Source
));
389 ReturnValue
= Destination
;
391 *(Destination
++) = *(Source
++);
398 Copies one Null-terminated ASCII string with a maximum length to another
399 Null-terminated ASCII string with a maximum length and returns the new ASCII
402 This function copies the contents of the ASCII string Source to the ASCII
403 string Destination, and returns Destination. At most, Length ASCII characters
404 are copied from Source to Destination. If Length is 0, then Destination is
405 returned unmodified. If Length is greater that the number of ASCII characters
406 in Source, then Destination is padded with Null ASCII characters. If Source
407 and Destination overlap, then the results are undefined.
409 If Destination is NULL, then ASSERT().
410 If Source is NULL, then ASSERT().
411 If Source and Destination overlap, then ASSERT().
413 @param Destination Pointer to a Null-terminated ASCII string.
414 @param Source Pointer to a Null-terminated ASCII string.
415 @param Length Maximum number of ASCII characters to copy.
423 OUT CHAR8
*Destination
,
424 IN CONST CHAR8
*Source
,
435 // Destination cannot be NULL
437 ASSERT (Destination
!= NULL
);
440 // Destination and source cannot overlap
442 ASSERT ((UINTN
)(Destination
- Source
) > AsciiStrLen (Source
));
443 ASSERT ((UINTN
)(Source
- Destination
) >= Length
);
445 ReturnValue
= Destination
;
447 while (*Source
&& Length
> 0) {
448 *(Destination
++) = *(Source
++);
452 // ZeroMem (Destination, Length * sizeof (*Destination));
453 memset (Destination
, 0, Length
* sizeof (*Destination
));
458 Returns the length of a Null-terminated ASCII string.
460 This function returns the number of ASCII characters in the Null-terminated
461 ASCII string specified by String.
463 If String is NULL, then ASSERT().
465 @param String Pointer to a Null-terminated ASCII string.
467 @return The length of String.
473 IN CONST CHAR8
*String
478 ASSERT (String
!= NULL
);
480 for (Length
= 0; *String
!= '\0'; String
++, Length
++) {
487 Returns the size of a Null-terminated ASCII string in bytes, including the
490 This function returns the size, in bytes, of the Null-terminated ASCII string
493 If String is NULL, then ASSERT().
495 @param String Pointer to a Null-terminated ASCII string.
497 @return The size of String.
503 IN CONST CHAR8
*String
506 return (AsciiStrLen (String
) + 1) * sizeof (*String
);
510 Compares two Null-terminated ASCII strings, and returns the difference
511 between the first mismatched ASCII characters.
513 This function compares the Null-terminated ASCII string FirstString to the
514 Null-terminated ASCII string SecondString. If FirstString is identical to
515 SecondString, then 0 is returned. Otherwise, the value returned is the first
516 mismatched ASCII character in SecondString subtracted from the first
517 mismatched ASCII character in FirstString.
519 If FirstString is NULL, then ASSERT().
520 If SecondString is NULL, then ASSERT().
522 @param FirstString Pointer to a Null-terminated ASCII string.
523 @param SecondString Pointer to a Null-terminated ASCII string.
525 @retval 0 FirstString is identical to SecondString.
526 @retval !=0 FirstString is not identical to SecondString.
532 IN CONST CHAR8
*FirstString
,
533 IN CONST CHAR8
*SecondString
537 // ASSERT both strings should never be zero
539 ASSERT (AsciiStrSize (FirstString
));
540 ASSERT (AsciiStrSize (SecondString
));
542 while ((*FirstString
!= '\0') && (*FirstString
== *SecondString
)) {
547 return *FirstString
- *SecondString
;
557 return (Chr
>= 'a' && Chr
<= 'z') ? Chr
- ('a' - 'A') : Chr
;
561 Performs a case insensitive comparison of two Null-terminated ASCII strings,
562 and returns the difference between the first mismatched ASCII characters.
564 This function performs a case insensitive comparison of the Null-terminated
565 ASCII string FirstString to the Null-terminated ASCII string SecondString. If
566 FirstString is identical to SecondString, then 0 is returned. Otherwise, the
567 value returned is the first mismatched lower case ASCII character in
568 SecondString subtracted from the first mismatched lower case ASCII character
571 If FirstString is NULL, then ASSERT().
572 If SecondString is NULL, then ASSERT().
574 @param FirstString Pointer to a Null-terminated ASCII string.
575 @param SecondString Pointer to a Null-terminated ASCII string.
577 @retval 0 FirstString is identical to SecondString using case insensitive
579 @retval !=0 FirstString is not identical to SecondString using case
580 insensitive comparisons.
586 IN CONST CHAR8
*FirstString
,
587 IN CONST CHAR8
*SecondString
591 // ASSERT both strings should never be zero
593 ASSERT (AsciiStrSize (FirstString
));
594 ASSERT (AsciiStrSize (SecondString
));
596 while ((*FirstString
!= '\0') &&
597 (AsciiToUpper (*FirstString
) == AsciiToUpper (*SecondString
))) {
602 return AsciiToUpper (*FirstString
) - AsciiToUpper (*SecondString
);
606 Compares two Null-terminated ASCII strings with maximum lengths, and returns
607 the difference between the first mismatched ASCII characters.
609 This function compares the Null-terminated ASCII string FirstString to the
610 Null-terminated ASCII string SecondString. At most, Length ASCII characters
611 will be compared. If Length is 0, then 0 is returned. If FirstString is
612 identical to SecondString, then 0 is returned. Otherwise, the value returned
613 is the first mismatched ASCII character in SecondString subtracted from the
614 first mismatched ASCII character in FirstString.
616 If FirstString is NULL, then ASSERT().
617 If SecondString is NULL, then ASSERT().
619 @param FirstString Pointer to a Null-terminated ASCII string.
620 @param SecondString Pointer to a Null-terminated ASCII string.
622 @retval 0 FirstString is identical to SecondString.
623 @retval !=0 FirstString is not identical to SecondString.
629 IN CONST CHAR8
*FirstString
,
630 IN CONST CHAR8
*SecondString
,
635 // ASSERT both strings should never be zero
637 ASSERT (AsciiStrSize (FirstString
));
638 ASSERT (AsciiStrSize (SecondString
));
640 while ((*FirstString
!= '\0') &&
641 (*FirstString
== *SecondString
) &&
647 return *FirstString
- *SecondString
;
651 Concatenates one Null-terminated ASCII string to another Null-terminated
652 ASCII string, and returns the concatenated ASCII string.
654 This function concatenates two Null-terminated ASCII strings. The contents of
655 Null-terminated ASCII string Source are concatenated to the end of Null-
656 terminated ASCII string Destination. The Null-terminated concatenated ASCII
659 If Destination is NULL, then ASSERT().
660 If Source is NULL, then ASSERT().
662 @param Destination Pointer to a Null-terminated ASCII string.
663 @param Source Pointer to a Null-terminated ASCII string.
671 IN OUT CHAR8
*Destination
,
672 IN CONST CHAR8
*Source
675 AsciiStrCpy (Destination
+ AsciiStrLen (Destination
), Source
);
678 // Size of the resulting string should never be zero.
680 ASSERT (AsciiStrSize (Destination
) != 0);
685 Concatenates one Null-terminated ASCII string with a maximum length to the
686 end of another Null-terminated ASCII string, and returns the concatenated
689 This function concatenates two Null-terminated ASCII strings. The contents
690 of Null-terminated ASCII string Source are concatenated to the end of Null-
691 terminated ASCII string Destination, and Destination is returned. At most,
692 Length ASCII characters are concatenated from Source to the end of
693 Destination, and Destination is always Null-terminated. If Length is 0, then
694 Destination is returned unmodified. If Source and Destination overlap, then
695 the results are undefined.
697 If Destination is NULL, then ASSERT().
698 If Source is NULL, then ASSERT().
699 If Source and Destination overlap, then ASSERT().
701 @param Destination Pointer to a Null-terminated ASCII string.
702 @param Source Pointer to a Null-terminated ASCII string.
703 @param Length Maximum number of ASCII characters to concatenate from
712 IN OUT CHAR8
*Destination
,
713 IN CONST CHAR8
*Source
,
717 AsciiStrnCpy (Destination
+ AsciiStrLen (Destination
), Source
, Length
);
720 // Size of the resulting string should never be zero.
722 ASSERT (AsciiStrSize (Destination
) != 0);