🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #190

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

#190
From: fir
Date: Sun, 11 Oct 2026 08:54
171 lines
6583 bytes
fir pisze:
> fir pisze:
>> fir pisze:
>>> fir pisze:
>>>> fir pisze:
>>>>> Johann 'Myrkraverk' Oskarsson pisze:
>>>>>> On 10/11/2026 5:02 AM, fir wrote:
>>>>>>> if("cat" == "cat") will return true and
>>>>>>> if("cat" == "dog") will return false
>>>>>>>
>>>>>>>
>>>>>>> i probably asked that but if so once again
>>>>>>>
>>>>>>> is there way to enforce it to work?
>>>>>>
>>>>>> Sort of.  If you're /only/ comparing literals, you can apply the /GF
>>>>>> switch in MSVC, and then all the literals will compiled into a single
>>>>>> pool, and you can compare by the pointers.
>>>>>>
>>>>>> Of course, the pointers still point to the pool, and that's important
>>>>>> to remember.
>>>>>>
>>>>>> I'm sure GCC and clang have equivalent switches.
>>>>>
>>>>> there is  -megre-constants but im not sure if it 1) will guarantee 
>>>>> (at least ractically) that it will work 2) if it will work at least 
>>>>> in my own case
>>>>>
>>>>> i could eventually write a wrapper  tag_eq(char*, char*) or 
>>>>> something which will be comparing by == and != and have fallback 
>>>>> ino strcomp eventually, possibly its worth trying
>>>>
>>>> i mean something like that (which has ugly form but i got no time to 
>>>> think on form)
>>>>
>>>>
>>>>
>>>> enum {COMPARE_STRING_TAGS_BY_POINTERS = 1}; //may be dangerous, it 
>>>> assumes same text literals have same pointers
>>>> enum {TEST_TAG_COMPARISONS =1};
>>>>
>>>> static inline int tag_eq(const char *a,const char *b)
>>>> {
>>>>
>>>>      if(TEST_TAG_COMPARISONS)
>>>>      {
>>>>          int p = a==b;
>>>>          int s = strcmp(a,b)==0;
>>>>          if(p!=s) ERROR_("pointer comparison of string tag != 
>>>> strcopy version (which means it fails)");
>>>>          return p;
>>>>      }
>>>>
>>>>      if(COMPARE_STRING_TAGS_BY_POINTERS)
>>>>          return a==b;
>>>>      else
>>>>          return strcmp(a,b)==0;
>>>> }
>>>>
>>>>
>>>> i mean if app uses extensively this string literal tags and tag_eq
>>>> then if test is on then if there is difference there will be rontime 
>>>> error so if there is no error in non debug version thet test can be 
>>>> disabled and it should work
>>>>
>>> i tested in slightly in my app and it work i mean test dont fail
>>> but i only began to use this literal tags form yesterday and use it 
>>> very little
>>
>> oh no i ommited some change (from strcmp to tag eq) it fails
>>
>> probably depending on compilation flags
>>
>>
> nah it showed that in a code i got strcopy based solution before so it 
> should fail
> 
> changed on proper char* and now it runs ok, so as for now at least this 
> tags based on literal pointers seem to work
> 

overally maybe even this strcmp is not such bad (?)

it maybe depand what it really is (and i dont quite know what it really is)

if you have model tag usage case

draw(char* tag)
{
   if(tag_eq(tag, "ship")) {}
   if(tag_eq(tag, "bullet")) {}
   if(tag_eq(tag, "debris")) {}
   if(tag_eq(tag, "smoke")) {}


}

then the besic comparison like tag=="ship" is justified and
strcopy can be judged on what it do more over that

i assume it ocmparest the pointers at first step and if the same it has 
no cost over it

if this is not eq i assume it compare sign by sign and in most cases
ends on first sign differences

I. it needs to check by zeros though (and this is disadventage
(it seems it need to be then 2 compariosn on char
if(a[i]!=0 & a[i]!=b[i])  not one

so this is another argument that zero endong chars are bad

II. im not totally sure if it is fully inline.. there is
a way to write strcmp byself though

static inline int strcmp_(const char *a,const char *b)
{
     while(*a && *a==*b) { a++; b++; }
     return (unsigned char)*a-(unsigned char)*b;
}

is it bad? it not much good but may be standable maybe


some coud wrote a version that assumes tags are prepared to e aligned 
and zero padded at end


#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];
}


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

Technical Details
Message-ID: <11afiss$2fqv3$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>

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 10:54:51 +0200
Organization: A noiseless patient Spider
Lines: 171
Message-ID: <11afiss$2fqv3$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>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 11 Oct 2026 08:54:53 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="2616291"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX19wJ49H2vTJd9doJL4aqmq1aQQlxIi4whY="; 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:oODVsxS5dJcWp43SiVaEB8UNyW4= sha256:EajogAp5K81Ue9kNfgLaqg6CytYu5Ev6T4o4v73/elc=
	sha1:oN81AhQhyrFfy+TC5uzgGsMwexo= sha256:2aE6ZoaettpYqiKlxgPpi0Q4FMbpFG7GKswHYEZnmew=
In-Reply-To: <11aff4q$2egej$1@dont-email.me>