2016-09-28 14:40:37 +00:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
2018-02-07 14:13:13 +00:00
|
|
|
Copyright (c) 2016-2018 Chung, Hyung-Hwan. All rights reserved.
|
2016-09-28 14:40:37 +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.
|
|
|
|
*/
|
|
|
|
|
2018-02-05 10:43:25 +00:00
|
|
|
#include "hcl-prv.h"
|
|
|
|
|
|
|
|
/* some naming conventions
|
|
|
|
* bchars, uchars -> pointer and length
|
|
|
|
* bcstr, ucstr -> null-terminated string pointer
|
|
|
|
* btouchars -> bchars to uchars
|
|
|
|
* utobchars -> uchars to bchars
|
|
|
|
* btoucstr -> bcstr to ucstr
|
|
|
|
* utobcstr -> ucstr to bcstr
|
|
|
|
*/
|
2016-09-28 14:40:37 +00:00
|
|
|
|
2019-03-06 01:50:46 +00:00
|
|
|
hcl_oow_t hcl_hash_bytes_ (const hcl_oob_t* ptr, hcl_oow_t len)
|
2016-09-28 14:40:37 +00:00
|
|
|
{
|
2019-03-06 01:50:46 +00:00
|
|
|
hcl_oow_t hv;
|
|
|
|
HCL_HASH_BYTES (hv, ptr, len);
|
2018-02-05 10:43:25 +00:00
|
|
|
/* constrain the hash value to be representable in a small integer
|
|
|
|
* for convenience sake */
|
2019-03-06 01:50:46 +00:00
|
|
|
return hv % ((hcl_oow_t)HCL_SMOOI_MAX + 1);
|
2016-09-28 14:40:37 +00:00
|
|
|
}
|
|
|
|
|
2018-04-07 15:54:16 +00:00
|
|
|
int hcl_equal_uchars (const hcl_uch_t* str1, const hcl_uch_t* str2, hcl_oow_t len)
|
2016-09-28 14:40:37 +00:00
|
|
|
{
|
2018-02-05 10:43:25 +00:00
|
|
|
hcl_oow_t i;
|
|
|
|
|
|
|
|
/* NOTE: you should call this function after having ensured that
|
|
|
|
* str1 and str2 are in the same length */
|
|
|
|
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
{
|
|
|
|
if (str1[i] != str2[i]) return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
2016-09-28 14:40:37 +00:00
|
|
|
}
|
|
|
|
|
2018-04-07 15:54:16 +00:00
|
|
|
int hcl_equal_bchars (const hcl_bch_t* str1, const hcl_bch_t* str2, hcl_oow_t len)
|
2016-09-28 14:40:37 +00:00
|
|
|
{
|
|
|
|
hcl_oow_t i;
|
|
|
|
|
2018-02-05 10:43:25 +00:00
|
|
|
/* NOTE: you should call this function after having ensured that
|
|
|
|
* str1 and str2 are in the same length */
|
|
|
|
|
2016-09-28 14:40:37 +00:00
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
{
|
|
|
|
if (str1[i] != str2[i]) return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-04-07 15:54:16 +00:00
|
|
|
int hcl_comp_uchars (const hcl_uch_t* str1, hcl_oow_t len1, const hcl_uch_t* str2, hcl_oow_t len2)
|
2018-02-05 10:43:25 +00:00
|
|
|
{
|
|
|
|
hcl_uchu_t c1, c2;
|
|
|
|
const hcl_uch_t* end1 = str1 + len1;
|
|
|
|
const hcl_uch_t* end2 = str2 + len2;
|
|
|
|
|
|
|
|
while (str1 < end1)
|
|
|
|
{
|
|
|
|
c1 = *str1;
|
2021-03-28 18:12:07 +00:00
|
|
|
if (str2 < end2)
|
2018-02-05 10:43:25 +00:00
|
|
|
{
|
|
|
|
c2 = *str2;
|
|
|
|
if (c1 > c2) return 1;
|
|
|
|
if (c1 < c2) return -1;
|
|
|
|
}
|
|
|
|
else return 1;
|
|
|
|
str1++; str2++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (str2 < end2)? -1: 0;
|
|
|
|
}
|
|
|
|
|
2018-04-07 15:54:16 +00:00
|
|
|
int hcl_comp_bchars (const hcl_bch_t* str1, hcl_oow_t len1, const hcl_bch_t* str2, hcl_oow_t len2)
|
2018-02-05 10:43:25 +00:00
|
|
|
{
|
|
|
|
hcl_bchu_t c1, c2;
|
|
|
|
const hcl_bch_t* end1 = str1 + len1;
|
|
|
|
const hcl_bch_t* end2 = str2 + len2;
|
|
|
|
|
|
|
|
while (str1 < end1)
|
|
|
|
{
|
|
|
|
c1 = *str1;
|
2021-03-28 18:12:07 +00:00
|
|
|
if (str2 < end2)
|
2018-02-05 10:43:25 +00:00
|
|
|
{
|
|
|
|
c2 = *str2;
|
|
|
|
if (c1 > c2) return 1;
|
|
|
|
if (c1 < c2) return -1;
|
|
|
|
}
|
|
|
|
else return 1;
|
|
|
|
str1++; str2++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (str2 < end2)? -1: 0;
|
|
|
|
}
|
|
|
|
|
2018-04-07 15:54:16 +00:00
|
|
|
int hcl_comp_ucstr (const hcl_uch_t* str1, const hcl_uch_t* str2)
|
2016-09-28 14:40:37 +00:00
|
|
|
{
|
|
|
|
while (*str1 == *str2)
|
|
|
|
{
|
|
|
|
if (*str1 == '\0') return 0;
|
2018-02-05 10:43:25 +00:00
|
|
|
str1++; str2++;
|
2016-09-28 14:40:37 +00:00
|
|
|
}
|
|
|
|
|
2018-02-05 10:43:25 +00:00
|
|
|
return ((hcl_uchu_t)*str1 > (hcl_uchu_t)*str2)? 1: -1;
|
2016-09-28 14:40:37 +00:00
|
|
|
}
|
|
|
|
|
2018-04-07 15:54:16 +00:00
|
|
|
int hcl_comp_bcstr (const hcl_bch_t* str1, const hcl_bch_t* str2)
|
2016-09-28 14:40:37 +00:00
|
|
|
{
|
|
|
|
while (*str1 == *str2)
|
|
|
|
{
|
|
|
|
if (*str1 == '\0') return 0;
|
2018-02-05 10:43:25 +00:00
|
|
|
str1++; str2++;
|
2016-09-28 14:40:37 +00:00
|
|
|
}
|
|
|
|
|
2018-02-05 10:43:25 +00:00
|
|
|
return ((hcl_bchu_t)*str1 > (hcl_bchu_t)*str2)? 1: -1;
|
2016-09-28 14:40:37 +00:00
|
|
|
}
|
|
|
|
|
2018-04-07 15:54:16 +00:00
|
|
|
int hcl_comp_ucstr_bcstr (const hcl_uch_t* str1, const hcl_bch_t* str2)
|
2016-09-28 14:40:37 +00:00
|
|
|
{
|
|
|
|
while (*str1 == *str2)
|
|
|
|
{
|
|
|
|
if (*str1 == '\0') return 0;
|
2018-02-05 10:43:25 +00:00
|
|
|
str1++; str2++;
|
2016-09-28 14:40:37 +00:00
|
|
|
}
|
|
|
|
|
2018-02-05 10:43:25 +00:00
|
|
|
return ((hcl_uchu_t)*str1 > (hcl_bchu_t)*str2)? 1: -1;
|
|
|
|
}
|
|
|
|
|
2018-04-07 15:54:16 +00:00
|
|
|
int hcl_comp_uchars_ucstr (const hcl_uch_t* str1, hcl_oow_t len, const hcl_uch_t* str2)
|
2018-02-05 10:43:25 +00:00
|
|
|
{
|
|
|
|
/* for "abc\0" of length 4 vs "abc", the fourth character
|
|
|
|
* of the first string is equal to the terminating null of
|
2021-03-28 18:12:07 +00:00
|
|
|
* the second string. the first string is still considered
|
2018-02-05 10:43:25 +00:00
|
|
|
* bigger */
|
|
|
|
const hcl_uch_t* end = str1 + len;
|
2021-03-28 18:12:07 +00:00
|
|
|
while (str1 < end && *str2 != '\0')
|
2018-02-05 10:43:25 +00:00
|
|
|
{
|
|
|
|
if (*str1 != *str2) return ((hcl_uchu_t)*str1 > (hcl_uchu_t)*str2)? 1: -1;
|
|
|
|
str1++; str2++;
|
|
|
|
}
|
|
|
|
return (str1 < end)? 1: (*str2 == '\0'? 0: -1);
|
2016-09-28 14:40:37 +00:00
|
|
|
}
|
|
|
|
|
2018-04-07 15:54:16 +00:00
|
|
|
int hcl_comp_uchars_bcstr (const hcl_uch_t* str1, hcl_oow_t len, const hcl_bch_t* str2)
|
2016-09-28 14:40:37 +00:00
|
|
|
{
|
|
|
|
const hcl_uch_t* end = str1 + len;
|
2021-03-28 18:12:07 +00:00
|
|
|
while (str1 < end && *str2 != '\0')
|
2018-02-05 10:43:25 +00:00
|
|
|
{
|
|
|
|
if (*str1 != *str2) return ((hcl_uchu_t)*str1 > (hcl_bchu_t)*str2)? 1: -1;
|
|
|
|
str1++; str2++;
|
|
|
|
}
|
|
|
|
return (str1 < end)? 1: (*str2 == '\0'? 0: -1);
|
|
|
|
}
|
|
|
|
|
2018-04-07 15:54:16 +00:00
|
|
|
int hcl_comp_bchars_bcstr (const hcl_bch_t* str1, hcl_oow_t len, const hcl_bch_t* str2)
|
2018-02-05 10:43:25 +00:00
|
|
|
{
|
|
|
|
const hcl_bch_t* end = str1 + len;
|
2021-03-28 18:12:07 +00:00
|
|
|
while (str1 < end && *str2 != '\0')
|
2018-02-05 10:43:25 +00:00
|
|
|
{
|
|
|
|
if (*str1 != *str2) return ((hcl_bchu_t)*str1 > (hcl_bchu_t)*str2)? 1: -1;
|
|
|
|
str1++; str2++;
|
|
|
|
}
|
|
|
|
return (str1 < end)? 1: (*str2 == '\0'? 0: -1);
|
|
|
|
}
|
|
|
|
|
2018-04-07 15:54:16 +00:00
|
|
|
int hcl_comp_bchars_ucstr (const hcl_bch_t* str1, hcl_oow_t len, const hcl_uch_t* str2)
|
2018-02-05 10:43:25 +00:00
|
|
|
{
|
|
|
|
const hcl_bch_t* end = str1 + len;
|
2021-03-28 18:12:07 +00:00
|
|
|
while (str1 < end && *str2 != '\0')
|
2018-02-05 10:43:25 +00:00
|
|
|
{
|
|
|
|
if (*str1 != *str2) return ((hcl_bchu_t)*str1 > (hcl_uchu_t)*str2)? 1: -1;
|
|
|
|
str1++; str2++;
|
|
|
|
}
|
|
|
|
return (str1 < end)? 1: (*str2 == '\0'? 0: -1);
|
2016-09-28 14:40:37 +00:00
|
|
|
}
|
|
|
|
|
2018-04-07 15:54:16 +00:00
|
|
|
void hcl_copy_uchars (hcl_uch_t* dst, const hcl_uch_t* src, hcl_oow_t len)
|
2016-09-28 14:40:37 +00:00
|
|
|
{
|
2018-02-05 10:43:25 +00:00
|
|
|
/* take note of no forced null termination */
|
2016-09-28 14:40:37 +00:00
|
|
|
hcl_oow_t i;
|
|
|
|
for (i = 0; i < len; i++) dst[i] = src[i];
|
|
|
|
}
|
|
|
|
|
2018-04-07 15:54:16 +00:00
|
|
|
void hcl_copy_bchars (hcl_bch_t* dst, const hcl_bch_t* src, hcl_oow_t len)
|
2016-09-28 14:40:37 +00:00
|
|
|
{
|
2018-02-05 10:43:25 +00:00
|
|
|
/* take note of no forced null termination */
|
2016-09-28 14:40:37 +00:00
|
|
|
hcl_oow_t i;
|
|
|
|
for (i = 0; i < len; i++) dst[i] = src[i];
|
|
|
|
}
|
|
|
|
|
2018-04-07 15:54:16 +00:00
|
|
|
void hcl_copy_bchars_to_uchars (hcl_uch_t* dst, const hcl_bch_t* src, hcl_oow_t len)
|
2016-09-28 14:40:37 +00:00
|
|
|
{
|
2018-02-05 10:43:25 +00:00
|
|
|
/* copy without conversions.
|
2021-01-22 14:43:47 +00:00
|
|
|
* use hcl_convbtouchars() for conversion encoding */
|
2016-09-28 14:40:37 +00:00
|
|
|
hcl_oow_t i;
|
|
|
|
for (i = 0; i < len; i++) dst[i] = src[i];
|
|
|
|
}
|
|
|
|
|
2021-01-22 14:43:47 +00:00
|
|
|
void hcl_copy_uchars_to_bchars (hcl_bch_t* dst, const hcl_uch_t* src, hcl_oow_t len)
|
|
|
|
{
|
|
|
|
/* copy without conversions.
|
|
|
|
* use hcl_convutobchars() for conversion encoding */
|
|
|
|
hcl_oow_t i;
|
|
|
|
for (i = 0; i < len; i++) dst[i] = src[i];
|
|
|
|
}
|
|
|
|
|
2023-10-29 11:26:48 +00:00
|
|
|
hcl_oow_t hcl_copy_bcstr_to_ucstr (hcl_uch_t* dst, hcl_oow_t len, const hcl_bch_t* src)
|
|
|
|
{
|
|
|
|
/* copy without conversions.
|
|
|
|
* the code is the same as hcl_copy_bcstr() except type of src */
|
|
|
|
hcl_uch_t* p, * p2;
|
|
|
|
|
|
|
|
p = dst; p2 = dst + len - 1;
|
|
|
|
|
|
|
|
while (p < p2)
|
|
|
|
{
|
|
|
|
if (*src == '\0') break;
|
|
|
|
*p++ = *src++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len > 0) *p = '\0';
|
|
|
|
return p - dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
hcl_oow_t hcl_copy_ucstr_to_bcstr (hcl_bch_t* dst, hcl_oow_t len, const hcl_uch_t* src)
|
|
|
|
{
|
|
|
|
/* copy without conversions */
|
|
|
|
hcl_bch_t* p, * p2;
|
|
|
|
|
|
|
|
p = dst; p2 = dst + len - 1;
|
|
|
|
|
|
|
|
while (p < p2)
|
|
|
|
{
|
|
|
|
if (*src == '\0') break;
|
|
|
|
*p++ = *src++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len > 0) *p = '\0';
|
|
|
|
return p - dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-22 14:43:47 +00:00
|
|
|
hcl_oow_t hcl_copy_uchars_to_ucstr (hcl_uch_t* dst, hcl_oow_t dlen, const hcl_uch_t* src, hcl_oow_t slen)
|
|
|
|
{
|
|
|
|
hcl_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;
|
|
|
|
}
|
|
|
|
|
|
|
|
hcl_oow_t hcl_copy_bchars_to_bcstr (hcl_bch_t* dst, hcl_oow_t dlen, const hcl_bch_t* src, hcl_oow_t slen)
|
|
|
|
{
|
|
|
|
hcl_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;
|
|
|
|
}
|
|
|
|
|
|
|
|
hcl_oow_t hcl_copy_uchars_to_ucstr_unlimited (hcl_uch_t* dst, const hcl_uch_t* src, hcl_oow_t len)
|
|
|
|
{
|
|
|
|
hcl_oow_t i;
|
|
|
|
for (i = 0; i < len; i++) dst[i] = src[i];
|
|
|
|
dst[i] = '\0';
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
hcl_oow_t hcl_copy_bchars_to_bcstr_unlimited (hcl_bch_t* dst, const hcl_bch_t* src, hcl_oow_t len)
|
|
|
|
{
|
|
|
|
hcl_oow_t i;
|
|
|
|
for (i = 0; i < len; i++) dst[i] = src[i];
|
|
|
|
dst[i] = '\0';
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2018-04-07 15:54:16 +00:00
|
|
|
hcl_oow_t hcl_copy_ucstr (hcl_uch_t* dst, hcl_oow_t len, const hcl_uch_t* src)
|
2016-09-28 14:40:37 +00:00
|
|
|
{
|
|
|
|
hcl_uch_t* p, * p2;
|
|
|
|
|
|
|
|
p = dst; p2 = dst + len - 1;
|
|
|
|
|
|
|
|
while (p < p2)
|
|
|
|
{
|
|
|
|
if (*src == '\0') break;
|
|
|
|
*p++ = *src++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len > 0) *p = '\0';
|
|
|
|
return p - dst;
|
|
|
|
}
|
|
|
|
|
2018-04-07 15:54:16 +00:00
|
|
|
hcl_oow_t hcl_copy_bcstr (hcl_bch_t* dst, hcl_oow_t len, const hcl_bch_t* src)
|
2016-09-28 14:40:37 +00:00
|
|
|
{
|
|
|
|
hcl_bch_t* p, * p2;
|
|
|
|
|
|
|
|
p = dst; p2 = dst + len - 1;
|
|
|
|
|
|
|
|
while (p < p2)
|
|
|
|
{
|
|
|
|
if (*src == '\0') break;
|
|
|
|
*p++ = *src++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len > 0) *p = '\0';
|
|
|
|
return p - dst;
|
|
|
|
}
|
|
|
|
|
2021-01-22 14:43:47 +00:00
|
|
|
|
|
|
|
hcl_oow_t hcl_copy_ucstr_unlimited (hcl_uch_t* dst, const hcl_uch_t* src)
|
|
|
|
{
|
|
|
|
hcl_uch_t* org = dst;
|
|
|
|
while ((*dst++ = *src++) != '\0');
|
|
|
|
return dst - org - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
hcl_oow_t hcl_copy_bcstr_unlimited (hcl_bch_t* dst, const hcl_bch_t* src)
|
|
|
|
{
|
|
|
|
hcl_bch_t* org = dst;
|
|
|
|
while ((*dst++ = *src++) != '\0');
|
|
|
|
return dst - org - 1;
|
|
|
|
}
|
|
|
|
|
2019-02-18 17:18:21 +00:00
|
|
|
void hcl_fill_uchars (hcl_uch_t* dst, hcl_uch_t ch, hcl_oow_t len)
|
|
|
|
{
|
|
|
|
hcl_oow_t i;
|
|
|
|
for (i = 0; i < len; i++) dst[i] = ch;
|
|
|
|
}
|
|
|
|
|
|
|
|
void hcl_fill_bchars (hcl_bch_t* dst, hcl_bch_t ch, hcl_oow_t len)
|
|
|
|
{
|
|
|
|
hcl_oow_t i;
|
|
|
|
for (i = 0; i < len; i++) dst[i] = ch;
|
|
|
|
}
|
|
|
|
|
2018-04-07 15:54:16 +00:00
|
|
|
hcl_oow_t hcl_count_ucstr (const hcl_uch_t* str)
|
2016-09-28 14:40:37 +00:00
|
|
|
{
|
|
|
|
const hcl_uch_t* ptr = str;
|
|
|
|
while (*ptr != '\0') ptr++;
|
|
|
|
return ptr - str;
|
|
|
|
}
|
|
|
|
|
2018-04-07 15:54:16 +00:00
|
|
|
hcl_oow_t hcl_count_bcstr (const hcl_bch_t* str)
|
2016-09-28 14:40:37 +00:00
|
|
|
{
|
|
|
|
const hcl_bch_t* ptr = str;
|
|
|
|
while (*ptr != '\0') ptr++;
|
|
|
|
return ptr - str;
|
|
|
|
}
|
|
|
|
|
2018-04-07 15:54:16 +00:00
|
|
|
hcl_uch_t* hcl_find_uchar (const hcl_uch_t* ptr, hcl_oow_t len, hcl_uch_t c)
|
2016-09-28 14:40:37 +00:00
|
|
|
{
|
|
|
|
const hcl_uch_t* end;
|
|
|
|
|
|
|
|
end = ptr + len;
|
|
|
|
while (ptr < end)
|
|
|
|
{
|
|
|
|
if (*ptr == c) return (hcl_uch_t*)ptr;
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return HCL_NULL;
|
|
|
|
}
|
|
|
|
|
2018-04-07 15:54:16 +00:00
|
|
|
hcl_bch_t* hcl_find_bchar (const hcl_bch_t* ptr, hcl_oow_t len, hcl_bch_t c)
|
2016-09-28 14:40:37 +00:00
|
|
|
{
|
|
|
|
const hcl_bch_t* end;
|
|
|
|
|
|
|
|
end = ptr + len;
|
|
|
|
while (ptr < end)
|
|
|
|
{
|
|
|
|
if (*ptr == c) return (hcl_bch_t*)ptr;
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return HCL_NULL;
|
|
|
|
}
|
|
|
|
|
2018-04-07 15:54:16 +00:00
|
|
|
hcl_uch_t* hcl_rfind_uchar (const hcl_uch_t* ptr, hcl_oow_t len, hcl_uch_t c)
|
2018-02-05 10:43:25 +00:00
|
|
|
{
|
|
|
|
const hcl_uch_t* cur;
|
|
|
|
|
|
|
|
cur = ptr + len;
|
|
|
|
while (cur > ptr)
|
|
|
|
{
|
|
|
|
--cur;
|
|
|
|
if (*cur == c) return (hcl_uch_t*)cur;
|
|
|
|
}
|
|
|
|
|
|
|
|
return HCL_NULL;
|
|
|
|
}
|
|
|
|
|
2018-04-07 15:54:16 +00:00
|
|
|
hcl_bch_t* hcl_rfind_bchar (const hcl_bch_t* ptr, hcl_oow_t len, hcl_bch_t c)
|
2018-02-05 10:43:25 +00:00
|
|
|
{
|
|
|
|
const hcl_bch_t* cur;
|
|
|
|
|
|
|
|
cur = ptr + len;
|
|
|
|
while (cur > ptr)
|
|
|
|
{
|
|
|
|
--cur;
|
|
|
|
if (*cur == c) return (hcl_bch_t*)cur;
|
|
|
|
}
|
|
|
|
|
|
|
|
return HCL_NULL;
|
|
|
|
}
|
|
|
|
|
2018-04-07 15:54:16 +00:00
|
|
|
hcl_uch_t* hcl_find_uchar_in_ucstr (const hcl_uch_t* ptr, hcl_uch_t c)
|
2018-02-05 10:43:25 +00:00
|
|
|
{
|
|
|
|
while (*ptr != '\0')
|
|
|
|
{
|
|
|
|
if (*ptr == c) return (hcl_uch_t*)ptr;
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return HCL_NULL;
|
|
|
|
}
|
|
|
|
|
2018-04-07 15:54:16 +00:00
|
|
|
hcl_bch_t* hcl_find_bchar_in_bcstr (const hcl_bch_t* ptr, hcl_bch_t c)
|
2018-02-05 10:43:25 +00:00
|
|
|
{
|
|
|
|
while (*ptr != '\0')
|
|
|
|
{
|
|
|
|
if (*ptr == c) return (hcl_bch_t*)ptr;
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return HCL_NULL;
|
|
|
|
}
|
2018-02-08 07:40:27 +00:00
|
|
|
|
2018-02-05 10:43:25 +00:00
|
|
|
/* ----------------------------------------------------------------------- */
|
2022-01-04 15:26:17 +00:00
|
|
|
|
|
|
|
hcl_oow_t hcl_rotate_uchars (hcl_uch_t* str, hcl_oow_t len, int dir, hcl_oow_t n)
|
|
|
|
{
|
|
|
|
hcl_oow_t first, last, count, index, nk;
|
|
|
|
hcl_uch_t c;
|
|
|
|
|
|
|
|
if (dir == 0 || len == 0) return len;
|
|
|
|
if ((n %= len) == 0) return len;
|
|
|
|
|
|
|
|
if (dir > 0) n = len - n;
|
|
|
|
first = 0; nk = len - n; count = 0;
|
|
|
|
|
|
|
|
while (count < n)
|
|
|
|
{
|
|
|
|
last = first + nk;
|
|
|
|
index = first;
|
|
|
|
c = str[first];
|
|
|
|
do
|
|
|
|
{
|
|
|
|
count++;
|
|
|
|
while (index < nk)
|
|
|
|
{
|
|
|
|
str[index] = str[index + n];
|
|
|
|
index += n;
|
|
|
|
}
|
|
|
|
if (index == last) break;
|
|
|
|
str[index] = str[index - nk];
|
|
|
|
index -= nk;
|
|
|
|
}
|
|
|
|
while (1);
|
|
|
|
str[last] = c; first++;
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
hcl_oow_t hcl_rotate_bchars (hcl_bch_t* str, hcl_oow_t len, int dir, hcl_oow_t n)
|
|
|
|
{
|
|
|
|
hcl_oow_t first, last, count, index, nk;
|
|
|
|
hcl_bch_t c;
|
|
|
|
|
|
|
|
if (dir == 0 || len == 0) return len;
|
|
|
|
if ((n %= len) == 0) return len;
|
|
|
|
|
|
|
|
if (dir > 0) n = len - n;
|
|
|
|
first = 0; nk = len - n; count = 0;
|
|
|
|
|
|
|
|
while (count < n)
|
|
|
|
{
|
|
|
|
last = first + nk;
|
|
|
|
index = first;
|
|
|
|
c = str[first];
|
|
|
|
do
|
|
|
|
{
|
|
|
|
count++;
|
|
|
|
while (index < nk)
|
|
|
|
{
|
|
|
|
str[index] = str[index + n];
|
|
|
|
index += n;
|
|
|
|
}
|
|
|
|
if (index == last) break;
|
|
|
|
str[index] = str[index - nk];
|
|
|
|
index -= nk;
|
|
|
|
}
|
|
|
|
while (1);
|
|
|
|
str[last] = c; first++;
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------- */
|
2018-02-05 10:43:25 +00:00
|
|
|
|
2019-03-18 15:37:07 +00:00
|
|
|
hcl_oow_t hcl_byte_to_bcstr (hcl_uint8_t byte, hcl_bch_t* buf, hcl_oow_t size, int flagged_radix, hcl_bch_t fill)
|
|
|
|
{
|
2019-05-04 17:56:45 +00:00
|
|
|
hcl_bch_t tmp[(HCL_SIZEOF(hcl_uint8_t) * HCL_BITS_PER_BYTE)];
|
2019-03-18 15:37:07 +00:00
|
|
|
hcl_bch_t* p = tmp, * bp = buf, * be = buf + size - 1;
|
|
|
|
int radix;
|
|
|
|
hcl_bch_t radix_char;
|
|
|
|
|
|
|
|
radix = (flagged_radix & HCL_BYTE_TO_BCSTR_RADIXMASK);
|
|
|
|
radix_char = (flagged_radix & HCL_BYTE_TO_BCSTR_LOWERCASE)? 'a': 'A';
|
|
|
|
if (radix < 2 || radix > 36 || size <= 0) return 0;
|
|
|
|
|
2021-03-28 18:12:07 +00:00
|
|
|
do
|
2019-03-18 15:37:07 +00:00
|
|
|
{
|
2021-03-28 18:12:07 +00:00
|
|
|
hcl_uint8_t digit = byte % radix;
|
2019-03-18 15:37:07 +00:00
|
|
|
if (digit < 10) *p++ = digit + '0';
|
|
|
|
else *p++ = digit + radix_char - 10;
|
|
|
|
byte /= radix;
|
|
|
|
}
|
|
|
|
while (byte > 0);
|
|
|
|
|
2021-03-28 18:12:07 +00:00
|
|
|
if (fill != '\0')
|
2019-03-18 15:37:07 +00:00
|
|
|
{
|
2021-03-28 18:12:07 +00:00
|
|
|
while (size - 1 > p - tmp)
|
2019-03-18 15:37:07 +00:00
|
|
|
{
|
|
|
|
*bp++ = fill;
|
|
|
|
size--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while (p > tmp && bp < be) *bp++ = *--p;
|
|
|
|
*bp = '\0';
|
|
|
|
return bp - buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------- */
|
|
|
|
|
2019-05-04 17:56:45 +00:00
|
|
|
HCL_INLINE int hcl_conv_bchars_to_uchars_with_cmgr (const hcl_bch_t* bcs, hcl_oow_t* bcslen, hcl_uch_t* ucs, hcl_oow_t* ucslen, hcl_cmgr_t* cmgr, int all)
|
2018-02-05 10:43:25 +00:00
|
|
|
{
|
|
|
|
const hcl_bch_t* p;
|
|
|
|
int ret = 0;
|
|
|
|
hcl_oow_t mlen;
|
|
|
|
|
|
|
|
if (ucs)
|
|
|
|
{
|
2021-03-28 18:12:07 +00:00
|
|
|
/* destination buffer is specified.
|
2018-02-05 10:43:25 +00:00
|
|
|
* copy the conversion result to the buffer */
|
|
|
|
|
|
|
|
hcl_uch_t* q, * qend;
|
|
|
|
|
|
|
|
p = bcs;
|
|
|
|
q = ucs;
|
|
|
|
qend = ucs + *ucslen;
|
|
|
|
mlen = *bcslen;
|
|
|
|
|
|
|
|
while (mlen > 0)
|
|
|
|
{
|
|
|
|
hcl_oow_t n;
|
|
|
|
|
|
|
|
if (q >= qend)
|
|
|
|
{
|
|
|
|
/* buffer too small */
|
|
|
|
ret = -2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
n = cmgr->bctouc (p, mlen, q);
|
|
|
|
if (n == 0)
|
|
|
|
{
|
|
|
|
/* invalid sequence */
|
|
|
|
if (all)
|
|
|
|
{
|
|
|
|
n = 1;
|
|
|
|
*q = '?';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (n > mlen)
|
|
|
|
{
|
|
|
|
/* incomplete sequence */
|
|
|
|
if (all)
|
|
|
|
{
|
|
|
|
n = 1;
|
|
|
|
*q = '?';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = -3;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
q++;
|
|
|
|
p += n;
|
|
|
|
mlen -= n;
|
|
|
|
}
|
|
|
|
|
|
|
|
*ucslen = q - ucs;
|
|
|
|
*bcslen = p - bcs;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* no destination buffer is specified. perform conversion
|
|
|
|
* but don't copy the result. the caller can call this function
|
|
|
|
* without a buffer to find the required buffer size, allocate
|
2021-03-28 18:12:07 +00:00
|
|
|
* a buffer with the size and call this function again with
|
2018-02-05 10:43:25 +00:00
|
|
|
* the buffer. */
|
|
|
|
|
|
|
|
hcl_uch_t w;
|
|
|
|
hcl_oow_t wlen = 0;
|
|
|
|
|
|
|
|
p = bcs;
|
|
|
|
mlen = *bcslen;
|
|
|
|
|
|
|
|
while (mlen > 0)
|
|
|
|
{
|
|
|
|
hcl_oow_t n;
|
|
|
|
|
|
|
|
n = cmgr->bctouc (p, mlen, &w);
|
|
|
|
if (n == 0)
|
|
|
|
{
|
|
|
|
/* invalid sequence */
|
|
|
|
if (all) n = 1;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (n > mlen)
|
|
|
|
{
|
|
|
|
/* incomplete sequence */
|
|
|
|
if (all) n = 1;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = -3;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
p += n;
|
|
|
|
mlen -= n;
|
|
|
|
wlen += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*ucslen = wlen;
|
|
|
|
*bcslen = p - bcs;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-05-04 17:56:45 +00:00
|
|
|
HCL_INLINE int hcl_conv_bcstr_to_ucstr_with_cmgr (const hcl_bch_t* bcs, hcl_oow_t* bcslen, hcl_uch_t* ucs, hcl_oow_t* ucslen, hcl_cmgr_t* cmgr, int all)
|
2018-02-05 10:43:25 +00:00
|
|
|
{
|
|
|
|
const hcl_bch_t* bp;
|
|
|
|
hcl_oow_t mlen, wlen;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
for (bp = bcs; *bp != '\0'; bp++) /* nothing */ ;
|
|
|
|
|
|
|
|
mlen = bp - bcs; wlen = *ucslen;
|
2018-04-07 15:54:16 +00:00
|
|
|
n = hcl_conv_bchars_to_uchars_with_cmgr (bcs, &mlen, ucs, &wlen, cmgr, all);
|
2018-02-05 10:43:25 +00:00
|
|
|
if (ucs)
|
|
|
|
{
|
|
|
|
/* null-terminate the target buffer if it has room for it. */
|
|
|
|
if (wlen < *ucslen) ucs[wlen] = '\0';
|
|
|
|
else n = -2; /* buffer too small */
|
|
|
|
}
|
|
|
|
*bcslen = mlen; *ucslen = wlen;
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2019-05-04 17:56:45 +00:00
|
|
|
HCL_INLINE int hcl_conv_uchars_to_bchars_with_cmgr (const hcl_uch_t* ucs, hcl_oow_t* ucslen, hcl_bch_t* bcs, hcl_oow_t* bcslen, hcl_cmgr_t* cmgr)
|
2018-02-05 10:43:25 +00:00
|
|
|
{
|
|
|
|
const hcl_uch_t* p = ucs;
|
|
|
|
const hcl_uch_t* end = ucs + *ucslen;
|
2021-03-28 18:12:07 +00:00
|
|
|
int ret = 0;
|
2018-02-05 10:43:25 +00:00
|
|
|
|
|
|
|
if (bcs)
|
|
|
|
{
|
|
|
|
hcl_oow_t rem = *bcslen;
|
|
|
|
|
2021-03-28 18:12:07 +00:00
|
|
|
while (p < end)
|
2018-02-05 10:43:25 +00:00
|
|
|
{
|
|
|
|
hcl_oow_t n;
|
|
|
|
|
|
|
|
if (rem <= 0)
|
|
|
|
{
|
|
|
|
ret = -2; /* buffer too small */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
n = cmgr->uctobc (*p, bcs, rem);
|
2021-03-28 18:12:07 +00:00
|
|
|
if (n == 0)
|
2018-02-05 10:43:25 +00:00
|
|
|
{
|
|
|
|
ret = -1;
|
|
|
|
break; /* illegal character */
|
|
|
|
}
|
2021-03-28 18:12:07 +00:00
|
|
|
if (n > rem)
|
2018-02-05 10:43:25 +00:00
|
|
|
{
|
|
|
|
ret = -2; /* buffer too small */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
bcs += n; rem -= n; p++;
|
|
|
|
}
|
|
|
|
|
2021-03-28 18:12:07 +00:00
|
|
|
*bcslen -= rem;
|
2018-02-05 10:43:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-03-12 10:39:13 +00:00
|
|
|
hcl_bch_t bcsbuf[HCL_BCSIZE_MAX];
|
2018-02-05 10:43:25 +00:00
|
|
|
hcl_oow_t mlen = 0;
|
|
|
|
|
|
|
|
while (p < end)
|
|
|
|
{
|
|
|
|
hcl_oow_t n;
|
|
|
|
|
|
|
|
n = cmgr->uctobc (*p, bcsbuf, HCL_COUNTOF(bcsbuf));
|
2021-03-28 18:12:07 +00:00
|
|
|
if (n == 0)
|
2018-02-05 10:43:25 +00:00
|
|
|
{
|
|
|
|
ret = -1;
|
|
|
|
break; /* illegal character */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* it assumes that bcsbuf is large enough to hold a character */
|
|
|
|
/*HCL_ASSERT (hcl, n <= HCL_COUNTOF(bcsbuf));*/
|
|
|
|
|
|
|
|
p++; mlen += n;
|
|
|
|
}
|
|
|
|
|
2021-03-28 18:12:07 +00:00
|
|
|
/* this length excludes the terminating null character.
|
2018-02-05 10:43:25 +00:00
|
|
|
* this function doesn't even null-terminate the result. */
|
|
|
|
*bcslen = mlen;
|
|
|
|
}
|
|
|
|
|
|
|
|
*ucslen = p - ucs;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-05-04 17:56:45 +00:00
|
|
|
HCL_INLINE int hcl_conv_ucstr_to_bcstr_with_cmgr (const hcl_uch_t* ucs, hcl_oow_t* ucslen, hcl_bch_t* bcs, hcl_oow_t* bcslen, hcl_cmgr_t* cmgr)
|
2018-02-05 10:43:25 +00:00
|
|
|
{
|
|
|
|
const hcl_uch_t* p = ucs;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (bcs)
|
|
|
|
{
|
|
|
|
hcl_oow_t rem = *bcslen;
|
|
|
|
|
|
|
|
while (*p != '\0')
|
|
|
|
{
|
|
|
|
hcl_oow_t n;
|
|
|
|
|
|
|
|
if (rem <= 0)
|
|
|
|
{
|
|
|
|
ret = -2;
|
|
|
|
break;
|
|
|
|
}
|
2021-03-28 18:12:07 +00:00
|
|
|
|
2018-02-05 10:43:25 +00:00
|
|
|
n = cmgr->uctobc (*p, bcs, rem);
|
2021-03-28 18:12:07 +00:00
|
|
|
if (n == 0)
|
2018-02-05 10:43:25 +00:00
|
|
|
{
|
|
|
|
ret = -1;
|
|
|
|
break; /* illegal character */
|
|
|
|
}
|
2021-03-28 18:12:07 +00:00
|
|
|
if (n > rem)
|
2018-02-05 10:43:25 +00:00
|
|
|
{
|
|
|
|
ret = -2;
|
|
|
|
break; /* buffer too small */
|
|
|
|
}
|
|
|
|
|
|
|
|
bcs += n; rem -= n; p++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* update bcslen to the length of the bcs string converted excluding
|
|
|
|
* terminating null */
|
2021-03-28 18:12:07 +00:00
|
|
|
*bcslen -= rem;
|
2018-02-05 10:43:25 +00:00
|
|
|
|
|
|
|
/* null-terminate the multibyte sequence if it has sufficient space */
|
|
|
|
if (rem > 0) *bcs = '\0';
|
2021-03-28 18:12:07 +00:00
|
|
|
else
|
2018-02-05 10:43:25 +00:00
|
|
|
{
|
2021-03-28 18:12:07 +00:00
|
|
|
/* if ret is -2 and cs[cslen] == '\0',
|
2018-02-05 10:43:25 +00:00
|
|
|
* this means that the bcs buffer was lacking one
|
|
|
|
* slot for the terminating null */
|
|
|
|
ret = -2; /* buffer too small */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-03-12 10:39:13 +00:00
|
|
|
hcl_bch_t bcsbuf[HCL_BCSIZE_MAX];
|
2018-02-05 10:43:25 +00:00
|
|
|
hcl_oow_t mlen = 0;
|
|
|
|
|
|
|
|
while (*p != '\0')
|
|
|
|
{
|
|
|
|
hcl_oow_t n;
|
|
|
|
|
|
|
|
n = cmgr->uctobc (*p, bcsbuf, HCL_COUNTOF(bcsbuf));
|
2021-03-28 18:12:07 +00:00
|
|
|
if (n == 0)
|
2018-02-05 10:43:25 +00:00
|
|
|
{
|
|
|
|
ret = -1;
|
|
|
|
break; /* illegal character */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* it assumes that bcs is large enough to hold a character */
|
|
|
|
/*HCL_ASSERT (hcl, n <= HCL_COUNTOF(bcs));*/
|
|
|
|
|
|
|
|
p++; mlen += n;
|
|
|
|
}
|
|
|
|
|
2021-03-28 18:12:07 +00:00
|
|
|
/* this length holds the number of resulting multi-byte characters
|
2018-02-05 10:43:25 +00:00
|
|
|
* excluding the terminating null character */
|
|
|
|
*bcslen = mlen;
|
|
|
|
}
|
|
|
|
|
|
|
|
*ucslen = p - ucs; /* the number of wide characters handled. */
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static hcl_cmgr_t utf8_cmgr =
|
|
|
|
{
|
2018-04-06 16:33:17 +00:00
|
|
|
hcl_utf8_to_uc,
|
|
|
|
hcl_uc_to_utf8
|
2018-02-05 10:43:25 +00:00
|
|
|
};
|
|
|
|
|
2018-03-12 16:45:42 +00:00
|
|
|
hcl_cmgr_t* hcl_get_utf8_cmgr (void)
|
2018-02-05 10:43:25 +00:00
|
|
|
{
|
|
|
|
return &utf8_cmgr;
|
|
|
|
}
|
|
|
|
|
2018-03-12 16:45:42 +00:00
|
|
|
int hcl_conv_utf8_to_uchars (const hcl_bch_t* bcs, hcl_oow_t* bcslen, hcl_uch_t* ucs, hcl_oow_t* ucslen)
|
2018-02-05 10:43:25 +00:00
|
|
|
{
|
|
|
|
/* the source is length bound */
|
2018-04-07 15:54:16 +00:00
|
|
|
return hcl_conv_bchars_to_uchars_with_cmgr(bcs, bcslen, ucs, ucslen, &utf8_cmgr, 0);
|
2018-02-05 10:43:25 +00:00
|
|
|
}
|
|
|
|
|
2018-03-12 16:45:42 +00:00
|
|
|
int hcl_conv_uchars_to_utf8 (const hcl_uch_t* ucs, hcl_oow_t* ucslen, hcl_bch_t* bcs, hcl_oow_t* bcslen)
|
2018-02-05 10:43:25 +00:00
|
|
|
{
|
|
|
|
/* length bound */
|
2018-04-07 15:54:16 +00:00
|
|
|
return hcl_conv_uchars_to_bchars_with_cmgr(ucs, ucslen, bcs, bcslen, &utf8_cmgr);
|
2018-02-05 10:43:25 +00:00
|
|
|
}
|
|
|
|
|
2018-03-12 16:45:42 +00:00
|
|
|
int hcl_conv_utf8_to_ucstr (const hcl_bch_t* bcs, hcl_oow_t* bcslen, hcl_uch_t* ucs, hcl_oow_t* ucslen)
|
2018-02-05 10:43:25 +00:00
|
|
|
{
|
|
|
|
/* null-terminated. */
|
2019-03-19 13:29:59 +00:00
|
|
|
return hcl_conv_bcstr_to_ucstr_with_cmgr(bcs, bcslen, ucs, ucslen, &utf8_cmgr, 0);
|
2018-02-05 10:43:25 +00:00
|
|
|
}
|
|
|
|
|
2018-03-12 16:45:42 +00:00
|
|
|
int hcl_conv_ucstr_to_utf8 (const hcl_uch_t* ucs, hcl_oow_t* ucslen, hcl_bch_t* bcs, hcl_oow_t* bcslen)
|
2018-02-05 10:43:25 +00:00
|
|
|
{
|
|
|
|
/* null-terminated */
|
2019-03-19 13:29:59 +00:00
|
|
|
return hcl_conv_ucstr_to_bcstr_with_cmgr(ucs, ucslen, bcs, bcslen, &utf8_cmgr);
|
2018-02-05 10:43:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
int hcl_convbtouchars (hcl_t* hcl, const hcl_bch_t* bcs, hcl_oow_t* bcslen, hcl_uch_t* ucs, hcl_oow_t* ucslen)
|
|
|
|
{
|
|
|
|
/* length bound */
|
|
|
|
int n;
|
|
|
|
|
2019-06-21 12:36:25 +00:00
|
|
|
n = hcl_conv_bchars_to_uchars_with_cmgr(bcs, bcslen, ucs, ucslen, hcl_getcmgr(hcl), 0);
|
2018-02-05 10:43:25 +00:00
|
|
|
|
|
|
|
if (n <= -1)
|
|
|
|
{
|
|
|
|
/* -1: illegal character, -2: buffer too small, -3: incomplete sequence */
|
|
|
|
hcl_seterrnum (hcl, (n == -2)? HCL_EBUFFULL: HCL_EECERR);
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
int hcl_convutobchars (hcl_t* hcl, const hcl_uch_t* ucs, hcl_oow_t* ucslen, hcl_bch_t* bcs, hcl_oow_t* bcslen)
|
|
|
|
{
|
|
|
|
/* length bound */
|
|
|
|
int n;
|
|
|
|
|
2019-06-21 12:36:25 +00:00
|
|
|
n = hcl_conv_uchars_to_bchars_with_cmgr(ucs, ucslen, bcs, bcslen, hcl_getcmgr(hcl));
|
2018-02-05 10:43:25 +00:00
|
|
|
|
|
|
|
if (n <= -1)
|
|
|
|
{
|
|
|
|
hcl_seterrnum (hcl, (n == -2)? HCL_EBUFFULL: HCL_EECERR);
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
int hcl_convbtoucstr (hcl_t* hcl, const hcl_bch_t* bcs, hcl_oow_t* bcslen, hcl_uch_t* ucs, hcl_oow_t* ucslen)
|
|
|
|
{
|
|
|
|
/* null-terminated. */
|
|
|
|
int n;
|
|
|
|
|
2019-06-21 12:36:25 +00:00
|
|
|
n = hcl_conv_bcstr_to_ucstr_with_cmgr(bcs, bcslen, ucs, ucslen, hcl_getcmgr(hcl), 0);
|
2018-02-05 10:43:25 +00:00
|
|
|
|
|
|
|
if (n <= -1)
|
|
|
|
{
|
|
|
|
hcl_seterrnum (hcl, (n == -2)? HCL_EBUFFULL: HCL_EECERR);
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
int hcl_convutobcstr (hcl_t* hcl, const hcl_uch_t* ucs, hcl_oow_t* ucslen, hcl_bch_t* bcs, hcl_oow_t* bcslen)
|
|
|
|
{
|
|
|
|
/* null-terminated */
|
|
|
|
int n;
|
|
|
|
|
2019-06-21 12:36:25 +00:00
|
|
|
n = hcl_conv_ucstr_to_bcstr_with_cmgr(ucs, ucslen, bcs, bcslen, hcl_getcmgr(hcl));
|
2018-02-05 10:43:25 +00:00
|
|
|
|
|
|
|
if (n <= -1)
|
|
|
|
{
|
|
|
|
hcl_seterrnum (hcl, (n == -2)? HCL_EBUFFULL: HCL_EECERR);
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
HCL_INLINE hcl_uch_t* hcl_dupbtoucharswithheadroom (hcl_t* hcl, hcl_oow_t headroom_bytes, const hcl_bch_t* bcs, hcl_oow_t bcslen, hcl_oow_t* ucslen)
|
|
|
|
{
|
|
|
|
hcl_oow_t inlen, outlen;
|
|
|
|
hcl_uch_t* ptr;
|
|
|
|
|
|
|
|
inlen = bcslen;
|
2021-03-28 18:12:07 +00:00
|
|
|
if (hcl_convbtouchars (hcl, bcs, &inlen, HCL_NULL, &outlen) <= -1)
|
2018-02-05 10:43:25 +00:00
|
|
|
{
|
|
|
|
/* note it's also an error if no full conversion is made in this function */
|
|
|
|
return HCL_NULL;
|
|
|
|
}
|
|
|
|
|
2018-02-26 15:24:45 +00:00
|
|
|
ptr = (hcl_uch_t*)hcl_allocmem(hcl, headroom_bytes + ((outlen + 1) * HCL_SIZEOF(hcl_uch_t)));
|
2021-01-29 12:39:31 +00:00
|
|
|
if (HCL_UNLIKELY(!ptr)) return HCL_NULL;
|
2018-02-05 10:43:25 +00:00
|
|
|
|
|
|
|
inlen = bcslen;
|
|
|
|
|
|
|
|
ptr = (hcl_uch_t*)((hcl_oob_t*)ptr + headroom_bytes);
|
|
|
|
hcl_convbtouchars (hcl, bcs, &inlen, ptr, &outlen);
|
|
|
|
|
2021-03-28 18:12:07 +00:00
|
|
|
/* hcl_convbtouchars() doesn't null-terminate the target.
|
2018-02-05 10:43:25 +00:00
|
|
|
* but in hcl_dupbtouchars(), i allocate space. so i don't mind
|
|
|
|
* null-terminating it with 1 extra character overhead */
|
2021-03-28 18:12:07 +00:00
|
|
|
ptr[outlen] = '\0';
|
2018-02-05 10:43:25 +00:00
|
|
|
if (ucslen) *ucslen = outlen;
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
hcl_uch_t* hcl_dupbtouchars (hcl_t* hcl, const hcl_bch_t* bcs, hcl_oow_t bcslen, hcl_oow_t* ucslen)
|
|
|
|
{
|
|
|
|
return hcl_dupbtoucharswithheadroom (hcl, 0, bcs, bcslen, ucslen);
|
|
|
|
}
|
|
|
|
|
|
|
|
HCL_INLINE hcl_bch_t* hcl_duputobcharswithheadroom (hcl_t* hcl, hcl_oow_t headroom_bytes, const hcl_uch_t* ucs, hcl_oow_t ucslen, hcl_oow_t* bcslen)
|
|
|
|
{
|
|
|
|
hcl_oow_t inlen, outlen;
|
|
|
|
hcl_bch_t* ptr;
|
|
|
|
|
|
|
|
inlen = ucslen;
|
2021-03-28 18:12:07 +00:00
|
|
|
if (hcl_convutobchars(hcl, ucs, &inlen, HCL_NULL, &outlen) <= -1)
|
2018-02-05 10:43:25 +00:00
|
|
|
{
|
|
|
|
/* note it's also an error if no full conversion is made in this function */
|
|
|
|
return HCL_NULL;
|
|
|
|
}
|
|
|
|
|
2018-02-26 15:24:45 +00:00
|
|
|
ptr = (hcl_bch_t*)hcl_allocmem(hcl, headroom_bytes + ((outlen + 1) * HCL_SIZEOF(hcl_bch_t)));
|
2021-01-29 12:39:31 +00:00
|
|
|
if (HCL_UNLIKELY(!ptr)) return HCL_NULL;
|
2018-02-05 10:43:25 +00:00
|
|
|
|
|
|
|
inlen = ucslen;
|
|
|
|
ptr = (hcl_bch_t*)((hcl_oob_t*)ptr + headroom_bytes);
|
|
|
|
hcl_convutobchars (hcl, ucs, &inlen, ptr, &outlen);
|
|
|
|
|
|
|
|
ptr[outlen] = '\0';
|
|
|
|
if (bcslen) *bcslen = outlen;
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
hcl_bch_t* hcl_duputobchars (hcl_t* hcl, const hcl_uch_t* ucs, hcl_oow_t ucslen, hcl_oow_t* bcslen)
|
|
|
|
{
|
|
|
|
return hcl_duputobcharswithheadroom (hcl, 0, ucs, ucslen, bcslen);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
HCL_INLINE hcl_uch_t* hcl_dupbtoucstrwithheadroom (hcl_t* hcl, hcl_oow_t headroom_bytes, const hcl_bch_t* bcs, hcl_oow_t* ucslen)
|
|
|
|
{
|
|
|
|
hcl_oow_t inlen, outlen;
|
|
|
|
hcl_uch_t* ptr;
|
|
|
|
|
2021-03-28 18:12:07 +00:00
|
|
|
if (hcl_convbtoucstr(hcl, bcs, &inlen, HCL_NULL, &outlen) <= -1)
|
2018-02-05 10:43:25 +00:00
|
|
|
{
|
|
|
|
/* note it's also an error if no full conversion is made in this function */
|
|
|
|
return HCL_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
outlen++;
|
2021-01-29 12:39:31 +00:00
|
|
|
ptr = (hcl_uch_t*)hcl_allocmem(hcl, headroom_bytes + (outlen * HCL_SIZEOF(hcl_uch_t)));
|
|
|
|
if (HCL_UNLIKELY(!ptr)) return HCL_NULL;
|
2018-02-05 10:43:25 +00:00
|
|
|
|
|
|
|
hcl_convbtoucstr (hcl, bcs, &inlen, ptr, &outlen);
|
|
|
|
if (ucslen) *ucslen = outlen;
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
hcl_uch_t* hcl_dupbtoucstr (hcl_t* hcl, const hcl_bch_t* bcs, hcl_oow_t* ucslen)
|
|
|
|
{
|
|
|
|
return hcl_dupbtoucstrwithheadroom (hcl, 0, bcs, ucslen);
|
|
|
|
}
|
|
|
|
|
|
|
|
HCL_INLINE hcl_bch_t* hcl_duputobcstrwithheadroom (hcl_t* hcl, hcl_oow_t headroom_bytes, const hcl_uch_t* ucs, hcl_oow_t* bcslen)
|
|
|
|
{
|
|
|
|
hcl_oow_t inlen, outlen;
|
|
|
|
hcl_bch_t* ptr;
|
|
|
|
|
2021-03-28 18:12:07 +00:00
|
|
|
if (hcl_convutobcstr (hcl, ucs, &inlen, HCL_NULL, &outlen) <= -1)
|
2018-02-05 10:43:25 +00:00
|
|
|
{
|
|
|
|
/* note it's also an error if no full conversion is made in this function */
|
|
|
|
return HCL_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
outlen++;
|
2021-01-29 12:39:31 +00:00
|
|
|
ptr = (hcl_bch_t*)hcl_allocmem(hcl, headroom_bytes + (outlen * HCL_SIZEOF(hcl_bch_t)));
|
|
|
|
if (HCL_UNLIKELY(!ptr)) return HCL_NULL;
|
2018-02-05 10:43:25 +00:00
|
|
|
|
|
|
|
ptr = (hcl_bch_t*)((hcl_oob_t*)ptr + headroom_bytes);
|
|
|
|
|
|
|
|
hcl_convutobcstr (hcl, ucs, &inlen, ptr, &outlen);
|
|
|
|
if (bcslen) *bcslen = outlen;
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
hcl_bch_t* hcl_duputobcstr (hcl_t* hcl, const hcl_uch_t* ucs, hcl_oow_t* bcslen)
|
|
|
|
{
|
|
|
|
return hcl_duputobcstrwithheadroom (hcl, 0, ucs, bcslen);
|
|
|
|
}
|
|
|
|
/* ----------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
hcl_uch_t* hcl_dupuchars (hcl_t* hcl, const hcl_uch_t* ucs, hcl_oow_t ucslen)
|
|
|
|
{
|
|
|
|
hcl_uch_t* ptr;
|
|
|
|
|
2021-01-29 12:39:31 +00:00
|
|
|
ptr = (hcl_uch_t*)hcl_allocmem(hcl, (ucslen + 1) * HCL_SIZEOF(hcl_uch_t));
|
|
|
|
if (HCL_UNLIKELY(!ptr)) return HCL_NULL;
|
2018-02-05 10:43:25 +00:00
|
|
|
|
2018-04-07 15:54:16 +00:00
|
|
|
hcl_copy_uchars (ptr, ucs, ucslen);
|
2018-02-05 10:43:25 +00:00
|
|
|
ptr[ucslen] = '\0';
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
hcl_bch_t* hcl_dupbchars (hcl_t* hcl, const hcl_bch_t* bcs, hcl_oow_t bcslen)
|
|
|
|
{
|
|
|
|
hcl_bch_t* ptr;
|
|
|
|
|
2021-01-29 12:39:31 +00:00
|
|
|
ptr = (hcl_bch_t*)hcl_allocmem(hcl, (bcslen + 1) * HCL_SIZEOF(hcl_bch_t));
|
|
|
|
if (HCL_UNLIKELY(!ptr)) return HCL_NULL;
|
2018-02-05 10:43:25 +00:00
|
|
|
|
2018-04-07 15:54:16 +00:00
|
|
|
hcl_copy_bchars (ptr, bcs, bcslen);
|
2018-02-05 10:43:25 +00:00
|
|
|
ptr[bcslen] = '\0';
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2021-03-28 18:12:07 +00:00
|
|
|
hcl_uch_t* hcl_dupucstr (hcl_t* hcl, const hcl_uch_t* ucs, hcl_oow_t* ucslen)
|
|
|
|
{
|
|
|
|
hcl_oow_t len;
|
|
|
|
hcl_uch_t* ptr;
|
|
|
|
|
|
|
|
len = hcl_count_ucstr(ucs);
|
|
|
|
ptr = hcl_dupuchars(hcl, ucs, len);
|
|
|
|
if (HCL_UNLIKELY(!ptr)) return HCL_NULL;
|
|
|
|
|
|
|
|
if (ucslen) *ucslen = len;
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
hcl_bch_t* hcl_dupbcstr (hcl_t* hcl, const hcl_bch_t* bcs, hcl_oow_t* bcslen)
|
|
|
|
{
|
|
|
|
hcl_oow_t len;
|
|
|
|
hcl_bch_t* ptr;
|
|
|
|
|
|
|
|
len = hcl_count_bcstr(bcs);
|
|
|
|
ptr = hcl_dupbchars(hcl, bcs, len);
|
|
|
|
if (HCL_UNLIKELY(!ptr)) return HCL_NULL;
|
|
|
|
|
|
|
|
if (bcslen) *bcslen = len;
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2020-09-03 06:21:01 +00:00
|
|
|
/* ----------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
void hcl_add_ntime (hcl_ntime_t* z, const hcl_ntime_t* x, const hcl_ntime_t* y)
|
|
|
|
{
|
|
|
|
hcl_ntime_sec_t xs, ys;
|
|
|
|
hcl_ntime_nsec_t ns;
|
|
|
|
|
|
|
|
/*HCL_ASSERT (x->nsec >= 0 && x->nsec < HCL_NSECS_PER_SEC);
|
|
|
|
HCL_ASSERT (y->nsec >= 0 && y->nsec < HCL_NSECS_PER_SEC);*/
|
|
|
|
|
|
|
|
ns = x->nsec + y->nsec;
|
|
|
|
if (ns >= HCL_NSECS_PER_SEC)
|
|
|
|
{
|
|
|
|
ns = ns - HCL_NSECS_PER_SEC;
|
|
|
|
if (x->sec == HCL_TYPE_MAX(hcl_ntime_sec_t))
|
|
|
|
{
|
|
|
|
if (y->sec >= 0) goto overflow;
|
|
|
|
xs = x->sec;
|
|
|
|
ys = y->sec + 1; /* this won't overflow */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
xs = x->sec + 1; /* this won't overflow */
|
|
|
|
ys = y->sec;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
xs = x->sec;
|
|
|
|
ys = y->sec;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((ys >= 1 && xs > HCL_TYPE_MAX(hcl_ntime_sec_t) - ys) ||
|
|
|
|
(ys <= -1 && xs < HCL_TYPE_MIN(hcl_ntime_sec_t) - ys))
|
|
|
|
{
|
|
|
|
if (xs >= 0)
|
|
|
|
{
|
|
|
|
overflow:
|
|
|
|
xs = HCL_TYPE_MAX(hcl_ntime_sec_t);
|
|
|
|
ns = HCL_NSECS_PER_SEC - 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
xs = HCL_TYPE_MIN(hcl_ntime_sec_t);
|
|
|
|
ns = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
xs = xs + ys;
|
|
|
|
}
|
|
|
|
|
|
|
|
z->sec = xs;
|
|
|
|
z->nsec = ns;
|
|
|
|
}
|
|
|
|
|
|
|
|
void hcl_sub_ntime (hcl_ntime_t* z, const hcl_ntime_t* x, const hcl_ntime_t* y)
|
|
|
|
{
|
|
|
|
hcl_ntime_sec_t xs, ys;
|
|
|
|
hcl_ntime_nsec_t ns;
|
|
|
|
|
|
|
|
/*HCL_ASSERT (x->nsec >= 0 && x->nsec < HCL_NSECS_PER_SEC);
|
|
|
|
HCL_ASSERT (y->nsec >= 0 && y->nsec < HCL_NSECS_PER_SEC);*/
|
|
|
|
|
|
|
|
ns = x->nsec - y->nsec;
|
|
|
|
if (ns < 0)
|
|
|
|
{
|
|
|
|
ns = ns + HCL_NSECS_PER_SEC;
|
|
|
|
if (x->sec == HCL_TYPE_MIN(hcl_ntime_sec_t))
|
|
|
|
{
|
|
|
|
if (y->sec <= 0) goto underflow;
|
|
|
|
xs = x->sec;
|
|
|
|
ys = y->sec - 1; /* this won't underflow */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
xs = x->sec - 1; /* this won't underflow */
|
|
|
|
ys = y->sec;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
xs = x->sec;
|
|
|
|
ys = y->sec;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((ys >= 1 && xs < HCL_TYPE_MIN(hcl_ntime_sec_t) + ys) ||
|
|
|
|
(ys <= -1 && xs > HCL_TYPE_MAX(hcl_ntime_sec_t) + ys))
|
|
|
|
{
|
|
|
|
if (xs >= 0)
|
|
|
|
{
|
|
|
|
xs = HCL_TYPE_MAX(hcl_ntime_sec_t);
|
|
|
|
ns = HCL_NSECS_PER_SEC - 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
underflow:
|
|
|
|
xs = HCL_TYPE_MIN(hcl_ntime_sec_t);
|
|
|
|
ns = 0;
|
|
|
|
}
|
2021-03-28 18:12:07 +00:00
|
|
|
}
|
2020-09-03 06:21:01 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
xs = xs - ys;
|
|
|
|
}
|
|
|
|
|
|
|
|
z->sec = xs;
|
|
|
|
z->nsec = ns;
|
|
|
|
}
|
|
|
|
|