我使用的反序列化库(messagepack)不提供以空值结尾的字符串。相反,我得到一个指向字符串开头的指针和一个长度。将此字符串与正常的以空字符结尾的字符串进行比较的最快方法是什么?
问问题
2610 次
3 回答
2
最快的方法是strncmp()
限制要比较的长度。
if (strncmp(sa, sb, length)==0)
...
但是,这假定您使用的长度是两个字符串的最大长度。如果以空字符结尾的字符串可能有更大的长度,您首先必须比较长度。
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
...
于 2015-03-11T20:52:33.453 回答
1
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));
...
于 2015-03-11T21:03:33.520 回答
1
这是一种方法:
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;
}
}
于 2015-03-11T22:29:46.963 回答