added some more string copy functions

This commit is contained in:
hyunghwan.chung 2019-11-29 09:28:29 +00:00
parent 55a57dbf9e
commit e46356071b
3 changed files with 310 additions and 1 deletions

View File

@ -433,6 +433,23 @@ MOO_EXPORT void moo_copy_bchars_to_uchars (
const moo_bch_t* src,
moo_oow_t len
);
MOO_EXPORT void moo_copy_uchars_to_bchars (
moo_bch_t* dst,
const moo_uch_t* src,
moo_oow_t len
);
MOO_EXPORT moo_oow_t moo_copy_uchars_to_ucstr_unlimited (
moo_uch_t* dst,
const moo_uch_t* src,
moo_oow_t len
);
MOO_EXPORT moo_oow_t moo_copy_bchars_to_bcstr_unlimited (
moo_bch_t* dst,
const moo_bch_t* src,
moo_oow_t len
);
MOO_EXPORT moo_oow_t moo_copy_ucstr (
moo_uch_t* dst,
@ -446,6 +463,30 @@ MOO_EXPORT moo_oow_t moo_copy_bcstr (
const moo_bch_t* src
);
MOO_EXPORT moo_oow_t moo_copy_uchars_to_ucstr (
moo_uch_t* dst,
moo_uch_t dlen,
const moo_uch_t* src,
moo_oow_t slen
);
MOO_EXPORT moo_oow_t moo_copy_bchars_to_bcstr (
moo_bch_t* dst,
moo_bch_t dlen,
const moo_bch_t* src,
moo_oow_t slen
);
MOO_EXPORT moo_oow_t moo_copy_ucstr_unlimited (
moo_uch_t* dst,
const moo_uch_t* src
);
MOO_EXPORT moo_oow_t moo_copy_bcstr_unlimited (
moo_bch_t* dst,
const moo_bch_t* src
);
MOO_EXPORT void moo_fill_uchars (
moo_uch_t* dst,
const moo_uch_t ch,
@ -533,9 +574,18 @@ MOO_EXPORT moo_oow_t moo_count_bcstr_limited (
# define moo_comp_oochars_ucstr(str1,len1,str2) moo_comp_uchars_ucstr(str1,len1,str2)
# define moo_comp_oochars_oocstr(str1,len1,str2) moo_comp_uchars_ucstr(str1,len1,str2)
# define moo_comp_oocstr(str1,str2) moo_comp_ucstr(str1,str2)
# define moo_copy_oochars(dst,src,len) moo_copy_uchars(dst,src,len)
# define moo_copy_bchars_to_oochars(dst,src,len) moo_copy_bchars_to_uchars(dst,src,len)
# define moo_copy_oochars_to_bchars(dst,src,len) moo_copy_uchars_to_bchars(dst,src,len)
# define moo_copy_uchars_to_oochars(dst,src,len) moo_copy_uchars(dst,src,len)
# define moo_copy_oochars_to_uchars(dst,src,len) moo_copy_uchars(dst,src,len)
# define moo_copy_oochars_to_oocstr(dst,dlen,src,slen) moo_copy_uchars_to_ucstr(dst,dlen,src,slen)
# define moo_copy_oochars_to_oocstr_unlimited(dst,src,len) moo_copy_uchars_to_ucstr_unlimited(dst,src,len)
# define moo_copy_oocstr(dst,len,src) moo_copy_ucstr(dst,len,src)
# define moo_copy_oocstr_unlimited(dst,src) moo_copy_ucstr_unlimited(dst,src)
# define moo_fill_oochars(dst,ch,len) moo_fill_uchars(dst,ch,len)
# define moo_find_oochar(ptr,len,c) moo_find_uchar(ptr,len,c)
# define moo_rfind_oochar(ptr,len,c) moo_rfind_uchar(ptr,len,c)
@ -550,9 +600,19 @@ MOO_EXPORT moo_oow_t moo_count_bcstr_limited (
# define moo_comp_oochars_ucstr(str1,len1,str2) moo_comp_bchars_ucstr(str1,len1,str2)
# define moo_comp_oochars_oocstr(str1,len1,str2) moo_comp_bchars_bcstr(str1,len1,str2)
# define moo_comp_oocstr(str1,str2) moo_comp_bcstr(str1,str2)
# define moo_copy_oochars(dst,src,len) moo_copy_bchars(dst,src,len)
# define moo_copy_bchars_to_oochars(dst,src,len) moo_copy_bchars(dst,src,len)
# define moo_copy_oochars_to_bchars(dst,src,len) moo_copy_bchars(dst,src,len)
# define moo_copy_uchars_to_oochars(dst,src,len) moo_copy_uchars_to_bchars(dst,src,len)
# define moo_copy_oochars_to_uchars(dst,src,len) moo_copy_bchars_to_uchars(dst,src,len)
# define moo_copy_oochars_to_oocstr(dst,dlen,src,slen) moo_copy_bchars_to_bcstr(dst,dlen,src,slen)
# define moo_copy_oochars_to_oocstr_unlimited(dst,src,len) moo_copy_bchars_to_bcstr_unlimited(dst,src,len)
# define moo_copy_oocstr(dst,len,src) moo_copy_bcstr(dst,len,src)
# define moo_copy_oocstr_unlimited(dst,src) moo_copy_bcstr_unlimited(dst,src)
# define moo_fill_oochars(dst,ch,len) moo_fill_bchars(dst,ch,len)
# define moo_find_oochar(ptr,len,c) moo_find_bchar(ptr,len,c)
# define moo_rfind_oochar(ptr,len,c) moo_rfind_bchar(ptr,len,c)

View File

@ -1137,3 +1137,193 @@ int moo_ischildclassof (moo_t* moo, moo_oop_class_t c, moo_oop_class_t k)
return 0;
}
/* ----------------------------------------------------------------------- */
struct fmt_bch_buf_t
{
moo_t* moo;
moo_bch_t* ptr;
moo_oow_t len;
moo_oow_t capa;
};
typedef struct fmt_bch_buf_t fmt_bch_buf_t;
static int fmt_put_bchars_to_bch_buf (moo_fmtout_t* fmtout, const moo_bch_t* ptr, moo_oow_t len)
{
fmt_bch_buf_t* b = (fmt_bch_buf_t*)fmtout->ctx;
moo_oow_t n;
/* this function null-terminates the destination. so give the restored buffer size */
n = moo_copy_bchars_to_bcstr(&b->ptr[b->len], b->capa - b->len + 1, ptr, len);
b->len += n;
if (n < len)
{
moo_seterrnum (b->moo, MOO_EBUFFULL);
return 0; /* stop. insufficient buffer */
}
return 1; /* success */
}
static int fmt_put_uchars_to_bch_buf (moo_fmtout_t* fmtout, const moo_uch_t* ptr, moo_oow_t len)
{
fmt_bch_buf_t* b = (fmt_bch_buf_t*)fmtout->ctx;
moo_oow_t bcslen, ucslen;
int n;
bcslen = b->capa - b->len;
ucslen = len;
n = moo_conv_uchars_to_bchars_with_cmgr(ptr, &ucslen, &b->ptr[b->len], &bcslen, moo_getcmgr(b->moo));
b->len += bcslen;
if (n <= -1)
{
if (n == -2)
{
return 0; /* buffer full. stop */
}
else
{
moo_seterrnum (b->moo, MOO_EECERR);
return -1;
}
}
return 1; /* success. carry on */
}
moo_oow_t moo_vfmttobcstr (moo_t* moo, moo_bch_t* buf, moo_oow_t bsz, const moo_bch_t* fmt, va_list ap)
{
moo_fmtout_t fo;
fmt_bch_buf_t fb;
if (bsz <= 0) return 0;
MOO_MEMSET (&fo, 0, MOO_SIZEOF(fo));
//fo.mmgr = moo_getmmgr(moo);
fo.putbchars = fmt_put_bchars_to_bch_buf;
fo.putuchars = fmt_put_uchars_to_bch_buf;
fo.ctx = &fb;
MOO_MEMSET (&fb, 0, MOO_SIZEOF(fb));
fb.moo = moo;
fb.ptr = buf;
fb.capa = bsz - 1;
if (moo_bfmt_outv(&fo, fmt, ap) <= -1) return -1;
buf[fb.len] = '\0';
return fb.len;
}
moo_oow_t moo_fmttobcstr (moo_t* moo, moo_bch_t* buf, moo_oow_t bufsz, const moo_bch_t* fmt, ...)
{
moo_oow_t x;
va_list ap;
va_start (ap, fmt);
x = moo_vfmttobcstr(moo, buf, bufsz, fmt, ap);
va_end (ap);
return x;
}
/* ----------------------------------------------------------------------- */
struct fmt_uch_buf_t
{
moo_t* moo;
moo_uch_t* ptr;
moo_oow_t len;
moo_oow_t capa;
};
typedef struct fmt_uch_buf_t fmt_uch_buf_t;
static int fmt_put_bchars_to_uch_buf (moo_fmtout_t* fmtout, const moo_bch_t* ptr, moo_oow_t len)
{
fmt_uch_buf_t* b = (fmt_uch_buf_t*)fmtout->ctx;
moo_oow_t bcslen, ucslen;
int n;
bcslen = len;
ucslen = b->capa - b->len;
n = moo_conv_bchars_to_uchars_with_cmgr(ptr, &bcslen, &b->ptr[b->len], &ucslen, moo_getcmgr(b->moo), 1);
b->len += ucslen;
if (n <= -1)
{
if (n == -2)
{
return 0; /* buffer full. stop */
}
else
{
moo_seterrnum (b->moo, MOO_EECERR);
return -1;
}
}
return 1; /* success. carry on */
}
static int fmt_put_uchars_to_uch_buf (moo_fmtout_t* fmtout, const moo_uch_t* ptr, moo_oow_t len)
{
fmt_uch_buf_t* b = (fmt_uch_buf_t*)fmtout->ctx;
moo_oow_t n;
/* this function null-terminates the destination. so give the restored buffer size */
n = moo_copy_uchars_to_ucstr(&b->ptr[b->len], b->capa - b->len + 1, ptr, len);
b->len += n;
if (n < len)
{
moo_seterrnum (b->moo, MOO_EBUFFULL);
return 0; /* stop. insufficient buffer */
}
return 1; /* success */
}
moo_oow_t moo_vfmttoucstr (moo_t* moo, moo_uch_t* buf, moo_oow_t bsz, const moo_uch_t* fmt, va_list ap)
{
moo_fmtout_t fo;
fmt_uch_buf_t fb;
if (bsz <= 0) return 0;
MOO_MEMSET (&fo, 0, MOO_SIZEOF(fo));
//fo.mmgr = moo_getmmgr(moo);
fo.putbchars = fmt_put_bchars_to_uch_buf;
fo.putuchars = fmt_put_uchars_to_uch_buf;
fo.ctx = &fb;
MOO_MEMSET (&fb, 0, MOO_SIZEOF(fb));
fb.moo = moo;
fb.ptr = buf;
fb.capa = bsz - 1;
if (moo_ufmt_outv(&fo, fmt, ap) <= -1) return -1;
buf[fb.len] = '\0';
return fb.len;
}
moo_oow_t moo_fmttoucstr (moo_t* moo, moo_uch_t* buf, moo_oow_t bufsz, const moo_uch_t* fmt, ...)
{
moo_oow_t x;
va_list ap;
va_start (ap, fmt);
x = moo_vfmttoucstr(moo, buf, bufsz, fmt, ap);
va_end (ap);
return x;
}

View File

@ -259,11 +259,55 @@ void moo_copy_bchars (moo_bch_t* dst, const moo_bch_t* src, moo_oow_t len)
void moo_copy_bchars_to_uchars (moo_uch_t* dst, const moo_bch_t* src, moo_oow_t len)
{
/* copy without conversions.
* use moo_bctouchars() for conversion encoding */
* use moo_convbtouchars() for conversion encoding */
moo_oow_t i;
for (i = 0; i < len; i++) dst[i] = src[i];
}
void moo_copy_uchars_to_bchars (moo_bch_t* dst, const moo_uch_t* src, moo_oow_t len)
{
/* copy without conversions.
* use moo_convutobchars() for conversion encoding */
moo_oow_t i;
for (i = 0; i < len; i++) dst[i] = src[i];
}
moo_oow_t moo_copy_uchars_to_ucstr (moo_uch_t* dst, moo_uch_t dlen, const moo_uch_t* src, moo_oow_t slen)
{
moo_oow_t i;
if (dlen <= 0) return 0;
if (dlen <= slen) slen = dlen - 1;
for (i = 0; i < slen; i++) dst[i] = src[i];
dst[i] = '\0';
return i;
}
moo_oow_t moo_copy_bchars_to_bcstr (moo_bch_t* dst, moo_bch_t dlen, const moo_bch_t* src, moo_oow_t slen)
{
moo_oow_t i;
if (dlen <= 0) return 0;
if (dlen <= slen) slen = dlen - 1;
for (i = 0; i < slen; i++) dst[i] = src[i];
dst[i] = '\0';
return i;
}
moo_oow_t moo_copy_uchars_to_ucstr_unlimited (moo_uch_t* dst, const moo_uch_t* src, moo_oow_t len)
{
moo_oow_t i;
for (i = 0; i < len; i++) dst[i] = src[i];
dst[i] = '\0';
return i;
}
moo_oow_t moo_copy_bchars_to_bcstr_unlimited (moo_bch_t* dst, const moo_bch_t* src, moo_oow_t len)
{
moo_oow_t i;
for (i = 0; i < len; i++) dst[i] = src[i];
dst[i] = '\0';
return i;
}
moo_oow_t moo_copy_ucstr (moo_uch_t* dst, moo_oow_t len, const moo_uch_t* src)
{
moo_uch_t* p, * p2;
@ -296,6 +340,21 @@ moo_oow_t moo_copy_bcstr (moo_bch_t* dst, moo_oow_t len, const moo_bch_t* src)
return p - dst;
}
moo_oow_t moo_copy_ucstr_unlimited (moo_uch_t* dst, const moo_uch_t* src)
{
moo_uch_t* org = dst;
while ((*dst++ = *src++) != '\0');
return dst - org - 1;
}
moo_oow_t moo_copy_bcstr_unlimited (moo_bch_t* dst, const moo_bch_t* src)
{
moo_bch_t* org = dst;
while ((*dst++ = *src++) != '\0');
return dst - org - 1;
}
void moo_fill_uchars (moo_uch_t* dst, moo_uch_t ch, moo_oow_t len)
{
moo_oow_t i;