enhanced sprintf to format a byte string when format is a byte string

This commit is contained in:
hyung-hwan 2019-04-22 07:44:57 +00:00
parent 146cc6436b
commit b86ea48c86
3 changed files with 136 additions and 43 deletions

View File

@ -2484,6 +2484,11 @@ qse_awk_val_t* qse_awk_rtx_makembsval (
qse_size_t len qse_size_t len
); );
qse_awk_val_t* qse_awk_rtx_makembsvalwithmxstr (
qse_awk_rtx_t* rtx,
const qse_mcstr_t* mxstr
);
/** /**
* The qse_awk_rtx_makerexval() function creates a regular expression value. * The qse_awk_rtx_makerexval() function creates a regular expression value.
* \return value on success, #QSE_NULL on failure * \return value on success, #QSE_NULL on failure
@ -2789,6 +2794,19 @@ QSE_EXPORT void qse_awk_rtx_freevalstr (
qse_char_t* str /**< string pointer */ qse_char_t* str /**< string pointer */
); );
QSE_EXPORT qse_mchar_t* qse_awk_rtx_getvalmbs (
qse_awk_rtx_t* rtx, /**< runtime context */
const qse_awk_val_t* val, /**< value to convert */
qse_size_t* len /**< result length */
);
QSE_EXPORT void qse_awk_rtx_freevalmbs (
qse_awk_rtx_t* rtx, /**< runtime context */
const qse_awk_val_t* val, /**< value to convert */
qse_mchar_t* str /**< string pointer */
);
/** /**
* The qse_awk_rtx_valtonum() function converts a value to a number. * The qse_awk_rtx_valtonum() function converts a value to a number.
* If the value is converted to an integer, it is stored in the memory * If the value is converted to an integer, it is stored in the memory

View File

@ -1303,14 +1303,61 @@ int qse_awk_fnc_sprintf (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
{ {
qse_size_t nargs; qse_size_t nargs;
qse_awk_val_t* a0; qse_awk_val_t* a0;
qse_awk_val_type_t vtype;
nargs = qse_awk_rtx_getnargs (rtx);
QSE_ASSERT (nargs > 0);
a0 = qse_awk_rtx_getarg(rtx, 0);
vtype = QSE_AWK_RTX_GETVALTYPE(rtx, a0);
if (vtype == QSE_AWK_VAL_MBS)
{
qse_mbs_t out, fbu;
int out_inited = 0, fbu_inited = 0;
qse_mcstr_t cs0;
qse_mcstr_t x;
if (qse_mbs_init(&out, rtx->awk->mmgr, 256) <= -1)
{
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
goto oops_mbs;
}
out_inited = 1;
if (qse_mbs_init(&fbu, rtx->awk->mmgr, 256) <= -1)
{
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
goto oops_mbs;
}
fbu_inited = 1;
cs0.ptr = qse_awk_rtx_getvalmbs(rtx, a0, &cs0.len);
if (cs0.ptr == QSE_NULL) goto oops_mbs;
x.ptr = qse_awk_rtx_formatmbs(rtx, &out, &fbu, cs0.ptr, cs0.len, nargs, QSE_NULL, &x.len);
qse_awk_rtx_freevalmbs (rtx, a0, cs0.ptr);
if (!x.ptr) goto oops_mbs;
a0 = qse_awk_rtx_makembsvalwithmxstr(rtx, &x);
if (a0 == QSE_NULL) goto oops_mbs;
qse_mbs_fini (&fbu);
qse_mbs_fini (&out);
qse_awk_rtx_setretval (rtx, a0);
return 0;
oops_mbs:
if (fbu_inited) qse_mbs_fini (&fbu);
if (out_inited) qse_mbs_fini (&out);
return -1;
}
else
{
qse_str_t out, fbu; qse_str_t out, fbu;
int out_inited = 0, fbu_inited = 0; int out_inited = 0, fbu_inited = 0;
qse_cstr_t cs0; qse_cstr_t cs0;
qse_cstr_t x; qse_cstr_t x;
nargs = qse_awk_rtx_getnargs (rtx);
QSE_ASSERT (nargs > 0);
if (qse_str_init(&out, rtx->awk->mmgr, 256) <= -1) if (qse_str_init(&out, rtx->awk->mmgr, 256) <= -1)
{ {
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL); qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
@ -1325,7 +1372,6 @@ int qse_awk_fnc_sprintf (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
} }
fbu_inited = 1; fbu_inited = 1;
a0 = qse_awk_rtx_getarg (rtx, 0);
cs0.ptr = qse_awk_rtx_getvalstr(rtx, a0, &cs0.len); cs0.ptr = qse_awk_rtx_getvalstr(rtx, a0, &cs0.len);
if (cs0.ptr == QSE_NULL) goto oops; if (cs0.ptr == QSE_NULL) goto oops;
@ -1346,6 +1392,7 @@ oops:
if (out_inited) qse_str_fini (&out); if (out_inited) qse_str_fini (&out);
return -1; return -1;
} }
}
static int fnc_int (qse_awk_rtx_t* run, const qse_awk_fnc_info_t* fi) static int fnc_int (qse_awk_rtx_t* run, const qse_awk_fnc_info_t* fi)
{ {

View File

@ -422,6 +422,11 @@ qse_awk_val_t* qse_awk_rtx_makembsval (qse_awk_rtx_t* rtx, const qse_mchar_t* pt
return (qse_awk_val_t*)val; return (qse_awk_val_t*)val;
} }
qse_awk_val_t* qse_awk_rtx_makembsvalwithmxstr (qse_awk_rtx_t* rtx, const qse_mcstr_t* mxstr)
{
return qse_awk_rtx_makembsval(rtx, mxstr->ptr, mxstr->len);
}
qse_awk_val_t* qse_awk_rtx_makerexval (qse_awk_rtx_t* rtx, const qse_cstr_t* str, void* code[2]) qse_awk_val_t* qse_awk_rtx_makerexval (qse_awk_rtx_t* rtx, const qse_cstr_t* str, void* code[2])
{ {
qse_awk_val_rex_t* val; qse_awk_val_rex_t* val;
@ -1583,6 +1588,29 @@ void qse_awk_rtx_freevalstr (qse_awk_rtx_t* rtx, const qse_awk_val_t* v, qse_cha
} }
} }
qse_mchar_t* qse_awk_rtx_getvalmbs (qse_awk_rtx_t* rtx, const qse_awk_val_t* v, qse_size_t* len)
{
if (QSE_AWK_RTX_GETVALTYPE(rtx, v) == QSE_AWK_VAL_MBS)
{
if (len) *len = ((qse_awk_val_mbs_t*)v)->val.len;
return ((qse_awk_val_mbs_t*)v)->val.ptr;
}
else
{
return qse_awk_rtx_valtombsdup(rtx, v, len);
}
}
void qse_awk_rtx_freevalmbs (qse_awk_rtx_t* rtx, const qse_awk_val_t* v, qse_mchar_t* str)
{
if (QSE_AWK_RTX_GETVALTYPE(rtx, v) != QSE_AWK_VAL_MBS ||
str != ((qse_awk_val_mbs_t*)v)->val.ptr)
{
qse_awk_rtx_freemem (rtx, str);
}
}
static int val_ref_to_num (qse_awk_rtx_t* rtx, const qse_awk_val_ref_t* ref, qse_awk_int_t* l, qse_awk_flt_t* r) static int val_ref_to_num (qse_awk_rtx_t* rtx, const qse_awk_val_ref_t* ref, qse_awk_int_t* l, qse_awk_flt_t* r)
{ {
switch (ref->id) switch (ref->id)