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用户行为日志审计
http://my.oschina.net/xiangpang/blog/532999 http://my.oschina.net/chaichuan/blog/508494 http://my.os ...
- POJ 2186 Popular Cows(Tarjan)
http://poj.org/problem?id=2186 题意 :给你n头牛,m对关系,每对关系由两个编号组成,u和v代表着u认为v是受欢迎的,如果1认为2是受欢迎的,2认为3是受欢迎的,那1认为 ...
- 李洪强漫谈iOS开发[C语言-036]-C语言前四天学习小结
- springboot 配置多数据源
1.首先在创建应用对象时引入autoConfig package com; import org.springframework.boot.SpringApplication; import org. ...
- leetcode面试准备: Maximal Rectangle
leetcode面试准备: Maximal Rectangle 1 题目 Given a 2D binary matrix filled with 0's and 1's, find the larg ...
- Ember.js demo5
<!DOCTYPE html> <html> <head> <meta name="description" content=" ...
- SQL Server 2008R2 数据库出现“可疑”导致无法访问
日常对Sql Server 2005关系数据库进行操作时,有时对数据库(如:Sharepoint网站配置数据库名Sharepoint_Config)进行些不正常操作如数据库在读写时而无故停止数据库,从 ...
- 【HDOJ】1003 Max Sum
最开始使用递归DP解,stack overflow.化简了一些,复杂度为O(n)就过了. #include <stdio.h> int main() { int case_n, n; in ...
- mapreduce实现全局排序
直接附代码,说明都在源码里了. package com.hadoop.totalsort; import java.io.IOException; import java.util.ArrayList ...
- bzoj2753
第一问dfs不说 第二问很容易让人想到最小树形图,但是我不会,而且时间复杂度也不允许 还有什么不同的方法呢? 首先想到的是prim的思想,设根节点已经确定,其他点未确定 我们就不断从已确定的点延伸,找 ...