Skip to content

Commit

Permalink
Add number precision and padding
Browse files Browse the repository at this point in the history
Fix implementation for number with zero padding and precision!
  • Loading branch information
ehsankianifar committed Nov 3, 2023
1 parent 63f1133 commit 4a1f95c
Showing 1 changed file with 70 additions and 54 deletions.
124 changes: 70 additions & 54 deletions util/a2e/atoe_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ fstring(InstanceData *this, char *str, int left_justify, int min_width,
return ERROR_RETVAL;
}

for (width = 0; width < precision; width++) {
for (width = 0; (width < precision) || (precision < 0); width++) {
if ('\0' == str[width]) {
break;
}
Expand Down Expand Up @@ -183,13 +183,14 @@ typedef enum {
*************************************************************************/
static int
fnumber(InstanceData *this, long value, int format_type, int left_justify,
int min_width, bool_t zero_pad)
int min_width, int precision, bool_t zero_pad)
{
int sign_value = 0;
unsigned long uvalue;
char convert[MAX_DIGITS + 1];
int place = 0;
int pad_length = 0;
int zero_pad_length = 0;
static char digits[] = "0123456789abcdef";
int base = 0;
bool_t caps = FALSE;
Expand Down Expand Up @@ -241,42 +242,49 @@ fnumber(InstanceData *this, long value, int format_type, int left_justify,
} while (uvalue);
convert[place] = 0;

pad_length = min_width - place;
if (pad_length < 0) {
pad_length = 0;
if (precision > 0) {
zero_pad_length = precision - place;
} else if ((zero_pad == TRUE) && (left_justify == TRUE)) {
zero_pad_length = min_width - place;
if (sign_value) {
zero_pad_length--;
}
}

if (zero_pad_length < 0) {
zero_pad_length = 0;
}

pad_length = min_width - (place + zero_pad_length);
if (sign_value) {
pad_length--;
}

if (left_justify) {
if (zero_pad && pad_length > 0) {
if (sign_value) {
pchar(this, sign_value);
--pad_length;
sign_value = 0;
}
while (pad_length > 0) {
pchar(this, '0');
--pad_length;
}
} else {
while (pad_length > 0) {
pchar(this, ' ');
--pad_length;
}
while (pad_length > 0) {
pchar(this, ' ');
--pad_length;
}
}

if (sign_value) {
pchar(this, sign_value);
}

while (zero_pad_length > 0) {
pchar(this, '0');
zero_pad_length--;
}

while (place > 0) {
pchar(this, convert[--place]);
}

if (!left_justify) {
while (pad_length > 0) {
pchar(this, ' ');
--pad_length;
}
while (pad_length > 0) {
pchar(this, ' ');
--pad_length;
}

return SUCCESS;
}

Expand All @@ -296,13 +304,14 @@ fnumber(InstanceData *this, long value, int format_type, int left_justify,
*/
static int
flongnumber(InstanceData *this, signed long long value, int format_type, int left_justify,
int min_width, bool_t zero_pad)
int min_width, int precision, bool_t zero_pad)
{
int sign_value = 0;
unsigned long long uvalue;
char convert[MAX_DIGITS + 1];
int place = 0;
int pad_length = 0;
int zero_pad_length = 0;
static char digits[] = "0123456789abcdef";
int base = 0;
bool_t caps = FALSE;
Expand Down Expand Up @@ -354,42 +363,49 @@ flongnumber(InstanceData *this, signed long long value, int format_type, int lef
} while (uvalue);
convert[place] = 0;

pad_length = min_width - place;
if (pad_length < 0) {
pad_length = 0;
if (precision > 0) {
zero_pad_length = precision - place;
} else if ((zero_pad == TRUE) && (left_justify == TRUE)) {
zero_pad_length = min_width - place;
if (sign_value) {
zero_pad_length--;
}
}

if (zero_pad_length < 0) {
zero_pad_length = 0;
}

pad_length = min_width - (place + zero_pad_length);
if (sign_value) {
pad_length--;
}

if (left_justify) {
if (zero_pad && pad_length > 0) {
if (sign_value) {
pchar(this, sign_value);
--pad_length;
sign_value = 0;
}
while (pad_length > 0) {
pchar(this, '0');
--pad_length;
}
} else {
while (pad_length > 0) {
pchar(this, ' ');
--pad_length;
}
while (pad_length > 0) {
pchar(this, ' ');
--pad_length;
}
}

if (sign_value) {
pchar(this, sign_value);
}

while (zero_pad_length > 0) {
pchar(this, '0');
zero_pad_length--;
}

while (place > 0) {
pchar(this, convert[--place]);
}

if (!left_justify) {
while (pad_length > 0) {
pchar(this, ' ');
--pad_length;
}
while (pad_length > 0) {
pchar(this, ' ');
--pad_length;
}

return SUCCESS;
}

Expand Down Expand Up @@ -432,7 +448,7 @@ atoe_vsnprintf(char *str, size_t count, const char *fmt, va_list args)
pattern = fmt - 1; /*ibm@8665*/
left_justify = TRUE;
min_width = 0;
precision = INT_MAX;
precision = -1; /* -1 means unspecified */

next_char:
ch = *fmt++;
Expand Down Expand Up @@ -515,17 +531,17 @@ atoe_vsnprintf(char *str, size_t count, const char *fmt, va_list args)
signed long long value64 = va_arg(args, signed long long); /*ibm@9094*/ /* j9@72429 */

CheckRet(flongnumber(&this, value64, ch, left_justify, /*ibm@9094*/
min_width, zero_pad)); /*ibm@9094*/
min_width, precision, zero_pad)); /*ibm@9094*/
} else {
value = long_flag ? va_arg(args, long) : va_arg(args, int);
CheckRet(fnumber(&this, value, ch, left_justify,
min_width, zero_pad));
min_width, precision, zero_pad));
}
break;
case 'p':
value = (long) va_arg(args, char *);
CheckRet(fnumber(&this, value, ch, left_justify,
min_width, zero_pad));
min_width, precision, zero_pad));
break;
case 'e':
case 'E':
Expand Down Expand Up @@ -571,7 +587,7 @@ atoe_vsnprintf(char *str, size_t count, const char *fmt, va_list args)
signed long long value64 = va_arg(args, signed long long);

CheckRet(flongnumber(&this, value64, 'd', left_justify,
min_width, zero_pad));
min_width, precision, zero_pad));

fmt--; /*backup so we don't lose the current char */
break;
Expand Down

0 comments on commit 4a1f95c

Please sign in to comment.