fixed a bug of skipping setting output length in qse_awk_rtx_valtombsdup in the MCHAR mode.

added str::tombs() and str::frommbs()
changed mbs_to_str() to use rtx->awk->cmgr for mbs to wcs conversion
This commit is contained in:
hyung-hwan 2019-06-13 04:26:13 +00:00
parent 7f57afe262
commit df7a5eda5f
4 changed files with 79 additions and 15 deletions

View File

@ -51,17 +51,17 @@ extern "C" {
qse_awk_fnc_t* qse_awk_findfnc (qse_awk_t* awk, const qse_cstr_t* name);
/* EXPORT is required for linking on windows as they are referenced by mod-str.c */
QSE_EXPORT int qse_awk_fnc_index (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi);
QSE_EXPORT int qse_awk_fnc_rindex (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi);
QSE_EXPORT int qse_awk_fnc_length (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi);
QSE_EXPORT int qse_awk_fnc_substr (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi);
QSE_EXPORT int qse_awk_fnc_split (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi);
QSE_EXPORT int qse_awk_fnc_match (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi);
QSE_EXPORT int qse_awk_fnc_gsub (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi);
QSE_EXPORT int qse_awk_fnc_index (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi);
QSE_EXPORT int qse_awk_fnc_length (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi);
QSE_EXPORT int qse_awk_fnc_match (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi);
QSE_EXPORT int qse_awk_fnc_rindex (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi);
QSE_EXPORT int qse_awk_fnc_split (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi);
QSE_EXPORT int qse_awk_fnc_sprintf (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi);
QSE_EXPORT int qse_awk_fnc_sub (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi);
QSE_EXPORT int qse_awk_fnc_substr (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi);
QSE_EXPORT int qse_awk_fnc_tolower (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi);
QSE_EXPORT int qse_awk_fnc_toupper (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi);
QSE_EXPORT int qse_awk_fnc_sprintf (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi);
#if defined(__cplusplus)
}

View File

