Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #388400

Re: Command line globber/tokenizer library for C?

From Michael S <already5chosen@yahoo.com>
Newsgroups comp.lang.c
Subject Re: Command line globber/tokenizer library for C?
Date 2024-09-15 12:22 +0300
Organization A noiseless patient Spider
Message-ID <20240915122211.000058b1@yahoo.com> (permalink)
References (9 earlier) <20240912181625.00006e68@yahoo.com> <vbv4ra$b0hv$2@dont-email.me> <vbv6r1$bhc9$1@raubtier-asyl.eternal-september.org> <20240912223828.00005c10@yahoo.com> <861q1nfsjz.fsf@linuxsc.com>

Show all headers | View raw


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..]
> 
> > #include <stddef.h>
> >
> > void parse(const char* src,
> >   void (*OnToken)(const char* beg, size_t len, void* context),
> >   void* context) {
> >   char c0 = ' ', c1 = '\t';
> >   const char* beg = 0;
> >   for (;;src++) {
> >     char c = *src;
> >     if (c == c0 || c == c1 || c == 0) {
> >       if (beg) {
> >         OnToken(beg, src-beg, context);
> >         c0 = ' ', c1 = '\t';
> >         beg = 0;
> >       }
> >       if (c == 0)
> >         break;
> >     } else if (!beg) {
> >       beg = src;
> >       if (c == '"') {
> >         c0 = c1 = c;
> >         ++beg;
> >       }
> >     }
> >   }
> > }  
> 
> 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';
> }


Can you give an example implementation of go->f() ?
It seems to me that it would have to use CONTAINING_RECORD or
container_of or analogous non-standard macro.

