将以null结尾的字符串与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; } }