diff --git a/moo/lib/Makefile.am b/moo/lib/Makefile.am index b62f701..2e30a53 100644 --- a/moo/lib/Makefile.am +++ b/moo/lib/Makefile.am @@ -70,8 +70,6 @@ libmoo_la_SOURCES = \ err.c \ exec.c \ fmt.c \ - fmtout.c \ - fmtoutv.h \ gc.c \ heap.c \ mb8.c \ diff --git a/moo/lib/Makefile.in b/moo/lib/Makefile.in index 737f6ba..a31349a 100644 --- a/moo/lib/Makefile.in +++ b/moo/lib/Makefile.in @@ -160,11 +160,11 @@ am__DEPENDENCIES_1 = am_libmoo_la_OBJECTS = libmoo_la-bigint.lo libmoo_la-comp.lo \ libmoo_la-debug.lo libmoo_la-decode.lo libmoo_la-dic.lo \ libmoo_la-err.lo libmoo_la-exec.lo libmoo_la-fmt.lo \ - libmoo_la-fmtout.lo libmoo_la-gc.lo libmoo_la-heap.lo \ - libmoo_la-mb8.lo libmoo_la-moo.lo libmoo_la-number.lo \ - libmoo_la-obj.lo libmoo_la-pf-basic.lo libmoo_la-pf-sys.lo \ - libmoo_la-pf-utf8.lo libmoo_la-rbt.lo libmoo_la-sym.lo \ - libmoo_la-utf8.lo libmoo_la-utf16.lo libmoo_la-utl.lo + libmoo_la-gc.lo libmoo_la-heap.lo libmoo_la-mb8.lo \ + libmoo_la-moo.lo libmoo_la-number.lo libmoo_la-obj.lo \ + libmoo_la-pf-basic.lo libmoo_la-pf-sys.lo libmoo_la-pf-utf8.lo \ + libmoo_la-rbt.lo libmoo_la-sym.lo libmoo_la-utf8.lo \ + libmoo_la-utf16.lo libmoo_la-utl.lo libmoo_la_OBJECTS = $(am_libmoo_la_OBJECTS) AM_V_lt = $(am__v_lt_@AM_V@) am__v_lt_ = $(am__v_lt_@AM_DEFAULT_V@) @@ -406,7 +406,6 @@ pdfdir = @pdfdir@ prefix = @prefix@ program_transform_name = @program_transform_name@ psdir = @psdir@ -runstatedir = @runstatedir@ sbindir = @sbindir@ sharedstatedir = @sharedstatedir@ srcdir = @srcdir@ @@ -467,8 +466,6 @@ libmoo_la_SOURCES = \ err.c \ exec.c \ fmt.c \ - fmtout.c \ - fmtoutv.h \ gc.c \ heap.c \ mb8.c \ @@ -610,7 +607,6 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libmoo_la-err.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libmoo_la-exec.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libmoo_la-fmt.Plo@am__quote@ -@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libmoo_la-fmtout.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libmoo_la-gc.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libmoo_la-heap.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libmoo_la-mb8.Plo@am__quote@ @@ -709,13 +705,6 @@ libmoo_la-fmt.lo: fmt.c @AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ @am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libmoo_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o libmoo_la-fmt.lo `test -f 'fmt.c' || echo '$(srcdir)/'`fmt.c -libmoo_la-fmtout.lo: fmtout.c -@am__fastdepCC_TRUE@ $(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libmoo_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT libmoo_la-fmtout.lo -MD -MP -MF $(DEPDIR)/libmoo_la-fmtout.Tpo -c -o libmoo_la-fmtout.lo `test -f 'fmtout.c' || echo '$(srcdir)/'`fmtout.c -@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/libmoo_la-fmtout.Tpo $(DEPDIR)/libmoo_la-fmtout.Plo -@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='fmtout.c' object='libmoo_la-fmtout.lo' libtool=yes @AMDEPBACKSLASH@ -@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ -@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libmoo_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o libmoo_la-fmtout.lo `test -f 'fmtout.c' || echo '$(srcdir)/'`fmtout.c - libmoo_la-gc.lo: gc.c @am__fastdepCC_TRUE@ $(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libmoo_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT libmoo_la-gc.lo -MD -MP -MF $(DEPDIR)/libmoo_la-gc.Tpo -c -o libmoo_la-gc.lo `test -f 'gc.c' || echo '$(srcdir)/'`gc.c @am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/libmoo_la-gc.Tpo $(DEPDIR)/libmoo_la-gc.Plo diff --git a/moo/lib/fmt.c b/moo/lib/fmt.c index 59df2a6..6d31f1a 100644 --- a/moo/lib/fmt.c +++ b/moo/lib/fmt.c @@ -24,9 +24,63 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +/* + * This file contains a formatted output routine derived from kvprintf() + * of FreeBSD. It has been heavily modified and bug-fixed. + */ + +/* + * Copyright (c) 1986, 1988, 1991, 1993 + * The Regents of the University of California. All rights reserved. + * (c) UNIX System Laboratories, Inc. + * All or some portions of this file are derived from material licensed + * to the University of California by American Telephone and Telegraph + * Co. or Unix System Laboratories, Inc. and are reproduced herein with + * the permission of UNIX System Laboratories, Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ + #include "moo-prv.h" + +#if 0 +#include /* for snrintf(). used for floating-point number formatting */ +#if defined(_MSC_VER) || defined(__BORLANDC__) || (defined(__WATCOMC__) && (__WATCOMC__ < 1200)) +# define snprintf _snprintf +# if !defined(HAVE_SNPRINTF) +# define HAVE_SNPRINTF +# endif +#endif +#if defined(HAVE_QUADMATH_H) +# include /* for quadmath_snprintf() */ +#endif +#endif + /* 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) @@ -718,7 +772,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++) /* nothing */; + n = precision; } else { @@ -1842,51 +1896,6 @@ static int sprint_ucs (moo_fmtout_t* fmtout, const moo_uch_t* ptr, moo_oow_t len return 1; /* success */ } - -moo_ooi_t moo_sproutbfmt (moo_t* moo, moo_bitmask_t mask, const moo_bch_t* fmt, ...) -{ - int x; - va_list ap; - moo_fmtout_t fo; - - MOO_MEMSET (&fo, 0, MOO_SIZEOF(fo)); - fo.fmt_type = MOO_FMTOUT_FMT_TYPE_BCH; - fo.fmt_str = fmt; - fo.mask = mask; - fo.putbcs = sprint_bcs; - fo.putucs = sprint_ucs; - fo.putobj = moo_fmt_object_; - - va_start (ap, fmt); - x = fmt_outv(&fo, ap); - va_end (ap); - - return (x <= -1)? -1: fo.count; -} - -moo_ooi_t moo_sproutufmt (moo_t* moo, moo_bitmask_t mask, const moo_uch_t* fmt, ...) -{ - int x; - va_list ap; - moo_fmtout_t fo; - - MOO_MEMSET (&fo, 0, MOO_SIZEOF(fo)); - fo.fmt_type = MOO_FMTOUT_FMT_TYPE_UCH; - fo.fmt_str = fmt; - fo.mask = mask; - fo.putbcs = sprint_bcs; - fo.putucs = sprint_ucs; - fo.putobj = moo_fmt_object_; - - va_start (ap, fmt); - x = fmt_outv(&fo, ap); - va_end (ap); - - return (x <= -1)? -1: fo.count; -} - - - #define GET_NEXT_ARG_TO(moo,nargs,arg_state,arg) do { \ if ((arg_state)->idx >= nargs) { (arg_state)->stop = 1; goto invalid_format; } \ arg = MOO_STACK_GETARG(moo, nargs, (arg_state)->idx); \ @@ -1909,7 +1918,7 @@ static MOO_INLINE int format_stack_args (moo_fmtout_t* fmtout, moo_ooi_t nargs, moo_ooi_t extra, width, precision; moo_ooch_t padc, ooch; moo_ooci_t ch; - int flagc; + int flagc, lm_flag; struct { @@ -1977,7 +1986,7 @@ static MOO_INLINE int format_stack_args (moo_fmtout_t* fmtout, moo_ooi_t nargs, width = 0; precision = 0; neg = 0; sign = 0; - flagc = 0; + lm_flag = 0; flagc = 0; radix_flags = MOO_INTTOSTR_NONEWOBJ; reswitch: @@ -2090,6 +2099,14 @@ static MOO_INLINE int format_stack_args (moo_fmtout_t* fmtout, moo_ooi_t nargs, flagc |= FLAGC_WIDTH; } goto reswitch; + + /* length modifiers - used for k/K. not useful for s/S/d/i/o/u/x/X/b/f */ + case 'h': /* short int */ + case 'l': /* long int */ + if (lm_flag & (LF_L | LF_H)) goto invalid_format; + flagc |= FLAGC_LENMOD; + lm_flag |= lm_tab[ch - 'a'].flag; + goto reswitch; /* integer conversions */ case 'd': @@ -2258,26 +2275,193 @@ static MOO_INLINE int format_stack_args (moo_fmtout_t* fmtout, moo_ooi_t nargs, case 's': case 'S': { - const moo_ooch_t* oosp; - moo_oow_t oosl; - /* zeropad must not take effect for 'S' */ if (flagc & FLAGC_ZEROPAD) padc = ' '; + GET_NEXT_ARG_TO (moo, nargs, &arg_state, arg); + if (!MOO_OOP_IS_POINTER(arg)) goto invalid_format; + switch (MOO_OBJ_GET_FLAGS_TYPE(arg)) + { + case MOO_OBJ_TYPE_CHAR: + { + /* string, symbol */ + const moo_ooch_t* oosp; + moo_oow_t oosl; + + oosp = MOO_OBJ_GET_CHAR_SLOT(arg); + oosl = MOO_OBJ_GET_SIZE(arg); + + if (flagc & FLAGC_DOT) + { + if (oosl > precision) oosl = precision; + } + width -= oosl; + + if (!(flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (fmtout, padc, width); + PUT_OOCS (fmtout, oosp, oosl); + if ((flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (fmtout, padc, width); + break; + } + + case MOO_OBJ_TYPE_BYTE: + { + /* byte array */ + const moo_uint8_t* bsp; + moo_oow_t bsl; + + bsp = MOO_OBJ_GET_BYTE_SLOT(arg); + bsl = MOO_OBJ_GET_SIZE(arg); + + if (flagc & FLAGC_DOT) + { + if (bsl > precision) bsl = precision; + } + width -= bsl; + + if (!(flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (fmtout, padc, width); + PUT_BCS (fmtout, (const moo_bch_t*)bsp, bsl); + if ((flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (fmtout, padc, width); + break; + } + + default: + goto invalid_format; + } + + break; + } + + case 'k': + case 'K': + { + const moo_uint8_t* bsp; + moo_oow_t bsl, k_hex_width; + + GET_NEXT_ARG_TO (moo, nargs, &arg_state, arg); + if (!MOO_OOP_IS_POINTER(arg)) goto invalid_format; + + if (flagc & FLAGC_ZEROPAD) padc = ' '; + + switch (MOO_OBJ_GET_FLAGS_TYPE(arg)) + { + case MOO_OBJ_TYPE_CHAR: + bsp = (const moo_uint8_t*)MOO_OBJ_GET_CHAR_SLOT(arg); + bsl = MOO_OBJ_GET_SIZE(arg) * MOO_SIZEOF_OOCH_T; + goto format_byte_in_k; + + 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; + + if (flagc & FLAGC_DOT) + { + n = (precision > bsl)? bsl: precision; + } + else n = bsl; + + if (lm_flag & LF_H) + { + moo_oow_t i; + for (i = 0; i < n; i++) width -= BYTE_PRINTABLE(bsp[i])? 1: k_hex_width; + } + else + { + width -= (n * k_hex_width); + } + + if (!(flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (fmtout, padc, width); + + while (n--) + { + if ((lm_flag & LF_H) && BYTE_PRINTABLE(*bsp)) + { + PUT_BCH (fmtout, *bsp, 1); + } + else + { + moo_bch_t xbuf[3]; + moo_byte_to_bcstr (*bsp, xbuf, MOO_COUNTOF(xbuf), (16 | (ch == 'k'? MOO_BYTE_TO_BCSTR_LOWERCASE: 0)), '0'); + if (lm_flag & (LF_H | LF_L)) PUT_BCS (fmtout, "\\x", 2); + PUT_BCS (fmtout, xbuf, 2); + } + bsp++; + } + + if ((flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (fmtout, padc, width); + break; + + default: + goto invalid_format; + } + break; + } + + case 'w': + case 'W': + { + /* unicode string in unicode escape sequence. + * + * hw -> \uXXXX, \UXXXXXXXX, printable-byte(only in ascii range) + * w -> \uXXXX, \UXXXXXXXX + * lw -> all in \UXXXXXXXX + */ + const moo_uch_t* usp; + moo_oow_t usl, i, uwid; + GET_NEXT_ARG_TO (moo, nargs, &arg_state, arg); if (!MOO_OOP_IS_POINTER(arg) || MOO_OBJ_GET_FLAGS_TYPE(arg) != MOO_OBJ_TYPE_CHAR) goto invalid_format; - oosp = MOO_OBJ_GET_CHAR_SLOT(arg); - oosl = MOO_OBJ_GET_SIZE(arg); + if (flagc & FLAGC_ZEROPAD) padc = ' '; + + usp = MOO_OBJ_GET_CHAR_SLOT(arg); + usl = MOO_OBJ_GET_SIZE(arg); if (flagc & FLAGC_DOT) { - if (oosl > precision) oosl = precision; + n = (precision > usl)? usl: precision; + } + else n = usl; + + 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; + else uwid = 10; + width -= uwid; } - width -= oosl; if (!(flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (fmtout, padc, width); - PUT_OOCS (fmtout, oosp, oosl); + + while (n--) + { + if ((lm_flag & LF_H) && BYTE_PRINTABLE(*usp)) + { + PUT_OOCH(fmtout, *usp, 1); + } + else if (!(lm_flag & LF_L) && *usp <= 0xFFFF) + { + moo_uint16_t u16 = *usp; + int extra_flags = ((ch) == 'w'? MOO_BYTE_TO_BCSTR_LOWERCASE: 0); + PUT_BCS(fmtout, "\\u", 2); + PUT_BYTE_IN_HEX(fmtout, (u16 >> 8) & 0xFF, extra_flags); + PUT_BYTE_IN_HEX(fmtout, u16 & 0xFF, extra_flags); + } + else + { + moo_uint32_t u32 = *usp; + int extra_flags = ((ch) == 'w'? MOO_BYTE_TO_BCSTR_LOWERCASE: 0); + PUT_BCS(fmtout, "\\u", 2); + PUT_BYTE_IN_HEX(fmtout, (u32 >> 24) & 0xFF, extra_flags); + PUT_BYTE_IN_HEX(fmtout, (u32 >> 16) & 0xFF, extra_flags); + PUT_BYTE_IN_HEX(fmtout, (u32 >> 8) & 0xFF, extra_flags); + PUT_BYTE_IN_HEX(fmtout, u32 & 0xFF, extra_flags); + } + usp++; + } + if ((flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (fmtout, padc, width); break; } diff --git a/moo/lib/fmtout.c b/moo/lib/fmtout.c deleted file mode 100644 index af55c6a..0000000 --- a/moo/lib/fmtout.c +++ /dev/null @@ -1,1430 +0,0 @@ -/* - * $Id$ - * - Copyright (c) 2014-2018 Chung, Hyung-Hwan. All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions - are met: - 1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - - THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR - IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include "moo-prv.h" - -#if 0 - -/*#include */ /* for snrintf(). used for floating-point number formatting */ - -#if defined(_MSC_VER) || defined(__BORLANDC__) || (defined(__WATCOMC__) && (__WATCOMC__ < 1200)) -# define snprintf _snprintf -# if !defined(HAVE_SNPRINTF) -# define HAVE_SNPRINTF -# endif -#endif -#if defined(HAVE_QUADMATH_H) -# include /* for quadmath_snprintf() */ -#endif -/* TODO: remove stdio.h and quadmath.h once snprintf gets replaced by own -floting-point conversion implementation*/ - -/* 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) - -enum -{ - /* integer */ - LF_C = (1 << 0), - LF_H = (1 << 1), - LF_J = (1 << 2), - LF_L = (1 << 3), - LF_Q = (1 << 4), - LF_T = (1 << 5), - LF_Z = (1 << 6), - - /* long double */ - LF_LD = (1 << 7), - /* __float128 */ - LF_QD = (1 << 8) -}; - -static struct -{ - moo_uint8_t flag; /* for single occurrence */ - moo_uint8_t dflag; /* for double occurrence */ -} lm_tab[26] = -{ - { 0, 0 }, /* a */ - { 0, 0 }, /* b */ - { 0, 0 }, /* c */ - { 0, 0 }, /* d */ - { 0, 0 }, /* e */ - { 0, 0 }, /* f */ - { 0, 0 }, /* g */ - { LF_H, LF_C }, /* h */ - { 0, 0 }, /* i */ - { LF_J, 0 }, /* j */ - { 0, 0 }, /* k */ - { LF_L, LF_Q }, /* l */ - { 0, 0 }, /* m */ - { 0, 0 }, /* n */ - { 0, 0 }, /* o */ - { 0, 0 }, /* p */ - { LF_Q, 0 }, /* q */ - { 0, 0 }, /* r */ - { 0, 0 }, /* s */ - { LF_T, 0 }, /* t */ - { 0, 0 }, /* u */ - { 0, 0 }, /* v */ - { 0, 0 }, /* w */ - { 0, 0 }, /* z */ - { 0, 0 }, /* y */ - { LF_Z, 0 }, /* z */ -}; - - -enum -{ - FLAGC_DOT = (1 << 0), - FLAGC_SPACE = (1 << 1), - FLAGC_SHARP = (1 << 2), - FLAGC_SIGN = (1 << 3), - FLAGC_LEFTADJ = (1 << 4), - FLAGC_ZEROPAD = (1 << 5), - FLAGC_WIDTH = (1 << 6), - FLAGC_PRECISION = (1 << 7), - FLAGC_STAR1 = (1 << 8), - FLAGC_STAR2 = (1 << 9), - FLAGC_LENMOD = (1 << 10) /* length modifier */ -}; - -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[] = -{ - '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','H','Z' -}; - -static moo_uch_t uch_nullstr[] = { '(','n','u','l','l', ')','\0' }; -static moo_bch_t bch_nullstr[] = { '(','n','u','l','l', ')','\0' }; - -typedef int (*moo_fmtout_putch_t) ( - moo_t* moo, - moo_bitmask_t mask, - moo_ooch_t c, - moo_oow_t len -); - -typedef int (*moo_fmtout_putcs_t) ( - moo_t* moo, - moo_bitmask_t mask, - const moo_ooch_t* ptr, - moo_oow_t len -); - -typedef struct moo_fmtout_data_t moo_fmtout_data_t; -struct moo_fmtout_data_t -{ - moo_oow_t count; /* out */ - moo_bitmask_t mask; /* in */ - moo_fmtout_putch_t putch; /* in */ - moo_fmtout_putcs_t putcs; /* in */ -}; - - -typedef moo_ooi_t (*moo_outbfmt_t) ( - moo_t* moo, - moo_bitmask_t mask, - const moo_bch_t* fmt, - ... -); -/* ------------------------------------------------------------------------- */ -/* - * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse - * order; return an optional length and a pointer to the last character - * written in the buffer (i.e., the first character of the string). - * The buffer pointed to by `nbuf' must have length >= MAXNBUF. - */ - -static moo_bch_t* sprintn_lower (moo_bch_t* nbuf, moo_uintmax_t num, int base, moo_ooi_t* lenp) -{ - moo_bch_t* p; - - p = nbuf; - *p = '\0'; - do { *++p = hex2ascii_lower[num % base]; } while (num /= base); - - if (lenp) *lenp = p - nbuf; - return p; /* returns the end */ -} - -static moo_bch_t* sprintn_upper (moo_bch_t* nbuf, moo_uintmax_t num, int base, moo_ooi_t* lenp) -{ - moo_bch_t* p; - - p = nbuf; - *p = '\0'; - do { *++p = hex2ascii_upper[num % base]; } while (num /= base); - - if (lenp) *lenp = p - nbuf; - return p; /* returns the end */ -} - -/* ------------------------------------------------------------------------- */ -static int put_ooch (moo_t* moo, moo_bitmask_t mask, moo_ooch_t ch, moo_oow_t len) -{ - /* this is not equivalent to put_oocs(moo,mask,&ch, 1); - * this function is to emit a single character multiple times */ - moo_oow_t rem; - - if (len <= 0) return 1; - - if (moo->log.len > 0 && moo->log.last_mask != mask) - { - /* the mask has changed. commit the buffered text */ - -/* TODO: HANDLE LINE ENDING CONVENTION BETTER... */ - if (moo->log.ptr[moo->log.len - 1] != '\n') - { - /* no line ending - append a line terminator */ - moo->log.ptr[moo->log.len++] = '\n'; - } - - vmprim_log_write (moo, moo->log.last_mask, moo->log.ptr, moo->log.len); - moo->log.len = 0; - } - -redo: - rem = 0; - if (len > moo->log.capa - moo->log.len) - { - moo_oow_t newcapa, max; - moo_ooch_t* tmp; - - max = MOO_TYPE_MAX(moo_oow_t) - moo->log.len; - if (len > max) - { - /* data too big. */ - rem += len - max; - len = max; - } - - newcapa = MOO_ALIGN_POW2(moo->log.len + len, MOO_LOG_CAPA_ALIGN); /* TODO: adjust this capacity */ - if (newcapa > moo->option.log_maxcapa) - { - /* [NOTE] - * it doesn't adjust newcapa to moo->option.log_maxcapa. - * nor does it cut the input to fit it into the adjusted capacity. - * if maxcapa set is not aligned to MOO_LOG_CAPA_ALIGN, - * the largest buffer capacity may be suboptimal */ - goto make_do; - } - - /* +1 to handle line ending injection more easily */ - tmp = moo_reallocmem(moo, moo->log.ptr, (newcapa + 1) * MOO_SIZEOF(*tmp)); - if (!tmp) - { - make_do: - if (moo->log.len > 0) - { - /* can't expand the buffer. just flush the existing contents */ - /* TODO: HANDLE LINE ENDING CONVENTION BETTER... */ - if (moo->log.ptr[moo->log.len - 1] != '\n') - { - /* no line ending - append a line terminator */ - moo->log.ptr[moo->log.len++] = '\n'; - } - - vmprim_log_write (moo, moo->log.last_mask, moo->log.ptr, moo->log.len); - moo->log.len = 0; - } - - if (len > moo->log.capa) - { - rem += len - moo->log.capa; - len = moo->log.capa; - } - - } - else - { - moo->log.ptr = tmp; - moo->log.capa = newcapa; - } - } - - while (len > 0) - { - moo->log.ptr[moo->log.len++] = ch; - len--; - } - moo->log.last_mask = mask; - - if (rem > 0) - { - len = rem; - goto redo; - } - - return 1; /* success */ -} - -static int put_oocs (moo_t* moo, moo_bitmask_t mask, const moo_ooch_t* ptr, moo_oow_t len) -{ - moo_oow_t rem; - - if (len <= 0) return 1; - - if (moo->log.len > 0 && moo->log.last_mask != mask) - { - /* the mask has changed. commit the buffered text */ -/* TODO: HANDLE LINE ENDING CONVENTION BETTER... */ - if (moo->log.ptr[moo->log.len - 1] != '\n') - { - /* no line ending - append a line terminator */ - moo->log.ptr[moo->log.len++] = '\n'; - } - - vmprim_log_write (moo, moo->log.last_mask, moo->log.ptr, moo->log.len); - moo->log.len = 0; - } - -redo: - rem = 0; - if (len > moo->log.capa - moo->log.len) - { - moo_oow_t newcapa, max; - moo_ooch_t* tmp; - - max = MOO_TYPE_MAX(moo_oow_t) - moo->log.len; - if (len > max) - { - /* data too big. */ - rem += len - max; - len = max; - } - - newcapa = MOO_ALIGN_POW2(moo->log.len + len, 512); /* TODO: adjust this capacity */ - if (newcapa > moo->option.log_maxcapa) - { - /* [NOTE] - * it doesn't adjust newcapa to moo->option.log_maxcapa. - * nor does it cut the input to fit it into the adjusted capacity. - * if maxcapa set is not aligned to MOO_LOG_CAPA_ALIGN, - * the largest buffer capacity may be suboptimal */ - goto make_do; - } - - /* +1 to handle line ending injection more easily */ - tmp = moo_reallocmem(moo, moo->log.ptr, (newcapa + 1) * MOO_SIZEOF(*tmp)); - if (!tmp) - { - make_do: - if (moo->log.len > 0) - { - /* can't expand the buffer. just flush the existing contents */ - /* TODO: HANDLE LINE ENDING CONVENTION BETTER... */ - if (moo->log.ptr[moo->log.len - 1] != '\n') - { - /* no line ending - append a line terminator */ - moo->log.ptr[moo->log.len++] = '\n'; - } - vmprim_log_write (moo, moo->log.last_mask, moo->log.ptr, moo->log.len); - moo->log.len = 0; - } - - if (len > moo->log.capa) - { - rem += len - moo->log.capa; - len = moo->log.capa; - } - } - else - { - moo->log.ptr = tmp; - moo->log.capa = newcapa; - } - } - - MOO_MEMCPY (&moo->log.ptr[moo->log.len], ptr, len * MOO_SIZEOF(*ptr)); - moo->log.len += len; - moo->log.last_mask = mask; - - if (rem > 0) - { - ptr += len; - len = rem; - goto redo; - } - - return 1; /* success */ -} - -/* ------------------------------------------------------------------------- */ - -static int print_object (moo_t* moo, moo_bitmask_t mask, moo_oop_t oop, moo_outbfmt_t outbfmt) -{ - if (oop == moo->_nil) - { - if (outbfmt(moo, mask, "nil") <= -1) return -1; - } - else if (oop == moo->_true) - { - if (outbfmt(moo, mask, "true") <= -1) return -1; - } - else if (oop == moo->_false) - { - if (outbfmt(moo, mask, "false") <= -1) return -1; - } - else if (MOO_OOP_IS_SMOOI(oop)) - { - if (outbfmt(moo, mask, "%zd", MOO_OOP_TO_SMOOI(oop)) <= -1) return -1; - } - else if (MOO_OOP_IS_SMPTR(oop)) - { - if (outbfmt(moo, mask, "#\\p%X", MOO_OOP_TO_SMPTR(oop)) <= -1) return -1; - } - else if (MOO_OOP_IS_CHAR(oop)) - { - if (outbfmt(moo, mask, "$%.1jc", MOO_OOP_TO_CHAR(oop)) <= -1) return -1; - } - else if (MOO_OOP_IS_ERROR(oop)) - { - if (outbfmt(moo, mask, "#\\e%zd", MOO_OOP_TO_ERROR(oop)) <= -1) return -1; - } - else - { - moo_oop_class_t c; - moo_oow_t i; - - MOO_ASSERT (moo, MOO_OOP_IS_POINTER(oop)); - c = (moo_oop_class_t)MOO_OBJ_GET_CLASS(oop); /*MOO_CLASSOF(moo, oop);*/ - - if (c == moo->_large_negative_integer || c == moo->_large_positive_integer) - { - if (!moo_inttostr(moo, oop, 10 | MOO_INTTOSTR_NONEWOBJ)) return -1; - if (outbfmt(moo, mask, "%.*js", moo->inttostr.xbuf.len, moo->inttostr.xbuf.ptr) <= -1) return -1; - } - else if (c == moo->_fixed_point_decimal) - { - if (!moo_numtostr(moo, oop, 10 | MOO_NUMTOSTR_NONEWOBJ)) return -1; - if (outbfmt(moo, mask, "%.*js", moo->inttostr.xbuf.len, moo->inttostr.xbuf.ptr) <= -1) return -1; - } - else if (MOO_OBJ_GET_FLAGS_TYPE(oop) == MOO_OBJ_TYPE_CHAR) - { - if (c == moo->_symbol) - { - if (outbfmt(moo, mask, "#%.*js", MOO_OBJ_GET_SIZE(oop), MOO_OBJ_GET_CHAR_SLOT(oop)) <= -1) return -1; - } - else /*if ((moo_oop_t)c == moo->_string)*/ - { - moo_ooch_t ch; - int escape = 0; - - for (i = 0; i < MOO_OBJ_GET_SIZE(oop); i++) - { - ch = MOO_OBJ_GET_CHAR_SLOT(oop)[i]; - if (ch < ' ') - { - escape = 1; - break; - } - } - - if (escape) - { - moo_ooch_t escaped; - - if (outbfmt(moo, mask, "S'") <= -1) return -1; - for (i = 0; i < MOO_OBJ_GET_SIZE(oop); i++) - { - ch = MOO_OBJ_GET_CHAR_SLOT(oop)[i]; - if (ch < ' ') - { - switch (ch) - { - case '\0': - escaped = '0'; - break; - case '\n': - escaped = 'n'; - break; - case '\r': - escaped = 'r'; - break; - case '\t': - escaped = 't'; - break; - case '\f': - escaped = 'f'; - break; - case '\b': - escaped = 'b'; - break; - case '\v': - escaped = 'v'; - break; - case '\a': - escaped = 'a'; - break; - default: - escaped = ch; - break; - } - - if (escaped == ch) - { - if (outbfmt(moo, mask, "\\x%X", ch) <= -1) return -1; - } - else - { - if (outbfmt(moo, mask, "\\%jc", escaped) <= -1) return -1; - } - } - else - { - if (outbfmt(moo, mask, "%jc", ch) <= -1) return -1; - } - } - - if (outbfmt(moo, mask, "'") <= -1) return -1; - } - else - { - if (outbfmt(moo, mask, "'%.*js'", MOO_OBJ_GET_SIZE(oop), MOO_OBJ_GET_CHAR_SLOT(oop)) <= -1) return -1; - } - } - } - else if (MOO_OBJ_GET_FLAGS_TYPE(oop) == MOO_OBJ_TYPE_BYTE) - { - if (outbfmt(moo, mask, "#[") <= -1) return -1; - i = 0; - if (i < MOO_OBJ_GET_SIZE(oop)) - { - if (outbfmt(moo, mask, "%d", MOO_OBJ_GET_BYTE_SLOT(oop)[i]) <= -1) return -1; - for (++i; i < MOO_OBJ_GET_SIZE(oop); i++) - { - if (outbfmt(moo, mask, " %d", MOO_OBJ_GET_BYTE_SLOT(oop)[i]) <= -1) return -1; - } - } - if (outbfmt(moo, mask, "]") <= -1) return -1; - } - - else if (MOO_OBJ_GET_FLAGS_TYPE(oop) == MOO_OBJ_TYPE_HALFWORD) - { - if (outbfmt(moo, mask, "#[[") <= -1) return -1;; /* TODO: fix this symbol/notation */ - for (i = 0; i < MOO_OBJ_GET_SIZE(oop); i++) - { - if (outbfmt(moo, mask, " %zX", (moo_oow_t)(MOO_OBJ_GET_HALFWORD_SLOT(oop)[i])) <= -1) return -1; - } - outbfmt (moo, mask, "]]"); - } - else if (MOO_OBJ_GET_FLAGS_TYPE(oop) == MOO_OBJ_TYPE_WORD) - { - if (outbfmt(moo, mask, "#[[[") <= -1) return -1;; /* TODO: fix this symbol/notation */ - for (i = 0; i < MOO_OBJ_GET_SIZE(oop); i++) - { - if (outbfmt(moo, mask, " %zX", MOO_OBJ_GET_WORD_SLOT(oop)[i]) <= -1) return -1; - } - if (outbfmt(moo, mask, "]]]") <= -1) return -1; - } - else if (c == moo->_array) - { - if (outbfmt(moo, mask, "#(") <= -1) return -1; - i = 0; - if (i < MOO_OBJ_GET_SIZE(oop)) - { - if (print_object(moo, mask, MOO_OBJ_GET_OOP_VAL(oop, i), outbfmt) <= -1) return -1; - for (++i; i < MOO_OBJ_GET_SIZE(oop); i++) - { - if (outbfmt(moo, mask, " ") <= -1) return -1; - if (print_object(moo, mask, MOO_OBJ_GET_OOP_VAL(oop, i), outbfmt) <= -1) return -1; - } - } - if (outbfmt(moo, mask, ")") <= -1) return -1; - } - else if (c == moo->_class) - { - /* print the class name */ - if (outbfmt(moo, mask, "%.*js", MOO_OBJ_GET_SIZE(((moo_oop_class_t)oop)->name), MOO_OBJ_GET_CHAR_SLOT(((moo_oop_class_t)oop)->name)) <= -1) return -1; - } - else if (c == moo->_association) - { - if (outbfmt(moo, mask, "%O -> %O", ((moo_oop_association_t)oop)->key, ((moo_oop_association_t)oop)->value) <= -1) return -1; - } - else - { - if (outbfmt(moo, mask, "<<%.*js:%p>>", MOO_OBJ_GET_SIZE(c->name), MOO_OBJ_GET_CHAR_SLOT(c->name), oop) <= -1) return -1; - } - } - - return 0; -} - -/* ------------------------------------------------------------------------- */ - - -#undef FMTCHAR_IS_BCH -#undef FMTCHAR_IS_UCH -#undef FMTCHAR_IS_OOCH -#undef fmtchar_t -#undef fmtoutv -#define fmtchar_t moo_bch_t -#define fmtoutv __logbfmtv -#define FMTCHAR_IS_BCH -#if defined(MOO_OOCH_IS_BCH) -# define FMTCHAR_IS_OOCH -#endif -#include "fmtoutv.h" - -#undef FMTCHAR_IS_BCH -#undef FMTCHAR_IS_UCH -#undef FMTCHAR_IS_OOCH -#undef fmtchar_t -#undef fmtoutv -#define fmtchar_t moo_uch_t -#define fmtoutv __logufmtv -#define FMTCHAR_IS_UCH -#if defined(MOO_OOCH_IS_UCH) -# define FMTCHAR_IS_OOCH -#endif -#include "fmtoutv.h" - - -static int _logbfmtv (moo_t* moo, const moo_bch_t* fmt, moo_fmtout_data_t* data, va_list ap) -{ - return __logbfmtv(moo, fmt, data, ap, moo_logbfmt); -} - -static int _logufmtv (moo_t* moo, const moo_uch_t* fmt, moo_fmtout_data_t* data, va_list ap) -{ - return __logufmtv(moo, fmt, data, ap, moo_logbfmt); -} - -#if 0 - -moo_ooi_t moo_logbfmt (moo_t* moo, moo_bitmask_t mask, const moo_bch_t* fmt, ...) -{ - int x; - va_list ap; - moo_fmtout_data_t fo; - - 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; - - /* 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)) - { - /* no type is set in the given mask and no default type is set. - * make it UNTYPED. */ - mask |= MOO_LOG_UNTYPED; - } - - fo.mask = mask; - fo.putch = put_ooch; - fo.putcs = put_oocs; - - va_start (ap, fmt); - x = _logbfmtv(moo, fmt, &fo, ap); - va_end (ap); - - if (moo->log.len > 0 && moo->log.ptr[moo->log.len - 1] == '\n') - { - vmprim_log_write (moo, moo->log.last_mask, moo->log.ptr, moo->log.len); - moo->log.len = 0; - } - return (x <= -1)? -1: fo.count; -} - -moo_ooi_t moo_logufmt (moo_t* moo, moo_bitmask_t mask, const moo_uch_t* fmt, ...) -{ - int x; - va_list ap; - moo_fmtout_data_t fo; - - if (moo->log.default_type_mask & MOO_LOG_ALL_TYPES) - { - mask &= ~MOO_LOG_UNTYPED; - mask |= (moo->log.default_type_mask & MOO_LOG_ALL_TYPES); - } - else if (!(mask & MOO_LOG_ALL_TYPES)) - { - mask |= MOO_LOG_UNTYPED; - } - - fo.mask = mask; - fo.putch = put_ooch; - fo.putcs = put_oocs; - - va_start (ap, fmt); - x = _logufmtv(moo, fmt, &fo, ap); - va_end (ap); - - if (moo->log.len > 0 && moo->log.ptr[moo->log.len - 1] == '\n') - { - vmprim_log_write (moo, moo->log.last_mask, moo->log.ptr, moo->log.len); - moo->log.len = 0; - } - - return (x <= -1)? -1: fo.count; -} -#endif - -/* -------------------------------------------------------------------------- - * SUPPORT FOR FORMATTED OUTPUT TO BE USED BY BUILTIN PRIMITIVE FUNCTIONS - * -------------------------------------------------------------------------- */ - -#define PRINT_OOCH(c,n) do { \ - if (n > 0) { \ - int xx; \ - if ((xx = data->putch(moo, data->mask, c, n)) <= -1) goto oops; \ - if (xx == 0) goto done; \ - data->count += n; \ - } \ -} while (0) - -#define PRINT_OOCS(ptr,len) do { \ - if (len > 0) { \ - int xx; \ - if ((xx = data->putcs(moo, data->mask, ptr, len)) <= -1) goto oops; \ - if (xx == 0) goto done; \ - data->count += len; \ - } \ -} while(0) - -#define GET_NEXT_ARG_TO(moo,nargs,arg_state,arg) do { \ - if ((arg_state)->idx >= nargs) { (arg_state)->stop = 1; goto invalid_format; } \ - arg = MOO_STACK_GETARG(moo, nargs, (arg_state)->idx); \ - (arg_state)->idx++; \ -} while(0) - -#define GET_NEXT_CHAR_TO(moo,fmt,fmtend,ch) do { \ - if (fmt >= fmtend) ch = MOO_OOCI_EOF; \ - else { ch = *(fmt); (fmt)++; }\ -} while(0) - -static MOO_INLINE int print_formatted (moo_t* moo, moo_ooi_t nargs, moo_fmtout_data_t* data, moo_outbfmt_t outbfmt, int rcv_is_fmtstr) -{ - const moo_ooch_t* fmt, * fmtend; - const moo_ooch_t* checkpoint, * percent; - - int n, radix, neg, sign, radix_flags; - moo_ooi_t extra, width, precision; - moo_ooch_t padc, ooch; - moo_ooci_t ch; - int flagc; - - struct - { - moo_ooi_t idx; - int stop; - } arg_state; - moo_oop_t arg; - - if (rcv_is_fmtstr) - { - arg = MOO_STACK_GETRCV(moo, nargs); - arg_state.idx = 0; - } - else - { - arg = MOO_STACK_GETARG(moo, nargs, 0); - arg_state.idx = 1; - } - - 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, - * print all arguments as objects */ - if (print_object(moo, data->mask, arg, outbfmt) <= -1) goto oops; - for (i = arg_state.idx; i < nargs; i++) - { - arg = MOO_STACK_GETARG(moo, nargs, i); - if (print_object(moo, data->mask, arg, outbfmt) <= -1) goto oops; - } - return 0; - } - - arg_state.stop = 0; - - fmt = MOO_OBJ_GET_CHAR_SLOT(arg); - fmtend = fmt + MOO_OBJ_GET_SIZE(arg); - - data->count = 0; - - while (1) - { - checkpoint = fmt; - - while (1) - { - GET_NEXT_CHAR_TO (moo, fmt, fmtend, ch); - if (ch == '%' && !arg_state.stop) break; - - if (ch == MOO_OOCI_EOF) - { - /* fmt is not advanced when it is length-bounded. - * so not fmt - checkpoint - 1 */ - PRINT_OOCS (checkpoint, fmt - checkpoint); - goto done; - } - } - PRINT_OOCS (checkpoint, fmt - checkpoint - 1); - - percent = fmt - 1; - - padc = ' '; - width = 0; precision = 0; - neg = 0; sign = 0; - - flagc = 0; - radix_flags = MOO_INTTOSTR_NONEWOBJ; - - reswitch: - GET_NEXT_CHAR_TO (moo, fmt, fmtend, ch); - switch (ch) - { - case '%': /* %% */ - ooch = ch; - goto print_char; - - /* flag characters */ - case '.': - if (flagc & FLAGC_DOT) goto invalid_format; - flagc |= FLAGC_DOT; - goto reswitch; - - case '#': - if (flagc & (FLAGC_WIDTH | FLAGC_DOT | FLAGC_LENMOD)) goto invalid_format; - flagc |= FLAGC_SHARP; - goto reswitch; - - case ' ': - if (flagc & (FLAGC_WIDTH | FLAGC_DOT | FLAGC_LENMOD)) goto invalid_format; - flagc |= FLAGC_SPACE; - goto reswitch; - - case '+': /* place sign for signed conversion */ - if (flagc & (FLAGC_WIDTH | FLAGC_DOT | FLAGC_LENMOD)) goto invalid_format; - flagc |= FLAGC_SIGN; - goto reswitch; - - case '-': /* left adjusted */ - if (flagc & (FLAGC_WIDTH | FLAGC_DOT | FLAGC_LENMOD)) goto invalid_format; - if (flagc & FLAGC_DOT) - { - goto invalid_format; - } - else - { - flagc |= FLAGC_LEFTADJ; - if (flagc & FLAGC_ZEROPAD) - { - padc = ' '; - flagc &= ~FLAGC_ZEROPAD; - } - } - - goto reswitch; - - case '*': /* take the length from the parameter */ - 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(moo, arg, &precision) <= -1) goto invalid_format; - if (precision < 0) - { - /* if precision is less than 0, - * treat it as if no .precision is specified */ - flagc &= ~FLAGC_DOT; - precision = 0; - } - } - 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(moo, arg, &width) <= -1) goto invalid_format; - if (width < 0) - { - flagc |= FLAGC_LEFTADJ; - width = -width; - } - } - goto reswitch; - - case '0': /* zero pad */ - if (flagc & FLAGC_LENMOD) goto invalid_format; - if (!(flagc & (FLAGC_DOT | FLAGC_LEFTADJ))) - { - padc = '0'; - flagc |= FLAGC_ZEROPAD; - goto reswitch; - } - /* end of flags characters */ - - 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;; ++fmt) - { - n = n * 10 + ch - '0'; - ch = *fmt; - if (ch < '0' || ch > '9') break; - } - if (flagc & FLAGC_DOT) - { - if (flagc & FLAGC_STAR2) goto invalid_format; - precision = n; - flagc |= FLAGC_PRECISION; - } - else - { - if (flagc & FLAGC_STAR1) goto invalid_format; - width = n; - flagc |= FLAGC_WIDTH; - } - goto reswitch; - - /* integer conversions */ - case 'd': - case 'i': /* signed conversion */ - radix = 10; - sign = 1; - goto print_integer; - case 'o': - radix = 8; - goto print_integer; - case 'u': - radix = 10; - goto print_integer; - case 'x': - radix_flags |= MOO_INTTOSTR_LOWERCASE; - case 'X': - radix = 16; - goto print_integer; - case 'b': - radix = 2; - goto print_integer; - /* end of integer conversions */ - - case 'f': - { - const moo_ooch_t* nsptr; - moo_oow_t nslen; - moo_oow_t scale = 0; - - GET_NEXT_ARG_TO (moo, nargs, &arg_state, arg); - if (MOO_OOP_IS_CHAR(arg)) - { - arg = MOO_SMOOI_TO_OOP(MOO_OOP_TO_CHAR(arg)); - } - else if (MOO_OOP_IS_FPDEC(moo, arg)) - { - moo_oop_fpdec_t fa = (moo_oop_fpdec_t)arg; - scale = MOO_OOP_TO_SMOOI(fa->scale); - arg = fa->value; - } - - 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; - } - - nsptr = moo->inttostr.xbuf.ptr; - nslen = moo->inttostr.xbuf.len; - MOO_ASSERT (moo, nslen > 0); - - if (nsptr[0] == '-') - { - MOO_ASSERT (moo, (MOO_OOP_IS_SMOOI(arg) && MOO_OOP_TO_SMOOI(arg) < 0) || MOO_OOP_IS_NBIGINT(moo,arg)); - nsptr++; - nslen--; - neg = 1; - } - - if (!(flagc & FLAGC_DOT)) - { - precision = scale; - if (precision <= 0) precision = 1; - } - - if ((flagc & FLAGC_DOT) && precision < scale) - { - moo_oow_t diff = scale - precision; - scale = precision; - nslen = (nslen < diff)? 0: (nslen - diff); - } - - if (nslen < scale + 1) - { - extra = 1; - if (precision > 0) extra += 1 + scale; - } - else - { - extra = 0; - if (nslen > 0) extra += nslen - scale; - if (precision > 0) - { - extra += 1; - if (nslen > 0) extra += scale; - } - } - - if (neg) extra++; - else if (flagc & FLAGC_SIGN) extra++; - else if (flagc & FLAGC_SPACE) extra++; - - if ((flagc & FLAGC_DOT) && precision > scale) - { - /* trailing zeros in the fractional part */ - extra += precision - scale; - } - - if (!(flagc & FLAGC_LEFTADJ) && !(flagc & FLAGC_ZEROPAD) && width > extra) - { - width -= extra; - PRINT_OOCH (padc, width); - width = 0; - } - if (neg) PRINT_OOCH ('-', 1); - else if (flagc & FLAGC_SIGN) PRINT_OOCH ('+', 1); - else if (flagc & FLAGC_SPACE) PRINT_OOCH (' ', 1); - - if (!(flagc & FLAGC_LEFTADJ) && width > extra) - { - width -= extra; - PRINT_OOCH (padc, width); - } - - if (nslen < scale + 1) - { - PRINT_OOCH ('0', 1); - if (precision > 0) - { - PRINT_OOCH ('.', 1); - PRINT_OOCH ('0', scale - nslen); - PRINT_OOCS (nsptr, nslen); - } - } - else - { - if (nslen > 0) PRINT_OOCS (nsptr, nslen - scale); - if (precision > 0) - { - PRINT_OOCH ('.', 1); - if (nslen > 0) PRINT_OOCS (&nsptr[nslen - scale], scale); - } - } - if (precision > scale) - { - /* trailing zeros in the fractional part */ - PRINT_OOCH ('0', precision - scale); - } - - if ((flagc & FLAGC_LEFTADJ) && width > extra) - { - width -= extra; - PRINT_OOCH (padc, width); - } - break; - } - - case 'c': - case 'C': - GET_NEXT_ARG_TO (moo, nargs, &arg_state, arg); - if (MOO_OOP_IS_SMOOI(arg)) arg = MOO_CHAR_TO_OOP(MOO_OOP_TO_SMOOI(arg)); - if (!MOO_OOP_IS_CHAR(arg)) goto invalid_format; - ooch = MOO_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 */ - width--; - if (!(flagc & FLAGC_LEFTADJ) && width > 0) PRINT_OOCH (padc, width); - PRINT_OOCH (ooch, 1); - if ((flagc & FLAGC_LEFTADJ) && width > 0) PRINT_OOCH (padc, width); - break; - - case 's': - case 'S': - { - const moo_ooch_t* oosp; - moo_oow_t oosl; - - /* zeropad must not take effect for 'S' */ - if (flagc & FLAGC_ZEROPAD) padc = ' '; - - GET_NEXT_ARG_TO (moo, nargs, &arg_state, arg); - if (!MOO_OOP_IS_POINTER(arg) || MOO_OBJ_GET_FLAGS_TYPE(arg) != MOO_OBJ_TYPE_CHAR) goto invalid_format; - - oosp = MOO_OBJ_GET_CHAR_SLOT(arg); - oosl = MOO_OBJ_GET_SIZE(arg); - - if (flagc & FLAGC_DOT) - { - if (oosl > precision) oosl = precision; - } - width -= oosl; - - if (!(flagc & FLAGC_LEFTADJ) && width > 0) PRINT_OOCH (padc, width); - PRINT_OOCS (oosp, oosl); - if ((flagc & FLAGC_LEFTADJ) && width > 0) PRINT_OOCH (padc, width); - break; - } - - case 'O': /* object - ignore precision, width, adjustment */ - GET_NEXT_ARG_TO (moo, nargs, &arg_state, arg); - if (print_object(moo, data->mask, arg, outbfmt) <= -1) goto oops; - break; - - print_integer: - { - const moo_ooch_t* nsptr; - moo_oow_t nslen; - - GET_NEXT_ARG_TO (moo, nargs, &arg_state, arg); - if (MOO_OOP_IS_CHAR(arg)) - { - arg = MOO_SMOOI_TO_OOP(MOO_OOP_TO_CHAR(arg)); - } - else if (MOO_OOP_IS_FPDEC(moo, arg)) - { - moo_oop_t nv; - moo_oop_fpdec_t fa = (moo_oop_fpdec_t)arg; - - /* the given number for integer output is a fixed-point decimal. - * i will drop all digits after the fixed point */ - moo_pushvolat (moo, &arg); - nv = moo_truncfpdecval(moo, fa->value, MOO_OOP_TO_SMOOI(fa->scale), 0); - moo_popvolat (moo); - if (!nv) - { - MOO_LOG1 (moo, MOO_LOG_WARN | MOO_LOG_UNTYPED, "unable to truncate a fixed-point number %O to an integer for output\n", arg); - goto invalid_format; - } - - arg = nv; - } - - if (!moo_inttostr(moo, arg, radix | radix_flags)) - { - /*moo_seterrbfmt (moo, MOO_EINVAL, "not a valid number - %O", arg); - goto oops;*/ - MOO_LOG1 (moo, MOO_LOG_WARN | MOO_LOG_UNTYPED, "unable to convert integer %O to string \n", arg); - goto invalid_format; - } - - nsptr = moo->inttostr.xbuf.ptr; - nslen = moo->inttostr.xbuf.len; - - MOO_ASSERT (moo, nslen > 0); - if (nsptr[0] == '-') - { - /* 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--; - } - - 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 (radix == 2 || radix == 8 || radix == 16) extra += 2; - } - if (neg) extra++; - else if (flagc & FLAGC_SIGN) extra++; - else if (flagc & FLAGC_SPACE) extra++; - - if ((flagc & FLAGC_DOT) && precision > nslen) - { - /* extra zeros for precision specified */ - extra += (precision - nslen); - } - - if (!(flagc & FLAGC_LEFTADJ) && !(flagc & FLAGC_ZEROPAD) && width > 0 && (width -= extra) > 0) - { - PRINT_OOCH (padc, width); - width = 0; - } - - if (neg) PRINT_OOCH ('-', 1); - else if (flagc & FLAGC_SIGN) PRINT_OOCH ('+', 1); - else if (flagc & FLAGC_SPACE) PRINT_OOCH (' ', 1); - - if ((flagc & FLAGC_SHARP) && arg != MOO_SMOOI_TO_OOP(0)) - { - if (radix == 2) - { - PRINT_OOCH ('2', 1); - PRINT_OOCH ('r', 1); - } - if (radix == 8) - { - PRINT_OOCH ('8', 1); - PRINT_OOCH ('r', 1); - } - else if (radix == 16) - { - PRINT_OOCH ('1', 1); - PRINT_OOCH ('6', 1); - PRINT_OOCH ('r', 1); - } - } - - if ((flagc & FLAGC_DOT) && precision > nslen) - { - /* extra zeros for precision specified */ - PRINT_OOCH ('0', precision - nslen); - } - - if (!(flagc & FLAGC_LEFTADJ) && width > 0 && (width -= extra) > 0) - { - PRINT_OOCH (padc, width); - } - - PRINT_OOCS (nsptr, nslen); - - if ((flagc & FLAGC_LEFTADJ) && width > 0 && (width -= extra) > 0) - { - PRINT_OOCH (padc, width); - } - break; - } - - invalid_format: - PRINT_OOCS (percent, fmt - percent); - break; - - default: - PRINT_OOCS (percent, fmt - percent); - /* - * Since we ignore an formatting argument it is no - * longer safe to obey the remaining formatting - * arguments as the arguments will no longer match - * the format specs. - */ - arg_state.stop = 1; - break; - } - } - -done: - return 0; - -oops: - return -1; -} - -#if 0 -/* -------------------------------------------------------------------------- - * SUPPORT FOR THE BUILTIN PRINTF PRIMITIVE FUNCTION - * -------------------------------------------------------------------------- */ - -int moo_printfmtst (moo_t* moo, moo_ooi_t nargs) -{ - moo_fmtout_data_t fo; - MOO_MEMSET (&fo, 0, MOO_SIZEOF(fo)); - fo.putch = put_prch; - fo.putcs = put_prcs; - return print_formatted(moo, nargs, &fo, moo_proutbfmt, 0); -} - -/* -------------------------------------------------------------------------- - * SUPPORT FOR THE BUILTIN LOGF PRIMITIVE FUNCTION - * -------------------------------------------------------------------------- */ - -int moo_logfmtst (moo_t* moo, moo_ooi_t nargs) -{ - moo_fmtout_data_t fo; - - MOO_MEMSET (&fo, 0, MOO_SIZEOF(fo)); - fo.mask = MOO_LOG_FATAL | MOO_LOG_APP; - - 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 */ - fo.mask &= ~MOO_LOG_UNTYPED; - - /* if the default_type_mask has the UNTYPED bit on, - * it'll get turned back on */ - fo.mask |= (moo->log.default_type_mask & MOO_LOG_ALL_TYPES); - } - - fo.putch = put_logch; - fo.putcs = put_logcs; - return print_formatted(moo, nargs, &fo, moo_logbfmt, 0); -} -#endif - -/* -------------------------------------------------------------------------- - * SUPPORT FOR THE BUILTIN SPRINTF PRIMITIVE FUNCTION - * -------------------------------------------------------------------------- */ -static int put_sprcs (moo_t* moo, moo_bitmask_t mask, const moo_ooch_t* ptr, moo_oow_t len) -{ - if (len > moo->sprintf.xbuf.capa - moo->sprintf.xbuf.len) - { - moo_ooch_t* tmp; - moo_oow_t newcapa; - - newcapa = moo->sprintf.xbuf.len + len + 1; - newcapa = MOO_ALIGN_POW2(newcapa, 256); - - tmp = (moo_ooch_t*)moo_reallocmem(moo, moo->sprintf.xbuf.ptr, newcapa * MOO_SIZEOF(*tmp)); - if (!tmp) return -1; - - moo->sprintf.xbuf.ptr = tmp; - moo->sprintf.xbuf.capa = newcapa; - } - - MOO_MEMCPY (&moo->sprintf.xbuf.ptr[moo->sprintf.xbuf.len], ptr, len * MOO_SIZEOF(*ptr)); - moo->sprintf.xbuf.len += len; - return 1; /* success */ -} - -static int put_sprch (moo_t* moo, moo_bitmask_t mask, moo_ooch_t ch, moo_oow_t len) -{ - if (len > moo->sprintf.xbuf.capa - moo->sprintf.xbuf.len) - { - moo_ooch_t* tmp; - moo_oow_t newcapa; - - newcapa = moo->sprintf.xbuf.len + len + 1; - newcapa = MOO_ALIGN_POW2(newcapa, 256); - - tmp = (moo_ooch_t*)moo_reallocmem(moo, moo->sprintf.xbuf.ptr, newcapa * MOO_SIZEOF(*tmp)); - if (!tmp) return -1; - - moo->sprintf.xbuf.ptr = tmp; - moo->sprintf.xbuf.capa = newcapa; - } - - while (len > 0) - { - --len; - moo->sprintf.xbuf.ptr[moo->sprintf.xbuf.len++] = ch; - } - - return 1; /* success */ -} - -static moo_ooi_t __sprbfmtv (moo_t* moo, moo_bitmask_t mask, const moo_bch_t* fmt, ...); - -static int _sprbfmtv (moo_t* moo, const moo_bch_t* fmt, moo_fmtout_data_t* data, va_list ap) -{ - return __logbfmtv(moo, fmt, data, ap, __sprbfmtv); -} - -/* -static int _sprufmtv (moo_t* moo, const moo_uch_t* fmt, moo_fmtout_data_t* data, va_list ap) -{ - return __logufmtv (moo, fmt, data, ap, __sprbfmtv); -}*/ - -static moo_ooi_t __sprbfmtv(moo_t* moo, moo_bitmask_t mask, const moo_bch_t* fmt, ...) -{ - va_list ap; - moo_fmtout_data_t fo; - - fo.mask = mask; /* not used */ - fo.putch = put_sprch; - fo.putcs = put_sprcs; - - va_start (ap, fmt); - _sprbfmtv (moo, fmt, &fo, ap); - va_end (ap); - - return fo.count; -} - -#if 0 -moo_ooi_t moo_sproutbfmt (moo_t* moo, moo_bitmask_t mask, const moo_bch_t* fmt, ...) -{ - int x; - va_list ap; - moo_fmtout_data_t fo; - - fo.mask = mask; - fo.putch = put_sprch; - fo.putcs = put_sprcs; - - va_start (ap, fmt); - x = _sprbfmtv(moo, fmt, &fo, ap); - va_end (ap); - - return (x <= -1)? -1: fo.count; -} -#endif -/* -moo_ooi_t moo_sproutufmt (moo_t* moo, moo_bitmask_t mask, const moo_uch_t* fmt, ...) -{ - int x; - va_list ap; - moo_fmtout_data_t fo; - - fo.mask = mask; - fo.putch = put_sprch; - fo.putcs = put_sprcs; - - va_start (ap, fmt); - x = _sprufmtv(moo, fmt, &fo, ap); - va_end (ap); - - return (x <= -1)? -1: fo.count; -}*/ - - -int moo_strfmtcallstack (moo_t* moo, moo_ooi_t nargs) -{ - /* format a string using the receiver and arguments on the stack */ - moo_fmtout_data_t fo; - MOO_MEMSET (&fo, 0, MOO_SIZEOF(fo)); - fo.putch = put_sprch; - fo.putcs = put_sprcs; - moo->sprintf.xbuf.len = 0; - return print_formatted(moo, nargs, &fo, moo_sproutbfmt, 1); -} - -#endif diff --git a/moo/lib/fmtoutv.h b/moo/lib/fmtoutv.h deleted file mode 100644 index 2bd94ca..0000000 --- a/moo/lib/fmtoutv.h +++ /dev/null @@ -1,1189 +0,0 @@ -/* - * $Id$ - * - Copyright (c) 2014-2018 Chung, Hyung-Hwan. All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions - are met: - 1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - - THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR - IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -/* - * This file contains a formatted output routine derived from kvprintf() - * of FreeBSD. It has been heavily modified and bug-fixed. - */ - -/* - * Copyright (c) 1986, 1988, 1991, 1993 - * The Regents of the University of California. All rights reserved. - * (c) UNIX System Laboratories, Inc. - * All or some portions of this file are derived from material licensed - * to the University of California by American Telephone and Telegraph - * Co. or Unix System Laboratories, Inc. and are reproduced herein with - * the permission of UNIX System Laboratories, Inc. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 4. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - */ - - -/* NOTE: data output is aborted if the data limit is reached or - * I/O error occurs */ - -#undef PUT_OOCH -#undef PUT_OOCS -#undef PUT_BYTE_IN_HEX -#undef BYTE_PRINTABLE - -#define PUT_OOCH(c,n) do { \ - if (n > 0) { \ - int xx; \ - if ((xx = data->putch(moo, data->mask, c, n)) <= -1) goto oops; \ - if (xx == 0) goto done; \ - data->count += n; \ - } \ -} while (0) - -#define PUT_OOCS(ptr,len) do { \ - if (len > 0) { \ - int xx; \ - if ((xx = data->putcs(moo, data->mask, ptr, len)) <= -1) goto oops; \ - if (xx == 0) goto done; \ - data->count += len; \ - } \ -} while (0) - -#define PUT_BYTE_IN_HEX(byte,extra_flags) do { \ - moo_bch_t __xbuf[3]; \ - moo_byte_to_bcstr ((byte), __xbuf, MOO_COUNTOF(__xbuf), (16 | (extra_flags)), '0'); \ - PUT_OOCH(__xbuf[0], 1); \ - PUT_OOCH(__xbuf[1], 1); \ -} while (0) - -/* TODO: redefine this */ -#define BYTE_PRINTABLE(x) ((x >= 'a' && x <= 'z') || (x >= 'A' && x <= 'Z') || (x >= '0' && x <= '9') || (x == ' ')) - -static int fmtoutv (moo_t* moo, const fmtchar_t* fmt, moo_fmtout_data_t* data, va_list ap, moo_outbfmt_t outbfmt) -{ - const fmtchar_t* percent; - const fmtchar_t* checkpoint; - moo_bch_t nbuf[MAXNBUF], bch; - const moo_bch_t* nbufp; - int n, base, neg, sign; - moo_ooi_t tmp, width, precision; - moo_ooch_t ch, padc; -#if !defined(FMTCHAR_IS_OOCH) - fmtchar_t fch; -#endif - int lm_flag, lm_dflag, flagc, numlen; - moo_uintmax_t num = 0; - int stop = 0; - -#if 0 - moo_bchbuf_t* fltfmt; - moo_oochbuf_t* fltout; -#endif - moo_bch_t* (*sprintn) (moo_bch_t* nbuf, moo_uintmax_t num, int base, moo_ooi_t* lenp); - - data->count = 0; - -#if 0 - fltfmt = &moo->d->fltfmt; - fltout = &moo->d->fltout; - - fltfmt->ptr = fltfmt->buf; - fltfmt->capa = MOO_COUNTOF(fltfmt->buf) - 1; - - fltout->ptr = fltout->buf; - fltout->capa = MOO_COUNTOF(fltout->buf) - 1; -#endif - - while (1) - { - #if defined(FMTCHAR_IS_OOCH) - checkpoint = fmt; - while ((ch = *fmt++) != '%' || stop) - { - if (ch == '\0') - { - PUT_OOCS (checkpoint, fmt - checkpoint - 1); - goto done; - } - } - PUT_OOCS (checkpoint, fmt - checkpoint - 1); - #else - #if defined(MOO_OOCH_IS_UCH) - /* fmtchar is bch. ooch is uch. convert bch to uch */ - checkpoint = fmt; - while ((fch = *fmt++) != '%' || stop) - { - if (fch == '\0') break; - } - while (checkpoint < fmt - 1) - { - moo_oow_t cvlen, bclen; - moo_cmgr_t* cmgr; - - bclen = fmt - checkpoint - 1; - cmgr = moo->cmgr; - cvlen = cmgr->bctouc(checkpoint, bclen, &ch); - if (cvlen == 0 || cvlen > bclen) - { - /* conversion error. just emit the byte as it is. */ - ch = *checkpoint; - cvlen = 1; - } - checkpoint += cvlen; - PUT_OOCH (ch, 1); - } - if (fch == '\0') goto done; - #else - while ((fch = *fmt++) != '%' || stop) - { - moo_bch_t bcsbuf[MOO_MBLEN_MAX + 1]; - moo_oow_t ucslen, bcslen; - - if (fch == '\0') goto done; - - /* fmtchar is uch. ooch is bch. convert uch to bch */ - ucslen = 1; - bcslen = MOO_COUNTOF(bcsbuf); - if (moo_conv_uchars_to_bchars_with_cmgr(&fch, &ucslen, bcsbuf, &bcslen, moo->cmgr) <= -1) goto oops; - PUT_OOCS (bcsbuf, bcslen); - } - #endif - #endif - percent = fmt - 1; - - padc = ' '; - width = 0; precision = 0; - neg = 0; sign = 0; - - lm_flag = 0; lm_dflag = 0; flagc = 0; - sprintn = sprintn_lower; - - reswitch: - switch (ch = *fmt++) - { - case '%': /* %% */ - bch = ch; - goto print_lowercase_c; - - /* flag characters */ - case '.': - if (flagc & FLAGC_DOT) goto invalid_format; - flagc |= FLAGC_DOT; - goto reswitch; - - case '#': - if (flagc & (FLAGC_WIDTH | FLAGC_DOT | FLAGC_LENMOD)) goto invalid_format; - flagc |= FLAGC_SHARP; - goto reswitch; - - case ' ': - if (flagc & (FLAGC_WIDTH | FLAGC_DOT | FLAGC_LENMOD)) goto invalid_format; - flagc |= FLAGC_SPACE; - goto reswitch; - - case '+': /* place sign for signed conversion */ - if (flagc & (FLAGC_WIDTH | FLAGC_DOT | FLAGC_LENMOD)) goto invalid_format; - flagc |= FLAGC_SIGN; - goto reswitch; - - case '-': /* left adjusted */ - if (flagc & (FLAGC_WIDTH | FLAGC_DOT | FLAGC_LENMOD)) goto invalid_format; - if (flagc & FLAGC_DOT) - { - goto invalid_format; - } - else - { - flagc |= FLAGC_LEFTADJ; - if (flagc & FLAGC_ZEROPAD) - { - padc = ' '; - flagc &= ~FLAGC_ZEROPAD; - } - } - - goto reswitch; - - case '*': /* take the length from the parameter */ - 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 is less than 0, - * treat it as if no .precision is specified */ - flagc &= ~FLAGC_DOT; - precision = 0; - } - } - 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 (flagc & FLAGC_LEFTADJ) - flagc &= ~FLAGC_LEFTADJ; - else - */ - flagc |= FLAGC_LEFTADJ; - width = -width; - } - } - goto reswitch; - - case '0': /* zero pad */ - if (flagc & FLAGC_LENMOD) goto invalid_format; - if (!(flagc & (FLAGC_DOT | FLAGC_LEFTADJ))) - { - padc = '0'; - flagc |= FLAGC_ZEROPAD; - goto reswitch; - } - /* end of flags characters */ - - 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;; ++fmt) - { - n = n * 10 + ch - '0'; - ch = *fmt; - if (ch < '0' || ch > '9') break; - } - if (flagc & FLAGC_DOT) - { - if (flagc & FLAGC_STAR2) goto invalid_format; - precision = n; - flagc |= FLAGC_PRECISION; - } - else - { - if (flagc & FLAGC_STAR1) goto invalid_format; - width = n; - flagc |= FLAGC_WIDTH; - } - goto reswitch; - - /* length modifiers */ - case 'h': /* short int */ - case 'l': /* long int */ - case 'q': /* long long int */ - case 'j': /* moo_intmax_t/moo_uintmax_t */ - case 'z': /* moo_ooi_t/moo_oow_t */ - case 't': /* ptrdiff_t */ - if (lm_flag & (LF_LD | LF_QD)) goto invalid_format; - - flagc |= FLAGC_LENMOD; - if (lm_dflag) - { - /* error */ - goto invalid_format; - } - else if (lm_flag) - { - if (lm_tab[ch - 'a'].dflag && lm_flag == lm_tab[ch - 'a'].flag) - { - lm_flag &= ~lm_tab[ch - 'a'].flag; - lm_flag |= lm_tab[ch - 'a'].dflag; - lm_dflag |= lm_flag; - goto reswitch; - } - else - { - /* error */ - goto invalid_format; - } - } - else - { - lm_flag |= lm_tab[ch - 'a'].flag; - goto reswitch; - } - break; - - case 'L': /* long double */ - if (flagc & FLAGC_LENMOD) - { - /* conflict with other length modifier */ - goto invalid_format; - } - flagc |= FLAGC_LENMOD; - lm_flag |= LF_LD; - goto reswitch; - - case 'Q': /* __float128 */ - if (flagc & FLAGC_LENMOD) - { - /* conflict with other length modifier */ - goto invalid_format; - } - flagc |= FLAGC_LENMOD; - lm_flag |= LF_QD; - goto reswitch; - /* end of length modifiers */ - - case 'n': /* number of characters printed so far */ - if (lm_flag & LF_J) /* j */ - *(va_arg(ap, moo_intmax_t*)) = data->count; - else if (lm_flag & LF_Z) /* z */ - *(va_arg(ap, moo_ooi_t*)) = data->count; - #if (MOO_SIZEOF_LONG_LONG > 0) - else if (lm_flag & LF_Q) /* ll */ - *(va_arg(ap, long long int*)) = data->count; - #endif - else if (lm_flag & LF_L) /* l */ - *(va_arg(ap, long int*)) = data->count; - else if (lm_flag & LF_H) /* h */ - *(va_arg(ap, short int*)) = data->count; - else if (lm_flag & LF_C) /* hh */ - *(va_arg(ap, char*)) = data->count; - else if (flagc & FLAGC_LENMOD) - goto invalid_format; - else - *(va_arg(ap, int*)) = data->count; - break; - - /* signed integer conversions */ - case 'd': - case 'i': /* signed conversion */ - base = 10; - sign = 1; - goto handle_sign; - /* end of signed integer conversions */ - - /* unsigned integer conversions */ - case 'o': - base = 8; - goto handle_nosign; - case 'u': - base = 10; - goto handle_nosign; - case 'X': - sprintn = sprintn_upper; - case 'x': - base = 16; - goto handle_nosign; - case 'b': - base = 2; - goto handle_nosign; - /* end of unsigned integer conversions */ - - case 'p': /* pointer */ - base = 16; - - if (width == 0) flagc |= FLAGC_SHARP; - else flagc &= ~FLAGC_SHARP; - - num = (moo_uintptr_t)va_arg(ap, void*); - goto number; - - case 'c': - { - /* zeropad must not take effect for 'c' */ - 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; - #endif - lowercase_c: - bch = MOO_SIZEOF(moo_bch_t) < MOO_SIZEOF(int)? va_arg(ap, int): va_arg(ap, moo_bch_t); - - print_lowercase_c: - /* precision 0 doesn't kill the letter */ - width--; - if (!(flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (padc, width); - PUT_OOCH (bch, 1); - if ((flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (padc, width); - break; - } - - case 'C': - { - moo_uch_t ooch; - - /* zeropad must not take effect for 'C' */ - if (flagc & FLAGC_ZEROPAD) padc = ' '; - if (lm_flag & LF_H) goto lowercase_c; - #if defined(MOO_OOCH_IS_BCH) - if (lm_flag & LF_J) goto lowercase_c; - #endif - uppercase_c: - ooch = MOO_SIZEOF(moo_uch_t) < MOO_SIZEOF(int)? va_arg(ap, int): va_arg(ap, moo_uch_t); - - /* precision 0 doesn't kill the letter */ - width--; - if (!(flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (padc, width); - PUT_OOCH (ooch, 1); - if ((flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (padc, width); - break; - } - - case 's': - { - const moo_bch_t* bsp; - moo_oow_t bslen, obslen, slen; - moo_cmgr_t* cmgr; - - /* zeropad must not take effect for 'S' */ - if (flagc & FLAGC_ZEROPAD) padc = ' '; - if (lm_flag & LF_L) goto uppercase_s; - #if defined(MOO_OOCH_IS_UCH) - if (lm_flag & LF_J) goto uppercase_s; - #endif - lowercase_s: - - bsp = va_arg(ap, moo_bch_t*); - if (!bsp) bsp = bch_nullstr; - - #if defined(MOO_OOCH_IS_UCH) - /* get the length */ - if (flagc & FLAGC_DOT) - { - for (obslen = 0; obslen < precision && bsp[obslen]; obslen++); - } - else - { - for (obslen = 0; bsp[obslen]; obslen++); - } - - /* get the required length for successful conversion in a fail-safe manner */ - cmgr = moo->cmgr; - bslen = obslen; - if (moo_conv_bchars_to_uchars_with_cmgr(bsp, &bslen, MOO_NULL, &slen, cmgr, 0) <= -1) goto oops; - - /* slen holds the length after conversion */ - n = slen; - if ((flagc & FLAGC_DOT) && precision < slen) n = precision; - width -= n; - - if (!(flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (padc, width); - - { - moo_ooch_t conv_buf[32]; - moo_oow_t conv_len, src_len, tot_len = 0; - while (n > 0) - { - MOO_ASSERT (moo, bslen > tot_len); - - src_len = bslen - tot_len; - conv_len = MOO_COUNTOF(conv_buf); - - /* this must not fail since the dry-run above was successful */ - moo_conv_bchars_to_uchars_with_cmgr(&bsp[tot_len], &src_len, conv_buf, &conv_len, cmgr, 0); - tot_len += src_len; - - if (conv_len > n) conv_len = n; - PUT_OOCS (conv_buf, conv_len); - - n -= conv_len; - } - } - - if ((flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (padc, width); - #else - if (flagc & FLAGC_DOT) - { - for (n = 0; n < precision && bsp[n]; n++); - } - else - { - for (n = 0; bsp[n]; n++); - } - - width -= n; - - if (!(flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (padc, width); - PUT_OOCS (bsp, n); - if ((flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (padc, width); - #endif - break; - } - - case 'S': - { - const moo_uch_t* usp; - moo_oow_t uslen, slen; - - /* zeropad must not take effect for 's' */ - if (flagc & FLAGC_ZEROPAD) padc = ' '; - if (lm_flag & LF_H) goto lowercase_s; - #if defined(MOO_OOCH_IS_UCH) - if (lm_flag & LF_J) goto lowercase_s; - #endif - uppercase_s: - usp = va_arg(ap, moo_uch_t*); - if (!usp) usp = uch_nullstr; - - #if defined(MOO_OOCH_IS_BCH) - /* get the length */ - if (flagc & FLAGC_DOT) - { - for (uslen = 0; uslen < precision && usp[uslen]; uslen++); - } - else - { - for (uslen = 0; usp[uslen]; uslen++); - } - - if (moo_conv_uchars_to_bchars_with_cmgr(usp, &uslen, MOO_NULL, &slen, moo->cmgr) <= -1) goto oops; - - /* slen holds the length after conversion */ - n = slen; - if ((flagc & FLAGC_DOT) && precision < slen) n = precision; - width -= n; - - if (!(flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (padc, width); - { - moo_ooch_t conv_buf[32]; - moo_oow_t conv_len, src_len, tot_len = 0; - while (n > 0) - { - MOO_ASSERT (moo, uslen > tot_len); - - src_len = uslen - tot_len; - conv_len = MOO_COUNTOF(conv_buf); - - /* this must not fail since the dry-run above was successful */ - moo_conv_uchars_to_bchars_with_cmgr (&usp[tot_len], &src_len, conv_buf, &conv_len, moo->cmgr); - tot_len += src_len; - - if (conv_len > n) conv_len = n; - PUT_OOCS (conv_buf, conv_len); - - n -= conv_len; - } - } - if ((flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (padc, width); - #else - if (flagc & FLAGC_DOT) - { - for (n = 0; n < precision && usp[n]; n++); - } - else - { - for (n = 0; usp[n]; n++); - } - - width -= n; - - if (!(flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (padc, width); - PUT_OOCS (usp, n); - if ((flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (padc, width); - #endif - break; - } - - case 'k': - case 'K': - { - /* byte or multibyte character string in escape sequence */ - - const moo_uint8_t* bsp; - moo_oow_t k_hex_width; - - /* 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 - * -------------------------------------------------------- - * hk -> \x + non-printable in lowercase hex - * k -> all in lowercase hex - * lk -> \x + all in lowercase hex - * -------------------------------------------------------- - * hK -> \x + non-printable in uppercase hex - * K -> all in uppercase hex - * lK -> \x + all in uppercase hex - * -------------------------------------------------------- - * with 'k' or 'K', i don't substitute "(null)" for the NULL pointer - */ - if (flagc & FLAGC_ZEROPAD) padc = ' '; - - bsp = va_arg(ap, moo_uint8_t*); - k_hex_width = (lm_flag & (LF_H | LF_L))? 4: 2; - - if (lm_flag& LF_H) - { - 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++) width -= BYTE_PRINTABLE(bsp[n])? 1: k_hex_width; - } - else - { - for (n = 0; bsp[n]; n++) width -= BYTE_PRINTABLE(bsp[n])? 1: k_hex_width; - } - } - else - { - 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++) /* nothing */; - } - else - { - for (n = 0; bsp[n]; n++) /* nothing */; - } - width -= (n * k_hex_width); - } - - if (!(flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (padc, width); - - while (n--) - { - if ((lm_flag & LF_H) && BYTE_PRINTABLE(*bsp)) - { - PUT_OOCH(*bsp, 1); - } - else - { - moo_bch_t xbuf[3]; - moo_byte_to_bcstr (*bsp, xbuf, MOO_COUNTOF(xbuf), (16 | (ch == 'k'? MOO_BYTE_TO_BCSTR_LOWERCASE: 0)), '0'); - if (lm_flag & (LF_H | LF_L)) - { - PUT_OOCH('\\', 1); - PUT_OOCH('x', 1); - } - PUT_OOCH(xbuf[0], 1); - PUT_OOCH(xbuf[1], 1); - } - bsp++; - } - - if ((flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (padc, width); - break; - } - - case 'w': - case 'W': - { - /* unicode string in unicode escape sequence. - * - * hw -> \uXXXX, \UXXXXXXXX, printable-byte(only in ascii range) - * w -> \uXXXX, \UXXXXXXXX - * lw -> all in \UXXXXXXXX - */ - const moo_uch_t* usp; - moo_oow_t uwid; - - if (flagc & FLAGC_ZEROPAD) padc = ' '; - usp = va_arg(ap, moo_uch_t*); - - 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++) - { - if ((lm_flag & LF_H) && BYTE_PRINTABLE(usp[n])) uwid = 1; - else if (!(lm_flag & LF_L) && usp[n] <= 0xFFFF) uwid = 6; - else uwid = 10; - width -= uwid; - } - } - else - { - for (n = 0; usp[n]; n++) - { - if ((lm_flag & LF_H) && BYTE_PRINTABLE(usp[n])) uwid = 1; - else if (!(lm_flag & LF_L) && usp[n] <= 0xFFFF) uwid = 6; - else uwid = 10; - width -= uwid; - } - } - - if (!(flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (padc, width); - - while (n--) - { - if ((lm_flag & LF_H) && BYTE_PRINTABLE(*usp)) - { - PUT_OOCH(*usp, 1); - } - else if (!(lm_flag & LF_L) && *usp <= 0xFFFF) - { - moo_uint16_t u16 = *usp; - int extra_flags = ((ch) == 'w'? MOO_BYTE_TO_BCSTR_LOWERCASE: 0); - PUT_OOCH('\\', 1); - PUT_OOCH('u', 1); - PUT_BYTE_IN_HEX((u16 >> 8) & 0xFF, extra_flags); - PUT_BYTE_IN_HEX(u16 & 0xFF, extra_flags); - } - else - { - moo_uint32_t u32 = *usp; - int extra_flags = ((ch) == 'w'? MOO_BYTE_TO_BCSTR_LOWERCASE: 0); - PUT_OOCH('\\', 1); - PUT_OOCH('U', 1); - PUT_BYTE_IN_HEX((u32 >> 24) & 0xFF, extra_flags); - PUT_BYTE_IN_HEX((u32 >> 16) & 0xFF, extra_flags); - PUT_BYTE_IN_HEX((u32 >> 8) & 0xFF, extra_flags); - PUT_BYTE_IN_HEX(u32 & 0xFF, extra_flags); - } - usp++; - } - - if ((flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (padc, width); - break; - } - - case 'O': /* object - ignore precision, width, adjustment */ - if (print_object(moo, data->mask, va_arg(ap, moo_oop_t), outbfmt) <= -1) goto oops; - break; - -#if 0 - case 'e': - case 'E': - case 'f': - case 'F': - case 'g': - case 'G': - /* - case 'a': - case 'A': - */ - { - /* let me rely on snprintf until i implement float-point to string conversion */ - int q; - moo_oow_t fmtlen; - #if (MOO_SIZEOF___FLOAT128 > 0) && defined(HAVE_QUADMATH_SNPRINTF) - __float128 v_qd; - #endif - long double v_ld; - double v_d; - int dtype = 0; - moo_oow_t newcapa; - - if (lm_flag & LF_J) - { - #if (MOO_SIZEOF___FLOAT128 > 0) && defined(HAVE_QUADMATH_SNPRINTF) && (MOO_SIZEOF_FLTMAX_T == MOO_SIZEOF___FLOAT128) - v_qd = va_arg (ap, moo_fltmax_t); - dtype = LF_QD; - #elif MOO_SIZEOF_FLTMAX_T == MOO_SIZEOF_DOUBLE - v_d = va_arg (ap, moo_fltmax_t); - #elif MOO_SIZEOF_FLTMAX_T == MOO_SIZEOF_LONG_DOUBLE - v_ld = va_arg (ap, moo_fltmax_t); - dtype = LF_LD; - #else - #error Unsupported moo_flt_t - #endif - } - else if (lm_flag & LF_Z) - { - /* moo_flt_t is limited to double or 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. - */ - #if MOO_SIZEOF_FLT_T == MOO_SIZEOF_DOUBLE - v_d = va_arg (ap, moo_flt_t); - #elif MOO_SIZEOF_FLT_T == MOO_SIZEOF_LONG_DOUBLE - v_ld = va_arg (ap, moo_flt_t); - dtype = LF_LD; - #else - #error Unsupported moo_flt_t - #endif - } - else if (lm_flag & (LF_LD | LF_L)) - { - v_ld = va_arg (ap, long double); - dtype = LF_LD; - } - #if (MOO_SIZEOF___FLOAT128 > 0) && defined(HAVE_QUADMATH_SNPRINTF) - else if (lm_flag & (LF_QD | LF_Q)) - { - v_qd = va_arg (ap, __float128); - dtype = LF_QD; - } - #endif - else if (flagc & FLAGC_LENMOD) - { - goto invalid_format; - } - else - { - v_d = va_arg (ap, double); - } - - fmtlen = fmt - percent; - if (fmtlen > fltfmt->capa) - { - if (fltfmt->ptr == fltfmt->buf) - { - fltfmt->ptr = MOO_MMGR_ALLOC(MOO_MMGR_GETDFL(), MOO_SIZEOF(*fltfmt->ptr) * (fmtlen + 1)); - if (!fltfmt->ptr) goto oops; - } - else - { - moo_bch_t* tmpptr; - - tmpptr = MOO_MMGR_REALLOC(MOO_MMGR_GETDFL(), fltfmt->ptr, MOO_SIZEOF(*fltfmt->ptr) * (fmtlen + 1)); - if (!tmpptr) goto oops; - fltfmt->ptr = tmpptr; - } - - fltfmt->capa = fmtlen; - } - - /* compose back the format specifier */ - fmtlen = 0; - fltfmt->ptr[fmtlen++] = '%'; - if (flagc & FLAGC_SPACE) fltfmt->ptr[fmtlen++] = ' '; - if (flagc & FLAGC_SHARP) fltfmt->ptr[fmtlen++] = '#'; - if (flagc & FLAGC_SIGN) fltfmt->ptr[fmtlen++] = '+'; - if (flagc & FLAGC_LEFTADJ) fltfmt->ptr[fmtlen++] = '-'; - if (flagc & FLAGC_ZEROPAD) fltfmt->ptr[fmtlen++] = '0'; - - if (flagc & FLAGC_STAR1) fltfmt->ptr[fmtlen++] = '*'; - else if (flagc & FLAGC_WIDTH) - { - fmtlen += moo_fmtuintmaxtombs ( - &fltfmt->ptr[fmtlen], fltfmt->capa - fmtlen, - width, 10, -1, '\0', MOO_NULL); - } - if (flagc & FLAGC_DOT) fltfmt->ptr[fmtlen++] = '.'; - if (flagc & FLAGC_STAR2) fltfmt->ptr[fmtlen++] = '*'; - else if (flagc & FLAGC_PRECISION) - { - fmtlen += moo_fmtuintmaxtombs ( - &fltfmt->ptr[fmtlen], fltfmt->capa - fmtlen, - precision, 10, -1, '\0', MOO_NULL); - } - - if (dtype == LF_LD) - fltfmt->ptr[fmtlen++] = 'L'; - #if (MOO_SIZEOF___FLOAT128 > 0) - else if (dtype == LF_QD) - fltfmt->ptr[fmtlen++] = 'Q'; - #endif - - fltfmt->ptr[fmtlen++] = ch; - fltfmt->ptr[fmtlen] = '\0'; - - #if defined(HAVE_SNPRINTF) - /* nothing special here */ - #else - /* 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 (fltout->capa < newcapa) - { - MOO_ASSERT (moo, fltout->ptr == fltout->buf); - - fltout->ptr = MOO_MMGR_ALLOC(MOO_MMGR_GETDFL(), MOO_SIZEOF(char_t) * (newcapa + 1)); - if (!fltout->ptr) goto oops; - fltout->capa = newcapa; - } - #endif - - while (1) - { - - if (dtype == LF_LD) - { - #if defined(HAVE_SNPRINTF) - q = snprintf ((moo_bch_t*)fltout->ptr, fltout->capa + 1, fltfmt->ptr, v_ld); - #else - q = sprintf ((moo_bch_t*)fltout->ptr, fltfmt->ptr, v_ld); - #endif - } - #if (MOO_SIZEOF___FLOAT128 > 0) && defined(HAVE_QUADMATH_SNPRINTF) - else if (dtype == LF_QD) - { - q = quadmath_snprintf((moo_bch_t*)fltout->ptr, fltout->capa + 1, fltfmt->ptr, v_qd); - } - #endif - else - { - #if defined(HAVE_SNPRINTF) - q = snprintf ((moo_bch_t*)fltout->ptr, fltout->capa + 1, fltfmt->ptr, v_d); - #else - q = sprintf ((moo_bch_t*)fltout->ptr, fltfmt->ptr, v_d); - #endif - } - if (q <= -1) goto oops; - if (q <= fltout->capa) break; - - newcapa = fltout->capa * 2; - if (newcapa < q) newcapa = q; - - if (fltout->ptr == fltout->sbuf) - { - fltout->ptr = MOO_MMGR_ALLOC(MOO_MMGR_GETDFL(), MOO_SIZEOF(char_t) * (newcapa + 1)); - if (!fltout->ptr) goto oops; - } - else - { - char_t* tmpptr; - - tmpptr = MOO_MMGR_REALLOC(MOO_MMGR_GETDFL(), fltout->ptr, MOO_SIZEOF(char_t) * (newcapa + 1)); - if (!tmpptr) goto oops; - fltout->ptr = tmpptr; - } - fltout->capa = newcapa; - } - - if (MOO_SIZEOF(char_t) != MOO_SIZEOF(moo_bch_t)) - { - fltout->ptr[q] = '\0'; - while (q > 0) - { - q--; - fltout->ptr[q] = ((moo_bch_t*)fltout->ptr)[q]; - } - } - - sp = fltout->ptr; - flagc &= ~FLAGC_DOT; - width = 0; - precision = 0; - goto print_lowercase_s; - } -#endif - - - handle_nosign: - sign = 0; - if (lm_flag & LF_J) - { - #if defined(__GNUC__) && \ - (MOO_SIZEOF_UINTMAX_T > MOO_SIZEOF_OOW_T) && \ - (MOO_SIZEOF_UINTMAX_T != MOO_SIZEOF_LONG_LONG) && \ - (MOO_SIZEOF_UINTMAX_T != MOO_SIZEOF_LONG) - /* GCC-compiled binaries crashed when getting moo_uintmax_t with va_arg. - * This is just a work-around for it */ - int i; - for (i = 0, num = 0; i < MOO_SIZEOF(moo_uintmax_t) / MOO_SIZEOF(moo_oow_t); i++) - { - #if defined(MOO_ENDIAN_BIG) - num = num << (8 * MOO_SIZEOF(moo_oow_t)) | (va_arg (ap, moo_oow_t)); - #else - register int shift = i * MOO_SIZEOF(moo_oow_t); - moo_oow_t x = va_arg (ap, moo_oow_t); - num |= (moo_uintmax_t)x << (shift * MOO_BITS_PER_BYTE); - #endif - } - #else - num = va_arg (ap, moo_uintmax_t); - #endif - } -#if 0 - else if (lm_flag & LF_T) - num = va_arg(ap, moo_ptrdiff_t); -#endif - else if (lm_flag & LF_Z) - num = va_arg(ap, moo_oow_t); - #if (MOO_SIZEOF_LONG_LONG > 0) - else if (lm_flag & LF_Q) - num = va_arg(ap, unsigned long long int); - #endif - else if (lm_flag & (LF_L | LF_LD)) - num = va_arg(ap, unsigned long int); - else if (lm_flag & LF_H) - num = (unsigned short int)va_arg(ap, int); - else if (lm_flag & LF_C) - num = (unsigned char)va_arg(ap, int); - else - num = va_arg(ap, unsigned int); - goto number; - - handle_sign: - if (lm_flag & LF_J) - { - #if defined(__GNUC__) && \ - (MOO_SIZEOF_INTMAX_T > MOO_SIZEOF_OOI_T) && \ - (MOO_SIZEOF_UINTMAX_T != MOO_SIZEOF_LONG_LONG) && \ - (MOO_SIZEOF_UINTMAX_T != MOO_SIZEOF_LONG) - /* GCC-compiled binraries crashed when getting moo_uintmax_t with va_arg. - * This is just a work-around for it */ - int i; - for (i = 0, num = 0; i < MOO_SIZEOF(moo_intmax_t) / MOO_SIZEOF(moo_oow_t); i++) - { - #if defined(MOO_ENDIAN_BIG) - num = num << (8 * MOO_SIZEOF(moo_oow_t)) | (va_arg (ap, moo_oow_t)); - #else - register int shift = i * MOO_SIZEOF(moo_oow_t); - moo_oow_t x = va_arg (ap, moo_oow_t); - num |= (moo_uintmax_t)x << (shift * MOO_BITS_PER_BYTE); - #endif - } - #else - num = va_arg (ap, moo_intmax_t); - #endif - } - -#if 0 - else if (lm_flag & LF_T) - num = va_arg(ap, moo_ptrdiff_t); -#endif - else if (lm_flag & LF_Z) - num = va_arg (ap, moo_ooi_t); - #if (MOO_SIZEOF_LONG_LONG > 0) - else if (lm_flag & LF_Q) - num = va_arg (ap, long long int); - #endif - else if (lm_flag & (LF_L | LF_LD)) - num = va_arg (ap, long int); - else if (lm_flag & LF_H) - num = (short int)va_arg (ap, int); - else if (lm_flag & LF_C) - num = (char)va_arg (ap, int); - else - num = va_arg (ap, int); - - number: - 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 (base == 2 || base == 8) tmp += 2; - else if (base == 16) tmp += 3; - } - if (neg) tmp++; - else if (flagc & FLAGC_SIGN) tmp++; - else if (flagc & FLAGC_SPACE) tmp++; - - numlen = (int)((const moo_bch_t*)nbufp - (const moo_bch_t*)nbuf); - if ((flagc & FLAGC_DOT) && precision > numlen) - { - /* extra zeros for precision specified */ - tmp += (precision - numlen); - } - - if (!(flagc & FLAGC_LEFTADJ) && !(flagc & FLAGC_ZEROPAD) && width > 0 && (width -= tmp) > 0) - { - PUT_OOCH (padc, width); - width = 0; - } - - if (neg) PUT_OOCH ('-', 1); - else if (flagc & FLAGC_SIGN) PUT_OOCH ('+', 1); - else if (flagc & FLAGC_SPACE) PUT_OOCH (' ', 1); - - if ((flagc & FLAGC_SHARP) && num != 0) - { - if (base == 2) - { - PUT_OOCH ('2', 1); - PUT_OOCH ('r', 1); - } - if (base == 8) - { - PUT_OOCH ('8', 1); - PUT_OOCH ('r', 1); - } - else if (base == 16) - { - PUT_OOCH ('1', 1); - PUT_OOCH ('6', 1); - PUT_OOCH ('r', 1); - } - } - - if ((flagc & FLAGC_DOT) && precision > numlen) - { - /* extra zeros for precision specified */ - PUT_OOCH ('0', precision - numlen); - } - - if (!(flagc & FLAGC_LEFTADJ) && width > 0 && (width -= tmp) > 0) - { - PUT_OOCH (padc, width); - } - - while (*nbufp) PUT_OOCH (*nbufp--, 1); /* output actual digits */ - - if ((flagc & FLAGC_LEFTADJ) && width > 0 && (width -= tmp) > 0) - { - PUT_OOCH (padc, width); - } - break; - - invalid_format: - #if defined(FMTCHAR_IS_OOCH) - PUT_OOCS (percent, fmt - percent); - #else - while (percent < fmt) PUT_OOCH (*percent++, 1); - #endif - break; - - default: - #if defined(FMTCHAR_IS_OOCH) - PUT_OOCS (percent, fmt - percent); - #else - while (percent < fmt) PUT_OOCH (*percent++, 1); - #endif - /* - * Since we ignore an formatting argument it is no - * longer safe to obey the remaining formatting - * arguments as the arguments will no longer match - * the format specs. - */ - stop = 1; - break; - } - } - -done: - return 0; - -oops: - return -1; -} -#undef PUT_OOCH diff --git a/moo/lib/moo-prv.h b/moo/lib/moo-prv.h index f595631..a041519 100644 --- a/moo/lib/moo-prv.h +++ b/moo/lib/moo-prv.h @@ -1491,16 +1491,6 @@ int moo_fmt_object_ ( moo_oop_t oop ); -/* ========================================================================= */ -/* fmtout.c */ -/* ========================================================================= */ -moo_ooi_t moo_sproutbfmt ( - moo_t* moo, - moo_bitmask_t mask, - const moo_bch_t* fmt, - ... -); - int moo_strfmtcallstack ( moo_t* moo, moo_ooi_t nargs, diff --git a/moo/t/Makefile.in b/moo/t/Makefile.in index ce24e11..7fe368c 100644 --- a/moo/t/Makefile.in +++ b/moo/t/Makefile.in @@ -332,7 +332,6 @@ pdfdir = @pdfdir@ prefix = @prefix@ program_transform_name = @program_transform_name@ psdir = @psdir@ -runstatedir = @runstatedir@ sbindir = @sbindir@ sharedstatedir = @sharedstatedir@ srcdir = @srcdir@ diff --git a/moo/t/t-003.c b/moo/t/t-003.c index 2afa833..0cf9f54 100644 --- a/moo/t/t-003.c +++ b/moo/t/t-003.c @@ -61,7 +61,14 @@ int main () /* this unit is the number of characters written. but some are written as bch and some other as uch. * if uch and bch data are mixed, the count returned doesn't really tell how many bytes or characters written */ bfmt_out ("wrote [%ld] units\n", cnt); - T_ASSERT1 (cnt == 98, "bfmt_out test #1"); +#if (MOO_SIZEOF_UCH_T == 2) +# define EXPECTED_LEN 98 +#elif (MOO_SIZEOF_UCH_T == 4) +# define EXPECTED_LEN 122 +#else +# error UNSUPPORTED UCH SIZE +#endif + T_ASSERT1 (cnt == EXPECTED_LEN, "bfmt_out test #1"); return 0;