一. 字符串长度与大小
因为ASCII字符串以NULL或者说以'\0'结尾,UNICODE以L'\0'结尾,那么字符串的大小和长度显示是不同的:字符串大小 = 字符串长度 + 1;
获取一个字符串的长度采用的库函数是
size_t strlen( char const *string );
获取字符串大小的函数可以根据和长度的关系自行构造。下面列举UEFI中的两个函数的实现,
ASCII码
UINTN
EFIAPI
AsciiStrLen (
IN CONST CHAR8 *String
)
{
UINTN Length;
ASSERT (String != NULL);
for (Length = 0; *String != '\0'; String++, Length++) {
//
// If PcdMaximumUnicodeStringLength is not zero,
// length should not more than PcdMaximumUnicodeStringLength
//
if (PcdGet32 (PcdMaximumAsciiStringLength) != 0) {
ASSERT (Length < PcdGet32 (PcdMaximumAsciiStringLength));
}
}
return Length;
}
UINTN
EFIAPI
AsciiStrSize (
IN CONST CHAR8 *String
)
{
return (AsciiStrLen (String) + 1) * sizeof (*String);
}
UNICODE码
UINTN
EFIAPI
StrLen (
IN CONST CHAR16 *String
)
{
UINTN Length;
ASSERT (String != NULL);
ASSERT (((UINTN) String & BIT0) == 0);
for (Length = 0; *String != L'\0'; String++, Length++) {
//
// If PcdMaximumUnicodeStringLength is not zero,
// length should not more than PcdMaximumUnicodeStringLength
//
if (PcdGet32 (PcdMaximumUnicodeStringLength) != 0) {
ASSERT (Length < PcdGet32 (PcdMaximumUnicodeStringLength));
}
}
return Length;
}
UINTN
EFIAPI
StrSize (
IN CONST CHAR16 *String
)
{
return (StrLen (String) + 1) * sizeof (*String);
}
二. 字符串比较
所谓字符串比较,就是看两个字符串是否相互包含,或者对比最先不匹配的字符,哪个在字符集中的序数较小。函数原型是:
int strcmp( char const *s1, char const *s2 )
可以看到该函数的返回值类型为INT,那么如果s1小于s2,返回值小于0;如果s1大于s2,返回值大于0;如果s1等于s2,返回值等于0。
UEFI中该库函数的实现如下:
ASCII码
INTN
EFIAPI
AsciiStrCmp (
IN CONST CHAR8 *FirstString,
IN CONST CHAR8 *SecondString
)
{
//
// ASSERT both strings are less long than PcdMaximumAsciiStringLength
//
ASSERT (AsciiStrSize (FirstString));
ASSERT (AsciiStrSize (SecondString));
while ((*FirstString != '\0') && (*FirstString == *SecondString)) {
Firs

本文介绍了在UEFI中如何处理ASCII和UNICODE字符串,涉及长度计算、字符串比较、strcpy/strcat/strnCpy/strCpy/strnCat等函数的实现,以及strstr查找函数的应用。
最低0.47元/天 解锁文章
1328

被折叠的 条评论
为什么被折叠?



