GNU LIBC源代码学习之strcmp

简介:

比較两个字符串

我的代码块

#include <string.h>


int my_strcmp(const char* s1,const char * s2)
{
 if((s1==NULL)||(s2==NULL))
  return 0;
 while(1)
 {
  if((*s1=='\0')||(*s2=='\0'))
   break;
  if(*s1>*s2)
   return 1;
  if(*s1<*s2)
   return -1;
  s1++;
  s2++;
 }
 //if(*s1==*s2=='\0')
 // return 0;
 if(*s1=='\0'&&*s2=='\0')
  return 0;
 if(*s1>*s2)
  return 1;
 else
  return -1;

}

int main()
{
 char* a="abcdef";
 char* b="abcdef";
 my_strcmp(a,b);
 return 0;
}

这里,上面的方法。不可靠,没有明白的内在逻辑。 
看看源代码

/* Copyright (C) 1991-2015 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <http://www.gnu.org/licenses/>.  */

#include <string.h>

#undef strcmp

/* Compare S1 and S2, returning less than, equal to or
   greater than zero if S1 is lexicographically less than,
   equal to or greater than S2.  */
int
strcmp (const char *p1, const char *p2)
{
  const unsigned char *s1 = (const unsigned char *) p1;
  const unsigned char *s2 = (const unsigned char *) p2;
  unsigned char c1, c2;

  do
    {
      c1 = (unsigned char) *s1++;
      c2 = (unsigned char) *s2++;
      if (c1 == '\0')
return c1 - c2;
    }
  while (c1 == c2);

  return c1 - c2;
}

要比較s1和s2的每个字节的大小,则,两者比較,最简单的分类就是相等和不相等,在相等的时候继续,不相等时。马上计算返回。

至于 
const unsigned char s1 = (const unsigned char ) p1; 
const unsigned char s2 = (const unsigned char ) p2; 
这样转换的妙处,未能体会





本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5340656.html,如需转载请自行联系原作者

相关文章
|
Rust 算法 Go
【密码学】一文读懂FNV Hash
FNV哈希全名为Fowler-Noll-Vo算法,是以三位发明人Glenn Fowler,Landon Curt Noll,Phong Vo的名字来命名的,最早在1991年提出。它可以快速hash大量的数据并保持较小的冲突概率,适合hash一些相近的字符串比如IP地址、URL、文件名等等。目前FNV算法有三个版本,分别是: FNV-0(已废弃)、FNV-1以及FNV-1a。这三个算法的结构非常相似,因此呢,在这里就一块说了。
4132 0
【密码学】一文读懂FNV Hash
|
6月前
|
存储 Android开发 虚拟化
机器码修改工具插件,软件机器码一键修改工具,可过任何检测【仅供学习】
本文档主要讲解了机器码组成原理、核心Hook技术实现及反检测策略。首先分析现代软件机器码检测涉及的硬件特征,如硬盘卷序列号、网卡MAC地址等
|
网络安全
蓝桥杯-网络安全-练习题-crypto-rsa
蓝桥杯-网络安全-练习题-crypto-rsa
蓝桥杯-网络安全-练习题-crypto-rsa
|
11月前
|
SQL Java Maven
docker部署apollo
docker部署apollo步骤
525 10
|
存储 测试技术 数据库
数据驱动测试和关键词驱动测试的区别
数据驱动测试 数据驱动测试或 DDT 也被称为参数化测试。
292 1
|
人工智能 Oracle Java
蚂蚁 CodeFuse 代码大模型技术解析:基于全仓库上下文的代码补全
CodeFuse 代码补全插件是 CodeFuse 系列产品中用户数量最多、留存率最大,调用AI能力最多的产品~欢迎大家体验试用https://github.com/codefuse-ai/RepoFuse
2230 7
蚂蚁 CodeFuse 代码大模型技术解析:基于全仓库上下文的代码补全
【开发课堂】支付宝小程序里如何使用自定义字体?
前言在小程序中为确保移动终端的通用性,官方是建议首选 PingFang SC 作为中文字体,以兼顾 Web 版和 Mobile 端。设计中字体大小与使用场景规范如下:在通配样式里字体如下:{font-style: normal;font-weight: normal;font: 0.
4382 11
【开发课堂】支付宝小程序里如何使用自定义字体?
|
Linux Windows
Installing, this may take a few minutes...WslRegisterDistribution failed with error: 0x80370114Err
Installing, this may take a few minutes...WslRegisterDistribution failed with error: 0x80370114Err
4057 3
|
Linux 网络安全
Linux(CentOS6.5)开放端口,配置防火墙
Linux(CentOS6.5)开放端口,配置防火墙
505 0