diff --git a/cores/esp32/WString.cpp b/cores/esp32/WString.cpp index f31024ea8ba..4c8a5ebcf1b 100644 --- a/cores/esp32/WString.cpp +++ b/cores/esp32/WString.cpp @@ -21,7 +21,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -#include +#include "Arduino.h" #include "WString.h" #include "stdlib_noniso.h" #include "esp32-hal-log.h" @@ -80,11 +80,7 @@ String::String(unsigned char value, unsigned char base) { String::String(int value, unsigned char base) { init(); char buf[2 + 8 * sizeof(int)]; - if (base == 10) { - sprintf(buf, "%d", value); - } else { - itoa(value, buf, base); - } + itoa(value, buf, base); *this = buf; } @@ -98,11 +94,7 @@ String::String(unsigned int value, unsigned char base) { String::String(long value, unsigned char base) { init(); char buf[2 + 8 * sizeof(long)]; - if (base==10) { - sprintf(buf, "%ld", value); - } else { - ltoa(value, buf, base); - } + ltoa(value, buf, base); *this = buf; } @@ -140,11 +132,7 @@ String::String(double value, unsigned int decimalPlaces) { String::String(long long value, unsigned char base) { init(); char buf[2 + 8 * sizeof(long long)]; - if (base==10) { - sprintf(buf, "%lld", value); // NOT SURE - NewLib Nano ... does it support %lld? - } else { - lltoa(value, buf, base); - } + lltoa(value, buf, base); *this = buf; } @@ -159,9 +147,9 @@ String::~String() { invalidate(); } -// /*********************************************/ -// /* Memory Management */ -// /*********************************************/ +/*********************************************/ +/* Memory Management */ +/*********************************************/ inline void String::init(void) { setSSO(false); @@ -222,8 +210,7 @@ unsigned char String::changeBuffer(unsigned int maxStrLen) { // Copy the SSO buffer into allocated space memmove(newbuffer, sso.buff, sizeof(sso.buff)); } - if (newSize > oldSize) - { + if (newSize > oldSize) { memset(newbuffer + oldSize, 0, newSize - oldSize); } setSSO(false); @@ -235,9 +222,9 @@ unsigned char String::changeBuffer(unsigned int maxStrLen) { return 0; } -// /*********************************************/ -// /* Copy and Move */ -// /*********************************************/ +/*********************************************/ +/* Copy and Move */ +/*********************************************/ String & String::copy(const char *cstr, unsigned int length) { if(!reserve(length)) { @@ -293,12 +280,10 @@ void String::move(String &rhs) { String & String::operator =(const String &rhs) { if(this == &rhs) return *this; - if(rhs.buffer()) copy(rhs.buffer(), rhs.len()); else invalidate(); - return *this; } @@ -321,7 +306,6 @@ String & String::operator =(const char *cstr) { copy(cstr, strlen(cstr)); else invalidate(); - return *this; } @@ -334,9 +318,9 @@ String & String::operator =(const __FlashStringHelper *pstr) { return *this; } -// /*********************************************/ -// /* concat */ -// /*********************************************/ +/*********************************************/ +/* concat */ +/*********************************************/ unsigned char String::concat(const String &s) { // Special case if we're concatting ourself (s += s;) since we may end up @@ -389,12 +373,14 @@ unsigned char String::concat(char c) { unsigned char String::concat(unsigned char num) { char buf[1 + 3 * sizeof(unsigned char)]; - return concat(buf, sprintf(buf, "%d", num)); + utoa(num, buf, 10); + return concat(buf, strlen(buf)); } unsigned char String::concat(int num) { char buf[2 + 3 * sizeof(int)]; - return concat(buf, sprintf(buf, "%d", num)); + itoa(num, buf, 10); + return concat(buf, strlen(buf)); } unsigned char String::concat(unsigned int num) { @@ -405,7 +391,8 @@ unsigned char String::concat(unsigned int num) { unsigned char String::concat(long num) { char buf[2 + 3 * sizeof(long)]; - return concat(buf, sprintf(buf, "%ld", num)); + ltoa(num, buf, 10); + return concat(buf, strlen(buf)); } unsigned char String::concat(unsigned long num) { @@ -414,6 +401,18 @@ unsigned char String::concat(unsigned long num) { return concat(buf, strlen(buf)); } +unsigned char String::concat(long long num) { + char buf[2 + 3 * sizeof(long long)]; + lltoa(num, buf, 10); + return concat(buf, strlen(buf)); +} + +unsigned char String::concat(unsigned long long num) { + char buf[1 + 3 * sizeof(unsigned long long)]; + ulltoa(num, buf, 10); + return concat(buf, strlen(buf)); +} + unsigned char String::concat(float num) { char buf[20]; char* string = dtostrf(num, 4, 2, buf); @@ -426,17 +425,6 @@ unsigned char String::concat(double num) { return concat(string, strlen(string)); } -unsigned char String::concat(long long num) { - char buf[2 + 3 * sizeof(long long)]; - return concat(buf, sprintf(buf, "%lld", num)); // NOT SURE - NewLib Nano ... does it support %lld? -} - -unsigned char String::concat(unsigned long long num) { - char buf[1 + 3 * sizeof(unsigned long long)]; - ulltoa(num, buf, 10); - return concat(buf, strlen(buf)); -} - unsigned char String::concat(const __FlashStringHelper * str) { if (!str) return 0; int length = strlen_P((PGM_P)str); @@ -544,9 +532,9 @@ StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHel return a; } -// /*********************************************/ -// /* Comparison */ -// /*********************************************/ +/*********************************************/ +/* Comparison */ +/*********************************************/ int String::compareTo(const String &s) const { if(!buffer() || !s.buffer()) { @@ -648,9 +636,9 @@ unsigned char String::endsWith(const String &s2) const { return strcmp(&buffer()[len() - s2.len()], s2.buffer()) == 0; } -// /*********************************************/ -// /* Character Access */ -// /*********************************************/ +/*********************************************/ +/* Character Access */ +/*********************************************/ char String::charAt(unsigned int loc) const { return operator[](loc); @@ -690,9 +678,9 @@ void String::getBytes(unsigned char *buf, unsigned int bufsize, unsigned int ind buf[n] = 0; } -// /*********************************************/ -// /* Search */ -// /*********************************************/ +/*********************************************/ +/* Search */ +/*********************************************/ int String::indexOf(char c) const { return indexOf(c, 0); @@ -701,7 +689,7 @@ int String::indexOf(char c) const { int String::indexOf(char ch, unsigned int fromIndex) const { if(fromIndex >= len()) return -1; - const char* temp = strchr(buffer() + fromIndex, ch); + const char *temp = strchr(buffer() + fromIndex, ch); if(temp == NULL) return -1; return temp - buffer(); @@ -771,9 +759,9 @@ String String::substring(unsigned int left, unsigned int right) const { return out; } -// /*********************************************/ -// /* Modification */ -// /*********************************************/ +/*********************************************/ +/* Modification */ +/*********************************************/ void String::replace(char find, char replace) { if(!buffer()) @@ -784,7 +772,7 @@ void String::replace(char find, char replace) { } } -void String::replace(const String& find, const String& replace) { +void String::replace(const String &find, const String &replace) { if(len() == 0 || find.len() == 0) return; int diff = replace.len() - find.len(); @@ -890,9 +878,9 @@ void String::trim(void) { wbuffer()[newlen] = 0; } -// /*********************************************/ -// /* Parsing / Conversion */ -// /*********************************************/ +/*********************************************/ +/* Parsing / Conversion */ +/*********************************************/ long String::toInt(void) const { if (buffer()) @@ -906,8 +894,7 @@ float String::toFloat(void) const { return 0; } -double String::toDouble(void) const -{ +double String::toDouble(void) const { if (buffer()) return atof(buffer()); return 0.0;