ported the portability fix in bigint.c

This commit is contained in:
2025-08-05 22:35:17 +09:00
parent 9afb8e3898
commit 0da2d2abd3
37 changed files with 1992 additions and 1988 deletions

View File

@ -31,10 +31,10 @@ int moo_log2_for_pow2 (moo_oow_t pow2v)
{
#if defined(MOO_HAVE_UINT32_T) && (MOO_SIZEOF_OOW_T == 4)
static const int debruijn[32] =
static const int debruijn[32] =
{
0, 1, 28, 2, 29, 14, 24, 3,
30, 22, 20, 15, 25, 17, 4, 8,
30, 22, 20, 15, 25, 17, 4, 8,
31, 27, 13, 23, 21, 19, 16, 7,
26, 12, 18, 6, 11, 5, 10, 9
};
@ -43,7 +43,7 @@ int moo_log2_for_pow2 (moo_oow_t pow2v)
#elif defined(MOO_HAVE_UINT64_T) && (MOO_SIZEOF_OOW_T == 8)
static const int debruijn[64] =
static const int debruijn[64] =
{
0, 1, 2, 53, 3, 7, 54, 27,
4, 38, 41, 8, 34, 55, 48, 28,
@ -126,7 +126,7 @@ int moo_comp_uchars (const moo_uch_t* str1, moo_oow_t len1, const moo_uch_t* str
while (str1 < end1)
{
c1 = *str1;
if (str2 < end2)
if (str2 < end2)
{
c2 = *str2;
if (c1 > c2) return 1;
@ -148,7 +148,7 @@ int moo_comp_bchars (const moo_bch_t* str1, moo_oow_t len1, const moo_bch_t* str
while (str1 < end1)
{
c1 = *str1;
if (str2 < end2)
if (str2 < end2)
{
c2 = *str2;
if (c1 > c2) return 1;
@ -198,10 +198,10 @@ int moo_comp_uchars_ucstr (const moo_uch_t* str1, moo_oow_t len, const moo_uch_t
{
/* for "abc\0" of length 4 vs "abc", the fourth character
* of the first string is equal to the terminating null of
* the second string. the first string is still considered
* the second string. the first string is still considered
* bigger */
const moo_uch_t* end = str1 + len;
while (str1 < end && *str2 != '\0')
while (str1 < end && *str2 != '\0')
{
if (*str1 != *str2) return ((moo_uchu_t)*str1 > (moo_uchu_t)*str2)? 1: -1;
str1++; str2++;
@ -212,7 +212,7 @@ int moo_comp_uchars_ucstr (const moo_uch_t* str1, moo_oow_t len, const moo_uch_t
int moo_comp_uchars_bcstr (const moo_uch_t* str1, moo_oow_t len, const moo_bch_t* str2)
{
const moo_uch_t* end = str1 + len;
while (str1 < end && *str2 != '\0')
while (str1 < end && *str2 != '\0')
{
if (*str1 != *str2) return ((moo_uchu_t)*str1 > (moo_bchu_t)*str2)? 1: -1;
str1++; str2++;
@ -223,7 +223,7 @@ int moo_comp_uchars_bcstr (const moo_uch_t* str1, moo_oow_t len, const moo_bch_t
int moo_comp_bchars_bcstr (const moo_bch_t* str1, moo_oow_t len, const moo_bch_t* str2)
{
const moo_bch_t* end = str1 + len;
while (str1 < end && *str2 != '\0')
while (str1 < end && *str2 != '\0')
{
if (*str1 != *str2) return ((moo_bchu_t)*str1 > (moo_bchu_t)*str2)? 1: -1;
str1++; str2++;
@ -234,7 +234,7 @@ int moo_comp_bchars_bcstr (const moo_bch_t* str1, moo_oow_t len, const moo_bch_t
int moo_comp_bchars_ucstr (const moo_bch_t* str1, moo_oow_t len, const moo_uch_t* str2)
{
const moo_bch_t* end = str1 + len;
while (str1 < end && *str2 != '\0')
while (str1 < end && *str2 != '\0')
{
if (*str1 != *str2) return ((moo_bchu_t)*str1 > (moo_uchu_t)*str2)? 1: -1;
str1++; str2++;
@ -560,18 +560,18 @@ moo_oow_t moo_byte_to_ucstr (moo_oob_t byte, moo_uch_t* buf, moo_oow_t size, int
radix_char = (flagged_radix & MOO_BYTE_TO_UCSTR_LOWERCASE)? 'a': 'A';
if (radix < 2 || radix > 36 || size <= 0) return 0;
do
do
{
moo_oob_t digit = byte % radix;
moo_oob_t digit = byte % radix;
if (digit < 10) *p++ = digit + '0';
else *p++ = digit + radix_char - 10;
byte /= radix;
}
while (byte > 0);
if (fill != '\0')
if (fill != '\0')
{
while (size - 1 > p - tmp)
while (size - 1 > p - tmp)
{
*bp++ = fill;
size--;
@ -594,18 +594,18 @@ moo_oow_t moo_byte_to_bcstr (moo_oob_t byte, moo_bch_t* buf, moo_oow_t size, int
radix_char = (flagged_radix & MOO_BYTE_TO_BCSTR_LOWERCASE)? 'a': 'A';
if (radix < 2 || radix > 36 || size <= 0) return 0;
do
do
{
moo_oob_t digit = byte % radix;
moo_oob_t digit = byte % radix;
if (digit < 10) *p++ = digit + '0';
else *p++ = digit + radix_char - 10;
byte /= radix;
}
while (byte > 0);
if (fill != '\0')
if (fill != '\0')
{
while (size - 1 > p - tmp)
while (size - 1 > p - tmp)
{
*bp++ = fill;
size--;
@ -629,7 +629,7 @@ MOO_INLINE int moo_conv_bchars_to_uchars_with_cmgr (
if (ucs)
{
/* destination buffer is specified.
/* destination buffer is specified.
* copy the conversion result to the buffer */
moo_uch_t* q, * qend;
@ -693,7 +693,7 @@ MOO_INLINE int moo_conv_bchars_to_uchars_with_cmgr (
/* 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
* a buffer with the size and call this function again with
* a buffer with the size and call this function again with
* the buffer. */
moo_uch_t w;
@ -769,13 +769,13 @@ MOO_INLINE int moo_conv_uchars_to_bchars_with_cmgr (
{
const moo_uch_t* p = ucs;
const moo_uch_t* end = ucs + *ucslen;
int ret = 0;
int ret = 0;
if (bcs)
{
moo_oow_t rem = *bcslen;
while (p < end)
while (p < end)
{
moo_oow_t n;
@ -786,12 +786,12 @@ MOO_INLINE int moo_conv_uchars_to_bchars_with_cmgr (
}
n = cmgr->uctobc(*p, bcs, rem);
if (n == 0)
if (n == 0)
{
ret = -1;
break; /* illegal character */
}
if (n > rem)
if (n > rem)
{
ret = -2; /* buffer too small */
break;
@ -799,7 +799,7 @@ MOO_INLINE int moo_conv_uchars_to_bchars_with_cmgr (
bcs += n; rem -= n; p++;
}
*bcslen -= rem;
*bcslen -= rem;
}
else
{
@ -811,7 +811,7 @@ MOO_INLINE int moo_conv_uchars_to_bchars_with_cmgr (
moo_oow_t n;
n = cmgr->uctobc(*p, bcsbuf, MOO_COUNTOF(bcsbuf));
if (n == 0)
if (n == 0)
{
ret = -1;
break; /* illegal character */
@ -823,7 +823,7 @@ MOO_INLINE int moo_conv_uchars_to_bchars_with_cmgr (
p++; mlen += n;
}
/* this length excludes the terminating null character.
/* this length excludes the terminating null character.
* this function doesn't even null-terminate the result. */
*bcslen = mlen;
}
@ -852,14 +852,14 @@ MOO_INLINE int moo_conv_ucstr_to_bcstr_with_cmgr (
ret = -2;
break;
}
n = cmgr->uctobc(*p, bcs, rem);
if (n == 0)
if (n == 0)
{
ret = -1;
break; /* illegal character */
}
if (n > rem)
if (n > rem)
{
ret = -2;
break; /* buffer too small */
@ -870,13 +870,13 @@ MOO_INLINE int moo_conv_ucstr_to_bcstr_with_cmgr (
/* update bcslen to the length of the bcs string converted excluding
* terminating null */
*bcslen -= rem;
*bcslen -= rem;
/* null-terminate the multibyte sequence if it has sufficient space */
if (rem > 0) *bcs = '\0';
else
else
{
/* if ret is -2 and cs[cslen] == '\0',
/* if ret is -2 and cs[cslen] == '\0',
* this means that the bcs buffer was lacking one
* slot for the terminating null */
ret = -2; /* buffer too small */
@ -892,7 +892,7 @@ MOO_INLINE int moo_conv_ucstr_to_bcstr_with_cmgr (
moo_oow_t n;
n = cmgr->uctobc(*p, bcsbuf, MOO_COUNTOF(bcsbuf));
if (n == 0)
if (n == 0)
{
ret = -1;
break; /* illegal character */
@ -904,7 +904,7 @@ MOO_INLINE int moo_conv_ucstr_to_bcstr_with_cmgr (
p++; mlen += n;
}
/* this length holds the number of resulting multi-byte characters
/* this length holds the number of resulting multi-byte characters
* excluding the terminating null character */
*bcslen = mlen;
}
@ -1101,7 +1101,7 @@ moo_uch_t* moo_dupbtoucharswithheadroom (moo_t* moo, moo_oow_t headroom_bytes, c
moo_uch_t* ptr;
inlen = bcslen;
if (moo_convbtouchars(moo, bcs, &inlen, MOO_NULL, &outlen) <= -1)
if (moo_convbtouchars(moo, bcs, &inlen, MOO_NULL, &outlen) <= -1)
{
/* note it's also an error if no full conversion is made in this function */
return MOO_NULL;
@ -1115,10 +1115,10 @@ moo_uch_t* moo_dupbtoucharswithheadroom (moo_t* moo, moo_oow_t headroom_bytes, c
ptr = (moo_uch_t*)((moo_oob_t*)ptr + headroom_bytes);
moo_convbtouchars (moo, bcs, &inlen, ptr, &outlen);
/* moo_convbtouchars() doesn't null-terminate the target.
/* moo_convbtouchars() doesn't null-terminate the target.
* but in moo_dupbtouchars(), i allocate space. so i don't mind
* null-terminating it with 1 extra character overhead */
ptr[outlen] = '\0';
ptr[outlen] = '\0';
if (ucslen) *ucslen = outlen;
return ptr;
}
@ -1129,7 +1129,7 @@ moo_bch_t* moo_duputobcharswithheadroom (moo_t* moo, moo_oow_t headroom_bytes, c
moo_bch_t* ptr;
inlen = ucslen;
if (moo_convutobchars(moo, ucs, &inlen, MOO_NULL, &outlen) <= -1)
if (moo_convutobchars(moo, ucs, &inlen, MOO_NULL, &outlen) <= -1)
{
/* note it's also an error if no full conversion is made in this function */
return MOO_NULL;
@ -1154,7 +1154,7 @@ moo_uch_t* moo_dupbtoucstrwithheadroom (moo_t* moo, moo_oow_t headroom_bytes, co
moo_oow_t inlen, outlen;
moo_uch_t* ptr;
if (moo_convbtoucstr(moo, bcs, &inlen, MOO_NULL, &outlen) <= -1)
if (moo_convbtoucstr(moo, bcs, &inlen, MOO_NULL, &outlen) <= -1)
{
/* note it's also an error if no full conversion is made in this function */
return MOO_NULL;
@ -1174,7 +1174,7 @@ moo_bch_t* moo_duputobcstrwithheadroom (moo_t* moo, moo_oow_t headroom_bytes, co
moo_oow_t inlen, outlen;
moo_bch_t* ptr;
if (moo_convutobcstr(moo, ucs, &inlen, MOO_NULL, &outlen) <= -1)
if (moo_convutobcstr(moo, ucs, &inlen, MOO_NULL, &outlen) <= -1)
{
/* note it's also an error if no full conversion is made in this function */
return MOO_NULL;
@ -1262,7 +1262,7 @@ static MOO_INLINE int secure_space_in_sbuf (moo_t* moo, moo_oow_t req, moo_sbuf_
newcapa = p->len + req + 1;
newcapa = MOO_ALIGN_POW2(newcapa, 512); /* TODO: adjust this capacity */
tmp = (moo_ooch_t*)moo_reallocmem(moo, p->ptr, newcapa * MOO_SIZEOF(*tmp));
tmp = (moo_ooch_t*)moo_reallocmem(moo, p->ptr, newcapa * MOO_SIZEOF(*tmp));
if (!tmp) return -1;
p->ptr = tmp;
@ -1416,7 +1416,7 @@ void moo_sub_ntime (moo_ntime_t* z, const moo_ntime_t* x, const moo_ntime_t* y)
xs = MOO_TYPE_MIN(moo_ntime_sec_t);
ns = 0;
}
}
}
else
{
xs = xs - ys;