Thread View: comp.lang.c
37 messages
37 total messages
Started by fir
Sat, 10 Oct 2026 21:02
compare literals / if( "cat"== "dog") {}
Author: fir
Date: Sat, 10 Oct 2026 21:02
Date: Sat, 10 Oct 2026 21:02
25 lines
2304 bytes
2304 bytes
i must have asked for that but i got somewhat holes im my memory and i
dont exactly remember sometimes on which i wrote and on which not
the problem is i need this adhos enums strongly and really, but those
'smth' are to shore i could use two of them f('some','thin') but it gets
uglier and such code is like degraded
in fact there could be quite easy solution - it is use string literals
as such tags - i mean you dont need to use strcomp , you just need to
have same string literals have same pointer then
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?
second thought: its really good in fact, in some case it shows you dont
need strcomp, and i guess such syntax as if (a=="cat") is practically
not used in C which shows a flaw becouse it should, it should be 'unlocked'
(if that would be 'unlocked' in language, then side effect woudl be
string literals could be usad as tags (adhoc enums)
Re: compare literals / if( "cat"== "dog") {}
Author: bart
Date: Sat, 10 Oct 2026 21:24
Date: Sat, 10 Oct 2026 21:24
46 lines
2931 bytes
2931 bytes
On 10/10/2026 22:02, fir wrote:
> i must have asked for that but i got somewhat holes im my memory and i
> dont exactly remember sometimes on which i wrote and on which not
>
> the problem is i need this adhos enums strongly and really, but those
> 'smth' are to shore i could use two of them f('some','thin') but it gets
> uglier and such code is like degraded
>
> in fact there could be quite easy solution - it is use string literals
> as such tags - i mean you dont need to use strcomp , you just need to
> have same string literals have same pointer then
>
> if("cat" == "cat") will return true and
> if("cat" == "dog") will return false
Identical string literals, both visible in the same C source file, MIGHT
have the same pointer value.
But there is also runtime string data; strings returned from external
functions, or somehow synthesised at runtime, strings pointing into data
structures etc, that will not have the same address even two strings are
the same.
This would be up to you to produce, for example create a function call
'intern' that takes a string and returns a unique index for each unique
string.
Then:
if (intern(S) == intern(T))
will be guaranteed true if S and T are in fact same. But as it is,
'intern' will be doing string comparison and more. You might as well
create a function 'eqstring(S, T)' that returns 'strcmp(S, T) == 0'.
> (if that would be 'unlocked' in language, then side effect woudl be
> string literals could be usad as tags (adhoc enums)
It really sounds like need a higher level language than C. Try Python
where you just write:
if S == T:
and it will work exactly as you want.
Re: compare literals / if( "cat"== "dog") {}
Author: Johann 'Myrkraverk' Oskarsson
Date: Sat, 10 Oct 2026 21:53
Date: Sat, 10 Oct 2026 21:53
22 lines
1932 bytes
1932 bytes
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.
--
Johann | email: invalid -> com | http://www.myrkraverk.com/blog/
I'm not from the Internet, I just work there. | via Easynews.com
https://bsky.app/profile/myrkraverk.bsky.social | for ( ;; ) _:;
Federated at https://fed.brid.gy/bsky/myrkraverk.bsky.social
Re: compare literals / if( "cat"== "dog") {}
Author: fir
Date: Sun, 11 Oct 2026 07:08
Date: Sun, 11 Oct 2026 07:08
26 lines
2280 bytes
2280 bytes
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
Re: compare literals / if( "cat"== "dog") {}
Author: fir
Date: Sun, 11 Oct 2026 07:08
Date: Sun, 11 Oct 2026 07:08
48 lines
3191 bytes
3191 bytes
bart pisze:
> On 10/10/2026 22:02, fir wrote:
>> i must have asked for that but i got somewhat holes im my memory and i
>> dont exactly remember sometimes on which i wrote and on which not
>>
>> the problem is i need this adhos enums strongly and really, but those
>> 'smth' are to shore i could use two of them f('some','thin') but it gets
>> uglier and such code is like degraded
>>
>> in fact there could be quite easy solution - it is use string literals
>> as such tags - i mean you dont need to use strcomp , you just need to
>> have same string literals have same pointer then
>>
>> if("cat" == "cat") will return true and
>> if("cat" == "dog") will return false
>
> Identical string literals, both visible in the same C source file, MIGHT
> have the same pointer value.
>
> But there is also runtime string data; strings returned from external
> functions, or somehow synthesised at runtime, strings pointing into data
> structures etc, that will not have the same address even two strings are
> the same.
>
> This would be up to you to produce, for example create a function call
> 'intern' that takes a string and returns a unique index for each unique
> string.
>
> Then:
>
> if (intern(S) == intern(T))
>
> will be guaranteed true if S and T are in fact same. But as it is,
> 'intern' will be doing string comparison and more. You might as well
> create a function 'eqstring(S, T)' that returns 'strcmp(S, T) == 0'.
>
>> (if that would be 'unlocked' in language, then side effect woudl be
>> string literals could be usad as tags (adhoc enums)
>
> It really sounds like need a higher level language than C. Try Python
> where you just write:
>
> if S == T:
>
> and it will work exactly as you want.
>
>
its not higher level i would say its lower lewel than old c
Re: compare literals / if( "cat"== "dog") {}
Author: fir
Date: Sun, 11 Oct 2026 07:13
Date: Sun, 11 Oct 2026 07:13
54 lines
3494 bytes
3494 bytes
fir pisze:
> bart pisze:
>> On 10/10/2026 22:02, fir wrote:
>>> i must have asked for that but i got somewhat holes im my memory and
>>> i dont exactly remember sometimes on which i wrote and on which not
>>>
>>> the problem is i need this adhos enums strongly and really, but those
>>> 'smth' are to shore i could use two of them f('some','thin') but it gets
>>> uglier and such code is like degraded
>>>
>>> in fact there could be quite easy solution - it is use string
>>> literals as such tags - i mean you dont need to use strcomp , you
>>> just need to
>>> have same string literals have same pointer then
>>>
>>> if("cat" == "cat") will return true and
>>> if("cat" == "dog") will return false
>>
>> Identical string literals, both visible in the same C source file,
>> MIGHT have the same pointer value.
>>
>> But there is also runtime string data; strings returned from external
>> functions, or somehow synthesised at runtime, strings pointing into
>> data structures etc, that will not have the same address even two
>> strings are the same.
>>
>> This would be up to you to produce, for example create a function call
>> 'intern' that takes a string and returns a unique index for each
>> unique string.
>>
>> Then:
>>
>> if (intern(S) == intern(T))
>>
>> will be guaranteed true if S and T are in fact same. But as it is,
>> 'intern' will be doing string comparison and more. You might as well
>> create a function 'eqstring(S, T)' that returns 'strcmp(S, T) == 0'.
>>
>>> (if that would be 'unlocked' in language, then side effect woudl be
>>> string literals could be usad as tags (adhoc enums)
>>
>> It really sounds like need a higher level language than C. Try Python
>> where you just write:
>>
>> if S == T:
>>
>> and it will work exactly as you want.
>>
>>
> its not higher level i would say its lower lewel than old c
why lower level? becouse in higher level they often like dont care for
some love level cases..so if c here dont cares on this tehnique of
using literals this way its like addition that care is lower level
Re: compare literals / if( "cat"== "dog") {}
Author: fir
Date: Sun, 11 Oct 2026 07:32
Date: Sun, 11 Oct 2026 07:32
61 lines
3196 bytes
3196 bytes
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
Re: compare literals / if( "cat"== "dog") {}
Author: fir
Date: Sun, 11 Oct 2026 07:34
Date: Sun, 11 Oct 2026 07:34
65 lines
3602 bytes
3602 bytes
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
Re: compare literals / if( "cat"== "dog") {}
Author: fir
Date: Sun, 11 Oct 2026 07:38
Date: Sun, 11 Oct 2026 07:38
73 lines
3831 bytes
3831 bytes
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
Re: compare literals / if( "cat"== "dog") {}
Author: fir
Date: Sun, 11 Oct 2026 07:50
Date: Sun, 11 Oct 2026 07:50
79 lines
4161 bytes
4161 bytes
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
Re: compare literals / if( "cat"== "dog") {}
Author: fir
Date: Sun, 11 Oct 2026 08:54
Date: Sun, 11 Oct 2026 08:54
171 lines
6583 bytes
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
Re: compare literals / if( "cat"== "dog") {}
Author: fir
Date: Sun, 11 Oct 2026 08:57
Date: Sun, 11 Oct 2026 08:57
52 lines
2794 bytes
2794 bytes
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
>
>
Re: compare literals / if( "cat"== "dog") {}
Author: Richard Harnden
Date: Sun, 11 Oct 2026 09:06
Date: Sun, 11 Oct 2026 09:06
97 lines
4714 bytes
4714 bytes
On 11/10/2026 08:50, fir wrote:
> 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
>
If you want to compare strings by their pointers, then those pointers
will need to come from a stable pool, like, say, a hash table.
const char *hash_str_insert(hash *hash, const char *s);
...
const char *a = hash_str_insert(hash, "cat");
const char *b = hash_str_insert(hash, "dog");
const char *c = hash_str_insert(hash, "cat");
if ( a == b ) /* no */
if ( a == c ) /* yes */
Re: compare literals / if( "cat"== "dog") {}
Author: fir
Date: Sun, 11 Oct 2026 09:32
Date: Sun, 11 Oct 2026 09:32
94 lines
4220 bytes
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];
}
Re: compare literals / if( "cat"== "dog") {}
Author: fir
Date: Sun, 11 Oct 2026 09:34
Date: Sun, 11 Oct 2026 09:34
103 lines
5006 bytes
5006 bytes
Richard Harnden pisze:
> On 11/10/2026 08:50, fir wrote:
>> 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
>>
>
> If you want to compare strings by their pointers, then those pointers
> will need to come from a stable pool, like, say, a hash table.
>
> const char *hash_str_insert(hash *hash, const char *s);
>
> ...
>
> const char *a = hash_str_insert(hash, "cat");
> const char *b = hash_str_insert(hash, "dog");
> const char *c = hash_str_insert(hash, "cat");
>
> if ( a == b ) /* no */
>
> if ( a == c ) /* yes */
>
>
it is an option, maybe not insert it but ancode it in some key
unsigned key = encode("asdasdas");
Re: compare literals / if( "cat"== "dog") {}
Author: fir
Date: Sun, 11 Oct 2026 09:45
Date: Sun, 11 Oct 2026 09:45
32 lines
2643 bytes
2643 bytes
fir pisze:
> 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];
> }
still conceptually its better just to compare pointers to literals
and this above is only a aligned and padded type of string literal that
is faster to strcpy..so i would need to rethink that and check how
convenient it is in practice
probably for some time i will ypu with this pointer comparison
of normal strings and rutntime testing if it fails..then i will see how
much convenient it is
Re: compare literals / if( "cat"== "dog") {}
Author: fir
Date: Sun, 11 Oct 2026 10:28
Date: Sun, 11 Oct 2026 10:28
68 lines
3539 bytes
3539 bytes
fir pisze:
> fir pisze:
>> 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];
>> }
>
>
with this abowe it also may be some problem as i think if i would
write
tag x = "aadadda";
then im not sure if this addres of x and the adres of this "aadadda"
constant is the same? i fact there should be not definition of type
but adding to literals they are aligned and zero padded
like
(align 4, zero pad up to 20) "aadadda"
and im not sure if something like that is in c here
amd if i must create each instance of tag based on soame literal
it would have copies by definition i think..so its not exactly what is
needed here
ai says there is an option -falign-strings=4
so eventually maybe this can be used if it is working but some would
need to padd it with zeros too
char* tag1 = "some\0\0\0\0\0\0\0"
i dont know..it becomes to be messy sadly
> still conceptually its better just to compare pointers to literals
>
> and this above is only a aligned and padded type of string literal that
> is faster to strcpy..so i would need to rethink that and check how
> convenient it is in practice
>
> probably for some time i will ypu with this pointer comparison
> of normal strings and rutntime testing if it fails..then i will see how
> much convenient it is
Re: compare literals / if( "cat"== "dog") {}
Author: Johann 'Myrkraverk' Oskarsson
Date: Sun, 11 Oct 2026 10:38
Date: Sun, 11 Oct 2026 10:38
119 lines
5497 bytes
5497 bytes
On 10/11/2026 5:34 PM, fir wrote:
> Richard Harnden pisze:
>> On 11/10/2026 08:50, fir wrote:
>>> 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
>>>
>>
>> If you want to compare strings by their pointers, then those pointers
>> will need to come from a stable pool, like, say, a hash table.
>>
>> const char *hash_str_insert(hash *hash, const char *s);
>>
>> ...
>>
>> const char *a = hash_str_insert(hash, "cat");
>> const char *b = hash_str_insert(hash, "dog");
>> const char *c = hash_str_insert(hash, "cat");
>>
>> if ( a == b ) /* no */
>>
>> if ( a == c ) /* yes */
>>
>>
> it is an option, maybe not insert it but ancode it in some key
>
> unsigned key = encode("asdasdas");
If you go this route, you can always check out Mailund's book, /The
Joys of Hashing/, but he does not cover the actual hashing function,
so you'll have to find that elsewhere. Lookup3() and MurmurHash are
not bad choices. See
https://en.wikipedia.org/wiki/Non-cryptographic_hash_function
for a list.
--
Johann | email: invalid -> com | http://www.myrkraverk.com/blog/
I'm not from the Internet, I just work there. | via Easynews.com
https://bsky.app/profile/myrkraverk.bsky.social | for ( ;; ) _:;
Federated at https://fed.brid.gy/bsky/myrkraverk.bsky.social
Re: compare literals / if( "cat"== "dog") {}
Author: Stefan Ram
Date: Sun, 11 Oct 2026 10:43
Date: Sun, 11 Oct 2026 10:43
8 lines
2082 bytes
2082 bytes
Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> wrote or quoted: >If you go this route, you can always check out Mailund's book, /The >Joys of Hashing/, but he does not cover the actual hashing function, When the strings are known at compile time, one can also create perfect hash functions using tools like gperf.
Re: compare literals / if( "cat"== "dog") {}
Author: Johann 'Myrkraverk' Oskarsson
Date: Sun, 11 Oct 2026 10:46
Date: Sun, 11 Oct 2026 10:46
19 lines
2316 bytes
2316 bytes
On 10/11/2026 6:43 PM, Stefan Ram wrote: > Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> wrote or quoted: >> If you go this route, you can always check out Mailund's book, /The >> Joys of Hashing/, but he does not cover the actual hashing function, > > When the strings are known at compile time, one can also create > perfect hash functions using tools like gperf. I've been interested in perfect hashing too. Do you know a good refer- ence if I want to recreate gperf? If I want to recreate Lex, I can read [Holub], but if I want to recreate gperf, I don't know what to read. Best wishes, and happy perfect hashes! -- Johann | email: invalid -> com | http://www.myrkraverk.com/blog/ I'm not from the Internet, I just work there. | via Easynews.com https://bsky.app/profile/myrkraverk.bsky.social | for ( ;; ) _:; Federated at https://fed.brid.gy/bsky/myrkraverk.bsky.social
Re: compare literals / if( "cat"== "dog") {}
Author: Johann 'Myrkraverk' Oskarsson
Date: Sun, 11 Oct 2026 10:48
Date: Sun, 11 Oct 2026 10:48
26 lines
2631 bytes
2631 bytes
On 10/11/2026 6:46 PM, Johann 'Myrkraverk' Oskarsson wrote: > On 10/11/2026 6:43 PM, Stefan Ram wrote: >> Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> wrote or >> quoted: >>> If you go this route, you can always check out Mailund's book, /The >>> Joys of Hashing/, but he does not cover the actual hashing function, >> >> When the strings are known at compile time, one can also create >> perfect hash functions using tools like gperf. > > I've been interested in perfect hashing too. Do you know a good refer- > ence if I want to recreate gperf? If I want to recreate Lex, I can read > [Holub], but if I want to recreate gperf, I don't know what to read. > > > Best wishes, and happy perfect hashes! I spoke in haste. The GPerf manual has some of this covered, so I'll look through their bibliography at leisure and amusement. https://www.gnu.org/software/gperf/manual/gperf.html#Bibliography -- Johann | email: invalid -> com | http://www.myrkraverk.com/blog/ I'm not from the Internet, I just work there. | via Easynews.com https://bsky.app/profile/myrkraverk.bsky.social | for ( ;; ) _:; Federated at https://fed.brid.gy/bsky/myrkraverk.bsky.social
Re: compare literals / if( "cat"== "dog") {}
Author: fir
Date: Sun, 11 Oct 2026 10:54
Date: Sun, 11 Oct 2026 10:54
164 lines
6671 bytes
6671 bytes
Johann 'Myrkraverk' Oskarsson pisze:
> On 10/11/2026 5:34 PM, fir wrote:
>> Richard Harnden pisze:
>>> On 11/10/2026 08:50, fir wrote:
>>>> 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
>>>>
>>>
>>> If you want to compare strings by their pointers, then those pointers
>>> will need to come from a stable pool, like, say, a hash table.
>>>
>>> const char *hash_str_insert(hash *hash, const char *s);
>>>
>>> ...
>>>
>>> const char *a = hash_str_insert(hash, "cat");
>>> const char *b = hash_str_insert(hash, "dog");
>>> const char *c = hash_str_insert(hash, "cat");
>>>
>>> if ( a == b ) /* no */
>>>
>>> if ( a == c ) /* yes */
>>>
>>>
>> it is an option, maybe not insert it but ancode it in some key
>>
>> unsigned key = encode("asdasdas");
>
> If you go this route, you can always check out Mailund's book, /The
> Joys of Hashing/, but he does not cover the actual hashing function,
> so you'll have to find that elsewhere. Lookup3() and MurmurHash are
> not bad choices. See
>
this hashing seems not so bad but it has disadvantages
tags need to be totally adhoc and if i got
something
.type = encode("ship");
inn add functions than in run function i need
if(tab[i].type == encode("ship"));
then it is a bit costly
and if i want to precompute it becomes less adhoc
(maybe not fully like normal shit enum where you got ttally non local
shit king definition above both add() and run()..but here you
got this precomputation above the run..it can be in run
like
static k; if(!k)
{
k=1;
static int ship, bullet, smoke;
ship = encode("ship");
bullet = encode("bullet");
smoke= encode("smoke");
}
but its not exactly the purest version
the best version as i said is
1) builtin tags in languege
2) guaranteea same literals have same pointers
(is there a reason not to do that?)
3) increase those 'djhbsdjhb' literals to work at least 64 bits,
preferablu 128 bits
> https://en.wikipedia.org/wiki/Non-cryptographic_hash_function
>
> for a list.
Re: compare literals / if( "cat"== "dog") {}
Author: fir
Date: Sun, 11 Oct 2026 11:00
Date: Sun, 11 Oct 2026 11:00
80 lines
3992 bytes
3992 bytes
fir pisze:
> fir pisze:
>> fir pisze:
>>> 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];
>>> }
>>
>>
>
>
> with this abowe it also may be some problem as i think if i would
> write
>
> tag x = "aadadda";
>
> then im not sure if this addres of x and the adres of this "aadadda"
> constant is the same? i fact there should be not definition of type
> but adding to literals they are aligned and zero padded
>
> like
>
> (align 4, zero pad up to 20) "aadadda"
>
> and im not sure if something like that is in c here
>
>
> amd if i must create each instance of tag based on soame literal
> it would have copies by definition i think..so its not exactly what is
> needed here
>
> ai says there is an option -falign-strings=4
>
on my old version of gcc this option seem not work anyway
as i understand hovever compiler in fact is free to align string
literals to 4 and padd the to large numeber of zeros at end so
maybe sometimes he can do it and then just strcmp uses those
int comparisont to work faster..i dont know
> so eventually maybe this can be used if it is working but some would
> need to padd it with zeros too
>
> char* tag1 = "some\0\0\0\0\0\0\0"
>
>
> i dont know..it becomes to be messy sadly
>
>
>
>> still conceptually its better just to compare pointers to literals
>>
>> and this above is only a aligned and padded type of string literal
>> that is faster to strcpy..so i would need to rethink that and check how
>> convenient it is in practice
>>
>> probably for some time i will ypu with this pointer comparison
>> of normal strings and rutntime testing if it fails..then i will see
>> how much convenient it is
>
Re: compare literals / if( "cat"== "dog") {}
Author: fir
Date: Sun, 11 Oct 2026 11:13
Date: Sun, 11 Oct 2026 11:13
65 lines
3166 bytes
3166 bytes
fir pisze:
>
> this hashing seems not so bad but it has disadvantages
>
> tags need to be totally adhoc and if i got
> something
>
> .type = encode("ship");
>
> inn add functions than in run function i need
>
>
> if(tab[i].type == encode("ship"));
>
> then it is a bit costly
>
> and if i want to precompute it becomes less adhoc
> (maybe not fully like normal shit enum where you got ttally non local
> shit king definition above both add() and run()..but here you
> got this precomputation above the run..it can be in run
>
> like
>
> static k; if(!k)
> {
> k=1;
>
> static int ship, bullet, smoke;
> ship = encode("ship");
> bullet = encode("bullet");
> smoke= encode("smoke");
>
> }
>
> but its not exactly the purest version
>
>
> the best version as i said is
>
> 1) builtin tags in languege
> 2) guaranteea same literals have same pointers
> (is there a reason not to do that?)
> 3) increase those 'djhbsdjhb' literals to work at least 64 bits,
> preferablu 128 bits
possible other option is maybe define int somewhere above the
add function
int tag_ship;
and in run type
extern tag_ship;
even not caring by value ust compare pointers
advantage is extern can be typed locally in function
distadvantage the definition as far as i know cant
it seems mpre adhoc than shit enum - which as far as i know cant
be defined in parts (in variosy places of code) but its not quite adhoc
just more than enum (iinw)
Re: compare literals / if( "cat"== "dog") {}
Author: MitchAlsup
Date: Sun, 11 Oct 2026 18:28
Date: Sun, 11 Oct 2026 18:28
20 lines
1402 bytes
1402 bytes
fir <profesor.fir@gmail.com> posted:
-------------------
>
> if("cat" == "cat") will return true and
> if("cat" == "dog") will return false
"cat" in C means a pointer p points at {'c' 'a' 't' 00} in memory; so,
if you want character comparison you want:
if( strcmp( "cat", "dog" ) == 0 ) //cat = dog
And that one will never have the flow of control enter the then
clause; whereas:
if( strcmp( "cat", "cat" ) == 0 ) //cat = cat
and you will always enter the then clause, regardless of whether
the compiler uses the same literal twice or not.
Re: compare literals / if( "cat"== "dog") {}
Author: MitchAlsup
Date: Sun, 11 Oct 2026 19:30
Date: Sun, 11 Oct 2026 19:30
33 lines
1725 bytes
1725 bytes
MitchAlsup <user5857@newsgrouper.org.invalid> posted:
>
> fir <profesor.fir@gmail.com> posted:
> -------------------
> >
> > if("cat" == "cat") will return true and
> > if("cat" == "dog") will return false
>
> "cat" in C means a pointer p points at {'c' 'a' 't' 00} in memory; so,
> if you want character comparison you want:
>
> if( strcmp( "cat", "dog" ) == 0 ) //cat = dog
>
> And that one will never have the flow of control enter the then
> clause; whereas:
>
> if( strcmp( "cat", "cat" ) == 0 ) //cat = cat
>
> and you will always enter the then clause, regardless of whether
> the compiler uses the same literal twice or not.
>
One can also add:
# define areEQUAL == 0
# define areINEQUAL != 0
# define areLESS < 0
# define areGREATER > 0
So you can write:
if( strcmp( "cat", "dog" ) areEQUAL )
Re: compare literals / if( "cat"== "dog") {}
Author: Kenny McCormack
Date: Sun, 11 Oct 2026 20:28
Date: Sun, 11 Oct 2026 20:28
22 lines
1667 bytes
1667 bytes
In article <1791743331-5857@newsgrouper.org>,
MitchAlsup <user5857@newsgrouper.org.invalid> wrote:
>
>fir <profesor.fir@gmail.com> posted:
>-------------------
>>
>> if("cat" == "cat") will return true and
>> if("cat" == "dog") will return false
>
>"cat" in C means a pointer p points at {'c' 'a' 't' 00} in memory; so,
>if you want character comparison you want:
>
> if( strcmp( "cat", "dog" ) == 0 ) //cat = dog
That's a different thing. That's not what we are talking about in this
thread.
Suggest you start a new thread to discuss the "strcmp" function.
--
It's possible that leasing office space to a Starbucks is a greater liability
in today's GOP than is hitting your mother on the head with a hammer.
Re: compare literals / if( "cat"== "dog") {}
Author: Lawrence D’Oliveiro
Date: Sun, 11 Oct 2026 21:05
Date: Sun, 11 Oct 2026 21:05
28 lines
2398 bytes
2398 bytes
On Sun, 11 Oct 2026 18:28:51 GMT, MitchAlsup wrote:
> "cat" in C means a pointer p points at {'c' 'a' 't' 00} in memory;
> so, if you want character comparison you want:
>
> if( strcmp( "cat", "dog" ) == 0 ) //cat = dog
>
> And that one will never have the flow of control enter the then
> clause; whereas:
>
> if( strcmp( "cat", "cat" ) == 0 ) //cat = cat
>
> and you will always enter the then clause, regardless of whether the
> compiler uses the same literal twice or not.
In C++ and Python, you can overload the “==” operator to define
equality as whatever makes sense for your custom object type. In C,
you can’t.
Java tried to copy the good parts from C++, but unfortunately one of
the things they left out was custom operator overloads. So that
suffers from the same problem with string comparisons as C does. Their
workaround is to introduce the concept of “interned” strings. But
then, there is no protection against accidentally mixing interned and
non-interned strings, leading to comparisons that work the way you
expect one time, but unexpectedly fail another time.
(Did someone say “worst of both worlds”?)
Re: compare literals / if( "cat"== "dog") {}
Author: Keith Thompson
Date: Sun, 11 Oct 2026 22:47
Date: Sun, 11 Oct 2026 22:47
26 lines
2223 bytes
2223 bytes
MitchAlsup <user5857@newsgrouper.org.invalid> writes:
> fir <profesor.fir@gmail.com> posted:
> -------------------
>>
>> if("cat" == "cat") will return true and
>> if("cat" == "dog") will return false
>
> "cat" in C means a pointer p points at {'c' 'a' 't' 00} in memory; so,
> if you want character comparison you want:
>
> if( strcmp( "cat", "dog" ) == 0 ) //cat = dog
[...]
Not quite. "cat" is of type char[4]. Like any expression of array
type, it is implicitly converted ("adjusted" might be more accurate)
to a pointer to the array object's first element in some but not
all contexts. This is why sizeof "cat" == 4 (regardless of the
size of a pointer), and &"cat" is of type char(*)[4] (pointer to
array of 4 chars), not char**.
But yes, in the context of ("cat" == "dog"), the array-to-pointer
conversion is applied and values of type char* are compared.
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */
Re: compare literals / if( "cat"== "dog") {}
Author: Keith Thompson
Date: Sun, 11 Oct 2026 22:50
Date: Sun, 11 Oct 2026 22:50
21 lines
1840 bytes
1840 bytes
MitchAlsup <user5857@newsgrouper.org.invalid> writes:
[...]
> One can also add:
>
> # define areEQUAL == 0
> # define areINEQUAL != 0
> # define areLESS < 0
> # define areGREATER > 0
>
> So you can write:
>
> if( strcmp( "cat", "dog" ) areEQUAL )
There's a proposal to add streq() to C2y, so streq(a, b) is equivalent
to (strcmp(a, b) == 0). I find that clearer than your areEQUAL macro.
https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3567.pdf
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */
Re: compare literals / if( "cat"== "dog") {}
Author: Lew Pitcher
Date: Sun, 11 Oct 2026 23:08
Date: Sun, 11 Oct 2026 23:08
31 lines
2332 bytes
2332 bytes
I don't see fir's posts, so please pardon my replying to fir
in this manner
On Sun, 11 Oct 2026 18:28:51 +0000, MitchAlsup wrote:
> fir <profesor.fir@gmail.com> posted:
> -------------------
>>
>> if("cat" == "cat") will return true and
Not necessarily. In reference to string literals (the "cat" and "cat" above),
the C standard notes that "it is unspecified whether these arrays are distinct
provided their elements have the appropriate values". (ISO/IEC 9899:2024 N3220
Working Draft, 6.4.5 String literals, point #7).
It is up to the compiler to determine whether it will store the second "cat"
string as a separate string from the first "cat" string. If it stores them
both in the same string, then ("cat" == "cat") will be true. However, if
it stores the second "cat" string literal separately from the first "cat"
string literal, then ("cat" == "cat") will be false.
>> if("cat" == "dog") will return false
[snip proper way to compare strings]
--
Lew Pitcher
"In Skills We Trust"
Not LLM output - I'm just like this.
Re: compare literals / if( "cat"== "dog") {}
Author: Keith Thompson
Date: Sun, 11 Oct 2026 23:38
Date: Sun, 11 Oct 2026 23:38
24 lines
2159 bytes
2159 bytes
Lew Pitcher <lew.pitcher@digitalfreehold.ca> writes:
> I don't see fir's posts, so please pardon my replying to fir
> in this manner
>
> On Sun, 11 Oct 2026 18:28:51 +0000, MitchAlsup wrote:
>
>> fir <profesor.fir@gmail.com> posted:
>> -------------------
>>>
>>> if("cat" == "cat") will return true and
>
> Not necessarily. In reference to string literals (the "cat" and "cat" above),
> the C standard notes that "it is unspecified whether these arrays are distinct
> provided their elements have the appropriate values". (ISO/IEC 9899:2024 N3220
> Working Draft, 6.4.5 String literals, point #7).
[...]
I don't believe fir was asserting that it currently works that way in C.
He was asking whether it can be made to work that way. That context
was omitted in the followup.
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */
Re: compare literals / if( "cat"== "dog") {}
Author: tTh
Date: Mon, 12 Oct 2026 00:31
Date: Mon, 12 Oct 2026 00:31
15 lines
1803 bytes
1803 bytes
On 10/12/26 00:50, Keith Thompson wrote:
> There's a proposal to add streq() to C2y, so streq(a, b) is equivalent
> to (strcmp(a, b) == 0). I find that clearer than your areEQUAL macro.
>
> https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3567.pdf
[like]
--
** **
* tTh des Bourtoulots *
* http://maison.tth.netlib.re/ *
** **
Re: compare literals / if( "cat"== "dog") {}
Author: fir
Date: Mon, 12 Oct 2026 12:40
Date: Mon, 12 Oct 2026 12:40
39 lines
2866 bytes
2866 bytes
Keith Thompson pisze:
> Lew Pitcher <lew.pitcher@digitalfreehold.ca> writes:
>> I don't see fir's posts, so please pardon my replying to fir
>> in this manner
>>
>> On Sun, 11 Oct 2026 18:28:51 +0000, MitchAlsup wrote:
>>
>>> fir <profesor.fir@gmail.com> posted:
>>> -------------------
>>>>
>>>> if("cat" == "cat") will return true and
>>
>> Not necessarily. In reference to string literals (the "cat" and "cat" above),
>> the C standard notes that "it is unspecified whether these arrays are distinct
>> provided their elements have the appropriate values". (ISO/IEC 9899:2024 N3220
>> Working Draft, 6.4.5 String literals, point #7).
> [...]
>
> I don't believe fir was asserting that it currently works that way in C.
> He was asking whether it can be made to work that way. That context
> was omitted in the followup.
>
in to ways:
1) if i can mady my code in c work this way
(thus using string literals as ad-hoc tags, where i could compare them
as 1-1 pointers)
2) if something is a reason not to introduce it in a standard;
"samestring literals have same pointers" (which would be handy)
if some really need those strings have different pointers (for some
reason? like what - making literals lyiong in memory and feeling it in
order or what?) what is that reason (if someone woul very like need it
it coud ad some ;keyword' or pragma directive or something ('makw unique
storage')
Re: compare literals / if( "cat"== "dog") {}
Author: Rosario19
Date: Thu, 15 Oct 2026 09:36
Date: Thu, 15 Oct 2026 09:36
37 lines
2367 bytes
2367 bytes
On Sat, 10 Oct 2026 23:02:54 +0200, fir
>have asked for that but i got somewhat holes im my memory and i
>dont exactly remember sometimes on which i wrote and on which not
>
>the problem is i need this adhos enums strongly and really, but those
>'smth' are to shore i could use two of them f('some','thin') but it gets
>uglier and such code is like degraded
>
>in fact there could be quite easy solution - it is use string literals
>as such tags - i mean you dont need to use strcomp , you just need to
>have same string literals have same pointer then
>
>if("cat" == "cat") will return true and
>if("cat" == "dog") will return false
I say if int is 4 chars this should return 1 in case of fist line
0 in case the second line
if((*(int*)"cat") == (*(int*)"cat")) return 1
if((*(int*)"cat") == (*(int*)"dog") ) return 0
otherwise there is a need one function that from the pointer check
strings are ugual as strcmp
I think can be too
strcpy(char* a, char* b)
{
if(a==b) return 0;
}
but who is sure a and b point a to 2 strings?
>i probably asked that but if so once again
>
>is there way to enforce it to work?
Re: compare literals / if( "cat"== "dog") {}
Author: Janis Papanagnou
Date: Thu, 15 Oct 2026 10:09
Date: Thu, 15 Oct 2026 10:09
46 lines
2783 bytes
2783 bytes
On 2026-10-15 11:36, Rosario19 wrote:
> On Sat, 10 Oct 2026 23:02:54 +0200, fir
>> have asked for that but i got somewhat holes im my memory and i
>> dont exactly remember sometimes on which i wrote and on which not
>>
>> the problem is i need this adhos enums strongly and really, but those
>> 'smth' are to shore i could use two of them f('some','thin') but it gets
>> uglier and such code is like degraded
>>
>> in fact there could be quite easy solution - it is use string literals
>> as such tags - i mean you dont need to use strcomp , you just need to
>> have same string literals have same pointer then
>>
>> if("cat" == "cat") will return true and
>> if("cat" == "dog") will return false
>
> I say if int is 4 chars this should return 1 in case of fist line
> 0 in case the second line
>
> if((*(int*)"cat") == (*(int*)"cat")) return 1
> if((*(int*)"cat") == (*(int*)"dog") ) return 0
You are seriously suggesting that the semantics of "C" should
depend on the actual string-length at runtime, that it should
not have an unambiguous uniform semantics in the first place?
Janis
>
> otherwise there is a need one function that from the pointer check
> strings are ugual as strcmp
>
> I think can be too
> strcpy(char* a, char* b)
> {
> if(a==b) return 0;
>
> }
>
> but who is sure a and b point a to 2 strings?
>
>> i probably asked that but if so once again
>>
>> is there way to enforce it to work?
>
Re: compare literals / if( "cat"== "dog") {}
Author: Rosario19
Date: Fri, 16 Oct 2026 21:25
Date: Fri, 16 Oct 2026 21:25
32 lines
2569 bytes
2569 bytes
On Thu, 15 Oct 2026 12:09:06 +0200, Janis Papanagnou wrote:
>On 2026-10-15 11:36, Rosario19 wrote:
>> On Sat, 10 Oct 2026 23:02:54 +0200, fir
>>> have asked for that but i got somewhat holes im my memory and i
>>> dont exactly remember sometimes on which i wrote and on which not
>>>
>>> the problem is i need this adhos enums strongly and really, but those
>>> 'smth' are to shore i could use two of them f('some','thin') but it gets
>>> uglier and such code is like degraded
>>>
>>> in fact there could be quite easy solution - it is use string literals
>>> as such tags - i mean you dont need to use strcomp , you just need to
>>> have same string literals have same pointer then
>>>
>>> if("cat" == "cat") will return true and
>>> if("cat" == "dog") will return false
>>
>> I say if int is 4 chars this should return 1 in case of fist line
>> 0 in case the second line
>>
>> if((*(int*)"cat") == (*(int*)"cat")) return 1
>> if((*(int*)"cat") == (*(int*)"dog") ) return 0
>
>You are seriously suggesting that the semantics of "C" should
>depend on the actual string-length at runtime, that it should
>not have an unambiguous uniform semantics in the first place?
>
>Janis
no, but see x86 assembly result of above in my pc can do that as
aspected
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads