25
25
#include " WString.h"
26
26
#include " stdlib_noniso.h"
27
27
28
+ #include < limits>
29
+
28
30
#define OOM_STRING_BORDER_DISPLAY 10
29
31
#define OOM_STRING_THRESHOLD_REALLOC_WARN 128
30
32
38
40
static String toString (unsigned char value, unsigned char base) {
39
41
String out;
40
42
41
- char buf[1 + 8 * sizeof ( unsigned char ) ];
43
+ char buf[1 + std::numeric_limits< unsigned char >::digits ];
42
44
out = utoa (value, buf, base);
43
45
44
46
return out;
@@ -47,20 +49,16 @@ static String toString(unsigned char value, unsigned char base) {
47
49
static String toString (int value, unsigned char base) {
48
50
String out;
49
51
50
- char buf[2 + 8 * sizeof (int )];
51
- if (base == 10 ) {
52
- out.concat (buf, sprintf (buf, " %d" , value));
53
- } else {
54
- out = itoa (value, buf, base);
55
- }
52
+ char buf[2 + std::numeric_limits<int >::digits];
53
+ out = itoa (value, buf, base);
56
54
57
55
return out;
58
56
}
59
57
60
58
static String toString (unsigned int value, unsigned char base) {
61
59
String out;
62
60
63
- char buf[1 + 8 * sizeof ( unsigned int ) ];
61
+ char buf[1 + std::numeric_limits< unsigned int >::digits ];
64
62
out = utoa (value, buf, base);
65
63
66
64
return out;
@@ -69,20 +67,16 @@ static String toString(unsigned int value, unsigned char base) {
69
67
static String toString (long value, unsigned char base) {
70
68
String out;
71
69
72
- char buf[2 + 8 * sizeof (long )];
73
- if (base == 10 ) {
74
- out.concat (buf, sprintf (buf, " %ld" , value));
75
- } else {
76
- out = ltoa (value, buf, base);
77
- }
70
+ char buf[2 + std::numeric_limits<long >::digits];
71
+ out = ltoa (value, buf, base);
78
72
79
73
return out;
80
74
}
81
75
82
76
static String toString (unsigned long value, unsigned char base) {
83
77
String out;
84
78
85
- char buf[1 + 8 * sizeof ( unsigned long ) ];
79
+ char buf[1 + std::numeric_limits< unsigned long >::digits ];
86
80
out = ultoa (value, buf, base);
87
81
88
82
return out;
@@ -93,30 +87,22 @@ static String toString(unsigned long value, unsigned char base) {
93
87
static String toString (long long value, unsigned char base) {
94
88
String out;
95
89
96
- char buf[2 + 8 * sizeof (long long )];
97
- if (base == 10 ) {
98
- out.concat (buf, sprintf (buf, " %lld" , value));
99
- } else {
100
- out = lltoa (value, buf, sizeof (buf), base);
101
- }
90
+ char buf[2 + std::numeric_limits<long long >::digits];
91
+ out = lltoa (value, buf, sizeof (buf), base);
102
92
103
93
return out;
104
94
}
105
95
106
96
static String toString (unsigned long long value, unsigned char base) {
107
97
String out;
108
98
109
- char buf[1 + 8 * sizeof (unsigned long long )];
110
- if (base == 10 ) {
111
- out.concat (buf, sprintf (buf, " %llu" , value));
112
- } else {
113
- out = ulltoa (value, buf, sizeof (buf), base);
114
- }
99
+ char buf[1 + std::numeric_limits<unsigned long long >::digits];
100
+ out = ulltoa (value, buf, sizeof (buf), base);
115
101
116
102
return out;
117
103
}
118
104
119
- static String toString (float value, unsigned char decimalPlaces) {
105
+ static String toString (double value, unsigned char decimalPlaces) {
120
106
String out;
121
107
122
108
char buf[33 ];
@@ -125,13 +111,8 @@ static String toString(float value, unsigned char decimalPlaces) {
125
111
return out;
126
112
}
127
113
128
- static String toString (double value, unsigned char decimalPlaces) {
129
- String out;
130
-
131
- char buf[33 ];
132
- out = dtostrf (value, (decimalPlaces + 2 ), decimalPlaces, buf);
133
-
134
- return out;
114
+ static String toString (float value, unsigned char decimalPlaces) {
115
+ return toString (static_cast <double >(value), decimalPlaces);
135
116
}
136
117
137
118
/* ********************************************/
0 commit comments