2017-01-09 09:54:49 +00:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
2018-02-21 10:11:39 +00:00
|
|
|
Copyright (c) 2014-2018 Chung, Hyung-Hwan. All rights reserved.
|
2017-01-09 09:54:49 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _MOO_UTL_H_
|
|
|
|
#define _MOO_UTL_H_
|
|
|
|
|
|
|
|
#include "moo-cmn.h"
|
|
|
|
|
2019-03-06 01:58:51 +00:00
|
|
|
/* =========================================================================
|
|
|
|
* DOUBLY LINKED LIST
|
|
|
|
* ========================================================================= */
|
2017-07-20 16:33:53 +00:00
|
|
|
#define MOO_APPEND_TO_LIST(list, node) do { \
|
|
|
|
(node)->next = MOO_NULL; \
|
|
|
|
(node)->prev = (list)->last; \
|
|
|
|
if ((list)->first) (list)->last->next = (node); \
|
|
|
|
else (list)->first = (node); \
|
|
|
|
(list)->last = (node); \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
#define MOO_PREPPEND_TO_LIST(list, node) do { \
|
|
|
|
(node)->prev = MOO_NULL; \
|
|
|
|
(node)->next = (list)->first; \
|
|
|
|
if ((list)->last) (list)->first->prev = (node); \
|
|
|
|
else (list)->last = (node); \
|
|
|
|
(list)->first = (node); \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
#define MOO_DELETE_FROM_LIST(list, node) do { \
|
|
|
|
if ((node)->prev) (node)->prev->next = (node)->next; \
|
|
|
|
else (list)->first = (node)->next; \
|
|
|
|
if ((node)->next) (node)->next->prev = (node)->prev; \
|
|
|
|
else (list)->last = (node)->prev; \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
|
2017-07-24 13:25:25 +00:00
|
|
|
#define MOO_APPEND_TO_OOP_LIST(moo, list, node_type, node, _link) do { \
|
|
|
|
(node)->_link.next = (node_type)(moo)->_nil; \
|
|
|
|
(node)->_link.prev = (list)->last; \
|
|
|
|
if ((moo_oop_t)(list)->last != (moo)->_nil) (list)->last->_link.next = (node); \
|
|
|
|
else (list)->first = (node); \
|
|
|
|
(list)->last = (node); \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
#define MOO_PREPPEND_TO_OOP_LIST(moo, list, node_type, node, _link) do { \
|
|
|
|
(node)->_link.prev = (node_type)(moo)->_nil; \
|
|
|
|
(node)->_link.next = (list)->first; \
|
|
|
|
if ((moo_oop_t)(list)->first != (moo)->_nil) (list)->first->_link.prev = (node); \
|
|
|
|
else (list)->last = (node); \
|
|
|
|
(list)->first = (node); \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
#define MOO_DELETE_FROM_OOP_LIST(moo, list, node, _link) do { \
|
|
|
|
if ((moo_oop_t)(node)->_link.prev != (moo)->_nil) (node)->_link.prev->_link.next = (node)->_link.next; \
|
|
|
|
else (list)->first = (node)->_link.next; \
|
|
|
|
if ((moo_oop_t)(node)->_link.next != (moo)->_nil) (node)->_link.next->_link.prev = (node)->_link.prev; \
|
|
|
|
else (list)->last = (node)->_link.prev; \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
/*
|
|
|
|
#define MOO_CLEANUP_FROM_OOP_LIST(moo, list, node, _link) do { \
|
|
|
|
MOO_DELETE_FROM_OOP_LIST (moo, list, node, _link); \
|
2017-08-22 13:45:37 +00:00
|
|
|
(node)->_link.prev = (node_type)(moo)->_nil; \
|
|
|
|
(node)->_link.next = (node_type)(moo)->_nil; \
|
2017-07-24 13:25:25 +00:00
|
|
|
} while(0);
|
|
|
|
*/
|
|
|
|
|
2019-03-06 01:58:51 +00:00
|
|
|
/* =========================================================================
|
|
|
|
* ENDIAN CHANGE
|
|
|
|
* ========================================================================= */
|
2018-01-10 14:39:31 +00:00
|
|
|
#define MOO_CONST_SWAP16(x) \
|
|
|
|
((qse_uint16_t)((((qse_uint16_t)(x) & (qse_uint16_t)0x00ffU) << 8) | \
|
|
|
|
(((qse_uint16_t)(x) & (qse_uint16_t)0xff00U) >> 8) ))
|
|
|
|
|
|
|
|
#define MOO_CONST_SWAP32(x) \
|
|
|
|
((qse_uint32_t)((((qse_uint32_t)(x) & (qse_uint32_t)0x000000ffUL) << 24) | \
|
|
|
|
(((qse_uint32_t)(x) & (qse_uint32_t)0x0000ff00UL) << 8) | \
|
|
|
|
(((qse_uint32_t)(x) & (qse_uint32_t)0x00ff0000UL) >> 8) | \
|
|
|
|
(((qse_uint32_t)(x) & (qse_uint32_t)0xff000000UL) >> 24) ))
|
|
|
|
|
|
|
|
#if defined(MOO_ENDIAN_LITTLE)
|
|
|
|
# define MOO_CONST_NTOH16(x) MOO_CONST_SWAP16(x)
|
|
|
|
# define MOO_CONST_HTON16(x) MOO_CONST_SWAP16(x)
|
|
|
|
# define MOO_CONST_NTOH32(x) MOO_CONST_SWAP32(x)
|
|
|
|
# define MOO_CONST_HTON32(x) MOO_CONST_SWAP32(x)
|
|
|
|
#elif defined(MOO_ENDIAN_BIG)
|
|
|
|
# define MOO_CONST_NTOH16(x) (x)
|
|
|
|
# define MOO_CONST_HTON16(x) (x)
|
|
|
|
# define MOO_CONST_NTOH32(x) (x)
|
|
|
|
# define MOO_CONST_HTON32(x) (x)
|
|
|
|
#else
|
|
|
|
# error UNKNOWN ENDIAN
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2019-03-06 01:58:51 +00:00
|
|
|
/* =========================================================================
|
|
|
|
* HASH
|
|
|
|
* ========================================================================= */
|
|
|
|
|
2019-03-07 12:53:23 +00:00
|
|
|
#if (MOO_SIZEOF_OOW_T == 4)
|
2019-03-06 01:58:51 +00:00
|
|
|
# define MOO_HASH_FNV_MAGIC_INIT (0x811c9dc5)
|
|
|
|
# define MOO_HASH_FNV_MAGIC_PRIME (0x01000193)
|
2019-03-07 12:53:23 +00:00
|
|
|
#elif (MOO_SIZEOF_OOW_T == 8)
|
2019-03-06 01:58:51 +00:00
|
|
|
# define MOO_HASH_FNV_MAGIC_INIT (0xCBF29CE484222325)
|
|
|
|
# define MOO_HASH_FNV_MAGIC_PRIME (0x100000001B3l)
|
2019-03-07 12:53:23 +00:00
|
|
|
#elif (MOO_SIZEOF_OOW_T == 16)
|
2019-03-06 01:58:51 +00:00
|
|
|
# define MOO_HASH_FNV_MAGIC_INIT (0x6C62272E07BB014262B821756295C58D)
|
|
|
|
# define MOO_HASH_FNV_MAGIC_PRIME (0x1000000000000000000013B)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MOO_HASH_FNV_MAGIC_INIT)
|
|
|
|
/* FNV-1 hash */
|
|
|
|
# define MOO_HASH_INIT MOO_HASH_FNV_MAGIC_INIT
|
|
|
|
# define MOO_HASH_VALUE(hv,v) (((hv) ^ (v)) * MOO_HASH_FNV_MAGIC_PRIME)
|
|
|
|
|
|
|
|
#else
|
|
|
|
/* SDBM hash */
|
|
|
|
# define MOO_HASH_INIT 0
|
|
|
|
# define MOO_HASH_VALUE(hv,v) (((hv) << 6) + ((hv) << 16) - (hv) + (v))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define MOO_HASH_VPTL(hv, ptr, len, type) do { \
|
|
|
|
hv = MOO_HASH_INIT; \
|
|
|
|
MOO_HASH_MORE_VPTL (hv, ptr, len, type); \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
#define MOO_HASH_MORE_VPTL(hv, ptr, len, type) do { \
|
|
|
|
type* __moo_hash_more_vptl_p = (type*)(ptr); \
|
|
|
|
type* __moo_hash_more_vptl_q = (type*)__moo_hash_more_vptl_p + (len); \
|
|
|
|
while (__moo_hash_more_vptl_p < __moo_hash_more_vptl_q) \
|
|
|
|
{ \
|
|
|
|
hv = MOO_HASH_VALUE(hv, *__moo_hash_more_vptl_p); \
|
|
|
|
__moo_hash_more_vptl_p++; \
|
|
|
|
} \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
#define MOO_HASH_VPTR(hv, ptr, type) do { \
|
|
|
|
hv = MOO_HASH_INIT; \
|
|
|
|
MOO_HASH_MORE_VPTR (hv, ptr, type); \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
#define MOO_HASH_MORE_VPTR(hv, ptr, type) do { \
|
|
|
|
type* __moo_hash_more_vptr_p = (type*)(ptr); \
|
|
|
|
while (*__moo_hash_more_vptr_p) \
|
|
|
|
{ \
|
|
|
|
hv = MOO_HASH_VALUE(hv, *__moo_hash_more_vptr_p); \
|
|
|
|
__moo_hash_more_vptr_p++; \
|
|
|
|
} \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
#define MOO_HASH_BYTES(hv, ptr, len) MOO_HASH_VPTL(hv, ptr, len, const moo_uint8_t)
|
|
|
|
#define MOO_HASH_MORE_BYTES(hv, ptr, len) MOO_HASH_MORE_VPTL(hv, ptr, len, const moo_uint8_t)
|
|
|
|
|
|
|
|
#define MOO_HASH_BCHARS(hv, ptr, len) MOO_HASH_VPTL(hv, ptr, len, const moo_bch_t)
|
|
|
|
#define MOO_HASH_MORE_BCHARS(hv, ptr, len) MOO_HASH_MORE_VPTL(hv, ptr, len, const moo_bch_t)
|
|
|
|
|
|
|
|
#define MOO_HASH_UCHARS(hv, ptr, len) MOO_HASH_VPTL(hv, ptr, len, const moo_uch_t)
|
|
|
|
#define MOO_HASH_MORE_UCHARS(hv, ptr, len) MOO_HASH_MORE_VPTL(hv, ptr, len, const moo_uch_t)
|
|
|
|
|
|
|
|
#define MOO_HASH_BCSTR(hv, ptr) MOO_HASH_VPTR(hv, ptr, const moo_bch_t)
|
|
|
|
#define MOO_HASH_MORE_BCSTR(hv, ptr) MOO_HASH_MORE_VPTR(hv, ptr, const moo_bch_t)
|
|
|
|
|
|
|
|
#define MOO_HASH_UCSTR(hv, ptr) MOO_HASH_VPTR(hv, ptr, const moo_uch_t)
|
|
|
|
#define MOO_HASH_MORE_UCSTR(hv, ptr) MOO_HASH_MORE_VPTR(hv, ptr, const moo_uch_t)
|
|
|
|
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
#if defined(__cplusplus)
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2019-03-06 01:58:51 +00:00
|
|
|
MOO_EXPORT moo_oow_t moo_hash_bytes_ (
|
2017-01-09 09:54:49 +00:00
|
|
|
const moo_oob_t* ptr,
|
|
|
|
moo_oow_t len
|
|
|
|
);
|
|
|
|
|
|
|
|
#if defined(MOO_HAVE_INLINE)
|
2019-03-06 01:58:51 +00:00
|
|
|
static MOO_INLINE moo_oow_t moo_hash_bytes (const moo_oob_t* ptr, moo_oow_t len)
|
|
|
|
{
|
|
|
|
moo_oow_t hv;
|
|
|
|
MOO_HASH_BYTES (hv, ptr, len);
|
|
|
|
/* constrain the hash value to be representable in a small integer
|
|
|
|
* for convenience sake */
|
|
|
|
return hv % ((moo_oow_t)MOO_SMOOI_MAX + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static MOO_INLINE moo_oow_t moo_hash_bchars (const moo_bch_t* ptr, moo_oow_t len)
|
2017-01-09 09:54:49 +00:00
|
|
|
{
|
2018-04-07 15:54:09 +00:00
|
|
|
return moo_hash_bytes((const moo_oob_t*)ptr, len * MOO_SIZEOF(moo_bch_t));
|
2017-01-09 09:54:49 +00:00
|
|
|
}
|
|
|
|
|
2019-03-06 01:58:51 +00:00
|
|
|
static MOO_INLINE moo_oow_t moo_hash_uchars (const moo_uch_t* ptr, moo_oow_t len)
|
2017-01-09 09:54:49 +00:00
|
|
|
{
|
2018-04-07 15:54:09 +00:00
|
|
|
return moo_hash_bytes((const moo_oob_t*)ptr, len * MOO_SIZEOF(moo_uch_t));
|
2017-01-09 09:54:49 +00:00
|
|
|
}
|
|
|
|
|
2019-03-06 01:58:51 +00:00
|
|
|
static MOO_INLINE moo_oow_t moo_hash_words (const moo_oow_t* ptr, moo_oow_t len)
|
2017-01-09 09:54:49 +00:00
|
|
|
{
|
2018-04-07 15:54:09 +00:00
|
|
|
return moo_hash_bytes((const moo_oob_t*)ptr, len * MOO_SIZEOF(moo_oow_t));
|
2017-01-09 09:54:49 +00:00
|
|
|
}
|
|
|
|
|
2019-03-06 01:58:51 +00:00
|
|
|
static MOO_INLINE moo_oow_t moo_hash_halfwords (const moo_oohw_t* ptr, moo_oow_t len)
|
2017-01-09 09:54:49 +00:00
|
|
|
{
|
2018-04-07 15:54:09 +00:00
|
|
|
return moo_hash_bytes((const moo_oob_t*)ptr, len * MOO_SIZEOF(moo_oohw_t));
|
2017-01-09 09:54:49 +00:00
|
|
|
}
|
|
|
|
#else
|
2019-03-06 01:58:51 +00:00
|
|
|
# define moo_hash_bytes(ptr,len) moo_hash_bytes_(ptr, len)
|
|
|
|
# define moo_hash_bchars(ptr,len) moo_hash_bytes((const moo_oob_t*)(ptr), (len) * MOO_SIZEOF(moo_bch_t))
|
|
|
|
# define moo_hash_uchars(ptr,len) moo_hash_bytes((const moo_oob_t*)(ptr), (len) * MOO_SIZEOF(moo_uch_t))
|
|
|
|
# define moo_hash_words(ptr,len) moo_hash_bytes((const moo_oob_t*)(ptr), (len) * MOO_SIZEOF(moo_oow_t))
|
|
|
|
# define moo_hash_halfwords(ptr,len) moo_hash_bytes((const moo_oob_t*)(ptr), (len) * MOO_SIZEOF(moo_oohw_t))
|
2017-01-09 09:54:49 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MOO_OOCH_IS_UCH)
|
2019-03-06 01:58:51 +00:00
|
|
|
# define moo_hash_oochars(ptr,len) moo_hash_uchars(ptr,len)
|
2017-01-09 09:54:49 +00:00
|
|
|
#else
|
2019-03-06 01:58:51 +00:00
|
|
|
# define moo_hash_oochars(ptr,len) moo_hash_bchars(ptr,len)
|
2017-01-09 09:54:49 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
2018-04-07 15:54:09 +00:00
|
|
|
* The moo_equal_uchars() function determines equality of two strings
|
2017-01-09 09:54:49 +00:00
|
|
|
* of the same length \a len.
|
|
|
|
*/
|
2018-04-07 15:54:09 +00:00
|
|
|
MOO_EXPORT int moo_equal_uchars (
|
2017-01-09 09:54:49 +00:00
|
|
|
const moo_uch_t* str1,
|
|
|
|
const moo_uch_t* str2,
|
|
|
|
moo_oow_t len
|
|
|
|
);
|
|
|
|
|
2018-04-07 15:54:09 +00:00
|
|
|
MOO_EXPORT int moo_equal_bchars (
|
2017-01-09 09:54:49 +00:00
|
|
|
const moo_bch_t* str1,
|
|
|
|
const moo_bch_t* str2,
|
|
|
|
moo_oow_t len
|
|
|
|
);
|
|
|
|
|
2018-04-07 15:54:09 +00:00
|
|
|
MOO_EXPORT int moo_comp_uchars (
|
2018-01-08 03:33:24 +00:00
|
|
|
const moo_uch_t* str1,
|
|
|
|
moo_oow_t len1,
|
|
|
|
const moo_uch_t* str2,
|
|
|
|
moo_oow_t len2
|
|
|
|
);
|
|
|
|
|
2018-04-07 15:54:09 +00:00
|
|
|
MOO_EXPORT int moo_comp_bchars (
|
2018-01-08 03:33:24 +00:00
|
|
|
const moo_bch_t* str1,
|
|
|
|
moo_oow_t len1,
|
|
|
|
const moo_bch_t* str2,
|
|
|
|
moo_oow_t len2
|
|
|
|
);
|
|
|
|
|
2018-04-07 15:54:09 +00:00
|
|
|
MOO_EXPORT int moo_comp_ucstr (
|
2017-01-09 09:54:49 +00:00
|
|
|
const moo_uch_t* str1,
|
|
|
|
const moo_uch_t* str2
|
|
|
|
);
|
|
|
|
|
2018-04-07 15:54:09 +00:00
|
|
|
MOO_EXPORT int moo_comp_bcstr (
|
2017-01-09 09:54:49 +00:00
|
|
|
const moo_bch_t* str1,
|
|
|
|
const moo_bch_t* str2
|
|
|
|
);
|
|
|
|
|
2018-04-07 15:54:09 +00:00
|
|
|
MOO_EXPORT int moo_comp_ucstr_bcstr (
|
2017-01-09 09:54:49 +00:00
|
|
|
const moo_uch_t* str1,
|
|
|
|
const moo_bch_t* str2
|
|
|
|
);
|
|
|
|
|
2018-04-07 15:54:09 +00:00
|
|
|
MOO_EXPORT int moo_comp_uchars_ucstr (
|
2017-03-31 14:21:22 +00:00
|
|
|
const moo_uch_t* str1,
|
|
|
|
moo_oow_t len,
|
|
|
|
const moo_uch_t* str2
|
|
|
|
);
|
|
|
|
|
2018-04-07 15:54:09 +00:00
|
|
|
MOO_EXPORT int moo_comp_uchars_bcstr (
|
2017-01-09 09:54:49 +00:00
|
|
|
const moo_uch_t* str1,
|
|
|
|
moo_oow_t len,
|
|
|
|
const moo_bch_t* str2
|
|
|
|
);
|
|
|
|
|
2018-04-07 15:54:09 +00:00
|
|
|
MOO_EXPORT int moo_comp_bchars_bcstr (
|
2017-01-09 09:54:49 +00:00
|
|
|
const moo_bch_t* str1,
|
|
|
|
moo_oow_t len,
|
|
|
|
const moo_bch_t* str2
|
|
|
|
);
|
|
|
|
|
2018-04-07 15:54:09 +00:00
|
|
|
MOO_EXPORT int moo_comp_bchars_ucstr (
|
2017-03-31 14:21:22 +00:00
|
|
|
const moo_bch_t* str1,
|
|
|
|
moo_oow_t len,
|
|
|
|
const moo_uch_t* str2
|
|
|
|
);
|
|
|
|
|
2018-04-07 15:54:09 +00:00
|
|
|
MOO_EXPORT void moo_copy_uchars (
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_uch_t* dst,
|
|
|
|
const moo_uch_t* src,
|
|
|
|
moo_oow_t len
|
|
|
|
);
|
|
|
|
|
2018-04-07 15:54:09 +00:00
|
|
|
MOO_EXPORT void moo_copy_bchars (
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_bch_t* dst,
|
|
|
|
const moo_bch_t* src,
|
|
|
|
moo_oow_t len
|
|
|
|
);
|
|
|
|
|
2018-04-07 15:54:09 +00:00
|
|
|
MOO_EXPORT void moo_copy_bchars_to_uchars (
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_uch_t* dst,
|
|
|
|
const moo_bch_t* src,
|
|
|
|
moo_oow_t len
|
|
|
|
);
|
|
|
|
|
2018-04-07 15:54:09 +00:00
|
|
|
MOO_EXPORT moo_oow_t moo_copy_ucstr (
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_uch_t* dst,
|
|
|
|
moo_oow_t len,
|
|
|
|
const moo_uch_t* src
|
|
|
|
);
|
|
|
|
|
2018-04-07 15:54:09 +00:00
|
|
|
MOO_EXPORT moo_oow_t moo_copy_bcstr (
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_bch_t* dst,
|
|
|
|
moo_oow_t len,
|
|
|
|
const moo_bch_t* src
|
|
|
|
);
|
|
|
|
|
2019-02-18 17:13:33 +00:00
|
|
|
MOO_EXPORT void moo_fill_uchars (
|
|
|
|
moo_uch_t* dst,
|
|
|
|
const moo_uch_t ch,
|
|
|
|
moo_oow_t len
|
|
|
|
);
|
|
|
|
|
|
|
|
MOO_EXPORT void moo_fill_bchars (
|
|
|
|
moo_bch_t* dst,
|
|
|
|
const moo_bch_t ch,
|
|
|
|
moo_oow_t len
|
|
|
|
);
|
|
|
|
|
|
|
|
|
2018-04-07 15:54:09 +00:00
|
|
|
MOO_EXPORT moo_uch_t* moo_find_uchar (
|
2017-01-09 09:54:49 +00:00
|
|
|
const moo_uch_t* ptr,
|
|
|
|
moo_oow_t len,
|
|
|
|
moo_uch_t c
|
|
|
|
);
|
|
|
|
|
2018-04-07 15:54:09 +00:00
|
|
|
MOO_EXPORT moo_bch_t* moo_find_bchar (
|
2017-01-09 09:54:49 +00:00
|
|
|
const moo_bch_t* ptr,
|
|
|
|
moo_oow_t len,
|
|
|
|
moo_bch_t c
|
|
|
|
);
|
|
|
|
|
2018-04-07 15:54:09 +00:00
|
|
|
MOO_EXPORT moo_uch_t* moo_rfind_uchar (
|
2017-01-09 09:54:49 +00:00
|
|
|
const moo_uch_t* ptr,
|
|
|
|
moo_oow_t len,
|
|
|
|
moo_uch_t c
|
|
|
|
);
|
|
|
|
|
2018-04-07 15:54:09 +00:00
|
|
|
MOO_EXPORT moo_bch_t* moo_rfind_bchar (
|
2017-01-09 09:54:49 +00:00
|
|
|
const moo_bch_t* ptr,
|
|
|
|
moo_oow_t len,
|
|
|
|
moo_bch_t c
|
|
|
|
);
|
|
|
|
|
2018-04-07 15:54:09 +00:00
|
|
|
MOO_EXPORT moo_uch_t* moo_find_uchar_in_ucstr (
|
2017-11-22 04:52:45 +00:00
|
|
|
const moo_uch_t* ptr,
|
|
|
|
moo_uch_t c
|
|
|
|
);
|
|
|
|
|
2018-04-07 15:54:09 +00:00
|
|
|
MOO_EXPORT moo_bch_t* moo_find_bchar_in_bcstr (
|
2017-11-22 04:52:45 +00:00
|
|
|
const moo_bch_t* ptr,
|
|
|
|
moo_bch_t c
|
|
|
|
);
|
2017-01-09 09:54:49 +00:00
|
|
|
|
2018-04-07 15:54:09 +00:00
|
|
|
MOO_EXPORT moo_oow_t moo_count_ucstr (
|
2017-01-09 09:54:49 +00:00
|
|
|
const moo_uch_t* str
|
|
|
|
);
|
|
|
|
|
2018-04-07 15:54:09 +00:00
|
|
|
MOO_EXPORT moo_oow_t moo_count_bcstr (
|
2017-01-09 09:54:49 +00:00
|
|
|
const moo_bch_t* str
|
|
|
|
);
|
|
|
|
|
|
|
|
#if defined(MOO_OOCH_IS_UCH)
|
2018-04-07 15:54:09 +00:00
|
|
|
# define moo_equal_oochars(str1,str2,len) moo_equal_uchars(str1,str2,len)
|
|
|
|
# define moo_comp_oochars(str1,len1,str2,len2) moo_comp_uchars(str1,len1,str2,len2)
|
|
|
|
# define moo_comp_oocstr_bcstr(str1,str2) moo_comp_ucstr_bcstr(str1,str2)
|
|
|
|
# define moo_comp_oochars_bcstr(str1,len1,str2) moo_comp_uchars_bcstr(str1,len1,str2)
|
|
|
|
# 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_oocstr(dst,len,src) moo_copy_ucstr(dst,len,src)
|
2019-02-18 17:13:33 +00:00
|
|
|
# define moo_fill_oochars(dst,ch,len) moo_fill_uchars(dst,ch,len)
|
2018-04-07 15:54:09 +00:00
|
|
|
# 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)
|
|
|
|
# define moo_find_oochar_in_oocstr(ptr,c) moo_find_uchar_in_ucstr(ptr,c)
|
|
|
|
# define moo_count_oocstr(str) moo_count_ucstr(str)
|
2017-01-09 09:54:49 +00:00
|
|
|
#else
|
2018-04-07 15:54:09 +00:00
|
|
|
# define moo_equal_oochars(str1,str2,len) moo_equal_bchars(str1,str2,len)
|
|
|
|
# define moo_comp_oochars(str1,len1,str2,len2) moo_comp_bchars(str1,len1,str2,len2)
|
|
|
|
# define moo_comp_oocstr_bcstr(str1,str2) moo_comp_bcstr(str1,str2)
|
|
|
|
# define moo_comp_oochars_bcstr(str1,len1,str2) moo_comp_bchars_bcstr(str1,len1,str2)
|
|
|
|
# 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_oocstr(dst,len,src) moo_copy_bcstr(dst,len,src)
|
2019-02-18 17:13:33 +00:00
|
|
|
# define moo_fill_oochars(dst,ch,len) moo_fill_bchars(dst,ch,len)
|
2018-04-07 15:54:09 +00:00
|
|
|
# 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)
|
|
|
|
# define moo_find_oochar_in_oocstr(ptr,c) moo_find_bchar_in_bcstr(ptr,c)
|
|
|
|
# define moo_count_oocstr(str) moo_count_bcstr(str)
|
2017-01-09 09:54:49 +00:00
|
|
|
#endif
|
|
|
|
|
2018-11-14 04:25:27 +00:00
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
MOO_EXPORT int moo_ucwidth (
|
|
|
|
moo_uch_t uc
|
|
|
|
);
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
|
2018-04-06 15:15:53 +00:00
|
|
|
#if defined(MOO_OOCH_IS_UCH)
|
|
|
|
# define moo_conv_oocs_to_bcs_with_cmgr(oocs,oocslen,bcs,bcslen,cmgr) moo_conv_ucs_to_bcs_with_cmgr(oocs,oocslen,bcs,bcslen,cmgr)
|
2018-04-07 15:54:09 +00:00
|
|
|
# define moo_conv_oochars_to_bchars_with_cmgr(oocs,oocslen,bcs,bcslen,cmgr) moo_conv_uchars_to_bchars_with_cmgr(oocs,oocslen,bcs,bcslen,cmgr)
|
2018-04-06 15:15:53 +00:00
|
|
|
#else
|
|
|
|
# define moo_conv_oocs_to_ucs_with_cmgr(oocs,oocslen,ucs,ucslen,cmgr) moo_conv_bcs_to_ucs_with_cmgr(oocs,oocslen,ucs,ucslen,cmgr,0)
|
2018-04-07 15:54:09 +00:00
|
|
|
# define moo_conv_oochars_to_uchars_with_cmgr(oocs,oocslen,ucs,ucslen,cmgr) moo_conv_bchars_to_uchars_with_cmgr(oocs,oocslen,ucs,ucslen,cmgr,0)
|
2018-04-06 15:15:53 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
MOO_EXPORT int moo_conv_bcs_to_ucs_with_cmgr (
|
|
|
|
const moo_bch_t* bcs,
|
|
|
|
moo_oow_t* bcslen,
|
|
|
|
moo_uch_t* ucs,
|
|
|
|
moo_oow_t* ucslen,
|
|
|
|
moo_cmgr_t* cmgr,
|
|
|
|
int all
|
|
|
|
);
|
2018-11-14 04:25:27 +00:00
|
|
|
|
2018-04-07 15:54:09 +00:00
|
|
|
MOO_EXPORT int moo_conv_bchars_to_uchars_with_cmgr (
|
2018-04-06 15:15:53 +00:00
|
|
|
const moo_bch_t* bcs,
|
|
|
|
moo_oow_t* bcslen,
|
|
|
|
moo_uch_t* ucs,
|
|
|
|
moo_oow_t* ucslen,
|
|
|
|
moo_cmgr_t* cmgr,
|
|
|
|
int all
|
|
|
|
);
|
|
|
|
|
|
|
|
MOO_EXPORT int moo_conv_ucs_to_bcs_with_cmgr (
|
|
|
|
const moo_uch_t* ucs,
|
|
|
|
moo_oow_t* ucslen,
|
|
|
|
moo_bch_t* bcs,
|
|
|
|
moo_oow_t* bcslen,
|
|
|
|
moo_cmgr_t* cmgr
|
2018-11-13 06:54:30 +00:00
|
|
|
);
|
2018-04-06 15:15:53 +00:00
|
|
|
|
2018-04-07 15:54:09 +00:00
|
|
|
MOO_EXPORT int moo_conv_uchars_to_bchars_with_cmgr (
|
2018-04-06 15:15:53 +00:00
|
|
|
const moo_uch_t* ucs,
|
|
|
|
moo_oow_t* ucslen,
|
|
|
|
moo_bch_t* bcs,
|
|
|
|
moo_oow_t* bcslen,
|
|
|
|
moo_cmgr_t* cmgr
|
|
|
|
);
|
|
|
|
|
2018-11-14 04:25:27 +00:00
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
|
2018-03-12 16:46:21 +00:00
|
|
|
MOO_EXPORT moo_cmgr_t* moo_get_utf8_cmgr (
|
2017-01-09 09:54:49 +00:00
|
|
|
void
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
2018-04-06 16:33:35 +00:00
|
|
|
* The moo_conv_uchars_to_utf8() function converts a unicode character string \a ucs
|
2017-01-09 09:54:49 +00:00
|
|
|
* to a UTF8 string and writes it into the buffer pointed to by \a bcs, but
|
|
|
|
* not more than \a bcslen bytes including the terminating null.
|
|
|
|
*
|
|
|
|
* Upon return, \a bcslen is modified to the actual number of bytes written to
|
|
|
|
* \a bcs excluding the terminating null; \a ucslen is modified to the number of
|
|
|
|
* wide characters converted.
|
|
|
|
*
|
|
|
|
* You may pass #MOO_NULL for \a bcs to dry-run conversion or to get the
|
|
|
|
* required buffer size for conversion. -2 is never returned in this case.
|
|
|
|
*
|
|
|
|
* \return
|
|
|
|
* - 0 on full conversion,
|
|
|
|
* - -1 on no or partial conversion for an illegal character encountered,
|
|
|
|
* - -2 on no or partial conversion for a small buffer.
|
|
|
|
*
|
|
|
|
* \code
|
|
|
|
* const moo_uch_t ucs[] = { 'H', 'e', 'l', 'l', 'o' };
|
|
|
|
* moo_bch_t bcs[10];
|
|
|
|
* moo_oow_t ucslen = 5;
|
|
|
|
* moo_oow_t bcslen = MOO_COUNTOF(bcs);
|
2018-04-06 16:33:35 +00:00
|
|
|
* n = moo_conv_uchars_to_utf8 (ucs, &ucslen, bcs, &bcslen);
|
2017-01-09 09:54:49 +00:00
|
|
|
* if (n <= -1)
|
|
|
|
* {
|
|
|
|
* // conversion error
|
|
|
|
* }
|
|
|
|
* \endcode
|
|
|
|
*/
|
2018-04-06 16:33:35 +00:00
|
|
|
MOO_EXPORT int moo_conv_uchars_to_utf8 (
|
2017-01-09 09:54:49 +00:00
|
|
|
const moo_uch_t* ucs,
|
|
|
|
moo_oow_t* ucslen,
|
|
|
|
moo_bch_t* bcs,
|
|
|
|
moo_oow_t* bcslen
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
2018-04-06 16:33:35 +00:00
|
|
|
* The moo_conv_utf8_to_uchars() function converts a UTF8 string to a uncide string.
|
2017-01-09 09:54:49 +00:00
|
|
|
*
|
|
|
|
* It never returns -2 if \a ucs is #MOO_NULL.
|
|
|
|
*
|
|
|
|
* \code
|
|
|
|
* const moo_bch_t* bcs = "test string";
|
|
|
|
* moo_uch_t ucs[100];
|
|
|
|
* moo_oow_t ucslen = MOO_COUNTOF(buf), n;
|
|
|
|
* moo_oow_t bcslen = 11;
|
|
|
|
* int n;
|
2018-04-06 16:33:35 +00:00
|
|
|
* n = moo_conv_utf8_to_uchars (bcs, &bcslen, ucs, &ucslen);
|
2017-01-09 09:54:49 +00:00
|
|
|
* if (n <= -1) { invalid/incomplenete sequence or buffer to small }
|
|
|
|
* \endcode
|
|
|
|
*
|
|
|
|
* The resulting \a ucslen can still be greater than 0 even if the return
|
|
|
|
* value is negative. The value indiates the number of characters converted
|
|
|
|
* before the error has occurred.
|
|
|
|
*
|
|
|
|
* \return 0 on success.
|
|
|
|
* -1 if \a bcs contains an illegal character.
|
|
|
|
* -2 if the wide-character string buffer is too small.
|
|
|
|
* -3 if \a bcs is not a complete sequence.
|
|
|
|
*/
|
2018-04-06 16:33:35 +00:00
|
|
|
MOO_EXPORT int moo_conv_utf8_to_uchars (
|
2017-01-09 09:54:49 +00:00
|
|
|
const moo_bch_t* bcs,
|
|
|
|
moo_oow_t* bcslen,
|
|
|
|
moo_uch_t* ucs,
|
|
|
|
moo_oow_t* ucslen
|
|
|
|
);
|
|
|
|
|
|
|
|
|
2018-04-06 16:33:35 +00:00
|
|
|
MOO_EXPORT int moo_conv_ucstr_to_utf8 (
|
2017-01-09 09:54:49 +00:00
|
|
|
const moo_uch_t* ucs,
|
|
|
|
moo_oow_t* ucslen,
|
|
|
|
moo_bch_t* bcs,
|
|
|
|
moo_oow_t* bcslen
|
|
|
|
);
|
|
|
|
|
2018-04-06 16:33:35 +00:00
|
|
|
MOO_EXPORT int moo_conv_utf8_to_ucstr (
|
2017-01-09 09:54:49 +00:00
|
|
|
const moo_bch_t* bcs,
|
|
|
|
moo_oow_t* bcslen,
|
|
|
|
moo_uch_t* ucs,
|
|
|
|
moo_oow_t* ucslen
|
|
|
|
);
|
|
|
|
|
|
|
|
|
2018-04-06 16:33:35 +00:00
|
|
|
MOO_EXPORT moo_oow_t moo_uc_to_utf8 (
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_uch_t uc,
|
|
|
|
moo_bch_t* utf8,
|
|
|
|
moo_oow_t size
|
|
|
|
);
|
|
|
|
|
2018-04-06 16:33:35 +00:00
|
|
|
MOO_EXPORT moo_oow_t moo_utf8_to_uc (
|
2017-01-09 09:54:49 +00:00
|
|
|
const moo_bch_t* utf8,
|
|
|
|
moo_oow_t size,
|
|
|
|
moo_uch_t* uc
|
|
|
|
);
|
|
|
|
|
2018-11-14 04:25:27 +00:00
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
MOO_EXPORT moo_cmgr_t* moo_get_utf16_cmgr (
|
|
|
|
void
|
|
|
|
);
|
|
|
|
|
|
|
|
MOO_EXPORT int moo_conv_uchars_to_utf16 (
|
|
|
|
const moo_uch_t* ucs,
|
|
|
|
moo_oow_t* ucslen,
|
|
|
|
moo_bch_t* bcs,
|
|
|
|
moo_oow_t* bcslen
|
|
|
|
);
|
|
|
|
|
|
|
|
MOO_EXPORT int moo_conv_utf16_to_uchars (
|
|
|
|
const moo_bch_t* bcs,
|
|
|
|
moo_oow_t* bcslen,
|
|
|
|
moo_uch_t* ucs,
|
|
|
|
moo_oow_t* ucslen
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
MOO_EXPORT int moo_conv_ucstr_to_utf16 (
|
|
|
|
const moo_uch_t* ucs,
|
|
|
|
moo_oow_t* ucslen,
|
|
|
|
moo_bch_t* bcs,
|
|
|
|
moo_oow_t* bcslen
|
|
|
|
);
|
|
|
|
|
|
|
|
MOO_EXPORT int moo_conv_utf16_to_ucstr (
|
|
|
|
const moo_bch_t* bcs,
|
|
|
|
moo_oow_t* bcslen,
|
|
|
|
moo_uch_t* ucs,
|
|
|
|
moo_oow_t* ucslen
|
|
|
|
);
|
|
|
|
|
|
|
|
MOO_EXPORT moo_oow_t moo_uc_to_utf16 (
|
|
|
|
moo_uch_t uc,
|
|
|
|
moo_bch_t* utf16,
|
|
|
|
moo_oow_t size
|
2017-07-05 14:23:13 +00:00
|
|
|
);
|
2017-01-09 09:54:49 +00:00
|
|
|
|
2018-11-14 04:25:27 +00:00
|
|
|
MOO_EXPORT moo_oow_t moo_utf16_to_uc (
|
|
|
|
const moo_bch_t* utf16,
|
|
|
|
moo_oow_t size,
|
|
|
|
moo_uch_t* uc
|
|
|
|
);
|
2018-01-10 14:39:31 +00:00
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
#if defined(MOO_HAVE_UINT16_T)
|
|
|
|
MOO_EXPORT moo_uint16_t moo_ntoh16 (
|
|
|
|
moo_uint16_t x
|
|
|
|
);
|
|
|
|
|
|
|
|
MOO_EXPORT moo_uint16_t moo_hton16 (
|
|
|
|
moo_uint16_t x
|
|
|
|
);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MOO_HAVE_UINT32_T)
|
|
|
|
MOO_EXPORT moo_uint32_t moo_ntoh32 (
|
|
|
|
moo_uint32_t x
|
|
|
|
);
|
|
|
|
|
|
|
|
MOO_EXPORT moo_uint32_t moo_hton32 (
|
|
|
|
moo_uint32_t x
|
|
|
|
);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MOO_HAVE_UINT64_T)
|
|
|
|
MOO_EXPORT moo_uint64_t moo_ntoh64 (
|
|
|
|
moo_uint64_t x
|
|
|
|
);
|
|
|
|
|
|
|
|
MOO_EXPORT moo_uint64_t moo_hton64 (
|
|
|
|
moo_uint64_t x
|
|
|
|
);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MOO_HAVE_UINT128_T)
|
|
|
|
MOO_EXPORT moo_uint128_t moo_ntoh128 (
|
|
|
|
moo_uint128_t x
|
|
|
|
);
|
|
|
|
|
|
|
|
MOO_EXPORT moo_uint128_t moo_hton128 (
|
|
|
|
moo_uint128_t x
|
|
|
|
);
|
|
|
|
#endif
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
#if defined(__cplusplus)
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|