@ -354,6 +354,64 @@ static int fnc_tocharcode (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
return 0;
}
static int fnc_frommbs (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
{
/* str::frommbs(B"byte-string") */
qse_awk_val_t* a0, * r;
a0 = qse_awk_rtx_getarg(rtx, 0);
switch (QSE_AWK_RTX_GETVALTYPE(rtx, a0))
{
case QSE_AWK_VAL_STR:
r = a0;
break;
default:
{
qse_cstr_t str;
str.ptr = qse_awk_rtx_getvalstr(rtx, a0, &str.len);
if (!str.ptr) return -1;
r = qse_awk_rtx_makestrvalwithxstr(rtx, &str);
qse_awk_rtx_freevalstr (rtx, a0, str.ptr);
if (!r) return -1;
break;
}
}
qse_awk_rtx_setretval (rtx, r);
return 0;
}
static int fnc_tombs (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
{
/* str::tombs("string") */
qse_awk_val_t* a0, * r;
a0 = qse_awk_rtx_getarg(rtx, 0);
switch (QSE_AWK_RTX_GETVALTYPE(rtx, a0))
{
case QSE_AWK_VAL_MBS:
r = a0;
break;
default:
{
qse_mcstr_t str;
str.ptr = qse_awk_rtx_getvalmbs(rtx, a0, &str.len);
if (!str.ptr) return -1;
r = qse_awk_rtx_makembsvalwithmxstr(rtx, &str);
qse_awk_rtx_freevalmbs (rtx, a0, str.ptr);
if (!r) return -1;
break;
}
}
qse_awk_rtx_setretval (rtx, r);
return 0;
}
static int fnc_tonum (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
{
/* str::tonum(value) */
@ -443,6 +501,7 @@ static fnctab_t fnctab[] =
{
/* keep this table sorted for binary search in query(). */
{ QSE_T("fromcharcode"), { { 0, A_MAX, QSE_NULL }, fnc_fromcharcode, 0 } },
{ QSE_T("frommbs"), { { 1, 1, QSE_NULL }, fnc_frommbs, 0 } },
{ QSE_T("gsub"), { { 2, 3, QSE_T("xvr")}, qse_awk_fnc_gsub, 0 } },
{ QSE_T("index"), { { 2, 3, QSE_NULL }, qse_awk_fnc_index, 0 } },
{ QSE_T("isalnum"), { { 1, 1, QSE_NULL }, fnc_isalnum, 0 } },
@ -469,6 +528,7 @@ static fnctab_t fnctab[] =
{ QSE_T("substr"), { { 2, 3, QSE_NULL }, qse_awk_fnc_substr, 0 } },
{ QSE_T("tocharcode"), { { 1, 2, QSE_NULL }, fnc_tocharcode, 0 } },
{ QSE_T("tolower"), { { 1, 1, QSE_NULL }, qse_awk_fnc_tolower, 0 } },
{ QSE_T("tombs"), { { 1, 1, QSE_NULL }, fnc_tombs, 0 } },
{ QSE_T("tonum"), { { 1, 2, QSE_NULL }, fnc_tonum, 0 } },
{ QSE_T("toupper"), { { 1, 1, QSE_NULL }, qse_awk_fnc_toupper, 0 } },
{ QSE_T("trim"), { { 1, 2, QSE_NULL }, fnc_trim, 0 } }

View File

@ -1061,7 +1061,8 @@ static int mbs_to_str (qse_awk_rtx_t* rtx, const qse_mchar_t* str, qse_size_t st
mbslen = str_len;
wcslen = out->u.cplcpy.len;
if (qse_mbsntowcsnallwithcmgr(str, &mbslen, out->u.cplcpy.ptr, &wcslen, qse_findcmgrbyid(QSE_CMGR_MB8)) <= -1 || wcslen >= out->u.cplcpy.len)
/*if (qse_mbsntowcsnallwithcmgr(str, &mbslen, out->u.cplcpy.ptr, &wcslen, qse_findcmgrbyid(QSE_CMGR_MB8)) <= -1 || wcslen >= out->u.cplcpy.len)*/
if (qse_mbsntowcsnallwithcmgr(str, &mbslen, out->u.cplcpy.ptr, &wcslen, rtx->awk->cmgr) <= -1 || wcslen >= out->u.cplcpy.len)
{
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EINVAL, QSE_NULL); /* TODO: change error code */
return -1;
@ -1079,7 +1080,8 @@ static int mbs_to_str (qse_awk_rtx_t* rtx, const qse_mchar_t* str, qse_size_t st
qse_size_t mbslen, wcslen;
mbslen = str_len;
tmp = qse_mbsntowcsalldupwithcmgr(str, &mbslen, &wcslen, rtx->awk->mmgr, qse_findcmgrbyid(QSE_CMGR_MB8));
/*tmp = qse_mbsntowcsalldupwithcmgr(str, &mbslen, &wcslen, rtx->awk->mmgr, qse_findcmgrbyid(QSE_CMGR_MB8));*/
tmp = qse_mbsntowcsalldupwithcmgr(str, &mbslen, &wcslen, rtx->awk->mmgr, rtx->awk->cmgr);
if (!tmp)
{
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
@ -1096,7 +1098,8 @@ static int mbs_to_str (qse_awk_rtx_t* rtx, const qse_mchar_t* str, qse_size_t st
qse_size_t n;
qse_str_clear (out->u.strp);
n = qse_str_ncatmbs(out->u.strp, str, str_len, qse_findcmgrbyid(QSE_CMGR_MB8));
/*n = qse_str_ncatmbs(out->u.strp, str, str_len, qse_findcmgrbyid(QSE_CMGR_MB8));*/
n = qse_str_ncatmbs(out->u.strp, str, str_len, rtx->awk->cmgr);
if (n == (qse_size_t)-1)
{
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
@ -1109,7 +1112,8 @@ static int mbs_to_str (qse_awk_rtx_t* rtx, const qse_mchar_t* str, qse_size_t st
{
qse_size_t n;
n = qse_str_ncatmbs(out->u.strpcat, str, str_len, qse_findcmgrbyid(QSE_CMGR_MB8));
/*n = qse_str_ncatmbs(out->u.strpcat, str, str_len, qse_findcmgrbyid(QSE_CMGR_MB8));*/
n = qse_str_ncatmbs(out->u.strpcat, str, str_len, rtx->awk->cmgr);
if (n == (qse_size_t)-1)
{
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
@ -1515,10 +1519,10 @@ qse_mchar_t* qse_awk_rtx_valtombsdup (qse_awk_rtx_t* rtx, const qse_awk_val_t* v
#if defined(QSE_CHAR_IS_MCHAR)
mbs = qse_strxdup(str0, len0, rtx->awk->mmgr);
len1 = len0;
#else
mbs = qse_wcsntombsdupwithcmgr(str0, len0, &len1, rtx->awk->mmgr, rtx->awk->cmgr);
#endif
qse_awk_rtx_freevalstr (rtx, v, str0);
if (!mbs)
{