hio/lib/ecs.c

103 lines
3.3 KiB
C
Raw Normal View History

2020-04-30 16:20:31 +00:00
/*
2020-05-04 08:40:05 +00:00
Copyright (c) 2016-2020 Chung, Hyung-Hwan. All rights reserved.
2020-04-30 16:20:31 +00:00
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
2021-07-22 07:30:20 +00:00
#include <hio-ecs.h>
#include <hio-fmt.h>
#include "hio-prv.h"
2020-04-30 16:20:31 +00:00
2021-07-22 07:30:20 +00:00
#define _FN(type,verb) hio_ ## type ## _ ## verb
2020-04-30 16:20:31 +00:00
/* ------------------------------------------------------------------------- */
#undef FN
#undef T
#undef str_t
#undef char_t
#undef cstr_t
#undef count_chars
#define FN(verb) _FN(becs,verb)
2021-07-22 07:30:20 +00:00
#define T(x) HIO_BT(x)
#define str_t hio_becs_t
#define char_t hio_bch_t
#define cstr_t hio_bcs_t
#define count_chars(x) hio_count_bcstr(x)
2020-04-30 16:20:31 +00:00
#define BUILD_BECS
#include "ecs-imp.h"
/* ------------------------------------------------------------------------- */
#undef FN
#undef T
#undef str_t
#undef char_t
#undef cstr_t
#undef count_chars
#define FN(verb) _FN(uecs,verb)
2021-07-22 07:30:20 +00:00
#define T(x) HIO_UT(x)
#define str_t hio_uecs_t
#define char_t hio_uch_t
#define cstr_t hio_ucs_t
#define count_chars(x) hio_count_ucstr(x)
2020-04-30 16:20:31 +00:00
#define BUILD_UECS
#include "ecs-imp.h"
/* ------------------------------------------------------------------------- */
2021-07-22 07:30:20 +00:00
hio_oow_t hio_becs_ncatuchars (hio_becs_t* str, const hio_uch_t* s, hio_oow_t len, hio_cmgr_t* cmgr)
2020-04-30 16:20:31 +00:00
{
2021-07-22 07:30:20 +00:00
hio_oow_t bcslen, ucslen;
2020-04-30 16:20:31 +00:00
ucslen = len;
2021-07-22 07:30:20 +00:00
if (hio_conv_uchars_to_bchars_with_cmgr(s, &ucslen, HIO_NULL, &bcslen, cmgr) <= -1) return (hio_oow_t)-1;
2020-04-30 16:20:31 +00:00
2021-07-22 07:30:20 +00:00
if (hio_becs_resize_for_ncat(str, bcslen) <= 0) return -1;
2020-04-30 16:20:31 +00:00
ucslen = len;
bcslen = str->capa - str->val.len;
2021-07-22 07:30:20 +00:00
hio_conv_uchars_to_bchars_with_cmgr (s, &ucslen, &str->val.ptr[str->val.len], &bcslen, cmgr);
2020-04-30 16:20:31 +00:00
str->val.len += bcslen;
str->val.ptr[str->val.len] = '\0';
return str->val.len;
}
2021-07-22 07:30:20 +00:00
hio_oow_t hio_uecs_ncatbchars (hio_uecs_t* str, const hio_bch_t* s, hio_oow_t len, hio_cmgr_t* cmgr, int all)
2020-04-30 16:20:31 +00:00
{
2021-07-22 07:30:20 +00:00
hio_oow_t bcslen, ucslen;
2020-04-30 16:20:31 +00:00
bcslen = len;
2021-07-22 07:30:20 +00:00
if (hio_conv_bchars_to_uchars_with_cmgr(s, &bcslen, HIO_NULL, &ucslen, cmgr, all) <= -1) return (hio_oow_t)-1;
2020-04-30 16:20:31 +00:00
2021-07-22 07:30:20 +00:00
if (hio_uecs_resize_for_ncat(str, ucslen) <= 0) return -1;
2020-04-30 16:20:31 +00:00
bcslen = len;
ucslen = str->capa - str->val.len;
2021-07-22 07:30:20 +00:00
hio_conv_bchars_to_uchars_with_cmgr (s, &bcslen, &str->val.ptr[str->val.len], &ucslen, cmgr, all);
2020-04-30 16:20:31 +00:00
str->val.len += ucslen;
str->val.ptr[str->val.len] = '\0';
return str->val.len;
}