]>
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 Returns the length of a Null-terminated Unicode string.
26 This function returns the number of Unicode characters in the Null-terminated
27 Unicode string specified by String.
29 If String is NULL, then ASSERT().
31 @param String Pointer to a Null-terminated Unicode string.
33 @return The length of String.
39 IN CONST CHAR16
*String
44 ASSERT (String
!= NULL
);
46 for (Length
= 0; *String
!= L
'\0'; String
++, Length
++) {
53 Returns the length of a Null-terminated ASCII string.
55 This function returns the number of ASCII characters in the Null-terminated
56 ASCII string specified by String.
58 If String is NULL, then ASSERT().
60 @param String Pointer to a Null-terminated ASCII string.
62 @return The length of String.
68 IN CONST CHAR8
*String
73 ASSERT (String
!= NULL
);
75 for (Length
= 0; *String
!= '\0'; String
++, Length
++) {
82 Copies one Null-terminated Unicode string to another Null-terminated Unicode
83 string and returns the new Unicode string.
85 This function copies the contents of the Unicode string Source to the Unicode
86 string Destination, and returns Destination. If Source and Destination
87 overlap, then the results are undefined.
89 If Destination is NULL, then ASSERT().
90 If Source is NULL, then ASSERT().
91 If Source and Destination overlap, then ASSERT().
93 @param Destination Pointer to a Null-terminated Unicode string.
94 @param Source Pointer to a Null-terminated Unicode string.
102 OUT CHAR16
*Destination
,
103 IN CONST CHAR16
*Source
109 // Destination cannot be NULL
111 ASSERT (Destination
!= NULL
);
114 // Destination and source cannot overlap
116 ASSERT ((UINTN
)(Destination
- Source
) > StrLen (Source
));
117 ASSERT ((UINTN
)(Source
- Destination
) > StrLen (Source
));
119 ReturnValue
= Destination
;
121 *(Destination
++) = *(Source
++);
128 Copies one Null-terminated Unicode string with a maximum length to another
129 Null-terminated Unicode string with a maximum length and returns the new
132 This function copies the contents of the Unicode string Source to the Unicode
133 string Destination, and returns Destination. At most, Length Unicode
134 characters are copied from Source to Destination. If Length is 0, then
135 Destination is returned unmodified. If Length is greater that the number of
136 Unicode characters in Source, then Destination is padded with Null Unicode
137 characters. If Source and Destination overlap, then the results are
140 If Destination is NULL, then ASSERT().
141 If Source is NULL, then ASSERT().
142 If Source and Destination overlap, then ASSERT().
144 @param Destination Pointer to a Null-terminated Unicode string.
145 @param Source Pointer to a Null-terminated Unicode string.
146 @param Length Maximum number of Unicode characters to copy.
154 OUT CHAR16
*Destination
,
155 IN CONST CHAR16
*Source
,
166 // Destination cannot be NULL if Length is not zero
168 ASSERT (Destination
!= NULL
);
171 // Destination and source cannot overlap
172 // Q: Does Source have to be NULL-terminated?
174 ASSERT ((UINTN
)(Destination
- Source
) > StrLen (Source
));
175 ASSERT ((UINTN
)(Source
- Destination
) >= Length
);
177 ReturnValue
= Destination
;
179 while ((*Source
!= L
'\0') && (Length
> 0)) {
180 *(Destination
++) = *(Source
++);
184 memset (Destination
, 0, Length
* sizeof (*Destination
));
189 Returns the size of a Null-terminated Unicode string in bytes, including the
192 This function returns the size, in bytes, of the Null-terminated Unicode
193 string specified by String.
195 If String is NULL, then ASSERT().
197 @param String Pointer to a Null-terminated Unicode string.
199 @return The size of String.
205 IN CONST CHAR16
*String
208 return (StrLen (String
) + 1) * sizeof (*String
);
212 Compares two Null-terminated Unicode strings, and returns the difference
213 between the first mismatched Unicode characters.
215 This function compares the Null-terminated Unicode string FirstString to the
216 Null-terminated Unicode string SecondString. If FirstString is identical to
217 SecondString, then 0 is returned. Otherwise, the value returned is the first
218 mismatched Unicode character in SecondString subtracted from the first
219 mismatched Unicode character in FirstString.
221 If FirstString is NULL, then ASSERT().
222 If SecondString is NULL, then ASSERT().
224 @param FirstString Pointer to a Null-terminated Unicode string.
225 @param SecondString Pointer to a Null-terminated Unicode string.
227 @retval 0 FirstString is identical to SecondString.
228 @retval !=0 FirstString is not identical to SecondString.
234 IN CONST CHAR16
*FirstString
,
235 IN CONST CHAR16
*SecondString
239 // ASSERT both strings should never be zero
241 ASSERT (StrSize (FirstString
) != 0);
242 ASSERT (StrSize (SecondString
) != 0);
244 while ((*FirstString
!= L
'\0') && (*FirstString
== *SecondString
)) {
248 return *FirstString
- *SecondString
;
252 Compares two Null-terminated Unicode strings with maximum lengths, and
253 returns the difference between the first mismatched Unicode characters.
255 This function compares the Null-terminated Unicode string FirstString to the
256 Null-terminated Unicode string SecondString. At most, Length Unicode
257 characters will be compared. If Length is 0, then 0 is returned. If
258 FirstString is identical to SecondString, then 0 is returned. Otherwise, the
259 value returned is the first mismatched Unicode character in SecondString
260 subtracted from the first mismatched Unicode character in FirstString.
262 If FirstString is NULL, then ASSERT().
263 If SecondString is NULL, then ASSERT().
265 @param FirstString Pointer to a Null-terminated Unicode string.
266 @param SecondString Pointer to a Null-terminated Unicode string.
267 @param Length Maximum number of Unicode characters to compare.
269 @retval 0 FirstString is identical to SecondString.
270 @retval !=0 FirstString is not identical to SecondString.
276 IN CONST CHAR16
*FirstString
,
277 IN CONST CHAR16
*SecondString
,
286 // ASSERT both strings should never be zero
288 ASSERT (StrSize (FirstString
) != 0);
289 ASSERT (StrSize (SecondString
) != 0);
291 while ((*FirstString
!= L
'\0') &&
292 (*FirstString
== *SecondString
) &&
299 return *FirstString
- *SecondString
;
303 Concatenates one Null-terminated Unicode string to another Null-terminated
304 Unicode string, and returns the concatenated Unicode string.
306 This function concatenates two Null-terminated Unicode strings. The contents
307 of Null-terminated Unicode string Source are concatenated to the end of
308 Null-terminated Unicode string Destination. The Null-terminated concatenated
309 Unicode String is returned. If Source and Destination overlap, then the
310 results are undefined.
312 If Destination is NULL, then ASSERT().
313 If Source is NULL, then ASSERT().
314 If Source and Destination overlap, then ASSERT().
316 @param Destination Pointer to a Null-terminated Unicode string.
317 @param Source Pointer to a Null-terminated Unicode string.
325 IN OUT CHAR16
*Destination
,
326 IN CONST CHAR16
*Source
329 StrCpy (Destination
+ StrLen (Destination
), Source
);
332 // Size of the resulting string should never be zero.
334 ASSERT (StrSize (Destination
) != 0);
339 Concatenates one Null-terminated Unicode string with a maximum length to the
340 end of another Null-terminated Unicode string, and returns the concatenated
343 This function concatenates two Null-terminated Unicode strings. The contents
344 of Null-terminated Unicode string Source are concatenated to the end of
345 Null-terminated Unicode string Destination, and Destination is returned. At
346 most, Length Unicode characters are concatenated from Source to the end of
347 Destination, and Destination is always Null-terminated. If Length is 0, then
348 Destination is returned unmodified. If Source and Destination overlap, then
349 the results are undefined.
351 If Destination is NULL, then ASSERT().
352 If Source is NULL, then ASSERT().
353 If Source and Destination overlap, then ASSERT().
355 @param Destination Pointer to a Null-terminated Unicode string.
356 @param Source Pointer to a Null-terminated Unicode string.
357 @param Length Maximum number of Unicode characters to concatenate from
366 IN OUT CHAR16
*Destination
,
367 IN CONST CHAR16
*Source
,
371 StrnCpy (Destination
+ StrLen (Destination
), Source
, Length
);
374 // Size of the resulting string should never be zero.
376 ASSERT (StrSize (Destination
) != 0);
381 Copies one Null-terminated ASCII string to another Null-terminated ASCII
382 string and returns the new ASCII string.
384 This function copies the contents of the ASCII string Source to the ASCII
385 string Destination, and returns Destination. If Source and Destination
386 overlap, then the results are undefined.
388 If Destination is NULL, then ASSERT().
389 If Source is NULL, then ASSERT().
390 If Source and Destination overlap, then ASSERT().
392 @param Destination Pointer to a Null-terminated ASCII string.
393 @param Source Pointer to a Null-terminated ASCII string.
401 OUT CHAR8
*Destination
,
402 IN CONST CHAR8
*Source
408 // Destination cannot be NULL
410 ASSERT (Destination
!= NULL
);
413 // Destination and source cannot overlap
415 ASSERT ((UINTN
)(Destination
- Source
) > AsciiStrLen (Source
));
416 ASSERT ((UINTN
)(Source
- Destination
) > AsciiStrLen (Source
));
418 ReturnValue
= Destination
;
420 *(Destination
++) = *(Source
++);
427 Copies one Null-terminated ASCII string with a maximum length to another
428 Null-terminated ASCII string with a maximum length and returns the new ASCII
431 This function copies the contents of the ASCII string Source to the ASCII
432 string Destination, and returns Destination. At most, Length ASCII characters
433 are copied from Source to Destination. If Length is 0, then Destination is
434 returned unmodified. If Length is greater that the number of ASCII characters
435 in Source, then Destination is padded with Null ASCII characters. If Source
436 and Destination overlap, then the results are undefined.
438 If Destination is NULL, then ASSERT().
439 If Source is NULL, then ASSERT().
440 If Source and Destination overlap, then ASSERT().
442 @param Destination Pointer to a Null-terminated ASCII string.
443 @param Source Pointer to a Null-terminated ASCII string.
444 @param Length Maximum number of ASCII characters to copy.
452 OUT CHAR8
*Destination
,
453 IN CONST CHAR8
*Source
,
464 // Destination cannot be NULL
466 ASSERT (Destination
!= NULL
);
469 // Destination and source cannot overlap
471 ASSERT ((UINTN
)(Destination
- Source
) > AsciiStrLen (Source
));
472 ASSERT ((UINTN
)(Source
- Destination
) >= Length
);
474 ReturnValue
= Destination
;
476 while (*Source
&& Length
> 0) {
477 *(Destination
++) = *(Source
++);
481 // ZeroMem (Destination, Length * sizeof (*Destination));
482 memset (Destination
, 0, Length
* sizeof (*Destination
));
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);