changed some functions to set no error upon failure in bigint.c
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
17cde13cbb
commit
151653aaf4
@ -100,7 +100,6 @@ libhcl_la_LIBADD = $(LIBADD_LIB_COMMON)
|
||||
if ENABLE_STATIC_MODULE
|
||||
libhcl_la_LIBADD += ../mod/libhcl-core.la
|
||||
libhcl_la_LIBADD += ../mod/libhcl-dic.la
|
||||
libhcl_la_LIBADD += ../mod/libhcl-str.la
|
||||
libhcl_la_LIBADD += ../mod/libhcl-sys.la
|
||||
endif
|
||||
|
||||
|
@ -94,7 +94,6 @@ host_triplet = @host@
|
||||
@MACOSX_FALSE@@WIN32_FALSE@am__append_5 = -DHCL_DEFAULT_PFMODPOSTFIX=\".so\"
|
||||
@ENABLE_STATIC_MODULE_TRUE@am__append_6 = ../mod/libhcl-core.la \
|
||||
@ENABLE_STATIC_MODULE_TRUE@ ../mod/libhcl-dic.la \
|
||||
@ENABLE_STATIC_MODULE_TRUE@ ../mod/libhcl-str.la \
|
||||
@ENABLE_STATIC_MODULE_TRUE@ ../mod/libhcl-sys.la
|
||||
@ENABLE_HCLX_TRUE@am__append_7 = libhclx.la
|
||||
@ENABLE_HCLX_TRUE@am__append_8 = hcl-x.h hcl-tmr.h hcl-json.h
|
||||
|
129
lib/bigint.c
129
lib/bigint.c
@ -221,7 +221,7 @@ static int is_normalized_integer (hcl_t* hcl, hcl_oop_t oop)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static HCL_INLINE int bigint_to_oow (hcl_t* hcl, hcl_oop_t num, hcl_oow_t* w)
|
||||
static HCL_INLINE int bigint_to_oow_noseterr (hcl_t* hcl, hcl_oop_t num, hcl_oow_t* w)
|
||||
{
|
||||
HCL_ASSERT (hcl, HCL_IS_BIGINT(hcl,num));
|
||||
|
||||
@ -253,7 +253,7 @@ static HCL_INLINE int bigint_to_oow (hcl_t* hcl, hcl_oop_t num, hcl_oow_t* w)
|
||||
return 0; /* not convertable */
|
||||
}
|
||||
|
||||
static HCL_INLINE int integer_to_oow (hcl_t* hcl, hcl_oop_t x, hcl_oow_t* w)
|
||||
static HCL_INLINE int integer_to_oow_noseterr (hcl_t* hcl, hcl_oop_t x, hcl_oow_t* w)
|
||||
{
|
||||
/* return value
|
||||
* 1 - a positive number including 0 that can fit into hcl_oow_t
|
||||
@ -279,10 +279,10 @@ static HCL_INLINE int integer_to_oow (hcl_t* hcl, hcl_oop_t x, hcl_oow_t* w)
|
||||
}
|
||||
|
||||
HCL_ASSERT (hcl, hcl_isbigint(hcl, x));
|
||||
return bigint_to_oow(hcl, x, w);
|
||||
return bigint_to_oow_noseterr(hcl, x, w);
|
||||
}
|
||||
|
||||
int hcl_inttooow (hcl_t* hcl, hcl_oop_t x, hcl_oow_t* w)
|
||||
int hcl_inttooow_noseterr (hcl_t* hcl, hcl_oop_t x, hcl_oow_t* w)
|
||||
{
|
||||
if (HCL_OOP_IS_SMOOI(x))
|
||||
{
|
||||
@ -301,14 +301,44 @@ int hcl_inttooow (hcl_t* hcl, hcl_oop_t x, hcl_oow_t* w)
|
||||
}
|
||||
}
|
||||
|
||||
if (hcl_isbigint(hcl, x)) return bigint_to_oow(hcl, x, w);
|
||||
/* 0 -> too big, too small, or not an integer */
|
||||
return hcl_isbigint(hcl, x)? bigint_to_oow_noseterr(hcl, x, w): 0;
|
||||
}
|
||||
|
||||
int hcl_inttooow (hcl_t* hcl, hcl_oop_t x, hcl_oow_t* w)
|
||||
{
|
||||
if (HCL_OOP_IS_SMOOI(x))
|
||||
{
|
||||
hcl_ooi_t v;
|
||||
|
||||
v = HCL_OOP_TO_SMOOI(x);
|
||||
if (v < 0)
|
||||
{
|
||||
*w = -v;
|
||||
hcl_seterrnum (hcl, HCL_ERANGE);
|
||||
return -1; /* negative number negated - kind of an error */
|
||||
}
|
||||
else
|
||||
{
|
||||
*w = v;
|
||||
return 1; /* zero or positive number */
|
||||
}
|
||||
}
|
||||
|
||||
if (hcl_isbigint(hcl, x))
|
||||
{
|
||||
int n;
|
||||
if ((n = bigint_to_oow_noseterr(hcl, x, w)) <= 0) hcl_seterrnum (hcl, HCL_ERANGE);
|
||||
return n;
|
||||
}
|
||||
|
||||
hcl_seterrbfmt (hcl, HCL_EINVAL, "parameter not integer - %O", x);
|
||||
return 0; /* not convertable - too big, too small, or not integer */
|
||||
}
|
||||
|
||||
int hcl_inttoooi (hcl_t* hcl, hcl_oop_t x, hcl_ooi_t* i)
|
||||
int hcl_inttoooi_noseterr (hcl_t* hcl, hcl_oop_t x, hcl_ooi_t* i)
|
||||
{
|
||||
#if 0
|
||||
hcl_oow_t w;
|
||||
int n;
|
||||
|
||||
@ -334,9 +364,82 @@ int hcl_inttoooi (hcl_t* hcl, hcl_oop_t x, hcl_ooi_t* i)
|
||||
}
|
||||
|
||||
return n;
|
||||
#else
|
||||
if (HCL_OOP_IS_SMOOI(x))
|
||||
{
|
||||
*i = HCL_OOP_TO_SMOOI(x);
|
||||
return (*i < 0)? -1: 1;
|
||||
}
|
||||
|
||||
if (hcl_isbigint(hcl, x))
|
||||
{
|
||||
hcl_oow_t w;
|
||||
int n;
|
||||
|
||||
n = bigint_to_oow_noseterr(hcl, x, &w);
|
||||
if (n < 0)
|
||||
{
|
||||
HCL_STATIC_ASSERT (HCL_TYPE_MAX(hcl_ooi_t) + HCL_TYPE_MIN(hcl_ooi_t) == -1); /* assume 2's complement */
|
||||
if (w > (hcl_oow_t)HCL_TYPE_MAX(hcl_ooi_t) + 1) return 0; /* too small */
|
||||
*i = -w; /* negate back */
|
||||
}
|
||||
else if (n > 0)
|
||||
{
|
||||
if (w > HCL_TYPE_MAX(hcl_ooi_t)) return 0; /* too big */
|
||||
*i = w;
|
||||
}
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
return 0; /* not integer */
|
||||
#endif
|
||||
}
|
||||
|
||||
int hcl_inttoooi (hcl_t* hcl, hcl_oop_t x, hcl_ooi_t* i)
|
||||
{
|
||||
if (HCL_OOP_IS_SMOOI(x))
|
||||
{
|
||||
*i = HCL_OOP_TO_SMOOI(x);
|
||||
return (*i < 0)? -1: 1;
|
||||
}
|
||||
|
||||
if (hcl_isbigint(hcl, x))
|
||||
{
|
||||
hcl_oow_t w;
|
||||
int n;
|
||||
|
||||
n = bigint_to_oow_noseterr(hcl, x, &w);
|
||||
if (n < 0)
|
||||
{
|
||||
HCL_STATIC_ASSERT (HCL_TYPE_MAX(hcl_ooi_t) + HCL_TYPE_MIN(hcl_ooi_t) == -1); /* assume 2's complement */
|
||||
if (w > (hcl_oow_t)HCL_TYPE_MAX(hcl_ooi_t) + 1)
|
||||
{
|
||||
hcl_seterrnum (hcl, HCL_ERANGE);
|
||||
return 0; /* too small */
|
||||
}
|
||||
*i = -w; /* negate back */
|
||||
}
|
||||
else if (n > 0)
|
||||
{
|
||||
if (w > HCL_TYPE_MAX(hcl_ooi_t))
|
||||
{
|
||||
hcl_seterrnum (hcl, HCL_ERANGE);
|
||||
return 0; /* too big */
|
||||
}
|
||||
*i = w;
|
||||
}
|
||||
else
|
||||
{
|
||||
hcl_seterrnum (hcl, HCL_ERANGE);
|
||||
}
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
hcl_seterrbfmt (hcl, HCL_EINVAL, "not an integer - %O", x);
|
||||
return 0; /* not integer */
|
||||
}
|
||||
|
||||
#if (HCL_SIZEOF_UINTMAX_T == HCL_SIZEOF_OOW_T)
|
||||
|
||||
@ -2941,7 +3044,7 @@ hcl_oop_t hcl_bitatint (hcl_t* hcl, hcl_oop_t x, hcl_oop_t y)
|
||||
|
||||
if (HCL_IS_NBIGINT(hcl, y)) return HCL_SMOOI_TO_OOP(0);
|
||||
|
||||
sign = bigint_to_oow (hcl, y, &w);
|
||||
sign = bigint_to_oow_noseterr(hcl, y, &w);
|
||||
HCL_ASSERT (hcl, sign >= 0);
|
||||
if (sign >= 1)
|
||||
{
|
||||
@ -2959,7 +3062,7 @@ hcl_oop_t hcl_bitatint (hcl_t* hcl, hcl_oop_t x, hcl_oop_t y)
|
||||
hcl_popvolat (hcl);
|
||||
if (!quo) return HCL_NULL;
|
||||
|
||||
sign = integer_to_oow (hcl, quo, &wp);
|
||||
sign = integer_to_oow_noseterr(hcl, quo, &wp);
|
||||
HCL_ASSERT (hcl, sign >= 0);
|
||||
if (sign == 0)
|
||||
{
|
||||
@ -3845,7 +3948,7 @@ static HCL_INLINE hcl_oop_t rshift_negative_bigint_and_normalize (hcl_t* hcl, hc
|
||||
hcl_popvolat (hcl);
|
||||
if (!y) return HCL_NULL;
|
||||
|
||||
sign = integer_to_oow (hcl, y, &shift);
|
||||
sign = integer_to_oow_noseterr(hcl, y, &shift);
|
||||
if (sign == 0) shift = HCL_SMOOI_MAX;
|
||||
else
|
||||
{
|
||||
@ -3933,7 +4036,7 @@ static HCL_INLINE hcl_oop_t rshift_positive_bigint_and_normalize (hcl_t* hcl, hc
|
||||
hcl_popvolat (hcl);
|
||||
if (!y) return HCL_NULL;
|
||||
|
||||
sign = integer_to_oow (hcl, y, &shift);
|
||||
sign = integer_to_oow_noseterr(hcl, y, &shift);
|
||||
if (sign == 0) shift = HCL_SMOOI_MAX;
|
||||
else
|
||||
{
|
||||
@ -3986,7 +4089,7 @@ static HCL_INLINE hcl_oop_t lshift_bigint_and_normalize (hcl_t* hcl, hcl_oop_t x
|
||||
hcl_popvolat (hcl);
|
||||
if (!y) return HCL_NULL;
|
||||
|
||||
sign = integer_to_oow (hcl, y, &shift);
|
||||
sign = integer_to_oow_noseterr(hcl, y, &shift);
|
||||
if (sign == 0) shift = HCL_SMOOI_MAX;
|
||||
else
|
||||
{
|
||||
@ -4147,7 +4250,7 @@ hcl_oop_t hcl_bitshiftint (hcl_t* hcl, hcl_oop_t x, hcl_oop_t y)
|
||||
negx = HCL_IS_NBIGINT(hcl, x);
|
||||
negy = HCL_IS_NBIGINT(hcl, y);
|
||||
|
||||
sign = bigint_to_oow (hcl, y, &shift);
|
||||
sign = bigint_to_oow_noseterr(hcl, y, &shift);
|
||||
if (sign == 0)
|
||||
{
|
||||
/* y is too big or too small */
|
||||
@ -4776,7 +4879,7 @@ hcl_oop_t hcl_inttostr (hcl_t* hcl, hcl_oop_t num, int flagged_radix)
|
||||
HCL_ASSERT (hcl, radix >= 2 && radix <= 36);
|
||||
|
||||
if (!hcl_isint(hcl,num)) goto oops_einval;
|
||||
v = integer_to_oow(hcl, num, &w);
|
||||
v = integer_to_oow_noseterr(hcl, num, &w);
|
||||
|
||||
if (v)
|
||||
{
|
||||
|
12
lib/hcl.h
12
lib/hcl.h
@ -2961,12 +2961,24 @@ HCL_EXPORT hcl_oop_t hcl_ooitoint (
|
||||
hcl_ooi_t i
|
||||
);
|
||||
|
||||
HCL_EXPORT int hcl_inttooow_noseterr (
|
||||
hcl_t* hcl,
|
||||
hcl_oop_t x,
|
||||
hcl_oow_t* w
|
||||
);
|
||||
|
||||
HCL_EXPORT int hcl_inttooow (
|
||||
hcl_t* hcl,
|
||||
hcl_oop_t x,
|
||||
hcl_oow_t* w
|
||||
);
|
||||
|
||||
HCL_EXPORT int hcl_inttoooi_noseterr (
|
||||
hcl_t* hcl,
|
||||
hcl_oop_t x,
|
||||
hcl_ooi_t* i
|
||||
);
|
||||
|
||||
HCL_EXPORT int hcl_inttoooi (
|
||||
hcl_t* hcl,
|
||||
hcl_oop_t x,
|
||||
|
17
mod/core.c
17
mod/core.c
@ -73,17 +73,18 @@ static hcl_pfrc_t pf_core_basic_at (hcl_t* hcl, hcl_mod_t* mod, hcl_ooi_t nargs)
|
||||
if (!HCL_OOP_IS_POINTER(obj) || !HCL_OBJ_GET_FLAGS_FLEXI(obj))
|
||||
{
|
||||
unindexable:
|
||||
/* the receiver is a special numeric object or a non-indexable object */
|
||||
hcl_seterrbfmt (hcl, HCL_EINVAL, "receiver not indexable - %O", obj);
|
||||
return HCL_PF_FAILURE;
|
||||
}
|
||||
|
||||
if (!HCL_OOP_IS_SMOOI(pos))
|
||||
if (hcl_inttooow_noseterr(hcl, pos, &index) <= 0)
|
||||
{
|
||||
hcl_seterrbfmt (hcl, HCL_EINVAL, "position not numeric - %O", pos);
|
||||
/* negative integer or not integer */
|
||||
hcl_seterrbfmt (hcl, HCL_EINVAL, "position not valid- %O", pos);
|
||||
return HCL_PF_FAILURE;
|
||||
}
|
||||
index = HCL_OOP_TO_SMOOI(pos);
|
||||
if (index < 0 || index >= HCL_OBJ_GET_SIZE(obj))
|
||||
if (index >= HCL_OBJ_GET_SIZE(obj))
|
||||
{
|
||||
hcl_seterrbfmt (hcl, HCL_EINVAL, "position(%zd) out of range - negative or greater than or equal to %zu", index, (hcl_ooi_t)HCL_OBJ_GET_SIZE(obj));
|
||||
return HCL_PF_FAILURE;
|
||||
@ -147,13 +148,13 @@ static hcl_pfrc_t pf_core_basic_at_put (hcl_t* hcl, hcl_mod_t* mod, hcl_ooi_t na
|
||||
return HCL_PF_FAILURE;
|
||||
}
|
||||
|
||||
if (!HCL_OOP_IS_SMOOI(pos))
|
||||
if (hcl_inttooow_noseterr(hcl, pos, &index) <= 0)
|
||||
{
|
||||
hcl_seterrbfmt (hcl, HCL_EINVAL, "position not numeric - %O", pos);
|
||||
/* negative integer or not integer */
|
||||
hcl_seterrbfmt (hcl, HCL_EINVAL, "position not valid- %O", pos);
|
||||
return HCL_PF_FAILURE;
|
||||
}
|
||||
index = HCL_OOP_TO_SMOOI(pos);
|
||||
if (index < 0 || index >= HCL_OBJ_GET_SIZE(obj))
|
||||
if (index >= HCL_OBJ_GET_SIZE(obj))
|
||||
{
|
||||
hcl_seterrbfmt (hcl, HCL_EINVAL, "position(%zd) out of range - negative or greater than or equal to %zu", index, (hcl_ooi_t)HCL_OBJ_GET_SIZE(obj));
|
||||
return HCL_PF_FAILURE;
|
||||
|
Loading…
Reference in New Issue
Block a user