🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #195

Re: compare literals / if( "cat"== "dog") {}

#195
From: fir
Date: Sun, 11 Oct 2026 09:32
94 lines
4220 bytes
fir pisze:
> fir pisze:
> 
>>
>> #include <string.h>
>> #include <stdint.h>
>>
>> static const char tag1[16] __attribute__((aligned(4))) = "addasD";
>> static const char tag2[16] __attribute__((aligned(4))) = "addasD";
>> static const char tag3[16] __attribute__((aligned(4))) = "addasE";
>>
>>
>>
>> static inline int tag_eq(const char *a,const char *b)
>> {
>>      if(a==b) return 1;
>>
>>      const uint32_t *x=(const uint32_t *)a;
>>      const uint32_t *y=(const uint32_t *)b;
>>
>>      return x[0]==y[0] && x[1]==y[1] && x[2]==y[2] && x[3]==y[3];
>> }
>>
> 
> 
> sorry the above part should be deleted the below is  ok and i not 
> noticed this above was in post
> 
>>
>> not tested it though but it should be ok probably (if work)
>>
>> typedef const char tag20[20] __attribute__((aligned(4)));
>>
>> //tag20 z="test";
>>
>> static inline int tag20_eq(tag20 a,tag20 b)
>> {
>>      if(a==b) return 1;
>>      const uint32_t *x=(const uint32_t *)a;
>>      const uint32_t *y=(const uint32_t *)b;
>>      return x[0]==y[0] && x[1]==y[1] && x[2]==y[2] && x[3]==y[3] && 
>> x[4]==y[4];
>> }
>>
>>
>>
>> it compiled but i not tested it
>>
>> overally it seems either first or second solution are real so i could 
>> take one
>>
>>
> 
for efficiency reasons probbly this version would be better

typedef const char tag12[12] __attribute__((aligned(4)));

//tag12 z="testin it";

static inline int tag12_eq(tag12 a,tag12 b)
{
     if(a==b) return 1;
     const uint32_t *x=(const uint32_t *)a;
     const uint32_t *y=(const uint32_t *)b;
     return x[0]==y[0] && x[1]==y[1] && x[2]==y[2];
}

if im not wrong this gives 11 chars and in worst case compares 3 ints 
(+1 initial)....11 chars is probably enougs - though some could say i 
could use longer and only use shortar tahs - but if unfortunate case
would go i coudl compara "zz" to "zz" and run all ints to compare
as code dont knows dont compare on zeros...coud eventually add this zero 
check an use longer, but i dont know at the moment

ai answered that so maybe this is final version


#include <stdint.h>

typedef const char tag[20] __attribute__((aligned(4)));

static inline int tag_eq(tag a,tag b)
{
     if(a==b) return 1;

     const uint32_t *x=(const uint32_t *)a;
     const uint32_t *y=(const uint32_t *)b;

     if(x[0]!=y[0]) return 0;    if(x[0]==0) return 1;
     if(x[1]!=y[1]) return 0;    if(x[1]==0) return 1;
     if(x[2]!=y[2]) return 0;    if(x[2]==0) return 1;
     if(x[3]!=y[3]) return 0;    if(x[3]==0) return 1;
     return x[4]==y[4];
}
Technical Details
Message-ID: <11afl3o$2giqh$1@dont-email.me>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
References: <11ae962$2365v$1@dont-email.me> <9tyyS.21373$VDn8.13923@fx14.ams4> <11afcku$2dli3$1@dont-email.me> <11afe36$2e577$1@dont-email.me> <11afe6j$2e577$2@dont-email.me> <11afedm$2e577$3@dont-email.me> <11aff4q$2egej$1@dont-email.me> <11afiss$2fqv3$1@dont-email.me> <11afj2m$2fqv3$2@dont-email.me>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
From: fir <profesor.fir@gmail.com>
Newsgroups: comp.lang.c
Subject: Re: compare literals / if( "cat"== "dog") {}
Date: Sun, 11 Oct 2026 11:32:39 +0200
Organization: A noiseless patient Spider
Lines: 94
Message-ID: <11afl3o$2giqh$1@dont-email.me>
References: <11ae962$2365v$1@dont-email.me> <9tyyS.21373$VDn8.13923@fx14.ams4>
 <11afcku$2dli3$1@dont-email.me> <11afe36$2e577$1@dont-email.me>
 <11afe6j$2e577$2@dont-email.me> <11afedm$2e577$3@dont-email.me>
 <11aff4q$2egej$1@dont-email.me> <11afiss$2fqv3$1@dont-email.me>
 <11afj2m$2fqv3$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 11 Oct 2026 09:32:41 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="2640721"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX19+Au5hnXXVGRDOsyk5sVuHT/J+YX1XofY="; posting-host="c78a7ee27a315dc33fa20d18f85fce92"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:140.0) Gecko/20100101
 Firefox/140.0 SeaMonkey/2.53.24
Cancel-Lock: sha1:rInFzxrngny5R9oAGfQeDT48uNY= sha256:hNdcTIR7aXtHZ5oaTgGK7tu9uU/cn6lkf3alTR+Ks9Q=
	sha1:1klo/4oq9GMptO/YizEInZ639p8= sha256:njRlnNXCDBOLzsG5PwoF8vwihWiCcWNMQOcF8pPi5tI=
In-Reply-To: <11afj2m$2fqv3$2@dont-email.me>