Also, while formally the program is written in C,  by spirit it's
something else. May be, Lisp. 
Lisp compilers are known to be very good at tail call elimination.
C compilers also can do it, but not reliably. In this particular case I
am afraid that common C compilers will implement it as written, i.e.
without turning recursion into iteration.

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.







Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Command line globber/tokenizer library for C? ted@loft.tnolan.com (Ted Nolan <tednolan>) - 2024-09-10 19:01 +0000
  Re: Command line globber/tokenizer library for C? Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-09-10 20:58 +0000
    Re: Command line globber/tokenizer library for C? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-09-10 14:12 -0700
  Re: Command line globber/tokenizer library for C? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-09-10 23:05 +0200
    Re: Command line globber/tokenizer library for C? ted@loft.tnolan.com (Ted Nolan <tednolan>) - 2024-09-10 22:11 +0000
  Re: Command line globber/tokenizer library for C? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-09-10 14:37 -0700
    Re: Command line globber/tokenizer library for C? ted@loft.tnolan.com (Ted Nolan <tednolan>) - 2024-09-10 22:13 +0000
  Re: Command line globber/tokenizer library for C? gazelle@shell.xmission.com (Kenny McCormack) - 2024-09-11 01:56 +0000
    Re: Command line globber/tokenizer library for C? ted@loft.tnolan.com (Ted Nolan <tednolan>) - 2024-09-11 02:54 +0000
  Re: Command line globber/tokenizer library for C? Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-11 14:17 +0200
    Re: Command line globber/tokenizer library for C? ted@loft.tnolan.com (Ted Nolan <tednolan>) - 2024-09-11 12:22 +0000
      Re: Command line globber/tokenizer library for C? Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-11 14:28 +0200
        Re: Command line globber/tokenizer library for C? ted@loft.tnolan.com (Ted Nolan <tednolan>) - 2024-09-11 12:44 +0000
    Re: Command line globber/tokenizer library for C? Bart <bc@freeuk.com> - 2024-09-11 13:42 +0100
    Re: Command line globber/tokenizer library for C? gazelle@shell.xmission.com (Kenny McCormack) - 2024-09-11 14:59 +0000
      Re: Command line globber/tokenizer library for C? Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-11 20:14 +0200
        Re: Command line globber/tokenizer library for C? gazelle@shell.xmission.com (Kenny McCormack) - 2024-09-11 18:17 +0000
        Re: Command line globber/tokenizer library for C? ted@loft.tnolan.com (Ted Nolan <tednolan>) - 2024-09-11 18:49 +0000
          Re: Command line globber/tokenizer library for C? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-09-11 14:43 -0700
            Re: Command line globber/tokenizer library for C? ted@loft.tnolan.com (Ted Nolan <tednolan>) - 2024-09-12 03:06 +0000
              Re: Command line globber/tokenizer library for C? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-09-11 20:37 -0700
                Re: Command line globber/tokenizer library for C? ted@loft.tnolan.com (Ted Nolan <tednolan>) - 2024-09-12 03:56 +0000
                Re: Command line globber/tokenizer library for C? gazelle@shell.xmission.com (Kenny McCormack) - 2024-09-12 13:22 +0000
                Re: Command line globber/tokenizer library for C? ted@loft.tnolan.com (Ted Nolan <tednolan>) - 2024-09-12 13:50 +0000
                Columbia (Was: Command line globber/tokenizer library for C?) gazelle@shell.xmission.com (Kenny McCormack) - 2024-09-12 14:06 +0000
                Re: Columbia (Was: Command line globber/tokenizer library for C?) Michael S <already5chosen@yahoo.com> - 2024-09-12 17:20 +0300
                Re: Columbia (Was: Command line globber/tokenizer library for C?) David Brown <david.brown@hesbynett.no> - 2024-09-12 16:43 +0200
                Re: Columbia (Was: Command line globber/tokenizer library for C?) Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-09-12 22:07 +0000
              Re: Command line globber/tokenizer library for C? Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-09-12 04:14 +0000
              Re: Command line globber/tokenizer library for C? Ben Bacarisse <ben@bsb.me.uk> - 2024-09-12 10:43 +0100
        Re: Command line globber/tokenizer library for C? Bart <bc@freeuk.com> - 2024-09-11 21:19 +0100
          Re: Command line globber/tokenizer library for C? Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-12 04:22 +0200
            Re: Command line globber/tokenizer library for C? Bart <bc@freeuk.com> - 2024-09-12 12:29 +0100
              Re: Command line globber/tokenizer library for C? gazelle@shell.xmission.com (Kenny McCormack) - 2024-09-12 12:13 +0000
                Re: Command line globber/tokenizer library for C? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-09-12 14:24 +0200
                Other programming languages (Was: Command line globber/tokenizer library for C?) gazelle@shell.xmission.com (Kenny McCormack) - 2024-09-12 13:20 +0000
                Re: Other programming languages (Was: Command line globber/tokenizer library for C?) Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-12 16:01 +0200
                Re: Other programming languages (Was: Command line globber/tokenizer library for C?) gazelle@shell.xmission.com (Kenny McCormack) - 2024-09-12 14:07 +0000
                Re: Other programming languages (Was: Command line globber/tokenizer library for C?) Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-12 16:14 +0200
                Re: Other programming languages (Was: Command line globber/tokenizer library for C?) Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-09-12 17:40 +0200
                Re: Other programming languages (Was: Command line globber/tokenizer library for C?) Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-12 17:48 +0200
                Re: Other programming languages (Was: Command line globber/tokenizer library for C?) Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-09-12 17:59 +0200
                Re: Other programming languages (Was: Command line globber/tokenizer library for C?) Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-09-12 22:32 +0000
                Re: Other programming languages (Was: Command line globber/tokenizer library for C?) James Kuyper <jameskuyper@alumni.caltech.edu> - 2024-09-12 18:50 -0400
                Re: Other programming languages (Was: Command line globber/tokenizer library for C?) Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-09-13 01:37 +0000
                Re: Other programming languages (Was: Command line globber/tokenizer library for C?) Michael S <already5chosen@yahoo.com> - 2024-09-13 11:30 +0300
                Re: Other programming languages (Was: Command line globber/tokenizer library for C?) Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-09-13 04:06 +0200
                Re: Other programming languages (Was: Command line globber/tokenizer library for C?) Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-09-13 02:58 +0000
                Re: Other programming languages (Was: Command line globber/tokenizer library for C?) Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-09-13 14:31 +0200
                Re: Other programming languages (Was: Command line globber/tokenizer library for C?) Kaz Kylheku <643-408-1753@kylheku.com> - 2024-09-13 04:18 +0000
                Re: Other programming languages (Was: Command line globber/tokenizer library for C?) Kaz Kylheku <643-408-1753@kylheku.com> - 2024-09-13 03:38 +0000
                Re: Command line globber/tokenizer library for C? Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-12 16:00 +0200
              Re: Command line globber/tokenizer library for C? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-09-12 14:20 +0200
                Re: Command line globber/tokenizer library for C? Bart <bc@freeuk.com> - 2024-09-12 14:44 +0100
                Re: Command line globber/tokenizer library for C? Bart <bc@freeuk.com> - 2024-09-12 15:16 +0100
                Re: Command line globber/tokenizer library for C? Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-12 16:23 +0200
                Re: Command line globber/tokenizer library for C? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-09-12 17:46 +0200
                Re: Command line globber/tokenizer library for C? Bart <bc@freeuk.com> - 2024-09-12 17:15 +0100
                Re: Command line globber/tokenizer library for C? Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-12 18:26 +0200
                Re: Command line globber/tokenizer library for C? Michael S <already5chosen@yahoo.com> - 2024-09-12 18:16 +0300
                Re: Command line globber/tokenizer library for C? scott@slp53.sl.home (Scott Lurndal) - 2024-09-12 15:37 +0000
                Re: Command line globber/tokenizer library for C? Michael S <already5chosen@yahoo.com> - 2024-09-12 18:49 +0300
                Re: Command line globber/tokenizer library for C? Bart <bc@freeuk.com> - 2024-09-12 17:28 +0100
                Re: Command line globber/tokenizer library for C? Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-12 19:02 +0200
                Re: Command line globber/tokenizer library for C? gazelle@shell.xmission.com (Kenny McCormack) - 2024-09-12 17:39 +0000
                Re: Command line globber/tokenizer library for C? Michael S <already5chosen@yahoo.com> - 2024-09-12 22:38 +0300
                Re: Command line globber/tokenizer library for C? Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-13 07:28 +0200
                Re: Command line globber/tokenizer library for C? Michael S <already5chosen@yahoo.com> - 2024-09-13 11:38 +0300
                Re: Command line globber/tokenizer library for C? Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-13 14:12 +0200
                Re: Command line globber/tokenizer library for C? Michael S <already5chosen@yahoo.com> - 2024-09-13 15:25 +0300
                Re: Command line globber/tokenizer library for C? Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-13 15:20 +0200
                Re: Command line globber/tokenizer library for C? Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-09-13 22:24 +0000
                Re: Command line globber/tokenizer library for C? Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-14 01:42 +0200
                Re: Command line globber/tokenizer library for C? Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-09-14 01:41 +0000
                Re: Command line globber/tokenizer library for C? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-09-13 09:05 -0700
                Re: Command line globber/tokenizer library for C? Michael S <already5chosen@yahoo.com> - 2024-09-15 12:22 +0300
                Re: Command line globber/tokenizer library for C? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-09-16 00:52 -0700
                Re: Command line globber/tokenizer library for C? Michael S <already5chosen@yahoo.com> - 2024-09-16 12:23 +0300
                Re: Command line globber/tokenizer library for C? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-09-17 03:12 -0700
                Re: Command line globber/tokenizer library for C? antispam@fricas.org - 2024-09-17 22:34 +0000
                Re: Command line globber/tokenizer library for C? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-09-17 16:33 -0700
                Re: Command line globber/tokenizer library for C? Michael S <already5chosen@yahoo.com> - 2024-09-18 02:46 +0300
                Re: Command line globber/tokenizer library for C? Bart <bc@freeuk.com> - 2024-09-18 01:07 +0100
                Re: Command line globber/tokenizer library for C? Michael S <already5chosen@yahoo.com> - 2024-09-18 11:43 +0300
                Re: Command line globber/tokenizer library for C? Bart <bc@freeuk.com> - 2024-09-18 10:49 +0100
                Re: Command line globber/tokenizer library for C? David Brown <david.brown@hesbynett.no> - 2024-09-18 12:44 +0200
                Re: Command line globber/tokenizer library for C? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-09-18 06:01 -0700
                Re: Command line globber/tokenizer library for C? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-09-17 18:31 -0700
                Re: Command line globber/tokenizer library for C? Michael S <already5chosen@yahoo.com> - 2024-09-18 11:03 +0300
                Re: Command line globber/tokenizer library for C? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-09-18 05:09 -0700
                Re: Command line globber/tokenizer library for C? Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-09-18 02:20 +0000
                Re: Command line globber/tokenizer library for C? Michael S <already5chosen@yahoo.com> - 2024-09-18 11:05 +0300
                Re: Command line globber/tokenizer library for C? Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-12 16:04 +0200
                Re: Command line globber/tokenizer library for C? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-09-12 17:30 +0200
                Re: Command line globber/tokenizer library for C? Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-12 17:47 +0200
                Re: Command line globber/tokenizer library for C? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-09-12 17:56 +0200
                Re: Command line globber/tokenizer library for C? Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-12 18:09 +0200
                Re: Command line globber/tokenizer library for C? Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-09-13 02:43 +0000
                Re: Command line globber/tokenizer library for C? Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-13 07:27 +0200
                Re: Command line globber/tokenizer library for C? Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-09-13 06:49 +0000
                Re: Command line globber/tokenizer library for C? Michael S <already5chosen@yahoo.com> - 2024-09-13 11:49 +0300
                Re: Command line globber/tokenizer library for C? Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-09-13 22:04 +0000
                Re: Command line globber/tokenizer library for C? Bart <bc@freeuk.com> - 2024-09-13 23:48 +0100
                Re: Command line globber/tokenizer library for C? Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-09-14 01:41 +0000
                Re: Command line globber/tokenizer library for C? Bart <bc@freeuk.com> - 2024-09-14 10:58 +0100
                Re: Command line globber/tokenizer library for C? Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-09-14 22:37 +0000
              Re: Command line globber/tokenizer library for C? Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-12 15:58 +0200
  Re: Command line globber/tokenizer library for C? Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-12 17:08 +0200
    More OT BS (Was: Command line globber/tokenizer library for C?) gazelle@shell.xmission.com (Kenny McCormack) - 2024-09-12 15:23 +0000
      Re: More OT BS (Was: Command line globber/tokenizer library for C?) Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-12 17:24 +0200
    Re: Command line globber/tokenizer library for C? Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-09-12 22:09 +0000

csiph-web