GNU libc Source Learning strcmp

Source: Internet
Author: User

Comparison of two strings

My block of code

#include <string.h>intMY_STRCMP (const char* s1,const char * s2) {if((s1==null) | | (S2==null))return 0; while(1) {if((*s1==' + ')|| (*s2==' + ')) Break;if(*s1>*s2)return 1;if(*s1<*s2)return-1;  s1++; s2++; } //if(*s1==*s2==' + ') //return 0;if(*s1==' + '&&*s2==' + ')return 0;if(*s1>*s2)return 1;Else  return-1;}intMain () {char* a="ABCdef"; char* b="ABCdef"; MY_STRCMP (A, b);return 0;}

Here, the above method, unreliable, without definite intrinsic logic,
Look at the source

/* Copyright (C)1991- -Free Software Foundation, Inc. This file isPart ofThe GNU C Library. The GNU C Library isFree software; You can redistribute it and/orModify it under the terms ofThe GNU Lesser general public License as published byThe free software Foundation; Either version2.1  ofThe License,or(at your option) any later version. The GNU C Library isDistributedinchThe hope that it'll be useful, but without any WARRANTY; Without even the implied warranty ofmerchantabilityorFITNESS for A particular PURPOSE. See the GNU Lesser general public License forMore details. You should has received a copy ofThe GNU Lesser general public License along withThe GNU C Library;if  not, see <http://www.gnu.org/licenses/. */#include <string.h>#undef strcmp/* Compare S1 andS2, returning less than, equal toorGreater than zeroifS1 islexicographically less than, equal toorGreater than S2. */INTSTRCMP (ConstChar *p1,ConstChar *p2) {Constunsigned char *s1 = (Constunsigned char *) p1;Constunsigned char *s2 = (Constunsigned char *) P2; unsigned char C1, C2; Do{C1 = (unsigned char) *s1++; C2 = (unsigned char) *s2++;if(C1 = =' + ')returnC1-C2; } while(C1 = = C2);returnC1-C2;}

To compare the size of each byte of the S1 and S2, the simplest classification is equal and unequal, and returns are calculated immediately when they are equal and not equal.

As for
Const unsigned char s1 = (const unsigned char ) p1;
const unsigned char s2 = (const unsigned char ) P2;
The beauty of such conversion, failed to comprehend

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

GNU libc Source Learning strcmp

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.