Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 196558e

Browse filesBrowse files
committed
[core]remove dtostrnf and use ESP32 version of dtostrf
1 parent eefae9b commit 196558e
Copy full SHA for 196558e

File tree

Expand file treeCollapse file tree

8 files changed

+110
-514
lines changed
Filter options
Expand file treeCollapse file tree

8 files changed

+110
-514
lines changed

‎core/WString.cpp

Copy file name to clipboardExpand all lines: core/WString.cpp
+6-6Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ String::String(float value, unsigned char decimalPlaces)
128128
char *buf = (char *)rt_malloc(FLOAT_BUF_SIZE);
129129
init();
130130
decimalPlaces = min(decimalPlaces, FLT_MAX_DECIMAL_PLACES);
131-
*this = dtostrnf(value, (decimalPlaces + 2U), decimalPlaces, buf, FLOAT_BUF_SIZE);
131+
*this = dtostrf(value, (decimalPlaces + 2U), decimalPlaces, buf);
132132
rt_free(buf);
133133
}
134134

@@ -138,7 +138,7 @@ String::String(double value, unsigned char decimalPlaces)
138138
char *buf = (char *)rt_malloc(DOUBLE_BUF_SIZE);
139139
init();
140140
decimalPlaces = min(decimalPlaces, DBL_MAX_DECIMAL_PLACES);
141-
*this = dtostrnf(value, (decimalPlaces + 2U), decimalPlaces, buf, DOUBLE_BUF_SIZE);
141+
*this = dtostrf(value, (decimalPlaces + 2U), decimalPlaces, buf);
142142
rt_free(buf);
143143
}
144144

@@ -330,15 +330,15 @@ bool String::concat(unsigned long num)
330330

331331
bool String::concat(float num)
332332
{
333-
char buf[20];
334-
char* string = dtostrnf(num, 4, 2, buf, sizeof(buf));
333+
char buf[20] = {0};
334+
char* string = dtostrf(num, 4, 2, buf);
335335
return concat(string);
336336
}
337337

338338
bool String::concat(double num)
339339
{
340-
char buf[20];
341-
char* string = dtostrnf(num, 4, 2, buf, sizeof(buf));
340+
char buf[20] = {0};
341+
char* string = dtostrf(num, 4, 2, buf);
342342
return concat(string);
343343
}
344344

‎core/avr/avr_stdlib.c

Copy file name to clipboardExpand all lines: core/avr/avr_stdlib.c
+78-14Lines changed: 78 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,85 @@
1313

1414
#include "stdlib.h"
1515
#include <rtthread.h>
16+
#include <math.h>
17+
18+
// port from ESP32's version
19+
char *dtostrf(double number, signed char width, unsigned char prec, char *s) {
20+
rt_bool_t negative = RT_FALSE;
21+
22+
if (isnan(number)) {
23+
rt_strcpy(s, "nan");
24+
return s;
25+
}
26+
if (isinf(number)) {
27+
rt_strcpy(s, "inf");
28+
return s;
29+
}
30+
31+
char *out = s;
32+
33+
int fillme = width; // how many cells to fill for the integer part
34+
if (prec > 0) {
35+
fillme -= (prec + 1);
36+
}
37+
38+
// Handle negative numbers
39+
if (number < 0.0) {
40+
negative = RT_TRUE;
41+
fillme--;
42+
number = -number;
43+
}
44+
45+
// Round correctly so that print(1.999, 2) prints as "2.00"
46+
// I optimized out most of the divisions
47+
double rounding = 2.0;
48+
for (unsigned int i = 0; i < prec; ++i) {
49+
rounding *= 10.0;
50+
}
51+
rounding = 1.0 / rounding;
52+
53+
number += rounding;
54+
55+
// Figure out how big our number really is
56+
double tenpow = 1.0;
57+
unsigned int digitcount = 1;
58+
while (number >= 10.0 * tenpow) {
59+
tenpow *= 10.0;
60+
digitcount++;
61+
}
62+
63+
number /= tenpow;
64+
fillme -= digitcount;
65+
66+
// Pad unused cells with spaces
67+
while (fillme-- > 0) {
68+
*out++ = ' ';
69+
}
70+
71+
// Handle negative sign
72+
if (negative) {
73+
*out++ = '-';
74+
}
75+
76+
// Print the digits, and if necessary, the decimal point
77+
digitcount += prec;
78+
int8_t digit = 0;
79+
while (digitcount-- > 0) {
80+
digit = (int8_t)number;
81+
if (digit > 9) {
82+
digit = 9; // insurance
83+
}
84+
*out++ = (char)('0' | digit);
85+
if ((digitcount == prec) && (prec > 0)) {
86+
*out++ = '.';
87+
}
88+
number -= digit;
89+
number *= 10.0;
90+
}
1691

17-
char *dtostrf(double val, signed char width, unsigned char prec, char *sout)
18-
{
19-
char fmt[20] = {0};
20-
rt_sprintf(fmt, "%%%d.%df", width, prec);
21-
rt_sprintf(sout, fmt, val);
22-
return sout;
23-
}
24-
25-
char *dtostrnf(double val, signed char width, unsigned char prec, char *sout, size_t sout_size)
26-
{
27-
char fmt[20] = {0};
28-
rt_snprintf(fmt, sizeof(fmt), "%%%d.%df", width, prec);
29-
rt_snprintf(sout, sout_size, fmt, val);
30-
return sout;
92+
// make sure the string is terminated
93+
*out = 0;
94+
return s;
3195
}
3296

3397
#if RT_VER_NUM < 0x40101

‎core/avr/stdlib.h

Copy file name to clipboardExpand all lines: core/avr/stdlib.h
-1Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ char* ultoa(unsigned long value, char *string, int radix);
4545
The dtostrf() function returns the pointer to the converted string \c s.
4646
*/
4747
char *dtostrf(double val, signed char width, unsigned char prec, char *sout);
48-
char *dtostrnf(double val, signed char width, unsigned char prec, char *sout, size_t sout_size);
4948

5049
#ifdef __cplusplus
5150
} /* extern "C" { */

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.