]>
git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/TianoTools/String/String.c
9e02231f364f420efd99db96b3d25da43c06ec69
2 Unicode string primatives.
4 Copyright (c) 2004-2006 Intel Corporation. All rights reserved
5 This software and associated documentation (if any) is furnished
6 under a license and may only be used or copied in accordance
7 with the terms of the license. Except as permitted by such
8 license, no part of this software or documentation may be
9 reproduced, stored in a retrieval system, or transmitted in any
10 form or by any means without the express written consent of
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
22 #include <Common/UefiBaseTypes.h>
24 #include "CommonLib.h"
27 Returns the length of a Null-terminated Unicode string.
29 This function returns the number of Unicode characters in the Null-terminated
30 Unicode string specified by String.
32 If String is NULL, then ASSERT().
34 @param String Pointer to a Null-terminated Unicode string.
36 @return The length of String.
42 IN CONST CHAR16
*String
47 ASSERT (String
!= NULL
);
49 for (Length
= 0; *String
!= L
'\0'; String
++, Length
++) {
56 Returns the length of a Null-terminated ASCII string.
58 This function returns the number of ASCII characters in the Null-terminated
59 ASCII string specified by String.
61 If String is NULL, then ASSERT().
63 @param String Pointer to a Null-terminated ASCII string.
65 @return The length of String.
71 IN CONST CHAR8
*String
76 ASSERT (String
!= NULL
);
78 for (Length
= 0; *String
!= '\0'; String
++, Length
++) {
85 Copies one Null-terminated Unicode string to another Null-terminated Unicode
86 string and returns the new Unicode string.
88 This function copies the contents of the Unicode string Source to the Unicode
89 string Destination, and returns Destination. If Source and Destination
90 overlap, then the results are undefined.
92 If Destination is NULL, then ASSERT().
93 If Source is NULL, then ASSERT().
94 If Source and Destination overlap, then ASSERT().
96 @param Destination Pointer to a Null-terminated Unicode string.
97 @param Source Pointer to a Null-terminated Unicode string.
105 OUT CHAR16
*Destination
,
106 IN CONST CHAR16
*Source
112 // Destination cannot be NULL
114 ASSERT (Destination
!= NULL
);
117 // Destination and source cannot overlap
119 ASSERT ((UINTN
)(Destination
- Source
) > StrLen (Source
));
120 ASSERT ((UINTN
)(Source
- Destination
) > StrLen (Source
));
122 ReturnValue
= Destination
;
124 *(Destination
++) = *(Source
++);
131 Copies one Null-terminated Unicode string with a maximum length to another
132 Null-terminated Unicode string with a maximum length and returns the new
135 This function copies the contents of the Unicode string Source to the Unicode
136 string Destination, and returns Destination. At most, Length Unicode
137 characters are copied from Source to Destination. If Length is 0, then
138 Destination is returned unmodified. If Length is greater that the number of
139 Unicode characters in Source, then Destination is padded with Null Unicode
140 characters. If Source and Destination overlap, then the results are
143 If Destination is NULL, then ASSERT().
144 If Source is NULL, then ASSERT().
145 If Source and Destination overlap, then ASSERT().
147 @param Destination Pointer to a Null-terminated Unicode string.
148 @param Source Pointer to a Null-terminated Unicode string.
149 @param Length Maximum number of Unicode characters to copy.
157 OUT CHAR16
*Destination
,
158 IN CONST CHAR16
*Source
,
169 // Destination cannot be NULL if Length is not zero
171 ASSERT (Destination
!= NULL
);
174 // Destination and source cannot overlap
175 // Q: Does Source have to be NULL-terminated?
177 ASSERT ((UINTN
)(Destination
- Source
) > StrLen (Source
));
178 ASSERT ((UINTN
)(Source
- Destination
) >= Length
);
180 ReturnValue
= Destination
;
182 while ((*Source
!= L
'\0') && (Length
> 0)) {
183 *(Destination
++) = *(Source
++);
187 memset (Destination
, 0, Length
* sizeof (*Destination
));
192 Returns the size of a Null-terminated Unicode string in bytes, including the
195 This function returns the size, in bytes, of the Null-terminated Unicode
196 string specified by String.
198 If String is NULL, then ASSERT().
200 @param String Pointer to a Null-terminated Unicode string.
202 @return The size of String.
208 IN CONST CHAR16
*String
211 return (StrLen (String
) + 1) * sizeof (*String
);
215 Compares two Null-terminated Unicode strings, and returns the difference
216 between the first mismatched Unicode characters.
218 This function compares the Null-terminated Unicode string FirstString to the
219 Null-terminated Unicode string SecondString. If FirstString is identical to
220 SecondString, then 0 is returned. Otherwise, the value returned is the first
221 mismatched Unicode character in SecondString subtracted from the first
222 mismatched Unicode character in FirstString.
224 If FirstString is NULL, then ASSERT().
225 If SecondString is NULL, then ASSERT().
227 @param FirstString Pointer to a Null-terminated Unicode string.
228 @param SecondString Pointer to a Null-terminated Unicode string.
230 @retval 0 FirstString is identical to SecondString.
231 @retval !=0 FirstString is not identical to SecondString.
237 IN CONST CHAR16
*FirstString
,
238 IN CONST CHAR16
*SecondString
242 // ASSERT both strings should never be zero
244 ASSERT (StrSize (FirstString
) != 0);
245 ASSERT (StrSize (SecondString
) != 0);
247 while ((*FirstString
!= L
'\0') && (*FirstString
== *SecondString
)) {
251 return *FirstString
- *SecondString
;
255 Compares two Null-terminated Unicode strings with maximum lengths, and
256 returns the difference between the first mismatched Unicode characters.
258 This function compares the Null-terminated Unicode string FirstString to the
259 Null-terminated Unicode string SecondString. At most, Length Unicode
260 characters will be compared. If Length is 0, then 0 is returned. If
261 FirstString is identical to SecondString, then 0 is returned. Otherwise, the
262 value returned is the first mismatched Unicode character in SecondString
263 subtracted from the first mismatched Unicode character in FirstString.
265 If FirstString is NULL, then ASSERT().
266 If SecondString is NULL, then ASSERT().
268 @param FirstString Pointer to a Null-terminated Unicode string.
269 @param SecondString Pointer to a Null-terminated Unicode string.
270 @param Length Maximum number of Unicode characters to compare.
272 @retval 0 FirstString is identical to SecondString.
273 @retval !=0 FirstString is not identical to SecondString.
279 IN CONST CHAR16
*FirstString
,
280 IN CONST CHAR16
*SecondString
,
289 // ASSERT both strings should never be zero
291 ASSERT (StrSize (FirstString
) != 0);
292 ASSERT (StrSize (SecondString
) != 0);
294 while ((*FirstString
!= L
'\0') &&
295 (*FirstString
== *SecondString
) &&
302 return *FirstString
- *SecondString
;
306 Concatenates one Null-terminated Unicode string to another Null-terminated
307 Unicode string, and returns the concatenated Unicode string.
309 This function concatenates two Null-terminated Unicode strings. The contents
310 of Null-terminated Unicode string Source are concatenated to the end of
311 Null-terminated Unicode string Destination. The Null-terminated concatenated
312 Unicode String is returned. If Source and Destination overlap, then the
313 results are undefined.
315 If Destination is NULL, then ASSERT().
316 If Source is NULL, then ASSERT().
317 If Source and Destination overlap, then ASSERT().
319 @param Destination Pointer to a Null-terminated Unicode string.
320 @param Source Pointer to a Null-terminated Unicode string.
328 IN OUT CHAR16
*Destination
,
329 IN CONST CHAR16
*Source
332 StrCpy (Destination
+ StrLen (Destination
), Source
);
335 // Size of the resulting string should never be zero.
337 ASSERT (StrSize (Destination
) != 0);
342 Concatenates one Null-terminated Unicode string with a maximum length to the
343 end of another Null-terminated Unicode string, and returns the concatenated
346 This function concatenates two Null-terminated Unicode strings. The contents
347 of Null-terminated Unicode string Source are concatenated to the end of
348 Null-terminated Unicode string Destination, and Destination is returned. At
349 most, Length Unicode characters are concatenated from Source to the end of
350 Destination, and Destination is always Null-terminated. If Length is 0, then
351 Destination is returned unmodified. If Source and Destination overlap, then
352 the results are undefined.
354 If Destination is NULL, then ASSERT().
355 If Source is NULL, then ASSERT().
356 If Source and Destination overlap, then ASSERT().
358 @param Destination Pointer to a Null-terminated Unicode string.
359 @param Source Pointer to a Null-terminated Unicode string.
360 @param Length Maximum number of Unicode characters to concatenate from
369 IN OUT CHAR16
*Destination
,
370 IN CONST CHAR16
*Source
,
374 StrnCpy (Destination
+ StrLen (Destination
), Source
, Length
);
377 // Size of the resulting string should never be zero.
379 ASSERT (StrSize (Destination
) != 0);
384 Copies one Null-terminated ASCII string to another Null-terminated ASCII
385 string and returns the new ASCII string.
387 This function copies the contents of the ASCII string Source to the ASCII
388 string Destination, and returns Destination. If Source and Destination
389 overlap, then the results are undefined.
391 If Destination is NULL, then ASSERT().
392 If Source is NULL, then ASSERT().
393 If Source and Destination overlap, then ASSERT().
395 @param Destination Pointer to a Null-terminated ASCII string.
396 @param Source Pointer to a Null-terminated ASCII string.
404 OUT CHAR8
*Destination
,
405 IN CONST CHAR8
*Source
411 // Destination cannot be NULL
413 ASSERT (Destination
!= NULL
);
416 // Destination and source cannot overlap
418 ASSERT ((UINTN
)(Destination
- Source
) > AsciiStrLen (Source
));
419 ASSERT ((UINTN
)(Source
- Destination
) > AsciiStrLen (Source
));
421 ReturnValue
= Destination
;
423 *(Destination
++) = *(Source
++);
430 Copies one Null-terminated ASCII string with a maximum length to another
431 Null-terminated ASCII string with a maximum length and returns the new ASCII
434 This function copies the contents of the ASCII string Source to the ASCII
435 string Destination, and returns Destination. At most, Length ASCII characters
436 are copied from Source to Destination. If Length is 0, then Destination is
437 returned unmodified. If Length is greater that the number of ASCII characters
438 in Source, then Destination is padded with Null ASCII characters. If Source
439 and Destination overlap, then the results are undefined.
441 If Destination is NULL, then ASSERT().
442 If Source is NULL, then ASSERT().
443 If Source and Destination overlap, then ASSERT().
445 @param Destination Pointer to a Null-terminated ASCII string.
446 @param Source Pointer to a Null-terminated ASCII string.
447 @param Length Maximum number of ASCII characters to copy.
455 OUT CHAR8
*Destination
,
456 IN CONST CHAR8
*Source
,
467 // Destination cannot be NULL
469 ASSERT (Destination
!= NULL
);
472 // Destination and source cannot overlap
474 ASSERT ((UINTN
)(Destination
- Source
) > AsciiStrLen (Source
));
475 ASSERT ((UINTN
)(Source
- Destination
) >= Length
);
477 ReturnValue
= Destination
;
479 while (*Source
&& Length
> 0) {
480 *(Destination
++) = *(Source
++);
484 // ZeroMem (Destination, Length * sizeof (*Destination));
485 memset (Destination
, 0, Length
* sizeof (*Destination
));
490 Returns the size of a Null-terminated ASCII string in bytes, including the
493 This function returns the size, in bytes, of the Null-terminated ASCII string
496 If String is NULL, then ASSERT().
498 @param String Pointer to a Null-terminated ASCII string.
500 @return The size of String.
506 IN CONST CHAR8
*String
509 return (AsciiStrLen (String
) + 1) * sizeof (*String
);
513 Compares two Null-terminated ASCII strings, and returns the difference
514 between the first mismatched ASCII characters.
516 This function compares the Null-terminated ASCII string FirstString to the
517 Null-terminated ASCII string SecondString. If FirstString is identical to
518 SecondString, then 0 is returned. Otherwise, the value returned is the first
519 mismatched ASCII character in SecondString subtracted from the first
520 mismatched ASCII character in FirstString.
522 If FirstString is NULL, then ASSERT().
523 If SecondString is NULL, then ASSERT().
525 @param FirstString Pointer to a Null-terminated ASCII string.
526 @param SecondString Pointer to a Null-terminated ASCII string.
528 @retval 0 FirstString is identical to SecondString.
529 @retval !=0 FirstString is not identical to SecondString.
535 IN CONST CHAR8
*FirstString
,
536 IN CONST CHAR8
*SecondString
540 // ASSERT both strings should never be zero
542 ASSERT (AsciiStrSize (FirstString
));
543 ASSERT (AsciiStrSize (SecondString
));
545 while ((*FirstString
!= '\0') && (*FirstString
== *SecondString
)) {
550 return *FirstString
- *SecondString
;
560 return (Chr
>= 'a' && Chr
<= 'z') ? Chr
- ('a' - 'A') : Chr
;
564 Performs a case insensitive comparison of two Null-terminated ASCII strings,
565 and returns the difference between the first mismatched ASCII characters.
567 This function performs a case insensitive comparison of the Null-terminated
568 ASCII string FirstString to the Null-terminated ASCII string SecondString. If
569 FirstString is identical to SecondString, then 0 is returned. Otherwise, the
570 value returned is the first mismatched lower case ASCII character in
571 SecondString subtracted from the first mismatched lower case ASCII character
574 If FirstString is NULL, then ASSERT().
575 If SecondString is NULL, then ASSERT().
577 @param FirstString Pointer to a Null-terminated ASCII string.
578 @param SecondString Pointer to a Null-terminated ASCII string.
580 @retval 0 FirstString is identical to SecondString using case insensitive
582 @retval !=0 FirstString is not identical to SecondString using case
583 insensitive comparisons.
589 IN CONST CHAR8
*FirstString
,
590 IN CONST CHAR8
*SecondString
594 // ASSERT both strings should never be zero
596 ASSERT (AsciiStrSize (FirstString
));
597 ASSERT (AsciiStrSize (SecondString
));
599 while ((*FirstString
!= '\0') &&
600 (AsciiToUpper (*FirstString
) == AsciiToUpper (*SecondString
))) {
605 return AsciiToUpper (*FirstString
) - AsciiToUpper (*SecondString
);
609 Compares two Null-terminated ASCII strings with maximum lengths, and returns
610 the difference between the first mismatched ASCII characters.
612 This function compares the Null-terminated ASCII string FirstString to the
613 Null-terminated ASCII string SecondString. At most, Length ASCII characters
614 will be compared. If Length is 0, then 0 is returned. If FirstString is
615 identical to SecondString, then 0 is returned. Otherwise, the value returned
616 is the first mismatched ASCII character in SecondString subtracted from the
617 first mismatched ASCII character in FirstString.
619 If FirstString is NULL, then ASSERT().
620 If SecondString is NULL, then ASSERT().
622 @param FirstString Pointer to a Null-terminated ASCII string.
623 @param SecondString Pointer to a Null-terminated ASCII string.
625 @retval 0 FirstString is identical to SecondString.
626 @retval !=0 FirstString is not identical to SecondString.
632 IN CONST CHAR8
*FirstString
,
633 IN CONST CHAR8
*SecondString
,
638 // ASSERT both strings should never be zero
640 ASSERT (AsciiStrSize (FirstString
));
641 ASSERT (AsciiStrSize (SecondString
));
643 while ((*FirstString
!= '\0') &&
644 (*FirstString
== *SecondString
) &&
650 return *FirstString
- *SecondString
;
654 Concatenates one Null-terminated ASCII string to another Null-terminated
655 ASCII string, and returns the concatenated ASCII string.
657 This function concatenates two Null-terminated ASCII strings. The contents of
658 Null-terminated ASCII string Source are concatenated to the end of Null-
659 terminated ASCII string Destination. The Null-terminated concatenated ASCII
662 If Destination is NULL, then ASSERT().
663 If Source is NULL, then ASSERT().
665 @param Destination Pointer to a Null-terminated ASCII string.
666 @param Source Pointer to a Null-terminated ASCII string.
674 IN OUT CHAR8
*Destination
,
675 IN CONST CHAR8
*Source
678 AsciiStrCpy (Destination
+ AsciiStrLen (Destination
), Source
);
681 // Size of the resulting string should never be zero.
683 ASSERT (AsciiStrSize (Destination
) != 0);
688 Concatenates one Null-terminated ASCII string with a maximum length to the
689 end of another Null-terminated ASCII string, and returns the concatenated
692 This function concatenates two Null-terminated ASCII strings. The contents
693 of Null-terminated ASCII string Source are concatenated to the end of Null-
694 terminated ASCII string Destination, and Destination is returned. At most,
695 Length ASCII characters are concatenated from Source to the end of
696 Destination, and Destination is always Null-terminated. If Length is 0, then
697 Destination is returned unmodified. If Source and Destination overlap, then
698 the results are undefined.
700 If Destination is NULL, then ASSERT().
701 If Source is NULL, then ASSERT().
702 If Source and Destination overlap, then ASSERT().
704 @param Destination Pointer to a Null-terminated ASCII string.
705 @param Source Pointer to a Null-terminated ASCII string.
706 @param Length Maximum number of ASCII characters to concatenate from
715 IN OUT CHAR8
*Destination
,
716 IN CONST CHAR8
*Source
,
720 AsciiStrnCpy (Destination
+ AsciiStrLen (Destination
), Source
, Length
);
723 // Size of the resulting string should never be zero.
725 ASSERT (AsciiStrSize (Destination
) != 0);