Path: ...!weretis.net!feeder8.news.weretis.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: bart Newsgroups: comp.lang.c Subject: Re: Interval Comparisons Date: Tue, 4 Jun 2024 16:58:43 +0100 Organization: A noiseless patient Spider Lines: 60 Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Tue, 04 Jun 2024 17:58:43 +0200 (CEST) Injection-Info: dont-email.me; posting-host="027492d54fa7a72a30fd705d730c9473"; logging-data="523298"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/vhBd6C3Rh7ZHCY9Xf4Wfd" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:NC0W6qAYNaWwixvfqIJZCWk6h08= Content-Language: en-GB In-Reply-To: Bytes: 2751 On 04/06/2024 16:27, Scott Lurndal wrote: > David Brown 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.