Skip to content

Commit d1f55de

Browse files
mrknmatzbot
authored andcommitted
[ruby/bigdecimal] Tweak VpAlloc
* Stop reusing mx and mf * Check szVal == NULL first * Treat special values before checking the leading `#` ruby/bigdecimal@14f3d965f8
1 parent bbb9f72 commit d1f55de

File tree

1 file changed

+32
-32
lines changed

1 file changed

+32
-32
lines changed

ext/bigdecimal/bigdecimal.c

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5054,11 +5054,11 @@ bigdecimal_parse_special_string(const char *str)
50545054
/*
50555055
* Allocates variable.
50565056
* [Input]
5057-
* mx ... allocation unit, if zero then mx is determined by szVal.
5058-
* The mx is the number of effective digits can to be stored.
5059-
* szVal ... value assigned(char). If szVal==NULL,then zero is assumed.
5060-
* If szVal[0]=='#' then Max. Prec. will not be considered(1.1.7),
5061-
* full precision specified by szVal is allocated.
5057+
* mx ... The number of decimal digits to be allocated, if zero then mx is determined by szVal.
5058+
* The mx will be the number of significant digits can to be stored.
5059+
* szVal ... The value assigned(char). If szVal==NULL, then zero is assumed.
5060+
* If szVal[0]=='#' then MaxPrec is not affected by the precision limit
5061+
* so that the full precision specified by szVal is allocated.
50625062
*
50635063
* [Returns]
50645064
* Pointer to the newly allocated variable, or
@@ -5069,48 +5069,48 @@ VpAlloc(size_t mx, const char *szVal, int strict_p, int exc)
50695069
{
50705070
const char *orig_szVal = szVal;
50715071
size_t i, j, ni, ipf, nf, ipe, ne, dot_seen, exp_seen, nalloc;
5072+
size_t len;
50725073
char v, *psz;
50735074
int sign=1;
50745075
Real *vp = NULL;
5075-
size_t mf = VpGetPrecLimit();
5076+
size_t prec_limit = VpGetPrecLimit();
50765077
VALUE buf;
50775078

5078-
mx = (mx + BASE_FIG - 1) / BASE_FIG; /* Determine allocation unit. */
5079-
if (mx == 0) ++mx;
5079+
len = (mx + BASE_FIG - 1) / BASE_FIG; /* Determine allocation unit. */
5080+
if (len == 0) ++len;
50805081

5081-
if (szVal) {
5082-
/* Skipping leading spaces */
5083-
while (ISSPACE(*szVal)) szVal++;
5084-
5085-
/* Processing the leading one `#` */
5086-
if (*szVal != '#') {
5087-
if (mf) {
5088-
mf = (mf + BASE_FIG - 1) / BASE_FIG + 2; /* Needs 1 more for div */
5089-
if (mx > mf) {
5090-
mx = mf;
5091-
}
5092-
}
5093-
}
5094-
else {
5095-
++szVal;
5096-
}
5097-
}
5098-
else {
5082+
if (szVal == NULL) {
50995083
return_zero:
51005084
/* necessary to be able to store */
51015085
/* at least mx digits. */
51025086
/* szVal==NULL ==> allocate zero value. */
51035087
vp = rbd_allocate_struct(mx);
5104-
vp->MaxPrec = mx; /* set max precision */
5088+
vp->MaxPrec = len; /* set max precision */
51055089
VpSetZero(vp, 1); /* initialize vp to zero. */
51065090
return vp;
51075091
}
51085092

5093+
/* Skipping leading spaces */
5094+
while (ISSPACE(*szVal)) szVal++;
5095+
51095096
/* Check on Inf & NaN */
51105097
if ((vp = bigdecimal_parse_special_string(szVal)) != NULL) {
51115098
return vp;
51125099
}
51135100

5101+
/* Processing the leading one `#` */
5102+
if (*szVal != '#') {
5103+
if (prec_limit) {
5104+
size_t const max_len = (prec_limit + BASE_FIG - 1) / BASE_FIG + 2; /* Needs 1 more for div */
5105+
if (len > max_len) {
5106+
len = max_len;
5107+
}
5108+
}
5109+
}
5110+
else {
5111+
++szVal;
5112+
}
5113+
51145114
/* Scanning digits */
51155115

51165116
/* A buffer for keeping scanned digits */
@@ -5272,11 +5272,11 @@ VpAlloc(size_t mx, const char *szVal, int strict_p, int exc)
52725272

52735273
nalloc = (ni + nf + BASE_FIG - 1) / BASE_FIG + 1; /* set effective allocation */
52745274
/* units for szVal[] */
5275-
if (mx == 0) mx = 1;
5276-
nalloc = Max(nalloc, mx);
5277-
mx = nalloc;
5278-
vp = rbd_allocate_struct(mx);
5279-
vp->MaxPrec = mx; /* set max precision */
5275+
if (len == 0) len = 1;
5276+
nalloc = Max(nalloc, len);
5277+
len = nalloc;
5278+
vp = rbd_allocate_struct(len);
5279+
vp->MaxPrec = len; /* set max precision */
52805280
VpSetZero(vp, sign);
52815281
VpCtoV(vp, psz, ni, psz + ipf, nf, psz + ipe, ne);
52825282
rb_str_resize(buf, 0);

0 commit comments

Comments
 (0)