Re: Command line globber/tokenizer library for C?

Liste des GroupesRevenir à l c 
Sujet : Re: Command line globber/tokenizer library for C?
De : bc (at) *nospam* freeuk.com (Bart)
Groupes : comp.lang.c
Date : 18. Sep 2024, 02:07:17
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vcd5jk$3oe5h$1@dont-email.me>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
User-Agent : Mozilla Thunderbird
On 18/09/2024 00:46, Michael S wrote:
On Tue, 17 Sep 2024 22:34:33 -0000 (UTC)
antispam@fricas.org wrote:
 
Michael S <already5chosen@yahoo.com> wrote:
On Fri, 13 Sep 2024 09:05:04 -0700
Tim Rentsch <tr.17687@z991.linuxsc.com> wrote:
  
Michael S <already5chosen@yahoo.com> writes:
>
[..iterate over words in a string..]
>
I couldn't resist writing some code along similar lines.  The
entry point is words_do(), which returns one on success and
zero if the end of string is reached inside double quotes.
>
>
typedef struct gopher_s *Gopher;
struct gopher_s { void (*f)( Gopher, const char *, const char * );
};
>
static  _Bool   collect_word( const char *, const char *, _Bool,
Gopher ); static  _Bool   is_space( char );
>
>
_Bool
words_do( const char *s, Gopher go ){
   char   c      =  *s;
>
     return
       is_space(c)       ?  words_do( s+1, go )
   : c                 ?  collect_word( s, s, 1, go )
: /***************/    1;
}
>
_Bool
collect_word( const char *s, const char *r, _Bool w, Gopher go ){
   char   c      =  *s;
>
     return
       c == 0            ?  go->f( go, r, s ),  w
   : is_space(c) && w  ?  go->f( go, r, s ),  words_do( s, go )
: /***************/    collect_word( s+1, r, w ^ c == '"', go );
}
>
_Bool
is_space( char c ){
     return  c == ' '  ||  c == '\t';
}
>
 
>
<snip>
 
Tested on godbolt.
gcc -O2 turns it into iteration starting from v.4.4
clang -O2 turns it into iteration starting from v.4.0
Latest icc still does not turn it into iteration at least along one
code paths.
Latest MSVC implements it as written, 100% recursion.
>
I tested using gcc 12.  AFAICS calls to 'go->f' in 'collect_word'
are not tail calls and gcc 12 compiles them as normal call.
 Naturally.
 
The other calls are compiled to jumps.  But call to 'collect_word'
in 'words_do' is not "sibicall" and dependig in calling convention
compiler may treat it narmal call.  Two other calls, that is
call to 'words_do' in 'words_do' and call to 'collect_word' in
'collect_word' are clearly tail self recursion and compiler
should always optimize them to a jump.
>
 "Should" or not, MSVC does not eliminate them.
 The funny thing is that it does eliminate all four calls after I rewrote
the code in more boring style.

static
_Bool
collect_word( const char *s, const char *r, _Bool w, Gopher go ){
   char   c      =  *s;
#if 1
   if (c == 0) {
     go->f( go, r, s );
     return  w;
   }
   if (is_space(c) && w) {
     go->f( go, r, s );
     return words_do( s, go );
   }
   return collect_word( s+1, r, w ^ c == '"', go );
#else
  return
   c == 0            ?  go->f( go, r, s ),  w                 :
   is_space(c) && w  ?  go->f( go, r, s ),  words_do( s, go ) :
   /***************/    collect_word( s+1, r, w ^ c == '"', go );
#endif
}
I find such a coding style pretty much impossible to grasp and unpleasant to look at. I had to refactor it like this:
---------------
static_Bool collect_word(char *s, char *r, _Bool w, Gopher go ) {
     char c = *s;
     #if 1
         if (c == 0) {
             go->f(go, r, s);
             return  w;
         }
         if (is_space(c) && w) {
             go->f(go, r, s);
             return words_do(s, go);
         }
         return collect_word(s+1, r, (w ^ c) == '"', go);
     #else
         if (c == 0) {
             go->f(go, r, s);
             return w;
         }
         else if (is_space(c) && w) {
             go->f(go, r, s);
             return words_do(s, go);
         }
         else {
             return collect_word(s+1, r, (w ^ c) = '"', go);
         }
     #endif
}
---------------
When I'd finished, I realised that those two conditional blocks do more or less the same thing! If that's what you mean by 'boring', then I'll all for it.

