added qse_str_ncatwcs()/qse_str_ncatmbs()

This commit is contained in:
hyung-hwan 2019-04-17 16:33:30 +00:00
parent f16437d7c0
commit f90ca01493
5 changed files with 129 additions and 24 deletions

View File

@ -3634,6 +3634,28 @@ QSE_EXPORT qse_size_t qse_wcs_vfmt (
# define qse_str_vfmt qse_wcs_vfmt
#endif
QSE_EXPORT qse_size_t qse_mbs_ncatwcs (
qse_mbs_t* str,
const qse_wchar_t* s,
qse_size_t len
);
qse_size_t qse_wcs_ncatmbs (
qse_wcs_t* str,
const qse_mchar_t* s,
qse_size_t len
);
#if defined(QSE_CHAR_IS_MCHAR)
# define qse_str_ncatwcs(str,s,len) qse_mbs_ncatwcs(str,s,len)
# define qse_str_ncatmbs(str,s,len) qse_mbs_ncat(str,s,len)
#else
# define qse_str_ncatwcs(str,s,len) qse_wcs_ncat(str,s,len)
# define qse_str_ncatmbs(str,s,len) qse_wcs_ncatmbs(str,s,len)
#endif
#if defined(__cplusplus)
}
#endif

View File

@ -1011,7 +1011,7 @@ static int str_to_str (qse_awk_rtx_t* rtx, const qse_char_t* str, qse_size_t str
if (str_len >= out->u.cplcpy.len)
{
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EINVAL, QSE_NULL);
out->u.cplcpy.len = str_len + 1; /* set the required length */
/*out->u.cplcpy.len = str_len + 1;*/ /* set the required length */
return -1;
}
@ -1085,17 +1085,15 @@ 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_mbsntowcsnwithcmgr(str, &mbslen, out->u.cplcpy.ptr, &wcslen, rtx->cmgr) <= -1)
if (qse_mbsntowcsnallwithcmgr(str, &mbslen, out->u.cplcpy.ptr, &wcslen, rtx->cmgr) <= -1 || wcslen >= out->u.cplcpy.len)
{
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EINVAL, QSE_NULL); /* TODO: change error code */
return -1;
}
if (mbslen < str_len)
{
/* TODO: ... */
}
out->u.cplcpy.ptr[wcslen] = QSE_T('\0');
out->u.cplcpy.len = wcslen;
return 0;
}
@ -1105,7 +1103,7 @@ 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_mbsntowcsdupwithcmgr(str, &mbslen, &wcslen, rtx->awk->mmgr, rtx->cmgr);
tmp = qse_mbsntowcsalldupwithcmgr(str, &mbslen, &wcslen, rtx->awk->mmgr, rtx->cmgr);
if (!tmp)
{
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
@ -1122,7 +1120,7 @@ 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_ncat(out->u.strp, str, str_len);
n = qse_str_ncatmbs(out->u.strp, str, str_len);
if (n == (qse_size_t)-1)
{
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
@ -1135,7 +1133,7 @@ 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_ncat(out->u.strpcat, str, str_len);
n = qse_str_ncatmbs(out->u.strpcat, str, str_len);
if (n == (qse_size_t)-1)
{
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);

View File

@ -278,3 +278,42 @@ static int mbs_to_wcs (
#define str_fcat qse_wcs_fcat
#define str_vfcat qse_wcs_vfcat
#include "str-dyn.h"
qse_size_t qse_mbs_ncatwcs (qse_mbs_t* str, const qse_wchar_t* s, qse_size_t len)
{
qse_size_t mbslen, wcslen;
qse_cmgr_t* cmgr = qse_getdflcmgr();
wcslen = len;
if (qse_wcsntombsnwithcmgr(s, &wcslen, QSE_NULL, &mbslen, cmgr) <= -1) return (qse_size_t)-1;
if (resize_for_mbs_ncat(str, mbslen) <= 0) return -1;
wcslen = len;
mbslen = str->capa - str->val.len;
qse_wcsntombsnwithcmgr(s, &wcslen, &str->val.ptr[str->val.len], &mbslen, cmgr);
str->val.len += mbslen;
str->val.ptr[str->val.len] = QSE_MT('\0');
return str->val.len;
}
qse_size_t qse_wcs_ncatmbs (qse_wcs_t* str, const qse_mchar_t* s, qse_size_t len)
{
qse_size_t mbslen, wcslen;
qse_cmgr_t* cmgr = qse_getdflcmgr();
mbslen = len;
if (qse_mbsntowcsnallwithcmgr(s, &mbslen, QSE_NULL, &wcslen, cmgr) <= -1) return (qse_size_t)-1;
if (resize_for_wcs_ncat(str, wcslen) <= 0) return -1;
mbslen = len;
wcslen = str->capa - str->val.len;
qse_mbsntowcsnallwithcmgr(s, &mbslen, &str->val.ptr[str->val.len], &wcslen, cmgr);
str->val.len += wcslen;
str->val.ptr[str->val.len] = QSE_WT('\0');
return str->val.len;
}

View File

@ -1,3 +1,4 @@
#include <qse/cmn/test.h>
#include <qse/cmn/mem.h>
#include <qse/cmn/str.h>
#include <qse/si/sio.h>
@ -399,6 +400,50 @@ static int test17 (void)
return 0;
}
static int test18 (void)
{
qse_str_t* s1 = QSE_NULL;
s1 = qse_str_open (QSE_MMGR_GETDFL(), 0, 5);
if (s1 == QSE_NULL)
{
qse_printf (QSE_T("cannot open a string\n"));
return -1;
}
qse_str_ncat (s1, QSE_T("["), 1);
qse_str_ncatwcs (s1, QSE_WT("hello"), 5);
qse_str_ncatmbs (s1, QSE_MT("world"), 5);
qse_str_ncat (s1, QSE_T("]"), 1);
QSE_TESASSERT1 (QSE_STR_LEN(s1) == 12, QSE_T("wrong length of dynamic string"));
qse_str_ncatmbs (s1, QSE_MT("\xEB\xAC\xB4\xEB\xAC\xB4\xEB\xAC\xB4"), 9);
#if defined(QSE_CHAR_IS_MCHAR)
QSE_TESASSERT1 (QSE_STR_LEN(s1) == 21, QSE_T("wrong length of dynamic string"));
#else
QSE_TESASSERT1 (QSE_STR_LEN(s1) == 15, QSE_T("wrong length of dynamic string"));
#endif
qse_str_ncatwcs (s1, QSE_WT("날아올라라"), 5);
#if defined(QSE_CHAR_IS_MCHAR)
QSE_TESASSERT1 (QSE_STR_LEN(s1) == 36, QSE_T("wrong length of dynamic string"));
#else
QSE_TESASSERT1 (QSE_STR_LEN(s1) == 20, QSE_T("wrong length of dynamic string"));
#endif
qse_printf (QSE_T("\t%js\n"), QSE_STR_PTR(s1));
qse_str_close (s1);
qse_printf (QSE_T("\tOK\n"));
return 0;
oops:
if (s1) qse_str_close (s1);
return -1;
}
int main ()
{
@ -433,6 +478,7 @@ int main ()
R (test15);
R (test16);
R (test17);
R (test18);
qse_close_stdsios ();