added QSE_FMTINTMAX_ZEROLEAD.

changed awk printf to use qse_fmtintmax for integer printing.
added 'b' and 'B' for awk printf
This commit is contained in:
2011-11-15 15:58:32 +00:00
parent 759925d816
commit e15fcd0c14
8 changed files with 294 additions and 238 deletions

View File

@ -22,43 +22,88 @@
#include <qse/cmn/str.h>
/* ==================== multibyte ===================================== */
static int fmt_unsigned_to_mbs (
qse_mchar_t* buf, int size,
qse_uintmax_t value, int base_and_flags, int prec,
qse_mchar_t fillchar, qse_mchar_t signchar, const qse_mchar_t* prefix)
{
qse_mchar_t tmp[(QSE_SIZEOF(qse_uintmax_t) * 8)];
int reslen, base, xsize, reqlen, pflen;
int reslen, base, fillsize, reqlen, pflen, preczero;
qse_mchar_t* p, * bp, * be;
const qse_mchar_t* xbasestr;
base = base_and_flags & 0xFF;
base = base_and_flags & 0x3F;
if (base < 2 || base > 36) return -1;
xbasestr = (base_and_flags & QSE_FMTINTMAXTOMBS_UPPERCASE)?
QSE_MT("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"):
QSE_MT("0123456789abcdefghijklmnopqrstuvwxyz");
/* store the resulting numeric string into 'tmp' first */
p = tmp;
do
{
*p++ = xbasestr[value % base];
value /= base;
}
while (value > 0);
/* reslen is the length of the resulting string without padding. */
reslen = (int)(p - tmp);
if ((base_and_flags & QSE_FMTINTMAXTOMBS_NOZERO) && value == 0)
{
p = tmp;
if (base_and_flags & QSE_FMTINTMAXTOMBS_ZEROLEAD)
{
/* NOZERO emits no digit, ZEROLEAD emits 1 digit.
* so it emits '0' */
reslen = 1;
preczero = 1;
}
else
{
/* since the value is zero, emit no digits */
reslen = 0;
preczero = 0;
}
}
else
{
/* store the resulting numeric string into 'tmp' first */
p = tmp;
do
{
*p++ = xbasestr[value % base];
value /= base;
}
while (value > 0);
/* reslen is the length of the resulting string without padding. */
reslen = (int)(p - tmp);
/* precision specified the minum number of digits to produce.
* so if the precision is larger that the digits produced,
* reslen should be adjusted to precision */
if (prec > reslen)
{
/* if the precision is greater than the actual digits
* made from the value, 0 is inserted in front.
* ZEROLEAD doesn't have to be handled explicitly
* since it's achieved effortlessly */
preczero = prec - reslen;
reslen = prec;
}
else
{
preczero = 0;
if (base_and_flags & QSE_FMTINTMAXTOMBS_ZEROLEAD)
{
preczero++;
reslen++;
}
}
}
if (signchar) reslen++; /* increment reslen for the sign character */
if (prefix)
{
/* since the length can be truncated for different type sizes,
* don't pass in a very long prefix. */
pflen = (int)qse_mbslen(prefix);
pflen = (int)qse_mbslen(prefix);
reslen += pflen;
}
else pflen = 0;
/* get the required buffer size for lossless formatting */
reqlen = (base_and_flags & QSE_FMTINTMAXTOMBS_NONULL)? reslen: (reslen + 1);
@ -69,9 +114,10 @@ static int fmt_unsigned_to_mbs (
return -reqlen;
}
xsize = (base_and_flags & QSE_FMTINTMAXTOMBS_NONULL)? size: (size - 1);
/* get the size to fill with fill characters */
fillsize = (base_and_flags & QSE_FMTINTMAXTOMBS_NONULL)? size: (size - 1);
bp = buf;
be = buf + xsize;
be = buf + fillsize;
/* fill space */
if (fillchar != QSE_MT('\0'))
@ -83,14 +129,22 @@ static int fmt_unsigned_to_mbs (
/* copy prefix if necessary */
if (prefix) while (*prefix && bp < be) *bp++ = *prefix++;
/* add 0s for precision */
while (preczero > 0 && bp < be)
{
*bp++ = QSE_MT('0');
preczero--;
}
/* copy the numeric string to the destination buffer */
while (p > tmp && bp < be) *bp++ = *--p;
/* fill the right side */
while (xsize > reslen)
while (fillsize > reslen)
{
*bp++ = fillchar;
xsize--;
fillsize--;
}
}
else if (base_and_flags & QSE_FMTINTMAXTOMBS_FILLCENTER)
@ -99,24 +153,32 @@ static int fmt_unsigned_to_mbs (
if (signchar && bp < be) *bp++ = signchar;
/* fill the left side */
while (xsize > reslen)
while (fillsize > reslen)
{
*bp++ = fillchar;
xsize--;
fillsize--;
}
/* copy prefix if necessary */
if (prefix) while (*prefix && bp < be) *bp++ = *prefix++;
/* add 0s for precision */
while (preczero > 0 && bp < be)
{
*bp++ = QSE_MT('0');
preczero--;
}
/* copy the numeric string to the destination buffer */
while (p > tmp && bp < be) *bp++ = *--p;
}
else
{
/* fill the left side */
while (xsize > reslen)
while (fillsize > reslen)
{
*bp++ = fillchar;
xsize--;
fillsize--;
}
/* emit sign */
@ -124,6 +186,14 @@ static int fmt_unsigned_to_mbs (
/* copy prefix if necessary */
if (prefix) while (*prefix && bp < be) *bp++ = *prefix++;
/* add 0s for precision */
while (preczero > 0 && bp < be)
{
*bp++ = QSE_MT('0');
preczero--;
}
/* copy the numeric string to the destination buffer */
while (p > tmp && bp < be) *bp++ = *--p;
}
@ -135,6 +205,14 @@ static int fmt_unsigned_to_mbs (
/* copy prefix if necessary */
if (prefix) while (*prefix && bp < be) *bp++ = *prefix++;
/* add 0s for precision */
while (preczero > 0 && bp < be)
{
*bp++ = QSE_MT('0');
preczero--;
}
/* copy the numeric string to the destination buffer */
while (p > tmp && bp < be) *bp++ = *--p;
}
@ -210,51 +288,71 @@ static int fmt_unsigned_to_wcs (
qse_wchar_t fillchar, qse_wchar_t signchar, const qse_wchar_t* prefix)
{
qse_wchar_t tmp[(QSE_SIZEOF(qse_uintmax_t) * 8)];
int reslen, base, xsize, reqlen, pflen, preczero;
int reslen, base, fillsize, reqlen, pflen, preczero;
qse_wchar_t* p, * bp, * be;
const qse_wchar_t* xbasestr;
base = base_and_flags & 0xFF;
base = base_and_flags & 0x3F;
if (base < 2 || base > 36) return -1;
xbasestr = (base_and_flags & QSE_FMTINTMAXTOWCS_UPPERCASE)?
QSE_WT("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"):
QSE_WT("0123456789abcdefghijklmnopqrstuvwxyz");
/*if (prec == 0 && value == 0) */
if ((base_and_flags & QSE_FMTINTMAXTOWCS_NOZERO) && value == 0)
{
reslen = 0;
preczero = 0;
p = tmp;
}
else
{
/* store the resulting numeric string into 'tmp' first */
p = tmp;
do
{
*p++ = xbasestr[value % base];
value /= base;
}
while (value > 0);
/* reslen is the length of the resulting string without padding. */
reslen = (int)(p - tmp);
if ((base_and_flags & QSE_FMTINTMAXTOWCS_NOZERO) && value == 0)
{
p = tmp;
if (base_and_flags & QSE_FMTINTMAXTOWCS_ZEROLEAD)
{
/* NOZERO emits no digit, ZEROLEAD emits 1 digit.
* so it emits '0' */
reslen = 1;
preczero = 1;
}
else
{
/* since the value is zero, emit no digits */
reslen = 0;
preczero = 0;
}
}
else
{
/* store the resulting numeric string into 'tmp' first */
p = tmp;
do
{
*p++ = xbasestr[value % base];
value /= base;
}
while (value > 0);
/* precision specified the minum number of digits to produce.
* so if the precision is larger that the digits produced,
* reslen should be adjusted to precision */
if (prec > reslen)
{
preczero = prec - reslen;
reslen = prec;
/* reslen is the length of the resulting string without padding. */
reslen = (int)(p - tmp);
/* precision specified the minum number of digits to produce.
* so if the precision is larger that the digits produced,
* reslen should be adjusted to precision */
if (prec > reslen)
{
/* if the precision is greater than the actual digits
* made from the value, 0 is inserted in front.
* ZEROLEAD doesn't have to be handled explicitly
* since it's achieved effortlessly */
preczero = prec - reslen;
reslen = prec;
}
else
{
preczero = 0;
if (base_and_flags & QSE_FMTINTMAXTOWCS_ZEROLEAD)
{
preczero++;
reslen++;
}
}
}
else
{
preczero = 0;
}
}
if (signchar) reslen++; /* increment reslen for the sign character */
if (prefix)
@ -276,9 +374,9 @@ else
}
/* get the size to fill with fill characters */
xsize = (base_and_flags & QSE_FMTINTMAXTOWCS_NONULL)? size: (size - 1);
fillsize = (base_and_flags & QSE_FMTINTMAXTOWCS_NONULL)? size: (size - 1);
bp = buf;
be = buf + xsize;
be = buf + fillsize;
/* fill space */
if (fillchar != QSE_WT('\0'))
@ -289,19 +387,12 @@ else
if (signchar && bp < be) *bp++ = signchar;
/* copy prefix if necessary */
if (prefix)
{
/*if (preczero > 0 && pflen == 1 && prefix[0] == QSE_WT('0')) goto skip; */
if (preczero <= 0 || pflen != 1 || prefix[0] != QSE_WT('0'))
{
while (*prefix && bp < be) *bp++ = *prefix++;
}
}
if (prefix) while (*prefix && bp < be) *bp++ = *prefix++;
/* add 0s for precision */
while (preczero > 0 && bp < be)
{
*bp++ = QSE_T('0');
*bp++ = QSE_WT('0');
preczero--;
}
@ -309,10 +400,10 @@ if (preczero <= 0 || pflen != 1 || prefix[0] != QSE_WT('0'))
while (p > tmp && bp < be) *bp++ = *--p;
/* fill the right side */
while (xsize > reslen)
while (fillsize > reslen)
{
*bp++ = fillchar;
xsize--;
fillsize--;
}
}
else if (base_and_flags & QSE_FMTINTMAXTOWCS_FILLCENTER)
@ -321,26 +412,19 @@ if (preczero <= 0 || pflen != 1 || prefix[0] != QSE_WT('0'))
if (signchar && bp < be) *bp++ = signchar;
/* fill the left side */
while (xsize > reslen)
while (fillsize > reslen)
{
*bp++ = fillchar;
xsize--;
fillsize--;
}
/* copy prefix if necessary */
if (prefix)
{
/*if (preczero > 0 && pflen == 1 && prefix[0] == QSE_WT('0')) goto skip; */
if (preczero <= 0 || pflen != 1 || prefix[0] != QSE_WT('0'))
{
while (*prefix && bp < be) *bp++ = *prefix++;
}
}
if (prefix) while (*prefix && bp < be) *bp++ = *prefix++;
/* add 0s for precision */
while (preczero > 0 && bp < be)
{
*bp++ = QSE_T('0');
*bp++ = QSE_WT('0');
preczero--;
}
@ -350,29 +434,22 @@ if (preczero <= 0 || pflen != 1 || prefix[0] != QSE_WT('0'))
else
{
/* fill the left side */
while (xsize > reslen)
while (fillsize > reslen)
{
*bp++ = fillchar;
xsize--;
fillsize--;
}
/* emit sign */
if (signchar && bp < be) *bp++ = signchar;
/* copy prefix if necessary */
if (prefix)
{
/*if (preczero > 0 && pflen == 1 && prefix[0] == QSE_WT('0')) goto skip; */
if (preczero <= 0 || pflen != 1 || prefix[0] != QSE_WT('0'))
{
while (*prefix && bp < be) *bp++ = *prefix++;
}
}
if (prefix) while (*prefix && bp < be) *bp++ = *prefix++;
/* add 0s for precision */
while (preczero > 0 && bp < be)
{
*bp++ = QSE_T('0');
*bp++ = QSE_WT('0');
preczero--;
}
@ -386,18 +463,12 @@ if (preczero <= 0 || pflen != 1 || prefix[0] != QSE_WT('0'))
if (signchar && bp < be) *bp++ = signchar;
/* copy prefix if necessary */
if (prefix)
{
if (preczero <= 0 || pflen != 1 || prefix[0] != QSE_WT('0'))
{
while (*prefix && bp < be) *bp++ = *prefix++;
}
}
if (prefix) while (*prefix && bp < be) *bp++ = *prefix++;
/* add 0s for precision */
while (preczero > 0 && bp < be)
{
*bp++ = QSE_T('0');
*bp++ = QSE_WT('0');
preczero--;
}

View File

@ -1958,7 +1958,7 @@ static int comp_cand (qse_lda_t* lda,
{
cand_t* c1 = (cand_t*)dptr1;
cand_t* c2 = (cand_t*)dptr2;
//qse_printf (QSE_T("%p(%d) %p(%d), %p %p, %d %d\n"), c1->node,c1->node->id, c2->node,c1->node->id, c1->mptr, c2->mptr, (int)c1->occ, (int)c2->occ);
/*qse_printf (QSE_T("%p(%d) %p(%d), %p %p, %d %d\n"), c1->node,c1->node->id, c2->node,c1->node->id, c1->mptr, c2->mptr, (int)c1->occ, (int)c2->occ);*/
return (c1->node == c2->node &&
c1->mptr == c2->mptr &&
c1->occ == c2->occ)? 0: 1;

View File

@ -75,9 +75,9 @@ qse_size_t qse_mbsfncpy (
{
if (*f == QSE_MT('\\'))
{
// get the escaped character and treat it normally.
// if the escaper is the last character, treat it
// normally also.
/* get the escaped character and treat it normally.
* if the escaper is the last character, treat it
* normally also. */
if (f[1] != QSE_MT('\0')) f++;
}
else if (*f == QSE_MT('$'))
@ -133,9 +133,9 @@ qse_size_t qse_mbsxfcpy (
{
if (*f == QSE_MT('\\'))
{
// get the escaped character and treat it normally.
// if the escaper is the last character, treat it
// normally also.
/* get the escaped character and treat it normally.
* if the escaper is the last character, treat it
* normally also. */
if (f[1] != QSE_MT('\0')) f++;
}
else if (*f == QSE_MT('$'))
@ -195,9 +195,9 @@ qse_size_t qse_mbsxfncpy (
{
if (*f == QSE_MT('\\'))
{
// get the escaped character and treat it normally.
// if the escaper is the last character, treat it
// normally also.
/* get the escaped character and treat it normally.
* if the escaper is the last character, treat it
* normally also. */
if (f[1] != QSE_MT('\0')) f++;
}
else if (*f == QSE_MT('$'))
@ -300,9 +300,9 @@ qse_size_t qse_wcsfncpy (
{
if (*f == QSE_WT('\\'))
{
// get the escaped character and treat it normally.
// if the escaper is the last character, treat it
// normally also.
/* get the escaped character and treat it normally.
* if the escaper is the last character, treat it
* normally also. */
if (f[1] != QSE_WT('\0')) f++;
}
else if (*f == QSE_WT('$'))
@ -358,9 +358,9 @@ qse_size_t qse_wcsxfcpy (
{
if (*f == QSE_WT('\\'))
{
// get the escaped character and treat it normally.
// if the escaper is the last character, treat it
// normally also.
/* get the escaped character and treat it normally.
* if the escaper is the last character, treat it
* normally also. */
if (f[1] != QSE_WT('\0')) f++;
}
else if (*f == QSE_WT('$'))
@ -420,9 +420,9 @@ qse_size_t qse_wcsxfncpy (
{
if (*f == QSE_WT('\\'))
{
// get the escaped character and treat it normally.
// if the escaper is the last character, treat it
// normally also.
/* get the escaped character and treat it normally.
* if the escaper is the last character, treat it
* normally also. */
if (f[1] != QSE_WT('\0')) f++;
}
else if (*f == QSE_WT('$'))

View File

@ -34,9 +34,9 @@ qse_size_t qse_mbsxsubst (
{
if (*f == QSE_MT('\\'))
{
// get the escaped character and treat it normally.
// if the escaper is the last character, treat it
// normally also.
/* get the escaped character and treat it normally.
* if the escaper is the last character, treat it
* normally also. */
if (f[1] != QSE_MT('\0')) f++;
}
else if (*f == QSE_MT('$'))
@ -96,9 +96,9 @@ qse_size_t qse_wcsxsubst (
{
if (*f == QSE_WT('\\'))
{
// get the escaped character and treat it normally.
// if the escaper is the last character, treat it
// normally also.
/* get the escaped character and treat it normally.
* if the escaper is the last character, treat it
* normally also. */
if (f[1] != QSE_WT('\0')) f++;
}
else if (*f == QSE_WT('$'))