Date Sujet#  Auteur
10 Sep 24 * Command line globber/tokenizer library for C?110ted@loft.tnolan.com (Ted Nolan
10 Sep 24 +* Re: Command line globber/tokenizer library for C?2Lawrence D'Oliveiro
10 Sep 24 i`- Re: Command line globber/tokenizer library for C?1Keith Thompson
10 Sep 24 +* Re: Command line globber/tokenizer library for C?2Janis Papanagnou
11 Sep 24 i`- Re: Command line globber/tokenizer library for C?1ted@loft.tnolan.com (Ted Nolan
10 Sep 24 +* Re: Command line globber/tokenizer library for C?2Keith Thompson
11 Sep 24 i`- Re: Command line globber/tokenizer library for C?1ted@loft.tnolan.com (Ted Nolan
11 Sep 24 +* Re: Command line globber/tokenizer library for C?2Kenny McCormack
11 Sep 24 i`- Re: Command line globber/tokenizer library for C?1ted@loft.tnolan.com (Ted Nolan
11 Sep 24 +* Re: Command line globber/tokenizer library for C?97Bonita Montero
11 Sep 24 i+* Re: Command line globber/tokenizer library for C?3ted@loft.tnolan.com (Ted Nolan
11 Sep 24 ii`* Re: Command line globber/tokenizer library for C?2Bonita Montero
11 Sep 24 ii `- Re: Command line globber/tokenizer library for C?1ted@loft.tnolan.com (Ted Nolan
11 Sep 24 i+- Re: Command line globber/tokenizer library for C?1Bart
11 Sep 24 i`* Re: Command line globber/tokenizer library for C?92Kenny McCormack
11 Sep 24 i `* Re: Command line globber/tokenizer library for C?91Bonita Montero
11 Sep 24 i  +- Re: Command line globber/tokenizer library for C?1Kenny McCormack
11 Sep 24 i  +* Re: Command line globber/tokenizer library for C?13ted@loft.tnolan.com (Ted Nolan
11 Sep 24 i  i`* Re: Command line globber/tokenizer library for C?12Keith Thompson
12 Sep 24 i  i `* Re: Command line globber/tokenizer library for C?11ted@loft.tnolan.com (Ted Nolan
12 Sep 24 i  i  +* Re: Command line globber/tokenizer library for C?8Keith Thompson
12 Sep 24 i  i  i`* Re: Command line globber/tokenizer library for C?7ted@loft.tnolan.com (Ted Nolan
12 Sep 24 i  i  i `* Re: Command line globber/tokenizer library for C?6Kenny McCormack
12 Sep 24 i  i  i  `* Re: Command line globber/tokenizer library for C?5ted@loft.tnolan.com (Ted Nolan
12 Sep 24 i  i  i   `* Columbia (Was: Command line globber/tokenizer library for C?)4Kenny McCormack
12 Sep 24 i  i  i    `* Re: Columbia (Was: Command line globber/tokenizer library for C?)3Michael S
12 Sep 24 i  i  i     +- Re: Columbia (Was: Command line globber/tokenizer library for C?)1David Brown
13 Sep 24 i  i  i     `- Re: Columbia (Was: Command line globber/tokenizer library for C?)1Lawrence D'Oliveiro
12 Sep 24 i  i  +- Re: Command line globber/tokenizer library for C?1Lawrence D'Oliveiro
12 Sep 24 i  i  `- Re: Command line globber/tokenizer library for C?1Ben Bacarisse
11 Sep 24 i  `* Re: Command line globber/tokenizer library for C?76Bart
12 Sep 24 i   `* Re: Command line globber/tokenizer library for C?75Bonita Montero
12 Sep 24 i    `* Re: Command line globber/tokenizer library for C?74Bart
12 Sep 24 i     +* Re: Command line globber/tokenizer library for C?19Kenny McCormack
12 Sep 24 i     i+* Re: Command line globber/tokenizer library for C?17Janis Papanagnou
12 Sep 24 i     ii`* Other programming languages (Was: Command line globber/tokenizer library for C?)16Kenny McCormack
12 Sep 24 i     ii `* Re: Other programming languages (Was: Command line globber/tokenizer library for C?)15Bonita Montero
12 Sep 24 i     ii  `* Re: Other programming languages (Was: Command line globber/tokenizer library for C?)14Kenny McCormack
12 Sep 24 i     ii   `* Re: Other programming languages (Was: Command line globber/tokenizer library for C?)13Bonita Montero
12 Sep 24 i     ii    `* Re: Other programming languages (Was: Command line globber/tokenizer library for C?)12Janis Papanagnou
12 Sep 24 i     ii     +* Re: Other programming languages (Was: Command line globber/tokenizer library for C?)2Bonita Montero
12 Sep 24 i     ii     i`- Re: Other programming languages (Was: Command line globber/tokenizer library for C?)1Janis Papanagnou
13 Sep 24 i     ii     `* Re: Other programming languages (Was: Command line globber/tokenizer library for C?)9Lawrence D'Oliveiro
13 Sep 24 i     ii      +* Re: Other programming languages (Was: Command line globber/tokenizer library for C?)3James Kuyper
13 Sep 24 i     ii      i`* Re: Other programming languages (Was: Command line globber/tokenizer library for C?)2Lawrence D'Oliveiro
13 Sep 24 i     ii      i `- Re: Other programming languages (Was: Command line globber/tokenizer library for C?)1Michael S
13 Sep 24 i     ii      +* Re: Other programming languages (Was: Command line globber/tokenizer library for C?)4Janis Papanagnou
13 Sep 24 i     ii      i+* Re: Other programming languages (Was: Command line globber/tokenizer library for C?)2Lawrence D'Oliveiro
13 Sep 24 i     ii      ii`- Re: Other programming languages (Was: Command line globber/tokenizer library for C?)1Janis Papanagnou
13 Sep 24 i     ii      i`- Re: Other programming languages (Was: Command line globber/tokenizer library for C?)1Kaz Kylheku
13 Sep 24 i     ii      `- Re: Other programming languages (Was: Command line globber/tokenizer library for C?)1Kaz Kylheku
12 Sep 24 i     i`- Re: Command line globber/tokenizer library for C?1Bonita Montero
12 Sep 24 i     +* Re: Command line globber/tokenizer library for C?53Janis Papanagnou
12 Sep 24 i     i+* Re: Command line globber/tokenizer library for C?38Bart
12 Sep 24 i     ii+* Re: Command line globber/tokenizer library for C?5Bart
12 Sep 24 i     iii+- Re: Command line globber/tokenizer library for C?1Bonita Montero
12 Sep 24 i     iii`* Re: Command line globber/tokenizer library for C?3Janis Papanagnou
12 Sep 24 i     iii `* Re: Command line globber/tokenizer library for C?2Bart
12 Sep 24 i     iii  `- Re: Command line globber/tokenizer library for C?1Bonita Montero
12 Sep 24 i     ii`* Re: Command line globber/tokenizer library for C?32Michael S
12 Sep 24 i     ii +- Re: Command line globber/tokenizer library for C?1Michael S
12 Sep 24 i     ii `* Re: Command line globber/tokenizer library for C?30Bart
12 Sep 24 i     ii  `* Re: Command line globber/tokenizer library for C?29Bonita Montero
12 Sep 24 i     ii   +- Re: Command line globber/tokenizer library for C?1Kenny McCormack
12 Sep 24 i     ii   `* Re: Command line globber/tokenizer library for C?27Michael S
13 Sep 24 i     ii    +* Re: Command line globber/tokenizer library for C?8Bonita Montero
13 Sep 24 i     ii    i`* Re: Command line globber/tokenizer library for C?7Michael S
13 Sep 24 i     ii    i `* Re: Command line globber/tokenizer library for C?6Bonita Montero
13 Sep 24 i     ii    i  +* Re: Command line globber/tokenizer library for C?2Michael S
13 Sep 24 i     ii    i  i`- Re: Command line globber/tokenizer library for C?1Bonita Montero
14 Sep 24 i     ii    i  `* Re: Command line globber/tokenizer library for C?3Lawrence D'Oliveiro
14 Sep 24 i     ii    i   `* Re: Command line globber/tokenizer library for C?2Bonita Montero
14 Sep 24 i     ii    i    `- Re: Command line globber/tokenizer library for C?1Lawrence D'Oliveiro
13 Sep 24 i     ii    `* Re: Command line globber/tokenizer library for C?18Tim Rentsch
15 Sep11:22 i     ii     `* Re: Command line globber/tokenizer library for C?17Michael S
16 Sep09:52 i     ii      +* Re: Command line globber/tokenizer library for C?3Tim Rentsch
16 Sep11:23 i     ii      i`* Re: Command line globber/tokenizer library for C?2Michael S
17 Sep12:12 i     ii      i `- Re: Command line globber/tokenizer library for C?1Tim Rentsch
18 Sep00:34 i     ii      `* Re: Command line globber/tokenizer library for C?13antispam
18 Sep01:33 i     ii       +- Re: Command line globber/tokenizer library for C?1Tim Rentsch
18 Sep01:46 i     ii       `* Re: Command line globber/tokenizer library for C?11Michael S
18 Sep02:07 i     ii        +* Re: Command line globber/tokenizer library for C?5Bart
18 Sep10:43 i     ii        i`* Re: Command line globber/tokenizer library for C?4Michael S
18 Sep11:49 i     ii        i +* Re: Command line globber/tokenizer library for C?2Bart
18 Sep12:44 i     ii        i i`- Re: Command line globber/tokenizer library for C?1David Brown
18 Sep15:01 i     ii        i `- Re: Command line globber/tokenizer library for C?1Tim Rentsch
18 Sep03:31 i     ii        +* Re: Command line globber/tokenizer library for C?3Tim Rentsch
18 Sep10:03 i     ii        i`* Re: Command line globber/tokenizer library for C?2Michael S
18 Sep14:09 i     ii        i `- Re: Command line globber/tokenizer library for C?1Tim Rentsch
18 Sep04:20 i     ii        `* Re: Command line globber/tokenizer library for C?2Lawrence D'Oliveiro
18 Sep10:05 i     ii         `- Re: Command line globber/tokenizer library for C?1Michael S
12 Sep 24 i     i`* Re: Command line globber/tokenizer library for C?14Bonita Montero
12 Sep 24 i     i `* Re: Command line globber/tokenizer library for C?13Janis Papanagnou
12 Sep 24 i     i  `* Re: Command line globber/tokenizer library for C?12Bonita Montero
12 Sep 24 i     i   +* Re: Command line globber/tokenizer library for C?2Janis Papanagnou
12 Sep 24 i     i   i`- Re: Command line globber/tokenizer library for C?1Bonita Montero
13 Sep 24 i     i   `* Re: Command line globber/tokenizer library for C?9Lawrence D'Oliveiro
13 Sep 24 i     i    `* Re: Command line globber/tokenizer library for C?8Bonita Montero
13 Sep 24 i     i     `* Re: Command line globber/tokenizer library for C?7Lawrence D'Oliveiro
13 Sep 24 i     i      `* Re: Command line globber/tokenizer library for C?6Michael S
14 Sep 24 i     i       `* Re: Command line globber/tokenizer library for C?5Lawrence D'Oliveiro
12 Sep 24 i     `- Re: Command line globber/tokenizer library for C?1Bonita Montero
12 Sep 24 `* Re: Command line globber/tokenizer library for C?4Bonita Montero

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal