# HG changeset patch # User Matti Hamalainen # Date 1455624231 -7200 # Node ID 38a0719359ef5cc1f27b04fce161377aef33da5d # Parent ca9cd98dbcff5bd9d4fbedf4f14ead9a977b8144 Add some comments. diff -r ca9cd98dbcff -r 38a0719359ef th_string.c --- a/th_string.c Tue Feb 16 13:59:51 2016 +0200 +++ b/th_string.c Tue Feb 16 14:03:51 2016 +0200 @@ -120,12 +120,14 @@ if (radix > 16) return 0; + // Check for negative value if (val < 0) { neg = TRUE; val = -val; } + // Render the value to a string in buf (reversed) do { int digit = val % radix; @@ -139,6 +141,7 @@ while (val > 0 && pos < sizeof(buf) - 1); buf[pos] = 0; + // Do we want a sign prefix? Not for unsigned values if (!unsig) { char ch = sign ? (neg ? '-' : '+') : (neg ? '-' : 0); @@ -146,8 +149,10 @@ goto out; } + // Calculate necessary padding, if any int nwidth = width - pos; + // Suffix padding? if (nwidth > 0 && padMode != '-') { while (nwidth--) @@ -155,12 +160,14 @@ goto out; } + // Output the value while (pos--) { if ((ret = vputch(ctx, buf[pos])) == EOF) goto out; } + // Postfix padding? if (nwidth > 0 && padMode == '-') { while (nwidth--) @@ -277,7 +284,7 @@ goto out; case 's': - if (padMode != '-' && padMode != ' ') + if ((padMode != '-' && padMode != ' ') || sign) goto out; if ((ret = th_vput_str(ctx, vputch, va_arg(ap, char *), padMode, width)) == EOF)