【C语言】字符串基本操作
注意:字符串以‘\0’结束
C语言中字符串的基本操作主要依赖于字符数组和指针。下面是一些常见的字符串操作:
1、字符串比较:
逐个比较两个字符串中的Unicode字符,直到发现第一个不匹配的字符为止。如果所有字符都匹配,则认为两个字符串相同。
返回0:如果FirstString和SecondString完全相同。
返回非零值:如果FirstString和SecondString的第一个不匹配字符之间存在差异,则返回这两个不匹配字符的Unicode码点之差。
Compares two Null-terminated Unicode strings, and returns the difference
between the first mismatched Unicode characters.
@param FirstString A pointer to a Null-terminated Unicode string.
@param SecondString A pointer to a Null-terminated Unicode string.
@retval 0 FirstString is identical to SecondString.
@return others FirstString is not identical to SecondString.
**/
INTN
EFIAPI
StrCmp (
IN CONST CHAR16 *FirstString,
IN CONST CHAR16 *SecondString
)
{
//
// ASSERT both strings are less long than PcdMaximumUnicodeStringLength
//
ASSERT (StrSize (FirstString) != 0);
ASSERT (StrSize (SecondString) != 0);
while ((*FirstString != L'\0') && (*FirstString == *SecondString)) {
FirstString++;
SecondString++;
}
return *FirstString - *SecondString;
}
2、字符串前几位比较
它用于比较两个以Null终止的Unicode字符串,但仅限于指定的字符数量(由参数Length决定)。这个函数的主要目的是在限定范围内判断两个字符串是否相同。
@param FirstString A pointer to a Null-terminated Unicode string.
@param SecondString A pointer to a Null-terminated Unicode string.
@param Length The maximum number of Unicode characters to compare.
@retval 0 FirstString is identical to SecondString.
@return others FirstString is not identical to SecondString.
**/
INTN
EFIAPI
StrnCmp (
IN CONST CHAR16 *FirstString,
IN CONST CHAR16 *SecondString,
IN UINTN Length
)
{
if (Length == 0) {
return 0;
}
//
// ASSERT both strings are less long than PcdMaximumUnicodeStringLength.
// Length tests are performed inside StrLen().
//
ASSERT (StrSize (FirstString) != 0);
ASSERT (StrSize (SecondString) != 0);
if (PcdGet32 (PcdMaximumUnicodeStringLength) != 0) {
ASSERT (Length <= PcdGet32 (PcdMaximumUnicodeStringLength));
}
while ((*FirstString != L'\0') &&
(*SecondString != L'\0') &&
(*FirstString == *SecondString) &&
(Length > 1))
{
FirstString++;
SecondString++;
Length--;
}
return *FirstString - *SecondString;
}
3、在一个字符串里搜索另一个字符串是否存在
查找子串功能,该函数在给定的Unicode字符串String中查找指定的Unicode子字符串SearchString,并返回子字符串在原字符串中的起始地址,若未找到则返回NULL。
CHAR16 *
EFIAPI
StrStr (
IN CONST CHAR16 *String,
IN CONST CHAR16 *SearchString
)
{
CONST CHAR16 *FirstMatch;
CONST CHAR16 *SearchStringTmp;
//
// ASSERT both strings are less long than PcdMaximumUnicodeStringLength.
// Length tests are performed inside StrLen().
//
ASSERT (StrSize (String) != 0);
ASSERT (StrSize (SearchString) != 0);
if (*SearchString == L'\0') {
return (CHAR16 *)String;
}
while (*String != L'\0') {
SearchStringTmp = SearchString;
FirstMatch = String;
while ( (*String == *SearchStringTmp)
&& (*String != L'\0'))
{
String++;
SearchStringTmp++;
}
if (*SearchStringTmp == L'\0') {
return (CHAR16 *)FirstMatch;
}
if (*String == L'\0') {
return NULL;
}
String = FirstMatch + 1;
}
return NULL;
}
4、字符串大小写转换
将一个Unicode小写字母字符转换为其大写形式
CHAR16
EFIAPI
CharToUpper (
IN CHAR16 Char
)
{
if ((Char >= L'a') && (Char <= L'z')) {
return (CHAR16)(Char - (L'a' - L'A'));
}
return Char;
}
5、获取字符串的长度
计算并返回一个以空字符(‘\0’)结束的ASCII字符串的长度。函数接受一个指向ASCII字符串的指针作为参数
@param String A pointer to a Null-terminated ASCII string.
@return The length of String.
**/
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;
}
6、比较字符串的相同部分
函数用于比较两个ASCII字符串是否相等以及它们之间的相对顺序。返回值为0表示两个字符串完全相同,返回值不为0则表示两个字符串不相同,且返回值给出了第一个不同字符的ASCII值之差。
@param FirstString A pointer to a Null-terminated ASCII string.
@param SecondString A pointer to a Null-terminated ASCII string.
@retval ==0 FirstString is identical to SecondString.
@retval !=0 FirstString is not identical to SecondString.
**/
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)) {
FirstString++;
SecondString++;
}
return *FirstString - *SecondString;
}
7、复制源指针的内容到新的指针
用于复制指定长度的数据从源内存地址到目标内存地址
VOID
CopyMem (
IN VOID *Destination,
IN VOID *Source,
IN UINTN Length
)
{
CHAR8 *Destination8;
CHAR8 *Source8;
Destination8 = Destination;
Source8 = Source;
while (Length--) {
*(Destination8++) = *(Source8++);
}
}
8、把指针长度里的值置0
将一段内存区域填充为0
VOID
ZeroMem (
IN VOID *Buffer,
IN UINTN Size
)
{
INT8 *Ptr;
Ptr = Buffer;
while (Size--) {
*(Ptr++) = 0;
}
}
9、比较两个Guid是否一致
用于比较两个GUID(全局唯一标识符)是否相等。GUID是一个128位的数字,在许多操作系统和编程环境中用于唯一标识对象,如类、接口或其他资源。
函数接受两个参数:
/*++
Routine Description:
Compares to GUIDs
Arguments:
Guid1 - guid to compare
Guid2 - guid to compare
Returns:
= 0 if Guid1 == Guid2
!= 0 if Guid1 != Guid2
--*/
INTN
CompareGuid (
IN EFI_GUID *Guid1,
IN EFI_GUID *Guid2
) {
INT32 *g1;
INT32 *g2;
INT32 r;
//
// Compare 32 bits at a time
//
g1 = (INT32 *) Guid1;
g2 = (INT32 *) Guid2;
r = g1[0] - g2[0];
r |= g1[1] - g2[1];
r |= g1[2] - g2[2];
r |= g1[3] - g2[3];
return r;
}
10、字符串的连接
将源字符串(包括终止空字符)追加到目标字符串的末尾
RETURN_STATUS
EFIAPI
StrCatS (
IN OUT CHAR16 *Destination,
IN UINTN DestMax,
IN CONST CHAR16 *Source
);