This commit is contained in:
hairrrrr
2020-02-08 16:12:52 +08:00
parent eef73f584c
commit 3b863ab573
3 changed files with 47 additions and 0 deletions

View File

@@ -0,0 +1,29 @@
//这是我能理解的格式
int mystrcmp(char* str1, char* str2) {
int ret = 0;
while ((ret = ((unsigned char)*str1 - (unsigned char)*str2)) == 0 && *str1) {
++str1, ++str2;
}
return ret;
}
//原版在这里
int __cdecl strcmp(
const char* src,
const char* dst
)
{
int ret = 0;
while ((ret = *(unsigned char*)src - *(unsigned char*)dst) == 0 && *dst)
{
++src, ++dst;
}
//这里不太理解,那个大神理解了可以给我讲一下,谢谢
return ((-ret) < 0) - (ret < 0); // (if positive) - (if negative) generates branchless code
}

View File

@@ -0,0 +1,11 @@
int main() {
char* str1 = "Hello ";
char* str2 = "Hello";
printf("%d\n", mystrcmp(str1, str2));
return 0;
}

View File

@@ -0,0 +1,7 @@
int mystrcmp(char* str1, char* str2) {
while (*str1 == *str2 && *str1)
++str1, ++str2;
return (*str1 - *str2);
}