You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by ad...@apache.org on 2017/04/20 01:07:30 UTC

[2/3] incubator-mynewt-core git commit: MYNEWT-735 tinyprintf: negative field width spec.

MYNEWT-735 tinyprintf: negative field width spec.

>From the printf(3) man page on my OS X:

 `-'          A negative field width flag; the converted value is to
              be left adjusted on the field boundary.  Except for n
              conversions, the converted value is padded on the right
              with blanks, rather than on the left with blanks or
              zeros.  A - overrides a 0 if both are given.


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/b83face5
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/b83face5
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/b83face5

Branch: refs/heads/master
Commit: b83face5dfb740328c79081424fd2abdd9ca8d1d
Parents: 31d00cb
Author: Christopher Collins <cc...@apache.org>
Authored: Wed Apr 19 16:30:27 2017 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Wed Apr 19 16:39:14 2017 -0700

----------------------------------------------------------------------
 libc/baselibc/src/tinyprintf.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b83face5/libc/baselibc/src/tinyprintf.c
----------------------------------------------------------------------
diff --git a/libc/baselibc/src/tinyprintf.c b/libc/baselibc/src/tinyprintf.c
index 7719d80..11bcf63 100644
--- a/libc/baselibc/src/tinyprintf.c
+++ b/libc/baselibc/src/tinyprintf.c
@@ -70,6 +70,7 @@ struct param {
     char sign:1;        /**<  The sign to display (if any) */
     char alt:1;         /**< alternate form */
     char uc:1;          /**<  Upper case (for base16 only) */
+    char left:1;        /**<  Force text to left (padding on right) */
     char base;  /**<  number base (e.g.: 8, 10, 16) */
     char *bf;           /**<  Buffer to output */
 };
@@ -155,8 +156,8 @@ static unsigned putchw(FILE *putp, struct param *p)
     else if (p->alt && p->base == 8)
         n--;
 
-    /* Fill with space, before alternate or sign */
-    if (!p->lz) {
+    /* Unless left-aligned, fill with space, before alternate or sign */
+    if (!p->lz && !p->left) {
         while (n-- > 0)
             written += putf(putp, ' ');
     }
@@ -183,6 +184,12 @@ static unsigned putchw(FILE *putp, struct param *p)
     bf = p->bf;
     while ((ch = *bf++))
         written += putf(putp, ch);
+
+    /* If left-aligned, pad the end with spaces. */
+    if (p->left) {
+        while (n-- > 0)
+            written += putf(putp, ' ');
+    }
     
     return written;
 }
@@ -246,17 +253,24 @@ size_t tfp_format(FILE *putp, const char *fmt, va_list va)
             p.alt = 0;
             p.width = 0;
             p.sign = 0;
+            p.left = 0;
             lng = 0;
 
             /* Flags */
             while ((ch = *(fmt++))) {
                 switch (ch) {
                 case '0':
-                    p.lz = 1;
+                    if (!p.left) {
+                        p.lz = 1;
+                    }
                     continue;
                 case '#':
                     p.alt = 1;
                     continue;
+                case '-':
+                    p.left = 1;
+                    p.lz = 0;
+                    continue;
                 default:
                     break;
                 }