printf "%*s" format

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

printf "%*s" format

Paul "LeoNerd" Evans
I often find it useful to be able to print a character buffer of
dynamically-known size by using some variant on

  printf("%.*s", len, buf);

It appears to me that avr-libc doesn't recognise this particular flag.
Neither the documentation nor the implementation in vfprintf.c seem to
mention it. If I try it, it just gives up. E.g.

  uint8_t len;
  uint8_t buf[16];
  ...
  printf("BUF=%d <%.*s>\n", len, len, buf);

I get output simply

  BUF=4 <

it stops parsing at the .*, and doesn't even get as far as printing the
'>' and trailing linefeed.

Would folk be amenable to a patch to add this feature? To my eye it
doesn't look like it would be more than about a 5-line change to add
it, I don't think it would massively increase code size.

But I thought I'd ask first, in case there's some specific reason for
its absence.

--
Paul "LeoNerd" Evans

[hidden email]      |  https://metacpan.org/author/PEVANS
http://www.leonerd.org.uk/  |  https://www.tindie.com/stores/leonerd/

_______________________________________________
AVR-GCC-list mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/avr-gcc-list

attachment0 (188 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: printf "%*s" format

Paul "LeoNerd" Evans
On Sun, 3 Jul 2016 13:52:08 +0100
"Paul \"LeoNerd\" Evans" <[hidden email]> wrote:

> Would folk be amenable to a patch to add this feature? To my eye it
> doesn't look like it would be more than about a 5-line change to add
> it, I don't think it would massively increase code size.
>
> But I thought I'd ask first, in case there's some specific reason for
> its absence.

Any response at all on this issue? Is this even the right mailing list
to ask?

I once again have run into a situation that would make use of it so I'm
keen to find out if we can get this added...

--
Paul "LeoNerd" Evans

[hidden email]      |  https://metacpan.org/author/PEVANS
http://www.leonerd.org.uk/  |  https://www.tindie.com/stores/leonerd/

_______________________________________________
AVR-GCC-list mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/avr-gcc-list

attachment0 (201 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: printf "%*s" format

Erik Christiansen-2
Reply-To: [hidden email]

On 03.07.16 13:52, Paul "LeoNerd" Evans wrote:
> I often find it useful to be able to print a character buffer of
> dynamically-known size by using some variant on
>
>   printf("%.*s", len, buf);
>
> It appears to me that avr-libc doesn't recognise this particular flag.
> Neither the documentation nor the implementation in vfprintf.c seem to
> mention it.

On 17.10.17 20:37, Paul "LeoNerd" Evans wrote:

> On Sun, 3 Jul 2016 13:52:08 +0100
> "Paul \"LeoNerd\" Evans" <[hidden email]> wrote:
>
> > Would folk be amenable to a patch to add this feature? To my eye it
> > doesn't look like it would be more than about a 5-line change to add
> > it, I don't think it would massively increase code size.
> >
> > But I thought I'd ask first, in case there's some specific reason for
> > its absence.
>
> Any response at all on this issue? Is this even the right mailing list
> to ask?
>
> I once again have run into a situation that would make use of it so I'm
> keen to find out if we can get this added...

My thoughts are:

   o  /usr/lib/avr/include/stdio.h warns that features have been
      deliberately omitted in order to maintain the greater benefit of
      small size. The lack of enthusiasm on the list suggests that the
      smaller size is still preferred.

   o  My only 'C' books are from 1978 and 1988, and I'm having trouble
      correlating your attempt to place control outside the control
      string, in the form of a non-output variable in the variable list.
     
   o  It would seem to be infinitely simpler to merely null terminate
      the string in buf, and use "%s", instead of adding unnecessary
      user complexity and increasing code size. (Assuming it is 'C'.)

   o  The null terminated string for printf could be generated as you
      show, using snprintf, which appears to be available.

      If that is done, the problem ceases to exist.

Erik


_______________________________________________
AVR-GCC-list mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/avr-gcc-list