Implementing the vprintf "%*s" and "%.*s" formats

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

Implementing the vprintf "%*s" and "%.*s" formats

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.
It is documented that

  The variable width or precision field (an asterisk * symbol) is not
  realized and will to abort the output.

But it's unclear from this whether the absence is deliberate, or simply
not implemented yet.

Would folk be amenable to a patch to add this feature? I have an
(as-yet untested) attempt at it it. I don't think it would massively
increase code size, and it doesn't consume any more RAM at runtime.

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

-----

=== modified file 'libc/stdio/vfprintf.c'
--- libc/stdio/vfprintf.c 2017-10-17 19:56:22 +0000
+++ libc/stdio/vfprintf.c 2017-10-17 19:57:23 +0000
@@ -344,6 +344,12 @@
     flags |= FL_WIDTH;
     continue;
  }
+ if (c == '*') {
+    if (flags & FL_PREC)
+ prec = va_arg(ap, int);
+    else
+ width = va_arg(ap, int);
+ }
  if (c == '.') {
     if (flags & FL_PREC)
  goto ret;


--
Paul "LeoNerd" Evans

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

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

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

Re: Implementing the vprintf "%*s" and "%.*s" formats

David Brown-4
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

It seems reasonable to me.  It is probably not a heavily used feature
in printf, but it is part of the standards and its absence could be
surprising to people.

David


On 17/10/17 22:10, 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. It is documented that
>
> The variable width or precision field (an asterisk * symbol) is
> not realized and will to abort the output.
>
> But it's unclear from this whether the absence is deliberate, or
> simply not implemented yet.
>
> Would folk be amenable to a patch to add this feature? I have an
> (as-yet untested) attempt at it it. I don't think it would
> massively increase code size, and it doesn't consume any more RAM
> at runtime.
>
> But I thought I'd ask, in case there's some specific reason for
> its absence.
>
> -----
>
> === modified file 'libc/stdio/vfprintf.c' --- libc/stdio/vfprintf.c
> 2017-10-17 19:56:22 +0000 +++ libc/stdio/vfprintf.c 2017-10-17
> 19:57:23 +0000 @@ -344,6 +344,12 @@ flags |= FL_WIDTH; continue; }
> + if (c == '*') { +    if (flags & FL_PREC) + prec =
> va_arg(ap, int); +    else + width = va_arg(ap, int); + } if
> (c == '.') { if (flags & FL_PREC) goto ret;
>
>
>
>
> _______________________________________________ AVR-libc-dev
> mailing list [hidden email]
> https://lists.nongnu.org/mailman/listinfo/avr-libc-dev
>

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)

iQEcBAEBAgAGBQJZ5wtFAAoJEPy4fZ5YP6apSzoH/iCKARNwbhhOhlJlR/H/00Px
PzJAgXdCAnylkKdg68J1gAcE2sSon4x1tE3zZKSKPs0rTWwJVSfTIq/Lx1fHp8SB
qR+u22yS5uwwwNX8W2goOfqsCLD54CPFVufmkKFSkPJXFQdgdQBVei/T+Xf/FYkn
xpMeQ0tCDpHehEiMIy7C5zOsUuVz9y9eFXjat0C6DkL0Nd3yyDUYJ7cHA+RZxzwE
mgMi6+FTOWAZRSlW5FeYWDNYHZ2FnXp4CGzkOHm0dRkMiD8vZFOWbJIHlT2u1XjJ
EkDZHy8ZsP4Pt3UOidmtOFcEtf4I0TjRrCNDJ0+9M3I/bPftQj2mv7bHHc4rGJQ=
=TMfS
-----END PGP SIGNATURE-----

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