Re: Interval Comparisons

Liste des GroupesRevenir à l c 
Sujet : Re: Interval Comparisons
De : bc (at) *nospam* freeuk.com (bart)
Groupes : comp.lang.c
Date : 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.

Date Sujet#  Auteur
4 Jun 24 * Interval Comparisons44Lawrence D'Oliveiro
4 Jun 24 +* Re: Interval Comparisons14David Brown
4 Jun 24 i`* Re: Interval Comparisons13Mikko
4 Jun 24 i +* Re: Interval Comparisons10David Brown
4 Jun 24 i i+* Re: Interval Comparisons8bart
4 Jun 24 i ii+* Re: Interval Comparisons6David Brown
4 Jun 24 i iii+* Re: Interval Comparisons2bart
4 Jun 24 i iiii`- Re: Interval Comparisons1David Brown
4 Jun 24 i iii`* Re: Interval Comparisons3bart
4 Jun 24 i iii `* Re: Interval Comparisons2Michael S
4 Jun 24 i iii  `- Re: Interval Comparisons1bart
5 Jun 24 i ii`- Re: Interval Comparisons1Lawrence D'Oliveiro
4 Jun 24 i i`- Re: Interval Comparisons1Mikko
4 Jun 24 i +- Re: Interval Comparisons1Janis Papanagnou
4 Jun 24 i `- Re: Interval Comparisons1Keith Thompson
4 Jun 24 +- Re: Interval Comparisons1bart
4 Jun 24 +* Re: Interval Comparisons3Thiago Adams
4 Jun 24 i+- Re: Interval Comparisons1Bonita Montero
5 Jun 24 i`- Re: Interval Comparisons1Keith Thompson
4 Jun 24 `* Re: Interval Comparisons25Blue-Maned_Hawk
4 Jun 24  +- Re: Interval Comparisons1Michael S
5 Jun 24  `* Re: Interval Comparisons23Lawrence D'Oliveiro
5 Jun 24   `* Re: Interval Comparisons22bart
5 Jun 24    `* Re: Interval Comparisons21Lawrence D'Oliveiro
6 Jun 24     `* Re: Interval Comparisons20bart
7 Jun 24      `* Re: Interval Comparisons19Lawrence D'Oliveiro
7 Jun 24       `* Re: Interval Comparisons18bart
7 Jun 24        `* Re: Interval Comparisons17Lawrence D'Oliveiro
7 Jun 24         `* Re: Interval Comparisons16Keith Thompson
7 Jun 24          +- Re: Interval Comparisons1Lawrence D'Oliveiro
7 Jun 24          `* Re: Interval Comparisons14David Brown
7 Jun 24           +* Re: Interval Comparisons4Keith Thompson
7 Jun 24           i`* Re: Interval Comparisons3David Brown
7 Jun 24           i `* Re: Interval Comparisons2Keith Thompson
8 Jun 24           i  `- Re: Interval Comparisons1David Brown
7 Jun 24           +* Re: Interval Comparisons8bart
7 Jun 24           i+* Re: Interval Comparisons2Lawrence D'Oliveiro
7 Jun 24           ii`- Re: Interval Comparisons1Michael S
7 Jun 24           i`* Re: Interval Comparisons5David Brown
7 Jun 24           i `* Re: Interval Comparisons4bart
9 Jun 24           i  `* Re: Interval Comparisons3David Brown
10 Jun 24           i   `* Re: Interval Comparisons2bart
10 Jun 24           i    `- Re: Interval Comparisons1David Brown
7 Jun 24           `- Re: Interval Comparisons1Lawrence D'Oliveiro

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal