fixed a bug printing %% in print_formatted.

changed to use #x #o #b for hexadecimal, octal, binary output
This commit is contained in:
hyung-hwan 2018-03-01 08:56:56 +00:00
parent 329fc6ce7d
commit e58d4e30cc
4 changed files with 87 additions and 43 deletions

View File

@ -803,7 +803,8 @@ static HCL_INLINE int print_formatted (hcl_t* hcl, hcl_ooi_t nargs, hcl_fmtout_t
if (ch == HCL_OOCI_EOF) if (ch == HCL_OOCI_EOF)
{ {
/* fmt is not advanced when it is length-bounded. so not fmt - checkpoint - 1 */ /* fmt is not advanced when it is length-bounded.
* so not fmt - checkpoint - 1 */
PRINT_OOCS (checkpoint, fmt - checkpoint); PRINT_OOCS (checkpoint, fmt - checkpoint);
goto done; goto done;
} }
@ -954,16 +955,15 @@ reswitch:
case 'c': case 'c':
case 'C': case 'C':
print_char:
/* zeropad must not take effect for 'c' */
if (flagc & FLAGC_ZEROPAD) padc = ' ';
GET_NEXT_ARG_TO (hcl, nargs, &arg_state, arg); GET_NEXT_ARG_TO (hcl, nargs, &arg_state, arg);
if (HCL_OOP_IS_SMOOI(arg)) arg = HCL_CHAR_TO_OOP(HCL_OOP_TO_SMOOI(arg)); if (HCL_OOP_IS_SMOOI(arg)) arg = HCL_CHAR_TO_OOP(HCL_OOP_TO_SMOOI(arg));
if (!HCL_OOP_IS_CHAR(arg)) goto invalid_format; if (!HCL_OOP_IS_CHAR(arg)) goto invalid_format;
ooch = HCL_OOP_TO_CHAR(arg); ooch = HCL_OOP_TO_CHAR(arg);
print_char:
/* zeropad must not take effect for 'c' */
if (flagc & FLAGC_ZEROPAD) padc = ' ';
/* precision 0 doesn't kill the letter */ /* precision 0 doesn't kill the letter */
width--; width--;
if (!(flagc & FLAGC_LEFTADJ) && width > 0) PRINT_OOCH (padc, width); if (!(flagc & FLAGC_LEFTADJ) && width > 0) PRINT_OOCH (padc, width);
@ -1024,11 +1024,9 @@ reswitch:
extra = nslen; extra = nslen;
if (sign && ((HCL_OOP_IS_SMOOI(arg) && HCL_OOP_TO_SMOOI(arg) < 0) || HCL_IS_NBIGINT(hcl,arg))) neg = 1; if (sign && ((HCL_OOP_IS_SMOOI(arg) && HCL_OOP_TO_SMOOI(arg) < 0) || HCL_IS_NBIGINT(hcl,arg))) neg = 1;
if ((flagc & FLAGC_SHARP) && arg != HCL_SMOOI_TO_OOP(0)) if ((flagc & FLAGC_SHARP) && arg != HCL_SMOOI_TO_OOP(0))
{ {
if (base == 8) extra++; if (base == 2 || base == 8 || base == 16 || base == -16) extra += 2;
else if (base == 16 || base == -16) extra += 2;
} }
if (neg) extra++; if (neg) extra++;
else if (flagc & FLAGC_SIGN) extra++; else if (flagc & FLAGC_SIGN) extra++;
@ -1052,19 +1050,19 @@ reswitch:
if ((flagc & FLAGC_SHARP) && arg != HCL_SMOOI_TO_OOP(0)) if ((flagc & FLAGC_SHARP) && arg != HCL_SMOOI_TO_OOP(0))
{ {
if (base == 2) if (base == 2)
{ {
PRINT_OOCH ('0', 1); PRINT_OOCH ('#', 1);
PRINT_OOCH ('b', 1); PRINT_OOCH ('b', 1);
} }
if (base == 8) if (base == 8)
{ {
PRINT_OOCH ('0', 1); PRINT_OOCH ('#', 1);
PRINT_OOCH ('o', 1);
} }
else if (base == 16 || base == -16) else if (base == 16 || base == -16)
{ {
PRINT_OOCH ('0', 1); PRINT_OOCH ('#', 1);
PRINT_OOCH ('x', 1); PRINT_OOCH ('x', 1);
} }
} }

View File

@ -899,8 +899,7 @@ number:
nbufp = sprintn (nbuf, num, base, &tmp); nbufp = sprintn (nbuf, num, base, &tmp);
if ((flagc & FLAGC_SHARP) && num != 0) if ((flagc & FLAGC_SHARP) && num != 0)
{ {
if (base == 8) tmp++; if (base == 2 || base == 8 || base == 16) tmp += 2;
else if (base == 16) tmp += 2;
} }
if (neg) tmp++; if (neg) tmp++;
else if (flagc & FLAGC_SIGN) tmp++; else if (flagc & FLAGC_SIGN) tmp++;
@ -925,18 +924,20 @@ number:
if ((flagc & FLAGC_SHARP) && num != 0) if ((flagc & FLAGC_SHARP) && num != 0)
{ {
/* it follows the HCL's number notation, not C's */
if (base == 2) if (base == 2)
{ {
PUT_OOCH ('0', 1); PUT_OOCH ('#', 1);
PUT_OOCH ('b', 1); PUT_OOCH ('b', 1);
} }
if (base == 8) if (base == 8)
{ {
PUT_OOCH ('0', 1); PUT_OOCH ('#', 1);
PUT_OOCH ('o', 1);
} }
else if (base == 16) else if (base == 16)
{ {
PUT_OOCH ('0', 1); PUT_OOCH ('#', 1);
PUT_OOCH ('x', 1); PUT_OOCH ('x', 1);
} }
} }

View File

@ -122,7 +122,7 @@ static HCL_INLINE int print_single_char (hcl_t* hcl, int mask, hcl_ooch_t ch, hc
if (chu < ' ' || chu >= 0x80) if (chu < ' ' || chu >= 0x80)
#endif #endif
{ {
hcl_ooch_t escaped; hcl_oochu_t escaped;
switch (chu) switch (chu)
{ {
@ -151,14 +151,32 @@ static HCL_INLINE int print_single_char (hcl_t* hcl, int mask, hcl_ooch_t ch, hc
escaped = 'a'; escaped = 'a';
break; break;
default: default:
escaped = ch; escaped = chu;
break; break;
} }
if (escaped == ch) if (escaped == chu)
{
#if (HCL_SIZEOF_OOCH_T >= 4)
if (chu >= 0x10000u)
{
if (outbfmt(hcl, mask, "\\U%X", chu) <= -1) return -1;
}
else
#endif
{
#if (HCL_SIZEOF_OOCH_T >= 2)
if (chu >= 0x100u)
{
if (outbfmt(hcl, mask, "\\u%X", chu) <= -1) return -1;
}
else
#endif
{ {
if (outbfmt(hcl, mask, "\\x%X", chu) <= -1) return -1; if (outbfmt(hcl, mask, "\\x%X", chu) <= -1) return -1;
} }
}
}
else else
{ {
if (outbfmt(hcl, mask, "\\%jc", escaped) <= -1) return -1; if (outbfmt(hcl, mask, "\\%jc", escaped) <= -1) return -1;

View File

@ -421,7 +421,7 @@ static int get_char (hcl_t* hcl)
if (n == 0) if (n == 0)
{ {
return_eof: return_eof:
hcl->c->curinp->lxc.c = HCL_UCI_EOF; hcl->c->curinp->lxc.c = HCL_OOCI_EOF;
hcl->c->curinp->lxc.l.line = hcl->c->curinp->line; hcl->c->curinp->lxc.l.line = hcl->c->curinp->line;
hcl->c->curinp->lxc.l.colm = hcl->c->curinp->colm; hcl->c->curinp->lxc.l.colm = hcl->c->curinp->colm;
hcl->c->curinp->lxc.l.file = hcl->c->curinp->name; hcl->c->curinp->lxc.l.file = hcl->c->curinp->name;
@ -490,7 +490,7 @@ static int skip_comment (hcl_t* hcl)
do do
{ {
GET_CHAR_TO (hcl, c); GET_CHAR_TO (hcl, c);
if (c == HCL_UCI_EOF) if (c == HCL_OOCI_EOF)
{ {
break; break;
} }
@ -526,7 +526,7 @@ static int get_string (hcl_t* hcl, hcl_ooch_t end_char, hcl_ooch_t esc_char, int
{ {
GET_CHAR_TO (hcl, c); GET_CHAR_TO (hcl, c);
if (c == HCL_UCI_EOF) if (c == HCL_OOCI_EOF)
{ {
hcl_setsynerr (hcl, HCL_SYNERR_STRCHRNC, TOKEN_LOC(hcl) /*LEXER_LOC(hcl)*/, HCL_NULL); hcl_setsynerr (hcl, HCL_SYNERR_STRCHRNC, TOKEN_LOC(hcl) /*LEXER_LOC(hcl)*/, HCL_NULL);
return -1; return -1;
@ -536,6 +536,7 @@ static int get_string (hcl_t* hcl, hcl_ooch_t end_char, hcl_ooch_t esc_char, int
{ {
if (c >= '0' && c <= '7') if (c >= '0' && c <= '7')
{ {
/* more octal digits */
c_acc = c_acc * 8 + c - '0'; c_acc = c_acc * 8 + c - '0';
digit_count++; digit_count++;
if (digit_count >= escaped) if (digit_count >= escaped)
@ -640,20 +641,24 @@ static int get_string (hcl_t* hcl, hcl_ooch_t end_char, hcl_ooch_t esc_char, int
c_acc = 0; c_acc = 0;
continue; continue;
} }
else if (c == 'u' && HCL_SIZEOF(hcl_ooch_t) >= 2) #if (HCL_SIZEOF_OOCH_T >= 2)
else if (c == 'u')
{ {
escaped = 4; escaped = 4;
digit_count = 0; digit_count = 0;
c_acc = 0; c_acc = 0;
continue; continue;
} }
else if (c == 'U' && HCL_SIZEOF(hcl_ooch_t) >= 4) #endif
#if (HCL_SIZEOF_OOCH_T >= 4)
else if (c == 'U')
{ {
escaped = 8; escaped = 8;
digit_count = 0; digit_count = 0;
c_acc = 0; c_acc = 0;
continue; continue;
} }
#endif
else if (regex) else if (regex)
{ {
/* if the following character doesn't compose a proper /* if the following character doesn't compose a proper
@ -734,7 +739,6 @@ static int get_sharp_token (hcl_t* hcl)
* #false * #false
* #include * #include
* #\C character * #\C character
* #\XHHHH unicode character
* #\xHHHH unicode character * #\xHHHH unicode character
* #\UHHHH unicode character * #\UHHHH unicode character
* #\uHHHH unicode character * #\uHHHH unicode character
@ -783,17 +787,20 @@ static int get_sharp_token (hcl_t* hcl)
if (TOKEN_NAME_LEN(hcl) >= 4) if (TOKEN_NAME_LEN(hcl) >= 4)
{ {
if (TOKEN_NAME_CHAR(hcl, 2) == 'P' || TOKEN_NAME_CHAR(hcl, 2) == 'p') int max_digit_count = 0;
{
SET_TOKEN_TYPE (hcl, HCL_IOTOK_SMPTRLIT); if (TOKEN_NAME_CHAR(hcl, 2) == 'x')
goto hexcharlit;
}
else if (TOKEN_NAME_CHAR(hcl, 2) == 'X' || TOKEN_NAME_CHAR(hcl, 2) == 'x' ||
TOKEN_NAME_CHAR(hcl, 2) == 'U' || TOKEN_NAME_CHAR(hcl, 2) == 'u')
{ {
hcl_oow_t i; hcl_oow_t i;
max_digit_count = 2;
hexcharlit: hexcharlit:
if (TOKEN_NAME_LEN(hcl) - 3 > max_digit_count)
{
hcl_setsynerrbfmt (hcl, HCL_SYNERR_CHARLIT, TOKEN_LOC(hcl), TOKEN_NAME(hcl),
"invalid hexadecimal character in %.*js", TOKEN_NAME_LEN(hcl), TOKEN_NAME_PTR(hcl));
return -1;
}
c = 0; c = 0;
for (i = 3; i < TOKEN_NAME_LEN(hcl); i++) for (i = 3; i < TOKEN_NAME_LEN(hcl); i++)
{ {
@ -803,9 +810,29 @@ static int get_sharp_token (hcl_t* hcl)
"invalid hexadecimal character in %.*js", TOKEN_NAME_LEN(hcl), TOKEN_NAME_PTR(hcl)); "invalid hexadecimal character in %.*js", TOKEN_NAME_LEN(hcl), TOKEN_NAME_PTR(hcl));
return -1; return -1;
} }
/* TODO: check for the max charcter value and raise an error... */
c = c * 16 + CHAR_TO_NUM(hcl->c->tok.name.ptr[i], 16); /* don't care if it is for 'p' */ c = c * 16 + CHAR_TO_NUM(hcl->c->tok.name.ptr[i], 16); /* don't care if it is for 'p' */
} }
}
#if (HCL_SIZEOF_OOCH_T >= 2)
else if (TOKEN_NAME_CHAR(hcl, 2) == 'u')
{
max_digit_count = 4;
goto hexcharlit;
}
#endif
#if (HCL_SIZEOF_OOCH_T >= 4)
else if (TOKEN_NAME_CHAR(hcl, 2) == 'U')
{
max_digit_count = 8;
goto hexcharlit;
}
#endif
else if (TOKEN_NAME_CHAR(hcl, 2) == 'P' || TOKEN_NAME_CHAR(hcl, 2) == 'p')
{
SET_TOKEN_TYPE (hcl, HCL_IOTOK_SMPTRLIT);
goto hexcharlit;
} }
else if (TOKEN_NAME_CHAR(hcl, 2) == 'E' || TOKEN_NAME_CHAR(hcl, 2) == 'e') else if (TOKEN_NAME_CHAR(hcl, 2) == 'E' || TOKEN_NAME_CHAR(hcl, 2) == 'e')
{ {
@ -976,7 +1003,7 @@ retry:
switch (c) switch (c)
{ {
case HCL_UCI_EOF: case HCL_OOCI_EOF:
{ {
int n; int n;