]>
git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/TianoTools/String/String.c
cdbf29964c693a2a331b0bd5e275feafccc9163b
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>
27 #include "CommonLib.h"
30 Returns the length of a Null-terminated Unicode string.
32 This function returns the number of Unicode characters in the Null-terminated
33 Unicode string specified by String.
35 If String is NULL, then ASSERT().
37 @param String Pointer to a Null-terminated Unicode string.
39 @return The length of String.
45 IN CONST CHAR16
*String
50 ASSERT (String
!= NULL
);
52 for (Length
= 0; *String
!= L
'\0'; String
++, Length
++) {
59 Returns the length of a Null-terminated ASCII string.
61 This function returns the number of ASCII characters in the Null-terminated
62 ASCII string specified by String.
64 If String is NULL, then ASSERT().
66 @param String Pointer to a Null-terminated ASCII string.
68 @return The length of String.
74 IN CONST CHAR8
*String
79 ASSERT (String
!= NULL
);
81 for (Length
= 0; *String
!= '\0'; String
++, Length
++) {
88 Copies one Null-terminated Unicode string to another Null-terminated Unicode
89 string and returns the new Unicode string.
91 This function copies the contents of the Unicode string Source to the Unicode
92 string Destination, and returns Destination. If Source and Destination
93 overlap, then the results are undefined.
95 If Destination is NULL, then ASSERT().
96 If Source is NULL, then ASSERT().
97 If Source and Destination overlap, then ASSERT().
99 @param Destination Pointer to a Null-terminated Unicode string.
100 @param Source Pointer to a Null-terminated Unicode string.
108 OUT CHAR16
*Destination
,
109 IN CONST CHAR16
*Source
115 // Destination cannot be NULL
117 ASSERT (Destination
!= NULL
);
120 // Destination and source cannot overlap
122 ASSERT ((UINTN
)(Destination
- Source
) > StrLen (Source
));
123 ASSERT ((UINTN
)(Source
- Destination
) > StrLen (Source
));
125 ReturnValue
= Destination
;
127 *(Destination
++) = *(Source
++);
134 Copies one Null-terminated Unicode string with a maximum length to another
135 Null-terminated Unicode string with a maximum length and returns the new
138 This function copies the contents of the Unicode string Source to the Unicode
139 string Destination, and returns Destination. At most, Length Unicode
140 characters are copied from Source to Destination. If Length is 0, then
141 Destination is returned unmodified. If Length is greater that the number of
142 Unicode characters in Source, then Destination is padded with Null Unicode
143 characters. If Source and Destination overlap, then the results are
146 If Destination is NULL, then ASSERT().
147 If Source is NULL, then ASSERT().
148 If Source and Destination overlap, then ASSERT().
150 @param Destination Pointer to a Null-terminated Unicode string.
151 @param Source Pointer to a Null-terminated Unicode string.
152 @param Length Maximum number of Unicode characters to copy.
160 OUT CHAR16
*Destination
,
161 IN CONST CHAR16
*Source
,
172 // Destination cannot be NULL if Length is not zero
174 ASSERT (Destination
!= NULL
);
177 // Destination and source cannot overlap
178 // Q: Does Source have to be NULL-terminated?
180 ASSERT ((UINTN
)(Destination
- Source
) > StrLen (Source
));
181 ASSERT ((UINTN
)(Source
- Destination
) >= Length
);
183 ReturnValue
= Destination
;
185 while ((*Source
!= L
'\0') && (Length
> 0)) {
186 *(Destination
++) = *(Source
++);
190 memset (Destination
, 0, Length
* sizeof (*Destination
));
195 Returns the size of a Null-terminated Unicode string in bytes, including the
198 This function returns the size, in bytes, of the Null-terminated Unicode
199 string specified by String.
201 If String is NULL, then ASSERT().
203 @param String Pointer to a Null-terminated Unicode string.
205 @return The size of String.
211 IN CONST CHAR16
*String
214 return (StrLen (String
) + 1) * sizeof (*String
);
218 Compares two Null-terminated Unicode strings, and returns the difference
219 between the first mismatched Unicode characters.
221 This function compares the Null-terminated Unicode string FirstString to the
222 Null-terminated Unicode string SecondString. If FirstString is identical to
223 SecondString, then 0 is returned. Otherwise, the value returned is the first
224 mismatched Unicode character in SecondString subtracted from the first
225 mismatched Unicode character in FirstString.
227 If FirstString is NULL, then ASSERT().
228 If SecondString is NULL, then ASSERT().
230 @param FirstString Pointer to a Null-terminated Unicode string.
231 @param SecondString Pointer to a Null-terminated Unicode string.
233 @retval 0 FirstString is identical to SecondString.
234 @retval !=0 FirstString is not identical to SecondString.
240 IN CONST CHAR16
*FirstString
,
241 IN CONST CHAR16
*SecondString
245 // ASSERT both strings should never be zero
247 ASSERT (StrSize (FirstString
) != 0);
248 ASSERT (StrSize (SecondString
) != 0);
250 while ((*FirstString
!= L
'\0') && (*FirstString
== *SecondString
)) {
254 return *FirstString
- *SecondString
;
258 Compares two Null-terminated Unicode strings with maximum lengths, and
259 returns the difference between the first mismatched Unicode characters.
261 This function compares the Null-terminated Unicode string FirstString to the
262 Null-terminated Unicode string SecondString. At most, Length Unicode
263 characters will be compared. If Length is 0, then 0 is returned. If
264 FirstString is identical to SecondString, then 0 is returned. Otherwise, the
265 value returned is the first mismatched Unicode character in SecondString
266 subtracted from the first mismatched Unicode character in FirstString.
268 If FirstString is NULL, then ASSERT().
269 If SecondString is NULL, then ASSERT().
271 @param FirstString Pointer to a Null-terminated Unicode string.
272 @param SecondString Pointer to a Null-terminated Unicode string.
273 @param Length Maximum number of Unicode characters to compare.
275 @retval 0 FirstString is identical to SecondString.
276 @retval !=0 FirstString is not identical to SecondString.
282 IN CONST CHAR16
*FirstString
,
283 IN CONST CHAR16
*SecondString
,
292 // ASSERT both strings should never be zero
294 ASSERT (StrSize (FirstString
) != 0);
295 ASSERT (StrSize (SecondString
) != 0);
297 while ((*FirstString
!= L
'\0') &&
298 (*FirstString
== *SecondString
) &&
305 return *FirstString
- *SecondString
;
309 Concatenates one Null-terminated Unicode string to another Null-terminated
310 Unicode string, and returns the concatenated Unicode string.
312 This function concatenates two Null-terminated Unicode strings. The contents
313 of Null-terminated Unicode string Source are concatenated to the end of
314 Null-terminated Unicode string Destination. The Null-terminated concatenated
315 Unicode String is returned. If Source and Destination overlap, then the
316 results are undefined.
318 If Destination is NULL, then ASSERT().
319 If Source is NULL, then ASSERT().
320 If Source and Destination overlap, then ASSERT().
322 @param Destination Pointer to a Null-terminated Unicode string.
323 @param Source Pointer to a Null-terminated Unicode string.
331 IN OUT CHAR16
*Destination
,
332 IN CONST CHAR16
*Source
335 StrCpy (Destination
+ StrLen (Destination
), Source
);
338 // Size of the resulting string should never be zero.
340 ASSERT (StrSize (Destination
) != 0);
345 Concatenates one Null-terminated Unicode string with a maximum length to the
346 end of another Null-terminated Unicode string, and returns the concatenated
349 This function concatenates two Null-terminated Unicode strings. The contents
350 of Null-terminated Unicode string Source are concatenated to the end of
351 Null-terminated Unicode string Destination, and Destination is returned. At
352 most, Length Unicode characters are concatenated from Source to the end of
353 Destination, and Destination is always Null-terminated. If Length is 0, then
354 Destination is returned unmodified. If Source and Destination overlap, then
355 the results are undefined.
357 If Destination is NULL, then ASSERT().
358 If Source is NULL, then ASSERT().
359 If Source and Destination overlap, then ASSERT().
361 @param Destination Pointer to a Null-terminated Unicode string.
362 @param Source Pointer to a Null-terminated Unicode string.
363 @param Length Maximum number of Unicode characters to concatenate from
372 IN OUT CHAR16
*Destination
,
373 IN CONST CHAR16
*Source
,
377 StrnCpy (Destination
+ StrLen (Destination
), Source
, Length
);
380 // Size of the resulting string should never be zero.
382 ASSERT (StrSize (Destination
) != 0);
387 Copies one Null-terminated ASCII string to another Null-terminated ASCII
388 string and returns the new ASCII string.
390 This function copies the contents of the ASCII string Source to the ASCII
391 string Destination, and returns Destination. If Source and Destination
392 overlap, then the results are undefined.
394 If Destination is NULL, then ASSERT().
395 If Source is NULL, then ASSERT().
396 If Source and Destination overlap, then ASSERT().
398 @param Destination Pointer to a Null-terminated ASCII string.
399 @param Source Pointer to a Null-terminated ASCII string.
407 OUT CHAR8
*Destination
,
408 IN CONST CHAR8
*Source
414 // Destination cannot be NULL
416 ASSERT (Destination
!= NULL
);
419 // Destination and source cannot overlap
421 ASSERT ((UINTN
)(Destination
- Source
) > AsciiStrLen (Source
));
422 ASSERT ((UINTN
)(Source
- Destination
) > AsciiStrLen (Source
));
424 ReturnValue
= Destination
;
426 *(Destination
++) = *(Source
++);
433 Copies one Null-terminated ASCII string with a maximum length to another
434 Null-terminated ASCII string with a maximum length and returns the new ASCII
437 This function copies the contents of the ASCII string Source to the ASCII
438 string Destination, and returns Destination. At most, Length ASCII characters
439 are copied from Source to Destination. If Length is 0, then Destination is
440 returned unmodified. If Length is greater that the number of ASCII characters
441 in Source, then Destination is padded with Null ASCII characters. If Source
442 and Destination overlap, then the results are undefined.
444 If Destination is NULL, then ASSERT().
445 If Source is NULL, then ASSERT().
446 If Source and Destination overlap, then ASSERT().
448 @param Destination Pointer to a Null-terminated ASCII string.
449 @param Source Pointer to a Null-terminated ASCII string.
450 @param Length Maximum number of ASCII characters to copy.
458 OUT CHAR8
*Destination
,
459 IN CONST CHAR8
*Source
,
470 // Destination cannot be NULL
472 ASSERT (Destination
!= NULL
);
475 // Destination and source cannot overlap
477 ASSERT ((UINTN
)(Destination
- Source
) > AsciiStrLen (Source
));
478 ASSERT ((UINTN
)(Source
- Destination
) >= Length
);
480 ReturnValue
= Destination
;
482 while (*Source
&& Length
> 0) {
483 *(Destination
++) = *(Source
++);
487 // ZeroMem (Destination, Length * sizeof (*Destination));
488 memset (Destination
, 0, Length
* sizeof (*Destination
));
493 Returns the size of a Null-terminated ASCII string in bytes, including the
496 This function returns the size, in bytes, of the Null-terminated ASCII string
499 If String is NULL, then ASSERT().
501 @param String Pointer to a Null-terminated ASCII string.
503 @return The size of String.
509 IN CONST CHAR8
*String
512 return (AsciiStrLen (String
) + 1) * sizeof (*String
);
516 Compares two Null-terminated ASCII strings, and returns the difference
517 between the first mismatched ASCII characters.
519 This function compares the Null-terminated ASCII string FirstString to the
520 Null-terminated ASCII string SecondString. If FirstString is identical to
521 SecondString, then 0 is returned. Otherwise, the value returned is the first
522 mismatched ASCII character in SecondString subtracted from the first
523 mismatched ASCII character in FirstString.
525 If FirstString is NULL, then ASSERT().
526 If SecondString is NULL, then ASSERT().
528 @param FirstString Pointer to a Null-terminated ASCII string.
529 @param SecondString Pointer to a Null-terminated ASCII string.
531 @retval 0 FirstString is identical to SecondString.
532 @retval !=0 FirstString is not identical to SecondString.
538 IN CONST CHAR8
*FirstString
,
539 IN CONST CHAR8
*SecondString
543 // ASSERT both strings should never be zero
545 ASSERT (AsciiStrSize (FirstString
));
546 ASSERT (AsciiStrSize (SecondString
));
548 while ((*FirstString
!= '\0') && (*FirstString
== *SecondString
)) {
553 return *FirstString
- *SecondString
;
563 return (Chr
>= 'a' && Chr
<= 'z') ? Chr
- ('a' - 'A') : Chr
;
567 Performs a case insensitive comparison of two Null-terminated ASCII strings,
568 and returns the difference between the first mismatched ASCII characters.
570 This function performs a case insensitive comparison of the Null-terminated
571 ASCII string FirstString to the Null-terminated ASCII string SecondString. If
572 FirstString is identical to SecondString, then 0 is returned. Otherwise, the
573 value returned is the first mismatched lower case ASCII character in
574 SecondString subtracted from the first mismatched lower case ASCII character
577 If FirstString is NULL, then ASSERT().
578 If SecondString is NULL, then ASSERT().
580 @param FirstString Pointer to a Null-terminated ASCII string.
581 @param SecondString Pointer to a Null-terminated ASCII string.
583 @retval 0 FirstString is identical to SecondString using case insensitive
585 @retval !=0 FirstString is not identical to SecondString using case
586 insensitive comparisons.
592 IN CONST CHAR8
*FirstString
,
593 IN CONST CHAR8
*SecondString
597 // ASSERT both strings should never be zero
599 ASSERT (AsciiStrSize (FirstString
));
600 ASSERT (AsciiStrSize (SecondString
));
602 while ((*FirstString
!= '\0') &&
603 (AsciiToUpper (*FirstString
) == AsciiToUpper (*SecondString
))) {
608 return AsciiToUpper (*FirstString
) - AsciiToUpper (*SecondString
);
612 Compares two Null-terminated ASCII strings with maximum lengths, and returns
613 the difference between the first mismatched ASCII characters.
615 This function compares the Null-terminated ASCII string FirstString to the
616 Null-terminated ASCII string SecondString. At most, Length ASCII characters
617 will be compared. If Length is 0, then 0 is returned. If FirstString is
618 identical to SecondString, then 0 is returned. Otherwise, the value returned
619 is the first mismatched ASCII character in SecondString subtracted from the
620 first mismatched ASCII character in FirstString.
622 If FirstString is NULL, then ASSERT().
623 If SecondString is NULL, then ASSERT().
625 @param FirstString Pointer to a Null-terminated ASCII string.
626 @param SecondString Pointer to a Null-terminated ASCII string.
628 @retval 0 FirstString is identical to SecondString.
629 @retval !=0 FirstString is not identical to SecondString.
635 IN CONST CHAR8
*FirstString
,
636 IN CONST CHAR8
*SecondString
,
641 // ASSERT both strings should never be zero
643 ASSERT (AsciiStrSize (FirstString
));
644 ASSERT (AsciiStrSize (SecondString
));
646 while ((*FirstString
!= '\0') &&
647 (*FirstString
== *SecondString
) &&
653 return *FirstString
- *SecondString
;
657 Concatenates one Null-terminated ASCII string to another Null-terminated
658 ASCII string, and returns the concatenated ASCII string.
660 This function concatenates two Null-terminated ASCII strings. The contents of
661 Null-terminated ASCII string Source are concatenated to the end of Null-
662 terminated ASCII string Destination. The Null-terminated concatenated ASCII
665 If Destination is NULL, then ASSERT().
666 If Source is NULL, then ASSERT().
668 @param Destination Pointer to a Null-terminated ASCII string.
669 @param Source Pointer to a Null-terminated ASCII string.
677 IN OUT CHAR8
*Destination
,
678 IN CONST CHAR8
*Source
681 AsciiStrCpy (Destination
+ AsciiStrLen (Destination
), Source
);
684 // Size of the resulting string should never be zero.
686 ASSERT (AsciiStrSize (Destination
) != 0);
691 Concatenates one Null-terminated ASCII string with a maximum length to the
692 end of another Null-terminated ASCII string, and returns the concatenated
695 This function concatenates two Null-terminated ASCII strings. The contents
696 of Null-terminated ASCII string Source are concatenated to the end of Null-
697 terminated ASCII string Destination, and Destination is returned. At most,
698 Length ASCII characters are concatenated from Source to the end of
699 Destination, and Destination is always Null-terminated. If Length is 0, then
700 Destination is returned unmodified. If Source and Destination overlap, then
701 the results are undefined.
703 If Destination is NULL, then ASSERT().
704 If Source is NULL, then ASSERT().
705 If Source and Destination overlap, then ASSERT().
707 @param Destination Pointer to a Null-terminated ASCII string.
708 @param Source Pointer to a Null-terminated ASCII string.
709 @param Length Maximum number of ASCII characters to concatenate from
718 IN OUT CHAR8
*Destination
,
719 IN CONST CHAR8
*Source
,
723 AsciiStrnCpy (Destination
+ AsciiStrLen (Destination
), Source
, Length
);
726 // Size of the resulting string should never be zero.
728 ASSERT (AsciiStrSize (Destination
) != 0);