1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-04 18:03:20 +03:00

Add atof implementation

Fix #27
This commit is contained in:
Ivan Grokhotkov 2015-04-03 00:55:41 +03:00
parent f6eb95558c
commit e6f3a59a52
2 changed files with 48 additions and 6 deletions

View File

@ -702,12 +702,12 @@ void ICACHE_FLASH_ATTR String::trim(void)
long ICACHE_FLASH_ATTR String::toInt(void) const
{
if (buffer) return atol_internal(buffer);
if (buffer) return atol(buffer);
return 0;
}
float ICACHE_FLASH_ATTR String::toFloat(void) const
{
if (buffer) return float(atof_internal(buffer));
if (buffer) return atof(buffer);
return 0;
}

View File

@ -26,7 +26,7 @@
#define sprintf ets_sprintf
#define strcpy ets_strcpy
long atol_internal(const char* s)
long atol(const char* s)
{
int result = 0;
int i;
@ -44,10 +44,52 @@ long atol_internal(const char* s)
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;
return result;
double result = 0;
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;
}