Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #388439
| From | Tim Rentsch <tr.17687@z991.linuxsc.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Command line globber/tokenizer library for C? |
| Date | 2024-09-17 18:31 -0700 |
| Organization | A noiseless patient Spider |
| Message-ID | <867cb9agtd.fsf@linuxsc.com> (permalink) |
| References | (10 earlier) <20240912223828.00005c10@yahoo.com> <861q1nfsjz.fsf@linuxsc.com> <20240915122211.000058b1@yahoo.com> <vcd05n$q3mn$1@paganini.bofh.team> <20240918024611.000002f3@yahoo.com> |
Michael S <already5chosen@yahoo.com> writes:
> 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.
>
> _Bool
> words_do( const char *s, Gopher go ){
> char c = *s;
> #if 1
> if (is_space(c))
> return words_do( s+1, go );
> if (c)
> return collect_word( s, s, 1, go );
> return 1;
> #else
> return
> is_space(c) ? words_do( s+1, go ) :
> c ? collect_word( s, s, 1, go ):
> /***************/ 1;
> #endif
> }
>
> 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
> }
That's amusing. :)
Do you know if icc will do tail call elimination for
the boring version of the code?
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar
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