2017-01-09 09:54:49 +00:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
2019-11-19 09:40:26 +00:00
|
|
|
Copyright (c) 2014-2019 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_
|
|
|
|
|
2019-11-15 09:39:12 +00:00
|
|
|
#include <moo-cmn.h>
|
2019-05-27 17:50:24 +00:00
|
|
|
#include <stdarg.h>
|
2017-01-09 09:54:49 +00:00
|
|
|
|
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
|
|
|
/* =========================================================================
|
2019-05-02 08:01:58 +00:00
|
|
|
* ENDIAN CHANGE OF A CONSTANT
|
2019-03-06 01:58:51 +00:00
|
|
|
* ========================================================================= */
|
2019-05-02 07:16:55 +00:00
|
|
|
#define MOO_CONST_BSWAP16(x) \
|
2019-05-02 08:01:58 +00:00
|
|
|
((moo_uint16_t)((((moo_uint16_t)(x) & ((moo_uint16_t)0xff << 0)) << 8) | \
|
|
|
|
(((moo_uint16_t)(x) & ((moo_uint16_t)0xff << 8)) >> 8)))
|
2018-01-10 14:39:31 +00:00
|
|
|
|
2019-05-02 07:16:55 +00:00
|
|
|
#define MOO_CONST_BSWAP32(x) \
|
2019-05-02 08:01:58 +00:00
|
|
|
((moo_uint32_t)((((moo_uint32_t)(x) & ((moo_uint32_t)0xff << 0)) << 24) | \
|
|
|
|
(((moo_uint32_t)(x) & ((moo_uint32_t)0xff << 8)) << 8) | \
|
|
|
|
(((moo_uint32_t)(x) & ((moo_uint32_t)0xff << 16)) >> 8) | \
|
|
|
|
(((moo_uint32_t)(x) & ((moo_uint32_t)0xff << 24)) >> 24)))
|
2019-05-02 07:16:55 +00:00
|
|
|
|
|
|
|
#if defined(MOO_HAVE_UINT64_T)
|
|
|
|
#define MOO_CONST_BSWAP64(x) \
|
2019-05-02 08:01:58 +00:00
|
|
|
((moo_uint64_t)((((moo_uint64_t)(x) & ((moo_uint64_t)0xff << 0)) << 56) | \
|
|
|
|
(((moo_uint64_t)(x) & ((moo_uint64_t)0xff << 8)) << 40) | \
|
|
|
|
(((moo_uint64_t)(x) & ((moo_uint64_t)0xff << 16)) << 24) | \
|
|
|
|
(((moo_uint64_t)(x) & ((moo_uint64_t)0xff << 24)) << 8) | \
|
|
|
|
(((moo_uint64_t)(x) & ((moo_uint64_t)0xff << 32)) >> 8) | \
|
|
|
|
(((moo_uint64_t)(x) & ((moo_uint64_t)0xff << 40)) >> 24) | \
|
|
|
|
(((moo_uint64_t)(x) & ((moo_uint64_t)0xff << 48)) >> 40) | \
|
|
|
|
(((moo_uint64_t)(x) & ((moo_uint64_t)0xff << 56)) >> 56)))
|
2019-05-02 07:16:55 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MOO_HAVE_UINT128_T)
|
|
|
|
#define MOO_CONST_BSWAP128(x) \
|
2019-05-02 08:01:58 +00:00
|
|
|
((moo_uint128_t)((((moo_uint128_t)(x) & ((moo_uint128_t)0xff << 0)) << 120) | \
|
|
|
|
(((moo_uint128_t)(x) & ((moo_uint128_t)0xff << 8)) << 104) | \
|
|
|
|
(((moo_uint128_t)(x) & ((moo_uint128_t)0xff << 16)) << 88) | \
|
|
|
|
(((moo_uint128_t)(x) & ((moo_uint128_t)0xff << 24)) << 72) | \
|
|
|
|
(((moo_uint128_t)(x) & ((moo_uint128_t)0xff << 32)) << 56) | \
|
|
|
|
(((moo_uint128_t)(x) & ((moo_uint128_t)0xff << 40)) << 40) | \
|
|
|
|
(((moo_uint128_t)(x) & ((moo_uint128_t)0xff << 48)) << 24) | \
|
|
|
|
(((moo_uint128_t)(x) & ((moo_uint128_t)0xff << 56)) << 8) | \
|
|
|
|
(((moo_uint128_t)(x) & ((moo_uint128_t)0xff << 64)) >> 8) | \
|
|
|
|
(((moo_uint128_t)(x) & ((moo_uint128_t)0xff << 72)) >> 24) | \
|
|
|
|
(((moo_uint128_t)(x) & ((moo_uint128_t)0xff << 80)) >> 40) | \
|
|
|
|
(((moo_uint128_t)(x) & ((moo_uint128_t)0xff << 88)) >> 56) | \
|
|
|
|
(((moo_uint128_t)(x) & ((moo_uint128_t)0xff << 96)) >> 72) | \
|
|
|
|
(((moo_uint128_t)(x) & ((moo_uint128_t)0xff << 104)) >> 88) | \
|
|
|
|
(((moo_uint128_t)(x) & ((moo_uint128_t)0xff << 112)) >> 104) | \
|
|
|
|
(((moo_uint128_t)(x) & ((moo_uint128_t)0xff << 120)) >> 120)))
|
2019-05-02 07:16:55 +00:00
|
|
|
#endif
|
2018-01-10 14:39:31 +00:00
|
|
|
|
|
|
|
#if defined(MOO_ENDIAN_LITTLE)
|
2019-05-02 07:16:55 +00:00
|
|
|
|
|
|
|
# if defined(MOO_HAVE_UINT16_T)
|
|
|
|
# define MOO_CONST_NTOH16(x) MOO_CONST_BSWAP16(x)
|
|
|
|
# define MOO_CONST_HTON16(x) MOO_CONST_BSWAP16(x)
|
|
|
|
# define MOO_CONST_HTOBE16(x) MOO_CONST_BSWAP16(x)
|
|
|
|
# define MOO_CONST_HTOLE16(x) (x)
|
|
|
|
# define MOO_CONST_BE16TOH(x) MOO_CONST_BSWAP16(x)
|
|
|
|
# define MOO_CONST_LE16TOH(x) (x)
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# if defined(MOO_HAVE_UINT32_T)
|
|
|
|
# define MOO_CONST_NTOH32(x) MOO_CONST_BSWAP32(x)
|
|
|
|
# define MOO_CONST_HTON32(x) MOO_CONST_BSWAP32(x)
|
|
|
|
# define MOO_CONST_HTOBE32(x) MOO_CONST_BSWAP32(x)
|
|
|
|
# define MOO_CONST_HTOLE32(x) (x)
|
|
|
|
# define MOO_CONST_BE32TOH(x) MOO_CONST_BSWAP32(x)
|
|
|
|
# define MOO_CONST_LE32TOH(x) (x)
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# if defined(MOO_HAVE_UINT64_T)
|
|
|
|
# define MOO_CONST_NTOH64(x) MOO_CONST_BSWAP64(x)
|
|
|
|
# define MOO_CONST_HTON64(x) MOO_CONST_BSWAP64(x)
|
|
|
|
# define MOO_CONST_HTOBE64(x) MOO_CONST_BSWAP64(x)
|
|
|
|
# define MOO_CONST_HTOLE64(x) (x)
|
|
|
|
# define MOO_CONST_BE64TOH(x) MOO_CONST_BSWAP64(x)
|
|
|
|
# define MOO_CONST_LE64TOH(x) (x)
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# if defined(MOO_HAVE_UINT128_T)
|
|
|
|
# define MOO_CONST_NTOH128(x) MOO_CONST_BSWAP128(x)
|
|
|
|
# define MOO_CONST_HTON128(x) MOO_CONST_BSWAP128(x)
|
|
|
|
# define MOO_CONST_HTOBE128(x) MOO_CONST_BSWAP128(x)
|
|
|
|
# define MOO_CONST_HTOLE128(x) (x)
|
|
|
|
# define MOO_CONST_BE128TOH(x) MOO_CONST_BSWAP128(x)
|
|
|
|
# define MOO_CONST_LE128TOH(x) (x)
|
|
|
|
#endif
|
|
|
|
|
2018-01-10 14:39:31 +00:00
|
|
|
#elif defined(MOO_ENDIAN_BIG)
|
2019-05-02 07:16:55 +00:00
|
|
|
|
|
|
|
# if defined(MOO_HAVE_UINT16_T)
|
|
|
|
# define MOO_CONST_NTOH16(x) (x)
|
|
|
|
# define MOO_CONST_HTON16(x) (x)
|
|
|
|
# define MOO_CONST_HTOBE16(x) (x)
|
|
|
|
# define MOO_CONST_HTOLE16(x) MOO_CONST_BSWAP16(x)
|
|
|
|
# define MOO_CONST_BE16TOH(x) (x)
|
|
|
|
# define MOO_CONST_LE16TOH(x) MOO_CONST_BSWAP16(x)
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# if defined(MOO_HAVE_UINT32_T)
|
|
|
|
# define MOO_CONST_NTOH32(x) (x)
|
|
|
|
# define MOO_CONST_HTON32(x) (x)
|
|
|
|
# define MOO_CONST_HTOBE32(x) (x)
|
|
|
|
# define MOO_CONST_HTOLE32(x) MOO_CONST_BSWAP32(x)
|
|
|
|
# define MOO_CONST_BE32TOH(x) (x)
|
|
|
|
# define MOO_CONST_LE32TOH(x) MOO_CONST_BSWAP32(x)
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# if defined(MOO_HAVE_UINT64_T)
|
|
|
|
# define MOO_CONST_NTOH64(x) (x)
|
|
|
|
# define MOO_CONST_HTON64(x) (x)
|
|
|
|
# define MOO_CONST_HTOBE64(x) (x)
|
|
|
|
# define MOO_CONST_HTOLE64(x) MOO_CONST_BSWAP64(x)
|
|
|
|
# define MOO_CONST_BE64TOH(x) (x)
|
|
|
|
# define MOO_CONST_LE64TOH(x) MOO_CONST_BSWAP64(x)
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# if defined(MOO_HAVE_UINT128_T)
|
|
|
|
# define MOO_CONST_NTOH128(x) (x)
|
|
|
|
# define MOO_CONST_HTON128(x) (x)
|
|
|
|
# define MOO_CONST_HTOBE128(x) (x)
|
|
|
|
# define MOO_CONST_HTOLE128(x) MOO_CONST_BSWAP128(x)
|
|
|
|
# define MOO_CONST_BE128TOH(x) (x)
|
|
|
|
# define MOO_CONST_LE128TOH(x) MOO_CONST_BSWAP128(x)
|
|
|
|
# endif
|
|
|
|
|
2018-01-10 14:39:31 +00:00
|
|
|
#else
|
2019-05-02 07:16:55 +00:00
|
|
|
# error UNKNOWN ENDIAN
|
2018-01-10 14:39:31 +00:00
|
|
|
#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)
|
|
|
|
|
2019-05-14 10:22:37 +00:00
|
|
|
/* =========================================================================
|
|
|
|
* CMGR
|
|
|
|
* ========================================================================= */
|
|
|
|
enum moo_cmgr_id_t
|
|
|
|
{
|
|
|
|
MOO_CMGR_UTF8,
|
|
|
|
MOO_CMGR_UTF16,
|
2019-09-28 09:14:21 +00:00
|
|
|
MOO_CMGR_MB8
|
2019-05-14 10:22:37 +00:00
|
|
|
};
|
|
|
|
typedef enum moo_cmgr_id_t moo_cmgr_id_t;
|
2019-03-06 01:58:51 +00:00
|
|
|
|
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)
|
2019-05-29 01:35:13 +00:00
|
|
|
# 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
|
|
|
|
);
|
2019-11-29 09:28:29 +00:00
|
|
|
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
|
|
|
|
);
|
2017-01-09 09:54:49 +00:00
|
|
|
|
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-11-29 09:28:29 +00:00
|
|
|
MOO_EXPORT moo_oow_t moo_copy_uchars_to_ucstr (
|
|
|
|
moo_uch_t* dst,
|
2020-05-20 14:25:41 +00:00
|
|
|
moo_oow_t dlen,
|
2019-11-29 09:28:29 +00:00
|
|
|
const moo_uch_t* src,
|
|
|
|
moo_oow_t slen
|
|
|
|
);
|
|
|
|
|
|
|
|
MOO_EXPORT moo_oow_t moo_copy_bchars_to_bcstr (
|
|
|
|
moo_bch_t* dst,
|
2020-05-20 14:25:41 +00:00
|
|
|
moo_oow_t dlen,
|
2019-11-29 09:28:29 +00:00
|
|
|
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
|
|
|
|
);
|
|
|
|
|
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
|
|
|
|
2019-10-01 17:15:55 +00:00
|
|
|
MOO_EXPORT moo_oow_t moo_rotate_uchars (
|
|
|
|
moo_uch_t* str,
|
|
|
|
moo_oow_t len,
|
|
|
|
int dir,
|
|
|
|
moo_oow_t n
|
|
|
|
);
|
|
|
|
|
|
|
|
MOO_EXPORT moo_oow_t moo_rotate_bchars (
|
|
|
|
moo_bch_t* str,
|
|
|
|
moo_oow_t len,
|
|
|
|
int dir,
|
|
|
|
moo_oow_t n
|
|
|
|
);
|
|
|
|
|
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
|
|
|
|
);
|
|
|
|
|
2019-11-25 15:03:05 +00:00
|
|
|
MOO_EXPORT moo_oow_t moo_count_ucstr_limited (
|
2019-11-03 09:15:24 +00:00
|
|
|
const moo_uch_t* str,
|
|
|
|
moo_oow_t maxlen
|
|
|
|
);
|
|
|
|
|
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
|
|
|
|
);
|
|
|
|
|
2019-11-25 15:03:05 +00:00
|
|
|
MOO_EXPORT moo_oow_t moo_count_bcstr_limited (
|
2019-11-03 09:15:24 +00:00
|
|
|
const moo_bch_t* str,
|
|
|
|
moo_oow_t maxlen
|
|
|
|
);
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
#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)
|
2019-11-29 09:28:29 +00:00
|
|
|
|
2018-04-07 15:54:09 +00:00
|
|
|
# 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)
|
2019-11-29 09:28:29 +00:00
|
|
|
# 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)
|
2018-04-07 15:54:09 +00:00
|
|
|
# define moo_copy_oocstr(dst,len,src) moo_copy_ucstr(dst,len,src)
|
2019-11-29 09:28:29 +00:00
|
|
|
# define moo_copy_oocstr_unlimited(dst,src) moo_copy_ucstr_unlimited(dst,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)
|
2019-10-01 17:15:55 +00:00
|
|
|
# define moo_rotate_oochars(str,len,dir,n) moo_rotate_uchars(str,len,dir,n)
|
2018-04-07 15:54:09 +00:00
|
|
|
# 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)
|
2019-11-29 09:28:29 +00:00
|
|
|
|
2018-04-07 15:54:09 +00:00
|
|
|
# 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)
|
2019-11-29 09:28:29 +00:00
|
|
|
# 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)
|
2018-04-07 15:54:09 +00:00
|
|
|
# define moo_copy_oocstr(dst,len,src) moo_copy_bcstr(dst,len,src)
|
2019-11-29 09:28:29 +00:00
|
|
|
# define moo_copy_oocstr_unlimited(dst,src) moo_copy_bcstr_unlimited(dst,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)
|
2019-10-01 17:15:55 +00:00
|
|
|
# define moo_rotate_oochars(str,len,dir,n) moo_rotate_bchars(str,len,dir,n)
|
2018-04-07 15:54:09 +00:00
|
|
|
# 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
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
|
2019-11-20 09:32:44 +00:00
|
|
|
#define MOO_BYTE_TO_OOCSTR_RADIXMASK (0xFF)
|
|
|
|
#define MOO_BYTE_TO_OOCSTR_LOWERCASE (1 << 8)
|
|
|
|
|
|
|
|
#define MOO_BYTE_TO_UCSTR_RADIXMASK MOO_BYTE_TO_OOCSTR_RADIXMASK
|
|
|
|
#define MOO_BYTE_TO_UCSTR_LOWERCASE MOO_BYTE_TO_OOCSTR_LOWERCASE
|
|
|
|
|
|
|
|
#define MOO_BYTE_TO_BCSTR_RADIXMASK MOO_BYTE_TO_OOCSTR_RADIXMASK
|
|
|
|
#define MOO_BYTE_TO_BCSTR_LOWERCASE MOO_BYTE_TO_OOCSTR_LOWERCASE
|
|
|
|
|
|
|
|
moo_oow_t moo_byte_to_ucstr (
|
|
|
|
moo_oob_t byte,
|
|
|
|
moo_uch_t* buf,
|
|
|
|
moo_oow_t size,
|
|
|
|
int flagged_radix,
|
|
|
|
moo_uch_t fill
|
|
|
|
);
|
2019-03-17 18:59:01 +00:00
|
|
|
|
|
|
|
moo_oow_t moo_byte_to_bcstr (
|
2019-11-20 09:32:44 +00:00
|
|
|
moo_oob_t byte,
|
2019-03-17 18:59:01 +00:00
|
|
|
moo_bch_t* buf,
|
|
|
|
moo_oow_t size,
|
|
|
|
int flagged_radix,
|
|
|
|
moo_bch_t fill
|
|
|
|
);
|
|
|
|
|
2019-11-20 09:32:44 +00:00
|
|
|
#if defined(MOO_OOCH_IS_UCH)
|
|
|
|
# define moo_byte_to_oocstr moo_byte_to_ucstr
|
|
|
|
#else
|
|
|
|
# define moo_byte_to_oocstr moo_byte_to_bcstr
|
|
|
|
#endif
|
|
|
|
|
2019-03-17 18:59:01 +00:00
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
|
2019-05-28 08:55:13 +00:00
|
|
|
MOO_EXPORT int moo_get_ucwidth (
|
2018-11-14 04:25:27 +00:00
|
|
|
moo_uch_t uc
|
|
|
|
);
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
|
2018-04-06 15:15:53 +00:00
|
|
|
#if defined(MOO_OOCH_IS_UCH)
|
2019-03-19 13:29:49 +00:00
|
|
|
# define moo_conv_oocstr_to_bcstr_with_cmgr(oocs,oocslen,bcs,bcslen,cmgr) moo_conv_ucstr_to_bcstr_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
|
2019-03-19 13:29:49 +00:00
|
|
|
# define moo_conv_oocstr_to_ucstr_with_cmgr(oocs,oocslen,ucs,ucslen,cmgr) moo_conv_bcstr_to_ucstr_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
|
|
|
|
|
|
|
|
|
2019-03-19 13:29:49 +00:00
|
|
|
MOO_EXPORT int moo_conv_bcstr_to_ucstr_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
|
|
|
|
);
|
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
|
|
|
|
);
|
|
|
|
|
2019-03-19 13:29:49 +00:00
|
|
|
MOO_EXPORT int moo_conv_ucstr_to_bcstr_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-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
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
|
2019-05-14 10:22:37 +00:00
|
|
|
MOO_EXPORT moo_cmgr_t* moo_get_cmgr_by_id (
|
|
|
|
moo_cmgr_id_t id
|
2017-01-09 09:54:49 +00:00
|
|
|
);
|
|
|
|
|
2019-05-14 10:22:37 +00:00
|
|
|
MOO_EXPORT moo_cmgr_t* moo_get_cmgr_by_bcstr (
|
|
|
|
const moo_bch_t* name
|
|
|
|
);
|
|
|
|
|
|
|
|
MOO_EXPORT moo_cmgr_t* moo_get_cmgr_by_ucstr (
|
|
|
|
const moo_uch_t* name
|
|
|
|
);
|
|
|
|
|
|
|
|
#if defined(MOO_OOCH_IS_UCH)
|
|
|
|
# define moo_get_cmgr_by_name(name) moo_get_cmgr_by_ucstr(name)
|
|
|
|
#else
|
|
|
|
# define moo_get_cmgr_by_name(name) moo_get_cmgr_by_bcstr(name)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define moo_get_utf8_cmgr() moo_get_cmgr_by_id(MOO_CMGR_UTF8)
|
|
|
|
#define moo_get_utf16_cmgr() moo_get_cmgr_by_id(MOO_CMGR_UTF16)
|
|
|
|
#define moo_get_mb8_cmgr() moo_get_cmgr_by_id(MOO_CMGR_MB8)
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
/**
|
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-11-14 04:25:27 +00:00
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
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
|
|
|
|
);
|
|
|
|
|
2019-05-07 17:29:48 +00:00
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
MOO_EXPORT int moo_conv_uchars_to_mb8 (
|
|
|
|
const moo_uch_t* ucs,
|
|
|
|
moo_oow_t* ucslen,
|
|
|
|
moo_bch_t* bcs,
|
|
|
|
moo_oow_t* bcslen
|
|
|
|
);
|
|
|
|
|
|
|
|
MOO_EXPORT int moo_conv_mb8_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_mb8 (
|
|
|
|
const moo_uch_t* ucs,
|
|
|
|
moo_oow_t* ucslen,
|
|
|
|
moo_bch_t* bcs,
|
|
|
|
moo_oow_t* bcslen
|
|
|
|
);
|
|
|
|
|
|
|
|
MOO_EXPORT int moo_conv_mb8_to_ucstr (
|
|
|
|
const moo_bch_t* bcs,
|
|
|
|
moo_oow_t* bcslen,
|
|
|
|
moo_uch_t* ucs,
|
|
|
|
moo_oow_t* ucslen
|
|
|
|
);
|
|
|
|
|
2019-05-04 04:27:27 +00:00
|
|
|
/* =========================================================================
|
|
|
|
* BIT SWAP
|
|
|
|
* ========================================================================= */
|
2019-05-02 07:16:55 +00:00
|
|
|
#if defined(MOO_HAVE_INLINE)
|
|
|
|
|
|
|
|
#if defined(MOO_HAVE_UINT16_T)
|
|
|
|
static MOO_INLINE moo_uint16_t moo_bswap16 (moo_uint16_t x)
|
|
|
|
{
|
2019-05-03 04:53:44 +00:00
|
|
|
#if defined(MOO_HAVE_BUILTIN_BSWAP16)
|
|
|
|
return __builtin_bswap16(x);
|
|
|
|
#elif defined(__GNUC__) && (defined(__x86_64) || defined(__amd64) || defined(__i386) || defined(i386))
|
2019-05-04 02:16:25 +00:00
|
|
|
__asm__ /*volatile*/ ("xchgb %b0, %h0" : "=Q"(x): "0"(x));
|
2019-05-03 04:53:44 +00:00
|
|
|
return x;
|
2019-05-04 04:27:27 +00:00
|
|
|
#elif defined(__GNUC__) && defined(__arm__) && (defined(__ARM_ARCH) && (__ARM_ARCH >= 6))
|
2019-05-04 02:16:25 +00:00
|
|
|
__asm__ /*volatile*/ ("rev16 %0, %0" : "+r"(x));
|
2019-05-04 04:27:27 +00:00
|
|
|
return x;
|
2019-05-03 04:53:44 +00:00
|
|
|
#else
|
2019-05-02 07:16:55 +00:00
|
|
|
return (x << 8) | (x >> 8);
|
2019-05-03 04:53:44 +00:00
|
|
|
#endif
|
2019-05-02 07:16:55 +00:00
|
|
|
}
|
2018-01-10 14:39:31 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MOO_HAVE_UINT32_T)
|
2019-05-02 07:16:55 +00:00
|
|
|
static MOO_INLINE moo_uint32_t moo_bswap32 (moo_uint32_t x)
|
|
|
|
{
|
2019-05-03 04:53:44 +00:00
|
|
|
#if defined(MOO_HAVE_BUILTIN_BSWAP32)
|
|
|
|
return __builtin_bswap32(x);
|
|
|
|
#elif defined(__GNUC__) && (defined(__x86_64) || defined(__amd64) || defined(__i386) || defined(i386))
|
2019-05-04 02:16:25 +00:00
|
|
|
__asm__ /*volatile*/ ("bswapl %0" : "=r"(x) : "0"(x));
|
|
|
|
return x;
|
2019-05-04 04:27:27 +00:00
|
|
|
#elif defined(__GNUC__) && defined(__aarch64__)
|
2019-05-04 02:16:25 +00:00
|
|
|
__asm__ /*volatile*/ ("rev32 %0, %0" : "+r"(x));
|
2019-05-04 04:27:27 +00:00
|
|
|
return x;
|
|
|
|
#elif defined(__GNUC__) && defined(__arm__) && (defined(__ARM_ARCH) && (__ARM_ARCH >= 6))
|
|
|
|
__asm__ /*volatile*/ ("rev %0, %0" : "+r"(x));
|
|
|
|
return x;
|
2019-05-04 02:16:25 +00:00
|
|
|
#elif defined(__GNUC__) && defined(__ARM_ARCH)
|
|
|
|
moo_uint32_t tmp;
|
|
|
|
__asm__ /*volatile*/ (
|
|
|
|
"eor %1, %0, %0, ror #16\n\t"
|
|
|
|
"bic %1, %1, #0x00ff0000\n\t"
|
|
|
|
"mov %0, %0, ror #8\n\t"
|
|
|
|
"eor %0, %0, %1, lsr #8\n\t"
|
|
|
|
:"+r"(x), "=&r"(tmp)
|
|
|
|
);
|
2019-05-03 04:53:44 +00:00
|
|
|
return x;
|
|
|
|
#else
|
2019-05-02 07:16:55 +00:00
|
|
|
return ((x >> 24)) |
|
|
|
|
((x >> 8) & ((moo_uint32_t)0xff << 8)) |
|
|
|
|
((x << 8) & ((moo_uint32_t)0xff << 16)) |
|
|
|
|
((x << 24));
|
2019-05-03 04:53:44 +00:00
|
|
|
#endif
|
2019-05-02 07:16:55 +00:00
|
|
|
}
|
2018-01-10 14:39:31 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MOO_HAVE_UINT64_T)
|
2019-05-02 07:16:55 +00:00
|
|
|
static MOO_INLINE moo_uint64_t moo_bswap64 (moo_uint64_t x)
|
|
|
|
{
|
2019-05-03 04:53:44 +00:00
|
|
|
#if defined(MOO_HAVE_BUILTIN_BSWAP64)
|
|
|
|
return __builtin_bswap64(x);
|
|
|
|
#elif defined(__GNUC__) && (defined(__x86_64) || defined(__amd64))
|
2019-05-04 02:16:25 +00:00
|
|
|
__asm__ /*volatile*/ ("bswapq %0" : "=r"(x) : "0"(x));
|
2019-05-03 04:53:44 +00:00
|
|
|
return x;
|
2019-05-04 02:16:25 +00:00
|
|
|
#elif defined(__GNUC__) && defined(__aarch64__)
|
2019-05-04 04:27:27 +00:00
|
|
|
__asm__ /*volatile*/ ("rev %0, %0" : "+r"(x));
|
|
|
|
return x;
|
2019-05-03 04:53:44 +00:00
|
|
|
#else
|
2019-05-02 07:16:55 +00:00
|
|
|
return ((x >> 56)) |
|
|
|
|
((x >> 40) & ((moo_uint64_t)0xff << 8)) |
|
|
|
|
((x >> 24) & ((moo_uint64_t)0xff << 16)) |
|
|
|
|
((x >> 8) & ((moo_uint64_t)0xff << 24)) |
|
|
|
|
((x << 8) & ((moo_uint64_t)0xff << 32)) |
|
|
|
|
((x << 24) & ((moo_uint64_t)0xff << 40)) |
|
|
|
|
((x << 40) & ((moo_uint64_t)0xff << 48)) |
|
|
|
|
((x << 56));
|
2019-05-03 04:53:44 +00:00
|
|
|
#endif
|
2019-05-02 07:16:55 +00:00
|
|
|
}
|
|
|
|
#endif
|
2018-01-10 14:39:31 +00:00
|
|
|
|
2019-05-02 07:16:55 +00:00
|
|
|
#if defined(MOO_HAVE_UINT128_T)
|
|
|
|
static MOO_INLINE moo_uint128_t moo_bswap128 (moo_uint128_t x)
|
|
|
|
{
|
2019-05-03 04:53:44 +00:00
|
|
|
#if defined(MOO_HAVE_BUILTIN_BSWAP128)
|
|
|
|
return __builtin_bswap128(x);
|
|
|
|
#else
|
2019-05-02 07:16:55 +00:00
|
|
|
return ((x >> 120)) |
|
|
|
|
((x >> 104) & ((moo_uint128_t)0xff << 8)) |
|
|
|
|
((x >> 88) & ((moo_uint128_t)0xff << 16)) |
|
|
|
|
((x >> 72) & ((moo_uint128_t)0xff << 24)) |
|
|
|
|
((x >> 56) & ((moo_uint128_t)0xff << 32)) |
|
|
|
|
((x >> 40) & ((moo_uint128_t)0xff << 40)) |
|
|
|
|
((x >> 24) & ((moo_uint128_t)0xff << 48)) |
|
|
|
|
((x >> 8) & ((moo_uint128_t)0xff << 56)) |
|
|
|
|
((x << 8) & ((moo_uint128_t)0xff << 64)) |
|
|
|
|
((x << 24) & ((moo_uint128_t)0xff << 72)) |
|
|
|
|
((x << 40) & ((moo_uint128_t)0xff << 80)) |
|
|
|
|
((x << 56) & ((moo_uint128_t)0xff << 88)) |
|
|
|
|
((x << 72) & ((moo_uint128_t)0xff << 96)) |
|
|
|
|
((x << 88) & ((moo_uint128_t)0xff << 104)) |
|
|
|
|
((x << 104) & ((moo_uint128_t)0xff << 112)) |
|
|
|
|
((x << 120));
|
2019-05-03 04:53:44 +00:00
|
|
|
#endif
|
2019-05-02 07:16:55 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#if defined(MOO_HAVE_UINT16_T)
|
2019-05-04 04:27:27 +00:00
|
|
|
# if defined(MOO_HAVE_BUILTIN_BSWAP16)
|
|
|
|
# define moo_bswap16(x) ((moo_uint16_t)__builtin_bswap16((moo_uint16_t)(x)))
|
|
|
|
# else
|
2019-05-02 07:16:55 +00:00
|
|
|
# define moo_bswap16(x) ((moo_uint16_t)(((moo_uint16_t)(x)) << 8) | (((moo_uint16_t)(x)) >> 8))
|
2019-05-04 04:27:27 +00:00
|
|
|
# endif
|
2019-05-02 07:16:55 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MOO_HAVE_UINT32_T)
|
2019-05-04 04:27:27 +00:00
|
|
|
# if defined(MOO_HAVE_BUILTIN_BSWAP32)
|
|
|
|
# define moo_bswap32(x) ((moo_uint32_t)__builtin_bswap32((moo_uint32_t)(x)))
|
|
|
|
# else
|
2019-05-02 07:16:55 +00:00
|
|
|
# define moo_bswap32(x) ((moo_uint32_t)(((((moo_uint32_t)(x)) >> 24)) | \
|
|
|
|
((((moo_uint32_t)(x)) >> 8) & ((moo_uint32_t)0xff << 8)) | \
|
|
|
|
((((moo_uint32_t)(x)) << 8) & ((moo_uint32_t)0xff << 16)) | \
|
|
|
|
((((moo_uint32_t)(x)) << 24))))
|
2019-05-04 04:27:27 +00:00
|
|
|
# endif
|
2019-05-02 07:16:55 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MOO_HAVE_UINT64_T)
|
2019-05-04 04:27:27 +00:00
|
|
|
# if defined(MOO_HAVE_BUILTIN_BSWAP64)
|
|
|
|
# define moo_bswap64(x) ((moo_uint64_t)__builtin_bswap64((moo_uint64_t)(x)))
|
|
|
|
# else
|
2019-05-02 07:16:55 +00:00
|
|
|
# define moo_bswap64(x) ((moo_uint64_t)(((((moo_uint64_t)(x)) >> 56)) | \
|
|
|
|
((((moo_uint64_t)(x)) >> 40) & ((moo_uint64_t)0xff << 8)) | \
|
|
|
|
((((moo_uint64_t)(x)) >> 24) & ((moo_uint64_t)0xff << 16)) | \
|
|
|
|
((((moo_uint64_t)(x)) >> 8) & ((moo_uint64_t)0xff << 24)) | \
|
|
|
|
((((moo_uint64_t)(x)) << 8) & ((moo_uint64_t)0xff << 32)) | \
|
|
|
|
((((moo_uint64_t)(x)) << 24) & ((moo_uint64_t)0xff << 40)) | \
|
|
|
|
((((moo_uint64_t)(x)) << 40) & ((moo_uint64_t)0xff << 48)) | \
|
|
|
|
((((moo_uint64_t)(x)) << 56))))
|
2019-05-04 04:27:27 +00:00
|
|
|
# endif
|
2018-01-10 14:39:31 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MOO_HAVE_UINT128_T)
|
2019-05-04 04:27:27 +00:00
|
|
|
# if defined(MOO_HAVE_BUILTIN_BSWAP128)
|
|
|
|
# define moo_bswap128(x) ((moo_uint128_t)__builtin_bswap128((moo_uint128_t)(x)))
|
|
|
|
# else
|
2019-05-02 07:16:55 +00:00
|
|
|
# define moo_bswap128(x) ((moo_uint128_t)(((((moo_uint128_t)(x)) >> 120)) | \
|
|
|
|
((((moo_uint128_t)(x)) >> 104) & ((moo_uint128_t)0xff << 8)) | \
|
|
|
|
((((moo_uint128_t)(x)) >> 88) & ((moo_uint128_t)0xff << 16)) | \
|
|
|
|
((((moo_uint128_t)(x)) >> 72) & ((moo_uint128_t)0xff << 24)) | \
|
|
|
|
((((moo_uint128_t)(x)) >> 56) & ((moo_uint128_t)0xff << 32)) | \
|
|
|
|
((((moo_uint128_t)(x)) >> 40) & ((moo_uint128_t)0xff << 40)) | \
|
|
|
|
((((moo_uint128_t)(x)) >> 24) & ((moo_uint128_t)0xff << 48)) | \
|
|
|
|
((((moo_uint128_t)(x)) >> 8) & ((moo_uint128_t)0xff << 56)) | \
|
|
|
|
((((moo_uint128_t)(x)) << 8) & ((moo_uint128_t)0xff << 64)) | \
|
|
|
|
((((moo_uint128_t)(x)) << 24) & ((moo_uint128_t)0xff << 72)) | \
|
|
|
|
((((moo_uint128_t)(x)) << 40) & ((moo_uint128_t)0xff << 80)) | \
|
|
|
|
((((moo_uint128_t)(x)) << 56) & ((moo_uint128_t)0xff << 88)) | \
|
|
|
|
((((moo_uint128_t)(x)) << 72) & ((moo_uint128_t)0xff << 96)) | \
|
|
|
|
((((moo_uint128_t)(x)) << 88) & ((moo_uint128_t)0xff << 104)) | \
|
|
|
|
((((moo_uint128_t)(x)) << 104) & ((moo_uint128_t)0xff << 112)) | \
|
|
|
|
((((moo_uint128_t)(x)) << 120))))
|
2019-05-04 04:27:27 +00:00
|
|
|
# endif
|
2019-05-02 07:16:55 +00:00
|
|
|
#endif
|
2018-01-10 14:39:31 +00:00
|
|
|
|
2019-05-02 07:16:55 +00:00
|
|
|
#endif /* MOO_HAVE_INLINE */
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(MOO_ENDIAN_LITTLE)
|
|
|
|
|
|
|
|
# if defined(MOO_HAVE_UINT16_T)
|
|
|
|
# define moo_hton16(x) moo_bswap16(x)
|
2019-05-03 03:20:26 +00:00
|
|
|
# define moo_ntoh16(x) moo_bswap16(x)
|
2019-05-02 07:16:55 +00:00
|
|
|
# define moo_htobe16(x) moo_bswap16(x)
|
|
|
|
# define moo_be16toh(x) moo_bswap16(x)
|
2019-05-03 03:20:26 +00:00
|
|
|
# define moo_htole16(x) ((moo_uint16_t)(x))
|
|
|
|
# define moo_le16toh(x) ((moo_uint16_t)(x))
|
2019-05-02 07:16:55 +00:00
|
|
|
# endif
|
|
|
|
|
|
|
|
# if defined(MOO_HAVE_UINT32_T)
|
|
|
|
# define moo_hton32(x) moo_bswap32(x)
|
2019-05-03 03:20:26 +00:00
|
|
|
# define moo_ntoh32(x) moo_bswap32(x)
|
2019-05-02 07:16:55 +00:00
|
|
|
# define moo_htobe32(x) moo_bswap32(x)
|
|
|
|
# define moo_be32toh(x) moo_bswap32(x)
|
2019-05-03 03:20:26 +00:00
|
|
|
# define moo_htole32(x) ((moo_uint32_t)(x))
|
|
|
|
# define moo_le32toh(x) ((moo_uint32_t)(x))
|
2019-05-02 07:16:55 +00:00
|
|
|
# endif
|
|
|
|
|
|
|
|
# if defined(MOO_HAVE_UINT64_T)
|
|
|
|
# define moo_hton64(x) moo_bswap64(x)
|
2019-05-03 03:20:26 +00:00
|
|
|
# define moo_ntoh64(x) moo_bswap64(x)
|
2019-05-02 07:16:55 +00:00
|
|
|
# define moo_htobe64(x) moo_bswap64(x)
|
|
|
|
# define moo_be64toh(x) moo_bswap64(x)
|
2019-05-03 03:20:26 +00:00
|
|
|
# define moo_htole64(x) ((moo_uint64_t)(x))
|
|
|
|
# define moo_le64toh(x) ((moo_uint64_t)(x))
|
2019-05-02 07:16:55 +00:00
|
|
|
# endif
|
|
|
|
|
|
|
|
# if defined(MOO_HAVE_UINT128_T)
|
2019-05-03 03:20:26 +00:00
|
|
|
|
2019-05-02 07:16:55 +00:00
|
|
|
# define moo_hton128(x) moo_bswap128(x)
|
2019-05-03 03:20:26 +00:00
|
|
|
# define moo_ntoh128(x) moo_bswap128(x)
|
2019-05-02 07:16:55 +00:00
|
|
|
# define moo_htobe128(x) moo_bswap128(x)
|
|
|
|
# define moo_be128toh(x) moo_bswap128(x)
|
2019-05-03 03:20:26 +00:00
|
|
|
# define moo_htole128(x) ((moo_uint128_t)(x))
|
|
|
|
# define moo_le128toh(x) ((moo_uint128_t)(x))
|
2019-05-02 07:16:55 +00:00
|
|
|
# endif
|
|
|
|
|
|
|
|
#elif defined(MOO_ENDIAN_BIG)
|
|
|
|
|
|
|
|
# if defined(MOO_HAVE_UINT16_T)
|
2019-05-03 03:20:26 +00:00
|
|
|
# define moo_hton16(x) ((moo_uint16_t)(x))
|
|
|
|
# define moo_ntoh16(x) ((moo_uint16_t)(x))
|
|
|
|
# define moo_htobe16(x) ((moo_uint16_t)(x))
|
|
|
|
# define moo_be16toh(x) ((moo_uint16_t)(x))
|
2019-05-02 07:16:55 +00:00
|
|
|
# define moo_htole16(x) moo_bswap16(x)
|
|
|
|
# define moo_le16toh(x) moo_bswap16(x)
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# if defined(MOO_HAVE_UINT32_T)
|
2019-05-03 03:20:26 +00:00
|
|
|
# define moo_hton32(x) ((moo_uint32_t)(x))
|
|
|
|
# define moo_ntoh32(x) ((moo_uint32_t)(x))
|
|
|
|
# define moo_htobe32(x) ((moo_uint32_t)(x))
|
|
|
|
# define moo_be32toh(x) ((moo_uint32_t)(x))
|
2019-05-02 07:16:55 +00:00
|
|
|
# define moo_htole32(x) moo_bswap32(x)
|
|
|
|
# define moo_le32toh(x) moo_bswap32(x)
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# if defined(MOO_HAVE_UINT64_T)
|
2019-05-03 03:20:26 +00:00
|
|
|
# define moo_hton64(x) ((moo_uint64_t)(x))
|
|
|
|
# define moo_ntoh64(x) ((moo_uint64_t)(x))
|
|
|
|
# define moo_htobe64(x) ((moo_uint64_t)(x))
|
|
|
|
# define moo_be64toh(x) ((moo_uint64_t)(x))
|
2019-05-02 07:16:55 +00:00
|
|
|
# define moo_htole64(x) moo_bswap64(x)
|
|
|
|
# define moo_le64toh(x) moo_bswap64(x)
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# if defined(MOO_HAVE_UINT128_T)
|
2019-05-03 03:20:26 +00:00
|
|
|
# define moo_hton128(x) ((moo_uint128_t)(x))
|
|
|
|
# define moo_ntoh128(x) ((moo_uint128_t)(x))
|
|
|
|
# define moo_htobe128(x) ((moo_uint128_t)(x))
|
|
|
|
# define moo_be128toh(x) ((moo_uint128_t)(x))
|
2019-05-02 07:16:55 +00:00
|
|
|
# define moo_htole128(x) moo_bswap128(x)
|
|
|
|
# define moo_le128toh(x) moo_bswap128(x)
|
|
|
|
# endif
|
|
|
|
|
|
|
|
#else
|
2019-05-02 08:01:58 +00:00
|
|
|
# error UNKNOWN ENDIAN
|
2018-01-10 14:39:31 +00:00
|
|
|
#endif
|
|
|
|
|
2019-05-04 04:27:27 +00:00
|
|
|
/* =========================================================================
|
|
|
|
* BIT POSITION
|
|
|
|
* ========================================================================= */
|
|
|
|
static MOO_INLINE int moo_get_pos_of_msb_set_pow2 (moo_oow_t x)
|
|
|
|
{
|
|
|
|
/* the caller must ensure that x is power of 2. if x happens to be zero,
|
|
|
|
* the return value is undefined as each method used may give different result. */
|
|
|
|
#if defined(MOO_HAVE_BUILTIN_CTZLL) && (MOO_SIZEOF_OOW_T == MOO_SIZEOF_LONG_LONG)
|
|
|
|
return __builtin_ctzll(x); /* count the number of trailing zeros */
|
|
|
|
#elif defined(MOO_HAVE_BUILTIN_CTZL) && (MOO_SIZEOF_OOW_T == MOO_SIZEOF_LONG)
|
|
|
|
return __builtin_ctzl(x); /* count the number of trailing zeros */
|
|
|
|
#elif defined(MOO_HAVE_BUILTIN_CTZ) && (MOO_SIZEOF_OOW_T == MOO_SIZEOF_INT)
|
|
|
|
return __builtin_ctz(x); /* count the number of trailing zeros */
|
|
|
|
#elif defined(__GNUC__) && (defined(__x86_64) || defined(__amd64) || defined(__i386) || defined(i386))
|
|
|
|
moo_oow_t pos;
|
|
|
|
/* use the Bit Scan Forward instruction */
|
|
|
|
#if 1
|
|
|
|
__asm__ volatile (
|
|
|
|
"bsf %1,%0\n\t"
|
|
|
|
: "=r"(pos) /* output */
|
|
|
|
: "r"(x) /* input */
|
|
|
|
);
|
|
|
|
#else
|
|
|
|
__asm__ volatile (
|
|
|
|
"bsf %[X],%[EXP]\n\t"
|
|
|
|
: [EXP]"=r"(pos) /* output */
|
|
|
|
: [X]"r"(x) /* input */
|
|
|
|
);
|
|
|
|
#endif
|
|
|
|
return (int)pos;
|
|
|
|
#elif defined(__GNUC__) && defined(__aarch64__) || (defined(__arm__) && (defined(__ARM_ARCH) && (__ARM_ARCH >= 5)))
|
|
|
|
moo_oow_t n;
|
|
|
|
/* CLZ is available in ARMv5T and above. there is no instruction to
|
|
|
|
* count trailing zeros or something similar. using RBIT with CLZ
|
|
|
|
* would be good in ARMv6T2 and above to avoid further calculation
|
|
|
|
* afte CLZ */
|
|
|
|
__asm__ volatile (
|
|
|
|
"clz %0,%1\n\t"
|
|
|
|
: "=r"(n) /* output */
|
|
|
|
: "r"(x) /* input */
|
|
|
|
);
|
|
|
|
return (int)(MOO_OOW_BITS - n - 1);
|
|
|
|
/* TODO: PPC - use cntlz, cntlzw, cntlzd, SPARC - use lzcnt, MIPS clz */
|
|
|
|
#else
|
|
|
|
int pos = 0;
|
|
|
|
while (x >>= 1) pos++;
|
|
|
|
return pos;
|
|
|
|
#endif
|
2017-01-09 09:54:49 +00:00
|
|
|
}
|
2019-05-04 04:27:27 +00:00
|
|
|
|
|
|
|
static MOO_INLINE int moo_get_pos_of_msb_set (moo_oow_t x)
|
|
|
|
{
|
|
|
|
/* x doesn't have to be power of 2. if x is zero, the result is undefined */
|
|
|
|
#if defined(MOO_HAVE_BUILTIN_CLZLL) && (MOO_SIZEOF_OOW_T == MOO_SIZEOF_LONG_LONG)
|
|
|
|
return MOO_OOW_BITS - __builtin_clzll(x) - 1; /* count the number of leading zeros */
|
|
|
|
#elif defined(MOO_HAVE_BUILTIN_CLZL) && (MOO_SIZEOF_OOW_T == MOO_SIZEOF_LONG)
|
|
|
|
return MOO_OOW_BITS - __builtin_clzl(x) - 1; /* count the number of leading zeros */
|
|
|
|
#elif defined(MOO_HAVE_BUILTIN_CLZ) && (MOO_SIZEOF_OOW_T == MOO_SIZEOF_INT)
|
|
|
|
return MOO_OOW_BITS - __builtin_clz(x) - 1; /* count the number of leading zeros */
|
|
|
|
#elif defined(__GNUC__) && (defined(__x86_64) || defined(__amd64) || defined(__i386) || defined(i386))
|
|
|
|
/* bit scan reverse. not all x86 CPUs have LZCNT. */
|
|
|
|
moo_oow_t pos;
|
|
|
|
__asm__ volatile (
|
|
|
|
"bsr %1,%0\n\t"
|
|
|
|
: "=r"(pos) /* output */
|
|
|
|
: "r"(x) /* input */
|
|
|
|
);
|
|
|
|
return (int)pos;
|
|
|
|
#elif defined(__GNUC__) && defined(__aarch64__) || (defined(__arm__) && (defined(__ARM_ARCH) && (__ARM_ARCH >= 5)))
|
|
|
|
moo_oow_t n;
|
|
|
|
__asm__ volatile (
|
|
|
|
"clz %0,%1\n\t"
|
|
|
|
: "=r"(n) /* output */
|
|
|
|
: "r"(x) /* input */
|
|
|
|
);
|
|
|
|
return (int)(MOO_OOW_BITS - n - 1);
|
|
|
|
/* TODO: PPC - use cntlz, cntlzw, cntlzd, SPARC - use lzcnt, MIPS clz */
|
|
|
|
#else
|
|
|
|
int pos = 0;
|
|
|
|
while (x >>= 1) pos++;
|
|
|
|
return pos;
|
2017-01-09 09:54:49 +00:00
|
|
|
#endif
|
2019-05-04 04:27:27 +00:00
|
|
|
}
|
2017-01-09 09:54:49 +00:00
|
|
|
|
2019-05-04 04:27:27 +00:00
|
|
|
#if defined(__cplusplus)
|
|
|
|
}
|
|
|
|
#endif
|
2017-01-09 09:54:49 +00:00
|
|
|
|
|
|
|
#endif
|