在 C 中比较以空字符结尾的字符串和非以空字符结尾的字符串

Comparing null-terminated string with a non null-terminated string in C

我使用的反序列化库(messagepack)不提供以null结尾的字符串。相反,我得到一个指向字符串开头的指针和一个长度。将此字符串与普通的以 null 结尾的字符串进行比较的最快方法是什么?

最快的方法是strncmp(),它限制了要比较的长度。

 if (strncmp(sa, sb, length)==0)  
    ...

但这假设您使用的长度是两个字符串的最大长度。如果以 null 结尾的字符串可能有更大的长度,您首先必须比较长度。

 if(strncmp(sa,sb, length)==0 && strlen(sa)<=length) // sa being the null terminated one
     ...

请注意,比较后会特意检查 strlen(),以避免在第一个字符甚至不匹配时不必要地遍历空终止字符串的所有字符。

最终变体是:

 if(strncmp(sa,sb, length)==0 && sa[length]==0) // sa being the null terminated one
     ...
int compare(char *one, size_t onelen, char *two, size_t twolen)
{
int dif;

  dif = memcmp(one, two, onelen < twolen ? onelen : twolen);
  if (dif) return dif;

  if (onelen == twolen) return 0;
  return onelen > twolen? 1 : -1;
}

用法:

...
int result;
char einz[4] = "einz"; // not terminated
char *zwei = "einz";   // terminated

result = compare(einz, sizeof einz, zwei, strlen(zwei));

...

这是一种方法:

bool is_same_string(char const *s1, char const *s2, size_t s2_len)
{
    char const *s2_end = s2 + s2_len;
    for (;;)
    {
        if ( s1[0] == 0 || s2 == s2_end )
            return s1[0] == 0 && s2 == s2_end;

        if ( *s1++ != *s2++ )
            return false;
    }
}