ported the portability fix in bigint.c

This commit is contained in:
2025-08-05 22:35:17 +09:00
parent 9afb8e3898
commit 0da2d2abd3
37 changed files with 1992 additions and 1988 deletions

266
lib/fmt.c
View File

@ -25,7 +25,7 @@
*/
/*
* This file contains a formatted output routine derived from kvprintf()
* This file contains a formatted output routine derived from kvprintf()
* of FreeBSD. It has been heavily modified and bug-fixed.
*/
@ -71,7 +71,7 @@
#include <stdio.h> /* for snrintf(). used for floating-point number formatting */
#if defined(_MSC_VER) || defined(__BORLANDC__) || (defined(__WATCOMC__) && (__WATCOMC__ < 1200))
# define snprintf _snprintf
# define snprintf _snprintf
# if !defined(HAVE_SNPRINTF)
# define HAVE_SNPRINTF
# endif
@ -82,7 +82,7 @@
#endif /* MOO_ENABLE_FLTFMT */
/* Max number conversion buffer length:
/* Max number conversion buffer length:
* moo_intmax_t in base 2, plus NUL byte. */
#define MAXNBUF (MOO_SIZEOF(moo_intmax_t) * MOO_BITS_PER_BYTE + 1)
@ -107,7 +107,7 @@ static struct
{
moo_uint8_t flag; /* for single occurrence */
moo_uint8_t dflag; /* for double occurrence */
} lm_tab[26] =
} lm_tab[26] =
{
{ 0, 0 }, /* a */
{ 0, 0 }, /* b */
@ -138,7 +138,7 @@ static struct
};
enum
enum
{
FLAGC_DOT = (1 << 0),
FLAGC_SPACE = (1 << 1),
@ -153,14 +153,14 @@ enum
FLAGC_LENMOD = (1 << 10) /* length modifier */
};
static const moo_bch_t hex2ascii_lower[] =
static const moo_bch_t hex2ascii_lower[] =
{
'0','1','2','3','4','5','6','7','8','9',
'a','b','c','d','e','f','g','h','i','j','k','l','m',
'n','o','p','q','r','s','t','u','v','w','x','y','z'
};
static const moo_bch_t hex2ascii_upper[] =
static const moo_bch_t hex2ascii_upper[] =
{
'0','1','2','3','4','5','6','7','8','9',
'A','B','C','D','E','F','G','H','I','J','K','L','M',
@ -187,7 +187,7 @@ static moo_bch_t bch_nullstr[] = { '(','n','u','l','l', ')','\0' };
#include "fmt-imp.h"
int moo_fmt_intmax_to_bcstr (
moo_bch_t* buf, int size,
moo_bch_t* buf, int size,
moo_intmax_t value, int base_and_flags, int prec,
moo_bch_t fillchar, const moo_bch_t* prefix)
{
@ -219,7 +219,7 @@ int moo_fmt_intmax_to_bcstr (
}
int moo_fmt_uintmax_to_bcstr (
moo_bch_t* buf, int size,
moo_bch_t* buf, int size,
moo_uintmax_t value, int base_and_flags, int prec,
moo_bch_t fillchar, const moo_bch_t* prefix)
{
@ -245,7 +245,7 @@ int moo_fmt_uintmax_to_bcstr (
/* ==================== wide-char ===================================== */
int moo_fmt_intmax_to_ucstr (
moo_uch_t* buf, int size,
moo_uch_t* buf, int size,
moo_intmax_t value, int base_and_flags, int prec,
moo_uch_t fillchar, const moo_uch_t* prefix)
{
@ -277,7 +277,7 @@ int moo_fmt_intmax_to_ucstr (
}
int moo_fmt_uintmax_to_ucstr (
moo_uch_t* buf, int size,
moo_uch_t* buf, int size,
moo_uintmax_t value, int base_and_flags, int prec,
moo_uch_t fillchar, const moo_uch_t* prefix)
{
@ -441,10 +441,10 @@ static int fmt_outv (moo_fmtout_t* fmtout, va_list ap)
fmtptr = (const moo_uint8_t*)fmtout->fmt_str;
switch (fmtout->fmt_type)
{
case MOO_FMTOUT_FMT_TYPE_BCH:
case MOO_FMTOUT_FMT_TYPE_BCH:
fmtchsz = MOO_SIZEOF_BCH_T;
break;
case MOO_FMTOUT_FMT_TYPE_UCH:
case MOO_FMTOUT_FMT_TYPE_UCH:
fmtchsz = MOO_SIZEOF_UCH_T;
break;
}
@ -477,9 +477,9 @@ static int fmt_outv (moo_fmtout_t* fmtout, va_list ap)
{
const moo_bch_t* start, * end;
start = end = (const moo_bch_t*)fmtptr;
while ((bch = *end++) != '%' || stop)
while ((bch = *end++) != '%' || stop)
{
if (bch == '\0')
if (bch == '\0')
{
PUT_BCS (fmtout, start, end - start - 1);
goto done;
@ -495,9 +495,9 @@ static int fmt_outv (moo_fmtout_t* fmtout, va_list ap)
{
const moo_uch_t* start, * end;
start = end = (const moo_uch_t*)fmtptr;
while ((uch = *end++) != '%' || stop)
while ((uch = *end++) != '%' || stop)
{
if (uch == '\0')
if (uch == '\0')
{
PUT_UCS (fmtout, start, end - start - 1);
goto done;
@ -510,15 +510,15 @@ static int fmt_outv (moo_fmtout_t* fmtout, va_list ap)
goto handle_percent;
handle_percent:
padc = ' ';
padc = ' ';
width = 0; precision = 0; neg = 0; sign = 0;
lm_flag = 0; lm_dflag = 0; flagc = 0;
lm_flag = 0; lm_dflag = 0; flagc = 0;
sprintn = sprintn_lower;
reswitch:
switch (fmtout->fmt_type)
{
case MOO_FMTOUT_FMT_TYPE_BCH:
case MOO_FMTOUT_FMT_TYPE_BCH:
uch = *(const moo_bch_t*)fmtptr;
break;
case MOO_FMTOUT_FMT_TYPE_UCH:
@ -527,7 +527,7 @@ static int fmt_outv (moo_fmtout_t* fmtout, va_list ap)
}
fmtptr += fmtchsz;
switch (uch)
switch (uch)
{
case '%': /* %% */
bch = uch;
@ -539,7 +539,7 @@ static int fmt_outv (moo_fmtout_t* fmtout, va_list ap)
flagc |= FLAGC_DOT;
goto reswitch;
case '#':
case '#':
if (flagc & (FLAGC_WIDTH | FLAGC_DOT | FLAGC_LENMOD)) goto invalid_format;
flagc |= FLAGC_SHARP;
goto reswitch;
@ -572,30 +572,30 @@ static int fmt_outv (moo_fmtout_t* fmtout, va_list ap)
goto reswitch;
case '*': /* take the length from the parameter */
if (flagc & FLAGC_DOT)
if (flagc & FLAGC_DOT)
{
if (flagc & (FLAGC_STAR2 | FLAGC_PRECISION)) goto invalid_format;
flagc |= FLAGC_STAR2;
precision = va_arg(ap, moo_ooi_t); /* this deviates from the standard printf that accepts 'int' */
if (precision < 0)
if (precision < 0)
{
/* if precision is less than 0,
/* if precision is less than 0,
* treat it as if no .precision is specified */
flagc &= ~FLAGC_DOT;
precision = 0;
}
}
else
}
else
{
if (flagc & (FLAGC_STAR1 | FLAGC_WIDTH)) goto invalid_format;
flagc |= FLAGC_STAR1;
width = va_arg(ap, moo_ooi_t); /* it deviates from the standard printf that accepts 'int' */
if (width < 0)
if (width < 0)
{
/*
if (flagc & FLAGC_LEFTADJ)
if (flagc & FLAGC_LEFTADJ)
flagc &= ~FLAGC_LEFTADJ;
else
*/
@ -619,12 +619,12 @@ static int fmt_outv (moo_fmtout_t* fmtout, va_list ap)
case '5': case '6': case '7': case '8': case '9':
{
if (flagc & FLAGC_LENMOD) goto invalid_format;
for (n = 0;; fmtptr += fmtchsz)
for (n = 0;; fmtptr += fmtchsz)
{
n = n * 10 + uch - '0';
switch (fmtout->fmt_type)
{
case MOO_FMTOUT_FMT_TYPE_BCH:
case MOO_FMTOUT_FMT_TYPE_BCH:
uch = *(const moo_bch_t*)fmtptr;
break;
case MOO_FMTOUT_FMT_TYPE_UCH:
@ -633,13 +633,13 @@ static int fmt_outv (moo_fmtout_t* fmtout, va_list ap)
}
if (uch < '0' || uch > '9') break;
}
if (flagc & FLAGC_DOT)
if (flagc & FLAGC_DOT)
{
if (flagc & FLAGC_STAR2) goto invalid_format;
precision = n;
flagc |= FLAGC_PRECISION;
}
else
else
{
if (flagc & FLAGC_STAR1) goto invalid_format;
width = n;
@ -678,7 +678,7 @@ static int fmt_outv (moo_fmtout_t* fmtout, va_list ap)
goto invalid_format;
}
}
else
else
{
lm_flag |= lm_tab[uch - 'a'].flag;
goto reswitch;
@ -686,10 +686,10 @@ static int fmt_outv (moo_fmtout_t* fmtout, va_list ap)
break;
case 'L': /* long double */
if (flagc & FLAGC_LENMOD)
if (flagc & FLAGC_LENMOD)
{
/* conflict with other length modifier */
goto invalid_format;
goto invalid_format;
}
flagc |= FLAGC_LENMOD;
lm_flag |= LF_LD;
@ -699,7 +699,7 @@ static int fmt_outv (moo_fmtout_t* fmtout, va_list ap)
if (flagc & FLAGC_LENMOD)
{
/* conflict with other length modifier */
goto invalid_format;
goto invalid_format;
}
flagc |= FLAGC_LENMOD;
lm_flag |= LF_QD;
@ -721,12 +721,12 @@ static int fmt_outv (moo_fmtout_t* fmtout, va_list ap)
*(va_arg(ap, short int*)) = fmtout->count;
else if (lm_flag & LF_C) /* hh */
*(va_arg(ap, char*)) = fmtout->count;
else if (flagc & FLAGC_LENMOD)
else if (flagc & FLAGC_LENMOD)
goto invalid_format;
else
*(va_arg(ap, int*)) = fmtout->count;
break;
/* signed integer conversions */
case 'd':
case 'i': /* signed conversion */
@ -736,7 +736,7 @@ static int fmt_outv (moo_fmtout_t* fmtout, va_list ap)
/* end of signed integer conversions */
/* unsigned integer conversions */
case 'o':
case 'o':
base = 8;
goto handle_nosign;
case 'u':
@ -764,7 +764,7 @@ static int fmt_outv (moo_fmtout_t* fmtout, va_list ap)
case 'c':
{
/* zeropad must not take effect for 'c' */
if (flagc & FLAGC_ZEROPAD) padc = ' ';
if (flagc & FLAGC_ZEROPAD) padc = ' ';
if (lm_flag & LF_L) goto uppercase_c;
#if defined(MOO_OOCH_IS_UCH)
if (lm_flag & LF_J) goto uppercase_c;
@ -872,12 +872,12 @@ static int fmt_outv (moo_fmtout_t* fmtout, va_list ap)
const moo_uint8_t* bsp;
moo_oow_t k_hex_width;
/* zeropad must not take effect for 'k' and 'K'
*
/* zeropad must not take effect for 'k' and 'K'
*
* 'h' & 'l' is not used to differentiate moo_bch_t and moo_uch_t
* because 'k' means moo_byte_t.
* 'l', results in uppercase hexadecimal letters.
* 'h' drops the leading \x in the output
* because 'k' means moo_byte_t.
* 'l', results in uppercase hexadecimal letters.
* 'h' drops the leading \x in the output
* --------------------------------------------------------
* hk -> \x + non-printable in lowercase hex
* k -> all in lowercase hex
@ -922,9 +922,9 @@ static int fmt_outv (moo_fmtout_t* fmtout, va_list ap)
if (!(flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (fmtout, padc, width);
while (n--)
while (n--)
{
if ((lm_flag & LF_H) && BYTE_PRINTABLE(*bsp))
if ((lm_flag & LF_H) && BYTE_PRINTABLE(*bsp))
{
PUT_BCH (fmtout, *bsp, 1);
}
@ -946,7 +946,7 @@ static int fmt_outv (moo_fmtout_t* fmtout, va_list ap)
case 'W':
{
/* unicode string in unicode escape sequence.
*
*
* hw -> \uXXXX, \UXXXXXXXX, printable-byte(only in ascii range)
* w -> \uXXXX, \UXXXXXXXX
* lw -> all in \UXXXXXXXX
@ -960,7 +960,7 @@ static int fmt_outv (moo_fmtout_t* fmtout, va_list ap)
if (flagc & FLAGC_DOT)
{
/* if precision is specifed, it doesn't stop at the value of zero unlike 's' or 'S' */
for (n = 0; n < precision; n++)
for (n = 0; n < precision; n++)
{
if ((lm_flag & LF_H) && BYTE_PRINTABLE(usp[n])) uwid = 1;
else if (!(lm_flag & LF_L) && usp[n] <= 0xFFFF) uwid = 6;
@ -981,13 +981,13 @@ static int fmt_outv (moo_fmtout_t* fmtout, va_list ap)
if (!(flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (fmtout, padc, width);
while (n--)
while (n--)
{
if ((lm_flag & LF_H) && BYTE_PRINTABLE(*usp))
if ((lm_flag & LF_H) && BYTE_PRINTABLE(*usp))
{
PUT_OOCH(fmtout, *usp, 1);
}
else if (!(lm_flag & LF_L) && *usp <= 0xFFFF)
else if (!(lm_flag & LF_L) && *usp <= 0xFFFF)
{
moo_uint16_t u16 = *usp;
int extra_flags = ((uch) == 'w'? MOO_BYTE_TO_BCSTR_LOWERCASE: 0);
@ -1062,7 +1062,7 @@ static int fmt_outv (moo_fmtout_t* fmtout, va_list ap)
{
/* moo_flt_t is limited to double or long double */
/* precedence goes to double if sizeof(double) == sizeof(long double)
/* precedence goes to double if sizeof(double) == sizeof(long double)
* for example, %Lf didn't work on some old platforms.
* so i prefer the format specifier with no modifier.
*/
@ -1126,18 +1126,18 @@ static int fmt_outv (moo_fmtout_t* fmtout, va_list ap)
if (flagc & FLAGC_ZEROPAD) fb.fmt.ptr[fmtlen++] = '0';
if (flagc & FLAGC_STAR1) fb.fmt.ptr[fmtlen++] = '*';
else if (flagc & FLAGC_WIDTH)
else if (flagc & FLAGC_WIDTH)
{
fmtlen += moo_fmt_uintmax_to_bcstr(
&fb.fmt.ptr[fmtlen], fb.fmt.capa - fmtlen,
&fb.fmt.ptr[fmtlen], fb.fmt.capa - fmtlen,
width, 10, -1, '\0', MOO_NULL);
}
if (flagc & FLAGC_DOT) fb.fmt.ptr[fmtlen++] = '.';
if (flagc & FLAGC_STAR2) fb.fmt.ptr[fmtlen++] = '*';
else if (flagc & FLAGC_PRECISION)
else if (flagc & FLAGC_PRECISION)
{
fmtlen += moo_fmt_uintmax_to_bcstr(
&fb.fmt.ptr[fmtlen], fb.fmt.capa - fmtlen,
&fb.fmt.ptr[fmtlen], fb.fmt.capa - fmtlen,
precision, 10, -1, '\0', MOO_NULL);
}
@ -1154,7 +1154,7 @@ static int fmt_outv (moo_fmtout_t* fmtout, va_list ap)
#if defined(HAVE_SNPRINTF)
/* nothing special here */
#else
/* best effort to avoid buffer overflow when no snprintf is available.
/* best effort to avoid buffer overflow when no snprintf is available.
* i really can't do much if it happens. */
newcapa = precision + width + 32;
if (fb.out.capa < newcapa)
@ -1305,14 +1305,14 @@ static int fmt_outv (moo_fmtout_t* fmtout, va_list ap)
num = va_arg(ap, int);
number:
if (sign && (moo_intmax_t)num < 0)
if (sign && (moo_intmax_t)num < 0)
{
neg = 1;
num = -(moo_intmax_t)num;
}
nbufp = sprintn(nbuf, num, base, &tmp);
if ((flagc & FLAGC_SHARP) && num != 0)
if ((flagc & FLAGC_SHARP) && num != 0)
{
if (base == 2 || base == 8) tmp += 2;
else if (base == 16) tmp += 3;
@ -1322,7 +1322,7 @@ static int fmt_outv (moo_fmtout_t* fmtout, va_list ap)
else if (flagc & FLAGC_SPACE) tmp++;
numlen = (int)((const moo_bch_t*)nbufp - (const moo_bch_t*)nbuf);
if ((flagc & FLAGC_DOT) && precision > numlen)
if ((flagc & FLAGC_DOT) && precision > numlen)
{
/* extra zeros for precision specified */
tmp += (precision - numlen);
@ -1338,19 +1338,19 @@ static int fmt_outv (moo_fmtout_t* fmtout, va_list ap)
else if (flagc & FLAGC_SIGN) PUT_OOCH (fmtout, '+', 1);
else if (flagc & FLAGC_SPACE) PUT_OOCH (fmtout, ' ', 1);
if ((flagc & FLAGC_SHARP) && num != 0)
if ((flagc & FLAGC_SHARP) && num != 0)
{
if (base == 2)
if (base == 2)
{
PUT_OOCH (fmtout, '2', 1);
PUT_OOCH (fmtout, 'r', 1);
}
if (base == 8)
if (base == 8)
{
PUT_OOCH (fmtout, '8', 1);
PUT_OOCH (fmtout, 'r', 1);
}
else if (base == 16)
}
else if (base == 16)
{
PUT_OOCH (fmtout, '1', 1);
PUT_OOCH (fmtout, '6', 1);
@ -1507,7 +1507,7 @@ int moo_ufmt_out (moo_fmtout_t* fmtout, const moo_uch_t* fmt, ...)
return n;
}
/* --------------------------------------------------------------------------
/* --------------------------------------------------------------------------
* OBJECT OUTPUT
* -------------------------------------------------------------------------- */
@ -1574,7 +1574,7 @@ int moo_fmt_object_ (moo_fmtout_t* fmtout, moo_oop_t oop)
for (i = 0; i < MOO_OBJ_GET_SIZE(oop); i++)
{
ch = MOO_OBJ_GET_CHAR_VAL(oop, i);
if ((ch >= '\0' && ch < ' ') || ch == '\\' || ch == '\"')
if ((ch >= '\0' && ch < ' ') || ch == '\\' || ch == '\"')
{
escape = 1;
break;
@ -1589,7 +1589,7 @@ int moo_fmt_object_ (moo_fmtout_t* fmtout, moo_oop_t oop)
for (i = 0; i < MOO_OBJ_GET_SIZE(oop); i++)
{
ch = MOO_OBJ_GET_CHAR_VAL(oop, i);
if (ch >= '\0' && ch < ' ')
if (ch >= '\0' && ch < ' ')
{
switch (ch)
{
@ -1618,7 +1618,7 @@ int moo_fmt_object_ (moo_fmtout_t* fmtout, moo_oop_t oop)
escaped = 'a';
break;
default:
/* since it's less than ' ' and greater than or equal to '\0' ,
/* since it's less than ' ' and greater than or equal to '\0' ,
* it should not exceed 0xFF regardless of character mode. %02X should be good enough */
if (moo_bfmt_out(fmtout, "\\x%02X", (moo_oochu_t)ch) <= -1) return -1;
continue;
@ -1657,7 +1657,7 @@ int moo_fmt_object_ (moo_fmtout_t* fmtout, moo_oop_t oop)
}
if (moo_bfmt_out(fmtout, "]") <= -1) return -1;
}
else if (MOO_OBJ_GET_FLAGS_TYPE(oop) == MOO_OBJ_TYPE_HALFWORD)
{
if (moo_bfmt_out(fmtout, "#[[") <= -1) return -1;; /* TODO: fix this symbol/notation */
@ -1857,7 +1857,7 @@ int moo_fmt_object_ (moo_fmtout_t* fmtout, moo_oop_t oop)
}
/* --------------------------------------------------------------------------
/* --------------------------------------------------------------------------
* FORMATTED LOG OUTPUT
* -------------------------------------------------------------------------- */
@ -1890,7 +1890,7 @@ redo:
moo_ooch_t* tmp;
max = MOO_TYPE_MAX(moo_oow_t) - moo->log.len;
if (len > max)
if (len > max)
{
/* data too big. */
rem += len - max;
@ -1910,7 +1910,7 @@ redo:
/* +1 to handle line ending injection more easily */
tmp = moo_reallocmem(moo, moo->log.ptr, (newcapa + 1) * MOO_SIZEOF(*tmp));
if (!tmp)
if (!tmp)
{
make_do:
if (moo->log.len > 0)
@ -2006,17 +2006,17 @@ moo_ooi_t moo_logbfmtv (moo_t* moo, moo_bitmask_t mask, const moo_bch_t* fmt, va
int x;
moo_fmtout_t fo;
if (moo->log.default_type_mask & MOO_LOG_ALL_TYPES)
if (moo->log.default_type_mask & MOO_LOG_ALL_TYPES)
{
/* if a type is given, it's not untyped any more.
* mask off the UNTYPED bit */
mask &= ~MOO_LOG_UNTYPED;
mask &= ~MOO_LOG_UNTYPED;
/* if the default_type_mask has the UNTYPED bit on,
* it'll get turned back on */
mask |= (moo->log.default_type_mask & MOO_LOG_ALL_TYPES);
}
else if (!(mask & MOO_LOG_ALL_TYPES))
else if (!(mask & MOO_LOG_ALL_TYPES))
{
/* no type is set in the given mask and no default type is set.
* make it UNTYPED. */
@ -2061,17 +2061,17 @@ moo_ooi_t moo_logufmtv (moo_t* moo, moo_bitmask_t mask, const moo_uch_t* fmt, va
int x;
moo_fmtout_t fo;
if (moo->log.default_type_mask & MOO_LOG_ALL_TYPES)
if (moo->log.default_type_mask & MOO_LOG_ALL_TYPES)
{
/* if a type is given, it's not untyped any more.
* mask off the UNTYPED bit */
mask &= ~MOO_LOG_UNTYPED;
mask &= ~MOO_LOG_UNTYPED;
/* if the default_type_mask has the UNTYPED bit on,
* it'll get turned back on */
mask |= (moo->log.default_type_mask & MOO_LOG_ALL_TYPES);
}
else if (!(mask & MOO_LOG_ALL_TYPES))
else if (!(mask & MOO_LOG_ALL_TYPES))
{
/* no type is set in the given mask and no default type is set.
* make it UNTYPED. */
@ -2221,7 +2221,7 @@ static MOO_INLINE int format_stack_args (moo_fmtout_t* fmtout, moo_ooi_t nargs,
moo_ooci_t ch;
int flagc, lm_flag;
struct
struct
{
moo_ooi_t idx;
int stop;
@ -2246,7 +2246,7 @@ static MOO_INLINE int format_stack_args (moo_fmtout_t* fmtout, moo_ooi_t nargs,
if (!MOO_OOP_IS_POINTER(arg) || MOO_OBJ_GET_FLAGS_TYPE(arg) != MOO_OBJ_TYPE_CHAR)
{
moo_ooi_t i;
/* if the first argument is not a valid formatting string,
/* if the first argument is not a valid formatting string,
* print all arguments as objects */
if (fmtout->putobj(fmtout, arg) <= -1) goto oops;
for (i = arg_state.idx; i < nargs; i++)
@ -2271,11 +2271,11 @@ static MOO_INLINE int format_stack_args (moo_fmtout_t* fmtout, moo_ooi_t nargs,
GET_NEXT_CHAR_TO (moo, fmtptr, fmtend, ch);
if (ch == '%' && !arg_state.stop) break;
if (ch == MOO_OOCI_EOF)
if (ch == MOO_OOCI_EOF)
{
/* fmt is not advanced when it is length-bounded.
* so not fmt - checkpoint - 1 */
PUT_OOCS (fmtout, checkpoint, fmtptr - checkpoint);
PUT_OOCS (fmtout, checkpoint, fmtptr - checkpoint);
goto done;
}
}
@ -2283,16 +2283,16 @@ static MOO_INLINE int format_stack_args (moo_fmtout_t* fmtout, moo_ooi_t nargs,
percent = fmtptr - 1;
padc = ' ';
padc = ' ';
width = 0; precision = 0;
neg = 0; sign = 0;
lm_flag = 0; flagc = 0;
lm_flag = 0; flagc = 0;
radix_flags = MOO_INTTOSTR_NONEWOBJ;
reswitch:
GET_NEXT_CHAR_TO (moo, fmtptr, fmtend, ch);
switch (ch)
switch (ch)
{
case '%': /* %% */
ooch = ch;
@ -2304,7 +2304,7 @@ static MOO_INLINE int format_stack_args (moo_fmtout_t* fmtout, moo_ooi_t nargs,
flagc |= FLAGC_DOT;
goto reswitch;
case '#':
case '#':
if (flagc & (FLAGC_WIDTH | FLAGC_DOT | FLAGC_LENMOD)) goto invalid_format;
flagc |= FLAGC_SHARP;
goto reswitch;
@ -2334,33 +2334,33 @@ static MOO_INLINE int format_stack_args (moo_fmtout_t* fmtout, moo_ooi_t nargs,
flagc &= ~FLAGC_ZEROPAD;
}
}
goto reswitch;
case '*': /* take the length from the parameter */
if (flagc & FLAGC_DOT)
if (flagc & FLAGC_DOT)
{
if (flagc & (FLAGC_STAR2 | FLAGC_PRECISION)) goto invalid_format;
flagc |= FLAGC_STAR2;
GET_NEXT_ARG_TO (moo, nargs, &arg_state, arg);
if (moo_inttoooi_noseterr(moo, arg, &precision) <= -1) goto invalid_format;
if (precision < 0)
if (precision < 0)
{
/* if precision is less than 0,
/* if precision is less than 0,
* treat it as if no .precision is specified */
flagc &= ~FLAGC_DOT;
precision = 0;
}
}
else
}
else
{
if (flagc & (FLAGC_STAR1 | FLAGC_WIDTH)) goto invalid_format;
flagc |= FLAGC_STAR1;
GET_NEXT_ARG_TO (moo, nargs, &arg_state, arg);
if (moo_inttoooi_noseterr(moo, arg, &width) <= -1) goto invalid_format;
if (width < 0)
if (width < 0)
{
flagc |= FLAGC_LEFTADJ;
width = -width;
@ -2381,19 +2381,19 @@ static MOO_INLINE int format_stack_args (moo_fmtout_t* fmtout, moo_ooi_t nargs,
case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
if (flagc & FLAGC_LENMOD) goto invalid_format;
for (n = 0;; ++fmtptr)
for (n = 0;; ++fmtptr)
{
n = n * 10 + ch - '0';
ch = *fmtptr;
if (ch < '0' || ch > '9') break;
}
if (flagc & FLAGC_DOT)
if (flagc & FLAGC_DOT)
{
if (flagc & FLAGC_STAR2) goto invalid_format;
precision = n;
flagc |= FLAGC_PRECISION;
}
else
else
{
if (flagc & FLAGC_STAR1) goto invalid_format;
width = n;
@ -2408,14 +2408,14 @@ static MOO_INLINE int format_stack_args (moo_fmtout_t* fmtout, moo_ooi_t nargs,
flagc |= FLAGC_LENMOD;
lm_flag |= lm_tab[ch - 'a'].flag;
goto reswitch;
/* integer conversions */
case 'd':
case 'i': /* signed conversion */
radix = 10;
sign = 1;
goto print_integer;
case 'o':
case 'o':
radix = 8;
goto print_integer;
case 'u':
@ -2438,7 +2438,7 @@ static MOO_INLINE int format_stack_args (moo_fmtout_t* fmtout, moo_ooi_t nargs,
moo_oow_t scale = 0;
GET_NEXT_ARG_TO (moo, nargs, &arg_state, arg);
if (MOO_OOP_IS_CHAR(arg))
if (MOO_OOP_IS_CHAR(arg))
{
arg = MOO_SMOOI_TO_OOP(MOO_OOP_TO_CHAR(arg));
}
@ -2449,7 +2449,7 @@ static MOO_INLINE int format_stack_args (moo_fmtout_t* fmtout, moo_ooi_t nargs,
arg = fa->value;
}
if (!moo_inttostr(moo, arg, 10 | MOO_INTTOSTR_NONEWOBJ))
if (!moo_inttostr(moo, arg, 10 | MOO_INTTOSTR_NONEWOBJ))
{
MOO_LOG1 (moo, MOO_LOG_WARN | MOO_LOG_UNTYPED, "unable to convert %O to string \n", arg);
goto invalid_format;
@ -2467,13 +2467,13 @@ static MOO_INLINE int format_stack_args (moo_fmtout_t* fmtout, moo_ooi_t nargs,
neg = 1;
}
if (!(flagc & FLAGC_DOT))
if (!(flagc & FLAGC_DOT))
{
precision = scale;
if (precision <= 0) precision = 1;
}
if ((flagc & FLAGC_DOT) && precision < scale)
if ((flagc & FLAGC_DOT) && precision < scale)
{
moo_oow_t diff = scale - precision;
scale = precision;
@ -2500,7 +2500,7 @@ static MOO_INLINE int format_stack_args (moo_fmtout_t* fmtout, moo_ooi_t nargs,
else if (flagc & FLAGC_SIGN) extra++;
else if (flagc & FLAGC_SPACE) extra++;
if ((flagc & FLAGC_DOT) && precision > scale)
if ((flagc & FLAGC_DOT) && precision > scale)
{
/* trailing zeros in the fractional part */
extra += precision - scale;
@ -2535,13 +2535,13 @@ static MOO_INLINE int format_stack_args (moo_fmtout_t* fmtout, moo_ooi_t nargs,
else
{
if (nslen > 0) PUT_OOCS (fmtout, nsptr, nslen - scale);
if (precision > 0)
if (precision > 0)
{
PUT_OOCH (fmtout, '.', 1);
if (nslen > 0) PUT_OOCS (fmtout, &nsptr[nslen - scale], scale);
}
}
if (precision > scale)
if (precision > scale)
{
/* trailing zeros in the fractional part */
PUT_OOCH (fmtout, '0', precision - scale);
@ -2564,7 +2564,7 @@ static MOO_INLINE int format_stack_args (moo_fmtout_t* fmtout, moo_ooi_t nargs,
print_char:
/* zeropad must not take effect for 'c' */
if (flagc & FLAGC_ZEROPAD) padc = ' ';
if (flagc & FLAGC_ZEROPAD) padc = ' ';
/* precision 0 doesn't kill the letter */
width--;
@ -2657,7 +2657,7 @@ static MOO_INLINE int format_stack_args (moo_fmtout_t* fmtout, moo_ooi_t nargs,
case MOO_OBJ_TYPE_BYTE:
bsp = MOO_OBJ_GET_BYTE_SLOT(arg);
bsl = MOO_OBJ_GET_SIZE(arg);
format_byte_in_k:
k_hex_width = (lm_flag & (LF_H | LF_L))? 4: 2;
@ -2679,9 +2679,9 @@ static MOO_INLINE int format_stack_args (moo_fmtout_t* fmtout, moo_ooi_t nargs,
if (!(flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (fmtout, padc, width);
while (n--)
while (n--)
{
if ((lm_flag & LF_H) && BYTE_PRINTABLE(*bsp))
if ((lm_flag & LF_H) && BYTE_PRINTABLE(*bsp))
{
PUT_BCH (fmtout, *bsp, 1);
}
@ -2715,7 +2715,7 @@ static MOO_INLINE int format_stack_args (moo_fmtout_t* fmtout, moo_ooi_t nargs,
case 'W':
{
/* unicode string in unicode escape sequence.
*
*
* hw -> \uXXXX, \UXXXXXXXX, printable-byte(only in ascii range)
* w -> \uXXXX, \UXXXXXXXX
* lw -> all in \UXXXXXXXX
@ -2737,7 +2737,7 @@ static MOO_INLINE int format_stack_args (moo_fmtout_t* fmtout, moo_ooi_t nargs,
}
else n = usl;
for (i = 0; i < n; i++)
for (i = 0; i < n; i++)
{
if ((lm_flag & LF_H) && BYTE_PRINTABLE(usp[n])) uwid = 1;
else if (!(lm_flag & LF_L) && usp[n] <= 0xFFFF) uwid = 6;
@ -2747,13 +2747,13 @@ static MOO_INLINE int format_stack_args (moo_fmtout_t* fmtout, moo_ooi_t nargs,
if (!(flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (fmtout, padc, width);
while (n--)
while (n--)
{
if ((lm_flag & LF_H) && BYTE_PRINTABLE(*usp))
if ((lm_flag & LF_H) && BYTE_PRINTABLE(*usp))
{
PUT_OOCH(fmtout, *usp, 1);
}
else if (!(lm_flag & LF_L) && *usp <= 0xFFFF)
else if (!(lm_flag & LF_L) && *usp <= 0xFFFF)
{
moo_uint16_t u16 = *usp;
int extra_flags = ((ch) == 'w'? MOO_BYTE_TO_BCSTR_LOWERCASE: 0);
@ -2790,7 +2790,7 @@ static MOO_INLINE int format_stack_args (moo_fmtout_t* fmtout, moo_ooi_t nargs,
moo_oow_t nslen;
GET_NEXT_ARG_TO (moo, nargs, &arg_state, arg);
if (MOO_OOP_IS_CHAR(arg))
if (MOO_OOP_IS_CHAR(arg))
{
arg = MOO_SMOOI_TO_OOP(MOO_OOP_TO_CHAR(arg));
}
@ -2813,7 +2813,7 @@ static MOO_INLINE int format_stack_args (moo_fmtout_t* fmtout, moo_ooi_t nargs,
arg = nv;
}
if (!moo_inttostr(moo, arg, radix | radix_flags))
if (!moo_inttostr(moo, arg, radix | radix_flags))
{
/*moo_seterrbfmt (moo, MOO_EINVAL, "not a valid number - %O", arg);
goto oops;*/
@ -2825,19 +2825,19 @@ static MOO_INLINE int format_stack_args (moo_fmtout_t* fmtout, moo_ooi_t nargs,
nslen = moo->inttostr.xbuf.len;
MOO_ASSERT (moo, nslen > 0);
if (nsptr[0] == '-')
if (nsptr[0] == '-')
{
/* a negative number was given. i must skip the minus sign
/* a negative number was given. i must skip the minus sign
* added by moo_inttostr() for a negative number. */
MOO_ASSERT (moo, (MOO_OOP_IS_SMOOI(arg) && MOO_OOP_TO_SMOOI(arg) < 0) || MOO_OOP_IS_NBIGINT(moo,arg));
nsptr++;
nslen--;
}
nslen--;
}
extra = nslen;
if (sign && ((MOO_OOP_IS_SMOOI(arg) && MOO_OOP_TO_SMOOI(arg) < 0) || MOO_OOP_IS_NBIGINT(moo,arg))) neg = 1;
if ((flagc & FLAGC_SHARP) && arg != MOO_SMOOI_TO_OOP(0))
if ((flagc & FLAGC_SHARP) && arg != MOO_SMOOI_TO_OOP(0))
{
if (radix == 2 || radix == 8 || radix == 16) extra += 2;
}
@ -2845,7 +2845,7 @@ static MOO_INLINE int format_stack_args (moo_fmtout_t* fmtout, moo_ooi_t nargs,
else if (flagc & FLAGC_SIGN) extra++;
else if (flagc & FLAGC_SPACE) extra++;
if ((flagc & FLAGC_DOT) && precision > nslen)
if ((flagc & FLAGC_DOT) && precision > nslen)
{
/* extra zeros for precision specified */
extra += (precision - nslen);
@ -2861,19 +2861,19 @@ static MOO_INLINE int format_stack_args (moo_fmtout_t* fmtout, moo_ooi_t nargs,
else if (flagc & FLAGC_SIGN) PUT_OOCH (fmtout, '+', 1);
else if (flagc & FLAGC_SPACE) PUT_OOCH (fmtout, ' ', 1);
if ((flagc & FLAGC_SHARP) && arg != MOO_SMOOI_TO_OOP(0))
if ((flagc & FLAGC_SHARP) && arg != MOO_SMOOI_TO_OOP(0))
{
if (radix == 2)
if (radix == 2)
{
PUT_OOCH (fmtout, '2', 1);
PUT_OOCH (fmtout, 'r', 1);
}
if (radix == 8)
if (radix == 8)
{
PUT_OOCH (fmtout, '8', 1);
PUT_OOCH (fmtout, 'r', 1);
}
else if (radix == 16)
}
else if (radix == 16)
{
PUT_OOCH (fmtout, '1', 1);
PUT_OOCH (fmtout, '6', 1);
@ -2936,7 +2936,7 @@ int moo_strfmtcallstack (moo_t* moo, moo_ooi_t nargs, int rcv_is_fmtstr)
fo.putbchars = sprint_bchars;
fo.putuchars = sprint_uchars;
fo.putobj = moo_fmt_object_;
/* format_stack_args doesn't use fmt_str and fmt_type.
/* format_stack_args doesn't use fmt_str and fmt_type.
* it takes the format string from the stack. */
moo->sprintf.xbuf.len = 0;
@ -2967,9 +2967,9 @@ static int fmt_put_bchars_to_uch_buf (moo_fmtout_t* fmtout, const moo_bch_t* ptr
ucslen = b->capa - b->len;
n = moo_conv_bchars_to_uchars_with_cmgr(ptr, &bcslen, &b->ptr[b->len], &ucslen, b->moo->_cmgr, 1);
b->len += ucslen;
if (n <= -1)
if (n <= -1)
{
if (n == -2)
if (n == -2)
{
return 0; /* buffer full. stop */
}