mirror of
https://github.com/esp8266/Arduino.git
synced 2025-06-07 16:23:38 +03:00
parent
f6eb95558c
commit
e6f3a59a52
@ -702,12 +702,12 @@ void ICACHE_FLASH_ATTR String::trim(void)
|
|||||||
|
|
||||||
long ICACHE_FLASH_ATTR String::toInt(void) const
|
long ICACHE_FLASH_ATTR String::toInt(void) const
|
||||||
{
|
{
|
||||||
if (buffer) return atol_internal(buffer);
|
if (buffer) return atol(buffer);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
float ICACHE_FLASH_ATTR String::toFloat(void) const
|
float ICACHE_FLASH_ATTR String::toFloat(void) const
|
||||||
{
|
{
|
||||||
if (buffer) return float(atof_internal(buffer));
|
if (buffer) return atof(buffer);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,7 @@
|
|||||||
#define sprintf ets_sprintf
|
#define sprintf ets_sprintf
|
||||||
#define strcpy ets_strcpy
|
#define strcpy ets_strcpy
|
||||||
|
|
||||||
long atol_internal(const char* s)
|
long atol(const char* s)
|
||||||
{
|
{
|
||||||
int result = 0;
|
int result = 0;
|
||||||
int i;
|
int i;
|
||||||
@ -44,10 +44,52 @@ long atol_internal(const char* s)
|
|||||||
return sign * result;
|
return sign * result;
|
||||||
}
|
}
|
||||||
|
|
||||||
float atof_internal(const char* s)
|
// Source:
|
||||||
|
// https://github.com/anakod/Sming/blob/master/Sming/system/stringconversion.cpp#L93
|
||||||
|
double atof(const char* s)
|
||||||
{
|
{
|
||||||
float result = 0;
|
double result = 0;
|
||||||
return result;
|
double sign = 1;
|
||||||
|
|
||||||
|
while (*s == ' ' || *s == '\t' || *s == '\r' || *s == '\n')
|
||||||
|
++s;
|
||||||
|
|
||||||
|
if (*s == 0)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
if (*s == '-')
|
||||||
|
{
|
||||||
|
sign = -1;
|
||||||
|
++s;
|
||||||
|
}
|
||||||
|
if (*s == '+')
|
||||||
|
{
|
||||||
|
++s;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool decimals = false;
|
||||||
|
double factor = 1.0;
|
||||||
|
char c;
|
||||||
|
while((c = *s))
|
||||||
|
{
|
||||||
|
if (c == '.')
|
||||||
|
{
|
||||||
|
decimals = true;
|
||||||
|
++s;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
int d = c - '0';
|
||||||
|
if (d < 0 || d > 9)
|
||||||
|
break;
|
||||||
|
|
||||||
|
result = 10.0 * result + d;
|
||||||
|
if (decimals)
|
||||||
|
factor *= 0.1;
|
||||||
|
++s;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result * factor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user