Skip to content

Commit

Permalink
Make VPrint function always available
Browse files Browse the repository at this point in the history
  • Loading branch information
mrkn committed Nov 13, 2022
1 parent 14f3d96 commit 5391f7e
Showing 1 changed file with 86 additions and 91 deletions.
177 changes: 86 additions & 91 deletions ext/bigdecimal/bigdecimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
*/

/* #define BIGDECIMAL_DEBUG 1 */
#ifdef BIGDECIMAL_DEBUG
# define BIGDECIMAL_ENABLE_VPRINT 1
#endif

#include "bigdecimal.h"
#include "ruby/util.h"

Expand Down Expand Up @@ -198,10 +196,7 @@ static VALUE VpCheckGetValue(Real *p);
static void VpInternalRound(Real *c, size_t ixDigit, DECDIG vPrev, DECDIG v);
static int VpLimitRound(Real *c, size_t ixDigit);
static Real *VpCopy(Real *pv, Real const* const x);

#ifdef BIGDECIMAL_ENABLE_VPRINT
static int VPrint(FILE *fp,const char *cntl_chr,Real *a);
#endif

/*
* **** BigDecimal part ****
Expand Down Expand Up @@ -4501,6 +4496,8 @@ Init_bigdecimal(void)
id_to_r = rb_intern_const("to_r");
id_eq = rb_intern_const("==");
id_half = rb_intern_const("half");

(void)VPrint; /* suppress unused warning */
}

/*
Expand Down Expand Up @@ -6272,7 +6269,6 @@ VpComp(Real *a, Real *b)
* Note: % must not appear more than once
* a ... VP variable to be printed
*/
#ifdef BIGDECIMAL_ENABLE_VPRINT
static int
VPrint(FILE *fp, const char *cntl_chr, Real *a)
{
Expand All @@ -6285,95 +6281,94 @@ VPrint(FILE *fp, const char *cntl_chr, Real *a)
/* nc : number of characters printed */
ZeroSup = 1; /* Flag not to print the leading zeros as 0.00xxxxEnn */
while (*(cntl_chr + j)) {
if (*(cntl_chr + j) == '%' && *(cntl_chr + j + 1) != '%') {
nc = 0;
if (VpIsNaN(a)) {
fprintf(fp, SZ_NaN);
nc += 8;
}
else if (VpIsPosInf(a)) {
fprintf(fp, SZ_INF);
nc += 8;
}
else if (VpIsNegInf(a)) {
fprintf(fp, SZ_NINF);
nc += 9;
}
else if (!VpIsZero(a)) {
if (BIGDECIMAL_NEGATIVE_P(a)) {
fprintf(fp, "-");
++nc;
}
nc += fprintf(fp, "0.");
switch (*(cntl_chr + j + 1)) {
default:
break;
if (*(cntl_chr + j) == '%' && *(cntl_chr + j + 1) != '%') {
nc = 0;
if (VpIsNaN(a)) {
fprintf(fp, SZ_NaN);
nc += 8;
}
else if (VpIsPosInf(a)) {
fprintf(fp, SZ_INF);
nc += 8;
}
else if (VpIsNegInf(a)) {
fprintf(fp, SZ_NINF);
nc += 9;
}
else if (!VpIsZero(a)) {
if (BIGDECIMAL_NEGATIVE_P(a)) {
fprintf(fp, "-");
++nc;
}
nc += fprintf(fp, "0.");
switch (*(cntl_chr + j + 1)) {
default:
break;

case '0': case 'z':
ZeroSup = 0;
++j;
sep = cntl_chr[j] == 'z' ? BIGDECIMAL_COMPONENT_FIGURES : 10;
break;
}
for (i = 0; i < a->Prec; ++i) {
m = BASE1;
e = a->frac[i];
while (m) {
nn = e / m;
if (!ZeroSup || nn) {
nc += fprintf(fp, "%lu", (unsigned long)nn); /* The leading zero(s) */
/* as 0.00xx will not */
/* be printed. */
++nd;
ZeroSup = 0; /* Set to print succeeding zeros */
}
if (nd >= sep) { /* print ' ' after every 10 digits */
nd = 0;
nc += fprintf(fp, " ");
}
e = e - nn * m;
m /= 10;
}
}
nc += fprintf(fp, "E%"PRIdSIZE, VpExponent10(a));
nc += fprintf(fp, " (%"PRIdVALUE", %lu, %lu)", a->exponent, a->Prec, a->MaxPrec);
}
else {
nc += fprintf(fp, "0.0");
}
}
else {
++nc;
if (*(cntl_chr + j) == '\\') {
switch (*(cntl_chr + j + 1)) {
case 'n':
fprintf(fp, "\n");
++j;
break;
case 't':
fprintf(fp, "\t");
++j;
break;
case 'b':
fprintf(fp, "\n");
++j;
break;
default:
fprintf(fp, "%c", *(cntl_chr + j));
break;
}
}
else {
fprintf(fp, "%c", *(cntl_chr + j));
if (*(cntl_chr + j) == '%') ++j;
}
}
j++;
case '0': case 'z':
ZeroSup = 0;
++j;
sep = cntl_chr[j] == 'z' ? BIGDECIMAL_COMPONENT_FIGURES : 10;
break;
}
for (i = 0; i < a->Prec; ++i) {
m = BASE1;
e = a->frac[i];
while (m) {
nn = e / m;
if (!ZeroSup || nn) {
nc += fprintf(fp, "%lu", (unsigned long)nn); /* The leading zero(s) */
/* as 0.00xx will not */
/* be printed. */
++nd;
ZeroSup = 0; /* Set to print succeeding zeros */
}
if (nd >= sep) { /* print ' ' after every 10 digits */
nd = 0;
nc += fprintf(fp, " ");
}
e = e - nn * m;
m /= 10;
}
}
nc += fprintf(fp, "E%"PRIdSIZE, VpExponent10(a));
nc += fprintf(fp, " (%"PRIdVALUE", %lu, %lu)", a->exponent, a->Prec, a->MaxPrec);
}
else {
nc += fprintf(fp, "0.0");
}
}
else {
++nc;
if (*(cntl_chr + j) == '\\') {
switch (*(cntl_chr + j + 1)) {
case 'n':
fprintf(fp, "\n");
++j;
break;
case 't':
fprintf(fp, "\t");
++j;
break;
case 'b':
fprintf(fp, "\n");
++j;
break;
default:
fprintf(fp, "%c", *(cntl_chr + j));
break;
}
}
else {
fprintf(fp, "%c", *(cntl_chr + j));
if (*(cntl_chr + j) == '%') ++j;
}
}
j++;
}

return (int)nc;
}
#endif

static void
VpFormatSt(char *psz, size_t fFmt)
Expand Down

0 comments on commit 5391f7e

Please sign in to comment.