Line | Source | Count |
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 | #include <config.h> | - |
29 | | - |
30 | #include "ftoastr.h" | - |
31 | | - |
32 | #include <float.h> | - |
33 | #include <stdio.h> | - |
34 | #include <stdlib.h> | - |
35 | | - |
36 | #if LENGTH == 3 | - |
37 | # define FLOAT long double | - |
38 | # define FLOAT_DIG LDBL_DIG | - |
39 | # define FLOAT_MIN LDBL_MIN | - |
40 | # define FLOAT_PREC_BOUND _GL_LDBL_PREC_BOUND | - |
41 | # define FTOASTR ldtoastr | - |
42 | # define PROMOTED_FLOAT long double | - |
43 | # if HAVE_C99_STRTOLD | - |
44 | # define STRTOF strtold | - |
45 | # endif | - |
46 | #elif LENGTH == 2 | - |
47 | # define FLOAT double | - |
48 | # define FLOAT_DIG DBL_DIG | - |
49 | # define FLOAT_MIN DBL_MIN | - |
50 | # define FLOAT_PREC_BOUND _GL_DBL_PREC_BOUND | - |
51 | # define FTOASTR dtoastr | - |
52 | # define PROMOTED_FLOAT double | - |
53 | #else | - |
54 | # define LENGTH 1 | - |
55 | # define FLOAT float | - |
56 | # define FLOAT_DIG FLT_DIG | - |
57 | # define FLOAT_MIN FLT_MIN | - |
58 | # define FLOAT_PREC_BOUND _GL_FLT_PREC_BOUND | - |
59 | # define FTOASTR ftoastr | - |
60 | # define PROMOTED_FLOAT double | - |
61 | # if HAVE_STRTOF | - |
62 | # define STRTOF strtof | - |
63 | # endif | - |
64 | #endif | - |
65 | | - |
66 | | - |
67 | | - |
68 | | - |
69 | #ifndef STRTOF | - |
70 | # define STRTOF strtod | - |
71 | #endif | - |
72 | | - |
73 | | - |
74 | | - |
75 | | - |
76 | #if ! GNULIB_SNPRINTF | - |
77 | # undef snprintf | - |
78 | # define snprintf ftoastr_snprintf | - |
79 | static int | - |
80 | ftoastr_snprintf (char *buf, size_t bufsize, char const *format, | - |
81 | int width, int prec, FLOAT x) | - |
82 | { | - |
83 | PROMOTED_FLOAT promoted_x = x; | - |
84 | char width_0_buffer[LENGTH == 1 ? FLT_BUFSIZE_BOUND | - |
85 | : LENGTH == 2 ? DBL_BUFSIZE_BOUND | - |
86 | : LDBL_BUFSIZE_BOUND]; | - |
87 | int n = width; | - |
88 | if (bufsize < sizeof width_0_buffer) | - |
89 | { | - |
90 | n = sprintf (width_0_buffer, format, 0, prec, promoted_x); | - |
91 | if (n < 0) | - |
92 | return n; | - |
93 | if (n < width) | - |
94 | n = width; | - |
95 | } | - |
96 | if (n < bufsize) | - |
97 | n = sprintf (buf, format, width, prec, promoted_x); | - |
98 | return n; | - |
99 | } | - |
100 | #endif | - |
101 | | - |
102 | int | - |
103 | FTOASTR (char *buf, size_t bufsize, int flags, int width, FLOAT x) | - |
104 | { | - |
105 | | - |
106 | | - |
107 | | - |
108 | | - |
109 | | - |
110 | | - |
111 | | - |
112 | | - |
113 | PROMOTED_FLOAT promoted_x = x; | - |
114 | char format[sizeof "%-+ 0*.*Lg"]; | - |
115 | FLOAT abs_x = x < 0 ? -x : x;TRUE | evaluated 6 times by 1 test | FALSE | evaluated 1076 times by 2 tests |
| 6-1076 |
116 | int prec; | - |
117 | | - |
118 | char *p = format; | - |
119 | *p++ = '%'; | - |
120 | | - |
121 | | - |
122 | *p = '-'; p += (flags & FTOASTR_LEFT_JUSTIFY ) != 0; | - |
123 | *p = '+'; p += (flags & FTOASTR_ALWAYS_SIGNED ) != 0; | - |
124 | *p = ' '; p += (flags & FTOASTR_SPACE_POSITIVE) != 0; | - |
125 | *p = '0'; p += (flags & FTOASTR_ZERO_PAD ) != 0; | - |
126 | | - |
127 | *p++ = '*'; | - |
128 | *p++ = '.'; | - |
129 | *p++ = '*'; | - |
130 | *p = 'L'; p += 2 < LENGTH; | - |
131 | *p++ = flags & FTOASTR_UPPER_E ? 'G' : 'g';TRUE | never evaluated | FALSE | evaluated 1082 times by 2 tests |
| 0-1082 |
132 | *p = '\0'; | - |
133 | | - |
134 | for (prec = abs_x < FLOAT_MIN ? 1 : FLOAT_DIG; ; prec++) | - |
135 | { | - |
136 | int n = snprintf (buf, bufsize, format, width, prec, promoted_x); | - |
137 | if (n < 0TRUE | never evaluated | FALSE | evaluated 2790 times by 2 tests |
| 0-2790 |
138 | || FLOAT_PREC_BOUND <= precTRUE | evaluated 357 times by 2 tests | FALSE | evaluated 2433 times by 2 tests |
| 357-2433 |
139 | || (n < bufsize && STRTOF (buf, NULL) == x))TRUE | evaluated 2433 times by 2 tests | FALSE | never evaluated |
TRUE | evaluated 725 times by 2 tests | FALSE | evaluated 1708 times by 2 tests |
| 0-2433 |
140 | return n;executed 1082 times by 2 tests: return n; | 1082 |
141 | }executed 1708 times by 2 tests: end of block | 1708 |
142 | } never executed: end of block | 0 |
| | |