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;
这样转换的妙处,未能体会
GNU LIBC源代码学习之strcmp的更多相关文章
- GNU工具链学习笔记
GNU工具链学习笔记 1..so为动态链接库,.a为静态连接库.他们在Linux下按照ELF格式存储.ELF有四种文件类型.可重定位文件(Relocatable file,*.o,*.a),包含代码和 ...
- struts2源代码学习之初始化(一)
看struts2源代码已有一段时日,从今天開始,就做一个总结吧. 首先,先看看怎么调试struts2源代码吧,主要是下面步骤: 使用Myeclipse创建一个webproject 导入struts2须 ...
- [Java] LinkedList / Queue - 源代码学习笔记
简单地画了下 LinkedList 的继承关系,如下图.只是画了关注的部分,并不是完整的关系图.本博文涉及的是 Queue, Deque, LinkedList 的源代码阅读笔记.关于 List 接口 ...
- 开源中国安卓client源代码学习(一) 渐变启动界面
开源中国安卓client源代码学习(一) 渐变启动界面 准备学习安卓开发, 看到网上有人推荐开源中国安卓client的源代码, 说里面包括了大部分技术, 于是准备好好研究研究. 特开通此系列博客来记录 ...
- 读Flask源代码学习Python--config原理
读Flask源代码学习Python--config原理 个人学习笔记,水平有限.如果理解错误的地方,请大家指出来,谢谢!第一次写文章,发现好累--!. 起因 莫名其妙在第一份工作中使用了从来没有接 ...
- char[]转换成wchar_t的转换方法(GNU Libc规定wchar_t为32位)
wchar_t是C/C++的字符数据类型,是一种扩展的字符存储方式,wchar_t类型主要用在国际化程序的实现中,但它不等同于unicode编码.unicode编码的字符一般以wchar_t类型存储. ...
- nginx源代码学习资源(不断更新)
nginx源代码学习是一个痛苦又快乐的过程,以下列出了一些nginx的学习资源. 首先要做的当然是下载一份nginx源代码,能够从nginx官方站点下载一份最新的. 看了nginx源代码,发现这是一份 ...
- JDK源代码学习系列07----Stack
JDK源代码学习系列07----Stack 1.Stack源代码很easy ...
- djangorestframework-jwt自带的认证视图进行用户登录验证源代码学习
Django REST framework JWT djangorestframework-jwt自带的认证视图进行用户登录验证源代码学习 SECRET_KEY = '1)q(f8jrz^edwtr2 ...
随机推荐
- Linux下SSH免密码登录
转自:http://haitao.iteye.com/blog/1744272 ssh配置 主机A:10.0.5.199 主机B:10.0.5.198 需要配置主机A无密码登录主机A,主机B 先确保所 ...
- ContentLoadingProgressBar不显示问题
ContentLoadingProgressBar需要设置style 并且在XML中布局的位置必须写在content布局的下面 <?xml version="1.0" enc ...
- VS2005代码自动提示功能失灵
http://bbs.csdn.net/topics/340036305 方法很简单:把整个项目复制一份(文件夹名与原来不同就行).打开项目(此时可以注意到状态条显示正在更新intelligence) ...
- IPv6 tutorial – Part 7: Zone ID and unique local IPv6 unicast addresses
The zone ID is used to distinguish ambiguous link-local and site-local addresses. Unique local IPv6 ...
- MyBatis学习总结2
这一篇讲述MyBatis对数据库的CRUD操作,内容不做重复,只做添加:查看学习总结1 一.使用MyBatis对表执行CRUD操作——基于XML的实现 在SQL映射文件userMapper.xml中添 ...
- mapreduce: 揭秘InputFormat--掌控Map Reduce任务执行的利器
随着越来越多的公司采用Hadoop,它所处理的问题类型也变得愈发多元化.随着Hadoop适用场景数量的不断膨胀,控制好怎样执行以及何处执行map任务显得至关重要.实现这种控制的方法之一就是自定义Inp ...
- bzoj3878
当初只会暴力,现在差不多觉得水了显然离线处理,对输入的数排序然后会发现不管怎么修改都是结果总是单调不降的对于每次处理,我们只要找到那段越界的即可显然上线段树,话说jsoi这么喜欢线段树?下面在bzoj ...
- iis 5.1 连接 sql 2005
VS2005+SQL2005 ASP.NET2.0数据库连接总结 通过上篇文章(http://www.cnblogs.com/user34j/archive/2007/01/23/628426.htm ...
- 【转】iTunes下载速度太慢?两招帮你提速!-- 不错
原文网址:http://bbs.app111.com/thread-275-1-1.html 不用说,很多朋友都发现在大陆,下载 iTunes 上的东西实在是慢如蜗牛,小一点的软件还能坚持一下,大一点 ...
- 数学物理学报Offprints and Remuneration