Skip to content

Commit 4c0d504

Browse files
committed
ext/bcmath: coding style: use indentation
And add braces to block statements, as the current code was pretty much unreadable with how inconsistent it was.
1 parent 02e8c16 commit 4c0d504

File tree

22 files changed

+1267
-1287
lines changed

22 files changed

+1267
-1287
lines changed

ext/bcmath/libbcmath/src/add.c

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -44,40 +44,37 @@
4444

4545
void bc_add (bc_num n1, bc_num n2, bc_num *result, int scale_min)
4646
{
47-
bc_num sum = NULL;
48-
int cmp_res;
49-
int res_scale;
47+
bc_num sum = NULL;
48+
int cmp_res;
49+
int res_scale;
5050

51-
if (n1->n_sign == n2->n_sign)
52-
{
53-
sum = _bc_do_add (n1, n2, scale_min);
54-
sum->n_sign = n1->n_sign;
55-
}
56-
else
57-
{
58-
/* subtraction must be done. */
59-
cmp_res = _bc_do_compare (n1, n2, FALSE, FALSE); /* Compare magnitudes. */
60-
switch (cmp_res)
61-
{
62-
case -1:
63-
/* n1 is less than n2, subtract n1 from n2. */
64-
sum = _bc_do_sub (n2, n1, scale_min);
65-
sum->n_sign = n2->n_sign;
66-
break;
67-
case 0:
68-
/* They are equal! return zero with the correct scale! */
69-
res_scale = MAX (scale_min, MAX(n1->n_scale, n2->n_scale));
70-
sum = bc_new_num (1, res_scale);
71-
memset (sum->n_value, 0, res_scale+1);
72-
break;
73-
case 1:
74-
/* n2 is less than n1, subtract n2 from n1. */
75-
sum = _bc_do_sub (n1, n2, scale_min);
76-
sum->n_sign = n1->n_sign;
51+
if (n1->n_sign == n2->n_sign) {
52+
sum = _bc_do_add (n1, n2, scale_min);
53+
sum->n_sign = n1->n_sign;
54+
} else {
55+
/* subtraction must be done. */
56+
/* Compare magnitudes. */
57+
cmp_res = _bc_do_compare(n1, n2, FALSE, FALSE);
58+
switch (cmp_res) {
59+
case -1:
60+
/* n1 is less than n2, subtract n1 from n2. */
61+
sum = _bc_do_sub (n2, n1, scale_min);
62+
sum->n_sign = n2->n_sign;
63+
break;
64+
case 0:
65+
/* They are equal! return zero with the correct scale! */
66+
res_scale = MAX (scale_min, MAX(n1->n_scale, n2->n_scale));
67+
sum = bc_new_num (1, res_scale);
68+
memset (sum->n_value, 0, res_scale+1);
69+
break;
70+
case 1:
71+
/* n2 is less than n1, subtract n2 from n1. */
72+
sum = _bc_do_sub (n1, n2, scale_min);
73+
sum->n_sign = n1->n_sign;
74+
}
7775
}
78-
}
7976

80-
/* Clean up and return. */
81-
bc_free_num (result);
82-
*result = sum;
77+
/* Clean up and return. */
78+
bc_free_num (result);
79+
*result = sum;
8380
}

ext/bcmath/libbcmath/src/bcmath.h

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,18 @@ typedef enum {PLUS, MINUS} sign;
3636

3737
typedef struct bc_struct *bc_num;
3838

39-
typedef struct bc_struct
40-
{
41-
sign n_sign;
42-
int n_len; /* The number of digits before the decimal point. */
43-
int n_scale; /* The number of digits after the decimal point. */
44-
int n_refs; /* The number of pointers to this number. */
45-
char *n_ptr; /* The pointer to the actual storage.
46-
If NULL, n_value points to the inside of
47-
another number (bc_multiply...) and should
48-
not be "freed." */
49-
char *n_value; /* The number. Not zero char terminated.
50-
May not point to the same place as n_ptr as
51-
in the case of leading zeros generated. */
52-
} bc_struct;
39+
typedef struct bc_struct {
40+
sign n_sign;
41+
int n_len; /* The number of digits before the decimal point. */
42+
int n_scale; /* The number of digits after the decimal point. */
43+
int n_refs; /* The number of pointers to this number. */
44+
char *n_ptr; /* The pointer to the actual storage.
45+
If NULL, n_value points to the inside of another number
46+
(bc_multiply...) and should not be "freed." */
47+
char *n_value; /* The number. Not zero char terminated.
48+
May not point to the same place as n_ptr as
49+
in the case of leading zeros generated. */
50+
} bc_struct;
5351

5452
#ifdef HAVE_CONFIG_H
5553
#include "config.h"

ext/bcmath/libbcmath/src/compare.c

Lines changed: 87 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -45,110 +45,104 @@
4545

4646
int _bc_do_compare(bc_num n1, bc_num n2, bool use_sign, bool ignore_last)
4747
{
48-
char *n1ptr, *n2ptr;
49-
int count;
50-
51-
/* First, compare signs. */
52-
if (use_sign && n1->n_sign != n2->n_sign)
53-
{
54-
if (n1->n_sign == PLUS)
55-
return (1); /* Positive N1 > Negative N2 */
56-
else
57-
return (-1); /* Negative N1 < Positive N1 */
58-
}
59-
60-
/* Now compare the magnitude. */
61-
if (n1->n_len != n2->n_len)
62-
{
63-
if (n1->n_len > n2->n_len)
64-
{
65-
/* Magnitude of n1 > n2. */
66-
if (!use_sign || n1->n_sign == PLUS)
67-
return (1);
68-
else
69-
return (-1);
48+
char *n1ptr, *n2ptr;
49+
int count;
50+
51+
/* First, compare signs. */
52+
if (use_sign && n1->n_sign != n2->n_sign) {
53+
if (n1->n_sign == PLUS) {
54+
return (1); /* Positive N1 > Negative N2 */
55+
} else {
56+
return (-1); /* Negative N1 < Positive N1 */
57+
}
7058
}
71-
else
72-
{
73-
/* Magnitude of n1 < n2. */
74-
if (!use_sign || n1->n_sign == PLUS)
75-
return (-1);
76-
else
77-
return (1);
59+
60+
/* Now compare the magnitude. */
61+
if (n1->n_len != n2->n_len) {
62+
if (n1->n_len > n2->n_len) {
63+
/* Magnitude of n1 > n2. */
64+
if (!use_sign || n1->n_sign == PLUS) {
65+
return (1);
66+
} else {
67+
return (-1);
68+
}
69+
} else {
70+
/* Magnitude of n1 < n2. */
71+
if (!use_sign || n1->n_sign == PLUS) {
72+
return (-1);
73+
} else {
74+
return (1);
75+
}
76+
}
7877
}
79-
}
80-
81-
/* If we get here, they have the same number of integer digits.
82-
check the integer part and the equal length part of the fraction. */
83-
count = n1->n_len + MIN (n1->n_scale, n2->n_scale);
84-
n1ptr = n1->n_value;
85-
n2ptr = n2->n_value;
86-
87-
while ((count > 0) && (*n1ptr == *n2ptr))
88-
{
89-
n1ptr++;
90-
n2ptr++;
91-
count--;
92-
}
93-
if (ignore_last && count == 1 && n1->n_scale == n2->n_scale)
94-
return (0);
95-
if (count != 0)
96-
{
97-
if (*n1ptr > *n2ptr)
98-
{
99-
/* Magnitude of n1 > n2. */
100-
if (!use_sign || n1->n_sign == PLUS)
101-
return (1);
102-
else
103-
return (-1);
78+
79+
/* If we get here, they have the same number of integer digits.
80+
check the integer part and the equal length part of the fraction. */
81+
count = n1->n_len + MIN (n1->n_scale, n2->n_scale);
82+
n1ptr = n1->n_value;
83+
n2ptr = n2->n_value;
84+
85+
while ((count > 0) && (*n1ptr == *n2ptr)) {
86+
n1ptr++;
87+
n2ptr++;
88+
count--;
10489
}
105-
else
106-
{
107-
/* Magnitude of n1 < n2. */
108-
if (!use_sign || n1->n_sign == PLUS)
109-
return (-1);
110-
else
111-
return (1);
90+
91+
if (ignore_last && count == 1 && n1->n_scale == n2->n_scale) {
92+
return (0);
11293
}
113-
}
114-
115-
/* They are equal up to the last part of the equal part of the fraction. */
116-
if (n1->n_scale != n2->n_scale)
117-
{
118-
if (n1->n_scale > n2->n_scale)
119-
{
120-
for (count = n1->n_scale-n2->n_scale; count>0; count--)
121-
if (*n1ptr++ != 0)
122-
{
123-
/* Magnitude of n1 > n2. */
124-
if (!use_sign || n1->n_sign == PLUS)
125-
return (1);
126-
else
127-
return (-1);
128-
}
94+
if (count != 0) {
95+
if (*n1ptr > *n2ptr) {
96+
/* Magnitude of n1 > n2. */
97+
if (!use_sign || n1->n_sign == PLUS) {
98+
return (1);
99+
} else {
100+
return (-1);
101+
}
102+
} else {
103+
/* Magnitude of n1 < n2. */
104+
if (!use_sign || n1->n_sign == PLUS) {
105+
return (-1);
106+
} else {
107+
return (1);
108+
}
109+
}
129110
}
130-
else
131-
{
132-
for (count = n2->n_scale-n1->n_scale; count>0; count--)
133-
if (*n2ptr++ != 0)
134-
{
135-
/* Magnitude of n1 < n2. */
136-
if (!use_sign || n1->n_sign == PLUS)
137-
return (-1);
138-
else
139-
return (1);
140-
}
111+
112+
/* They are equal up to the last part of the equal part of the fraction. */
113+
if (n1->n_scale != n2->n_scale) {
114+
if (n1->n_scale > n2->n_scale) {
115+
for (count = n1->n_scale-n2->n_scale; count>0; count--) {
116+
if (*n1ptr++ != 0) {
117+
/* Magnitude of n1 > n2. */
118+
if (!use_sign || n1->n_sign == PLUS) {
119+
return (1);
120+
} else {
121+
return (-1);
122+
}
123+
}
124+
}
125+
}
126+
} else {
127+
for (count = n2->n_scale-n1->n_scale; count>0; count--) {
128+
if (*n2ptr++ != 0) {
129+
/* Magnitude of n1 < n2. */
130+
if (!use_sign || n1->n_sign == PLUS) {
131+
return (-1);
132+
} else {
133+
return (1);
134+
}
135+
}
136+
}
141137
}
142-
}
143138

144-
/* They must be equal! */
145-
return (0);
139+
/* They must be equal! */
140+
return (0);
146141
}
147142

148143

149144
/* This is the "user callable" routine to compare numbers N1 and N2. */
150-
151145
int bc_compare(bc_num n1, bc_num n2)
152146
{
153-
return _bc_do_compare (n1, n2, true, false);
147+
return _bc_do_compare(n1, n2, true, false);
154148
}

ext/bcmath/libbcmath/src/debug.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,24 @@
4141

4242
static void out_char (char c)
4343
{
44-
putchar(c);
44+
putchar(c);
4545
}
4646

4747

48-
void pn (bc_num num)
48+
void pn(bc_num num)
4949
{
50-
bc_out_num(num, 10, out_char, 0);
51-
out_char ('\n');
50+
bc_out_num(num, 10, out_char, 0);
51+
out_char ('\n');
5252
}
5353

5454

5555
/* pv prints a character array as if it was a string of bcd digits. */
5656
void pv (char *name, unsigned char *num, int len)
5757
{
58-
int i;
59-
printf ("%s=", name);
60-
for (i=0; i<len; i++) printf ("%c",BCD_CHAR(num[i]));
61-
printf ("\n");
58+
int i;
59+
printf("%s=", name);
60+
for (i=0; i<len; i++){
61+
printf ("%c",BCD_CHAR(num[i]));
62+
}
63+
printf("\n");
6264
}

0 commit comments

Comments
 (0)