Sujet : Re: Interval Comparisons
De : bc (at) *nospam* freeuk.com (bart)
Groupes : comp.lang.cDate : 04. Jun 2024, 17:58:43
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v3ndji$fv12$1@dont-email.me>
References : 1 2 3 4 5 6 7
User-Agent : Mozilla Thunderbird
On 04/06/2024 16:27, Scott Lurndal wrote:
David Brown <david.brown@hesbynett.no> writes:
On 04/06/2024 13:23, bart wrote:
It is incredibly useful:
>
if c in [' ', '\t', '\n'] then ... # whitespace
if (strpbrk(c, " \t\n") != NULL) it_is_whitespace.
That doesn't do the same thing. In my example, c is a character, not a string.
To achieve the same thing using strpbrk requires code like this:
char c[2];
c[0]=rand()&255; // Create a string
c[1]=0;
if (strpbrk(c, " \t\n") != NULL) puts("whitespace");
If I compile this with gcc -O3, then the checking part is this:
lea rcx, 46[rsp]
mov BYTE PTR 47[rsp], 0
lea rdx, .LC0[rip]
mov BYTE PTR 46[rsp], al
call strpbrk // CALL TO LIBRARY FUNCTION
test rax, rax
je .L2
lea rcx, .LC1[rip]
call puts
I don't know what it gets up to inside strprbk. If I write this in my language:
if c in [9,10,32] then
puts("whitespace")
fi
The generated code is this (using alternate register names, D0 = rax):
mov D0, D3 # (could have tested D3 (= c) directly.)
cmp D0, 9
jz L4
cmp D0, 10
jz L4
cmp D0, 32
jnz L3
L4:
lea D10, [L5]
call puts*
L3:
Anyway, the construct is not limited to character codes that can be contained within a string. It works for 64-bit values which can include 0. And it could be extended to other scalar types like floats and pointers.