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


Groups > comp.lang.c > #160381

Re: simple positional printf code review

From Kaz Kylheku <563-365-8930@kylheku.com>
Newsgroups comp.lang.c
Subject Re: simple positional printf code review
Date 2021-04-26 21:53 +0000
Organization A noiseless patient Spider
Message-ID <20210426143450.508@kylheku.com> (permalink)
References <6e8017a1-ca34-4e99-b541-b2ba87b9cc66n@googlegroups.com>

Show all headers | View raw


On 2021-04-26, Thiago Adams <thiago.adams@gmail.com> wrote:
>
> I need a function similar of snprint where  I can specify
> the index of the argument in any order and reuse the 
> same argument.
>
> int format(char* s, size_t n, const char* format, ...);
>
> char buffer[10];
> format(buffer, sizeof buffer, "A %1 B %2 C %1", "ONE", "TWO");
> this should format "A ONE B TWO C ONE"
>
> this is what I have so far.
>
> int format(char* s, size_t n, const char* format, ...)
> {

I would do this:

  int formata(char *dst, size_t dstsz, const char *format,
              const char *args[], size_t nargs)
  {
    /* loop over format string, extracting from args[] array,
       checking %n values against nargs limit */
  }

  int format(char *dst, size_t dstsz, const char *format,
             const char * arg, ...)
  {
    /* wrapper around formata that extracts the required
       array from variadic arguments. */
  }

You can write separate test cases against these two functions,
and from time to time, the array one could be useful.

If you make the array null-terminated, so that args[nargs] == NULL,
you can pass the main functions argv/argc to it.

You could have a test driver program:

  int main(int argc, char **argv)
  {
     if (argc >= 2) {
       const char *fmt = argv[1];
       char buf[1024] = { 0 };
       formata(buf, sizeof buf, argv + 2, argc - 2);
       fputs(buf, stdout);
       return 0;
     }

     return EXIT_FAILURE:
  }

and test that from a script. E.g.

  [ "$(./format "%0-%1" foo bar)" == "foo-bar" ] || test_failed

If the code aborts for an out-of-bound argument reference, the
script can check for that also.

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


Thread

simple positional printf code review Thiago Adams <thiago.adams@gmail.com> - 2021-04-26 13:29 -0700
  Re: simple positional printf code review Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2021-04-26 20:39 +0000
    Re: simple positional printf code review Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2021-04-26 20:49 +0000
      Re: simple positional printf code review Thiago Adams <thiago.adams@gmail.com> - 2021-04-26 14:00 -0700
        Re: simple positional printf code review Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-04-26 22:09 +0100
          Re: simple positional printf code review Thiago Adams <thiago.adams@gmail.com> - 2021-04-26 14:18 -0700
            Re: simple positional printf code review Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-04-27 01:46 +0100
              Re: simple positional printf code review "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-04-26 18:19 -0700
                Re: simple positional printf code review Thiago Adams <thiago.adams@gmail.com> - 2021-04-27 04:44 -0700
        Re: simple positional printf code review Geoff <geoff@invalid.invalid> - 2021-04-26 17:58 -0700
          Re: simple positional printf code review Kaz Kylheku <563-365-8930@kylheku.com> - 2021-04-27 15:58 +0000
    Re: simple positional printf code review Thiago Adams <thiago.adams@gmail.com> - 2021-04-26 13:53 -0700
  Re: simple positional printf code review Kaz Kylheku <563-365-8930@kylheku.com> - 2021-04-26 21:53 +0000

csiph-web