-
Notifications
You must be signed in to change notification settings - Fork 26
/
jsond_dtoa.c
119 lines (114 loc) · 3.03 KB
/
jsond_dtoa.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Jakub Zelenka <bukka@php.net> |
+----------------------------------------------------------------------+
*/
#include "php.h"
#include "php_jsond_compat.h"
#include "php_jsond_dtoa.h"
#include <zend_strtod.h>
char *php_jsond_gcvt(double value, int ndigit, char dec_point, char exponent, char *buf)
{
char *digits, *dst, *src;
int i, decpt;
php_jsond_dtoa_sign_t sign;
int mode = ndigit >= 0 ? 2 : 0;
if (mode == 0) {
ndigit = 17;
}
digits = zend_dtoa(value, mode, ndigit, &decpt, &sign, NULL);
if (decpt == 9999) {
/*
* Infinity or NaN, convert to inf or nan with sign.
* We assume the buffer is at least ndigit long.
*/
snprintf(buf, ndigit + 1, "%s%s", (sign && *digits == 'I') ? "-" : "", *digits == 'I' ? "INF" : "NAN");
zend_freedtoa(digits);
return (buf);
}
dst = buf;
if (sign) {
*dst++ = '-';
}
if ((decpt >= 0 && decpt > ndigit) || decpt < -3) { /* use E-style */
/* exponential format (e.g. 1.2345e+13) */
if (--decpt < 0) {
sign = 1;
decpt = -decpt;
} else {
sign = 0;
}
src = digits;
*dst++ = *src++;
*dst++ = dec_point;
if (*src == '\0') {
*dst++ = '0';
} else {
do {
*dst++ = *src++;
} while (*src != '\0');
}
*dst++ = exponent;
if (sign) {
*dst++ = '-';
} else {
*dst++ = '+';
}
if (decpt < 10) {
*dst++ = '0' + decpt;
*dst = '\0';
} else {
/* XXX - optimize */
int n;
for (n = decpt, i = 0; (n /= 10) != 0; i++);
dst[i + 1] = '\0';
while (decpt != 0) {
dst[i--] = '0' + decpt % 10;
decpt /= 10;
}
}
} else if (decpt < 0) {
/* standard format 0. */
*dst++ = '0'; /* zero before decimal point */
*dst++ = dec_point;
do {
*dst++ = '0';
} while (++decpt < 0);
src = digits;
while (*src != '\0') {
*dst++ = *src++;
}
*dst = '\0';
} else {
/* standard format */
for (i = 0, src = digits; i < decpt; i++) {
if (*src != '\0') {
*dst++ = *src++;
} else {
*dst++ = '0';
}
}
if (*src != '\0') {
if (src == digits) {
*dst++ = '0'; /* zero before decimal point */
}
*dst++ = dec_point;
for (i = decpt; digits[i] != '\0'; i++) {
*dst++ = digits[i];
}
}
*dst = '\0';
}
zend_freedtoa(digits);
return (buf);
}