Skip to content

Fixed String(int64_t) #7765

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 50 additions & 63 deletions cores/esp32/WString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include <Arduino.h>
#include "Arduino.h"
#include "WString.h"
#include "stdlib_noniso.h"
#include "esp32-hal-log.h"
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -159,9 +147,9 @@ String::~String() {
invalidate();
}

// /*********************************************/
// /* Memory Management */
// /*********************************************/
/*********************************************/
/* Memory Management */
/*********************************************/

inline void String::init(void) {
setSSO(false);
Expand Down Expand Up @@ -221,8 +209,7 @@ bool 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);
Expand All @@ -234,9 +221,9 @@ bool String::changeBuffer(unsigned int maxStrLen) {
return false;
}

// /*********************************************/
// /* Copy and Move */
// /*********************************************/
/*********************************************/
/* Copy and Move */
/*********************************************/

String & String::copy(const char *cstr, unsigned int length) {
if(!reserve(length)) {
Expand Down Expand Up @@ -292,12 +279,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;
}

Expand All @@ -320,7 +305,6 @@ String & String::operator =(const char *cstr) {
copy(cstr, strlen(cstr));
else
invalidate();

return *this;
}

Expand All @@ -333,9 +317,9 @@ String & String::operator =(const __FlashStringHelper *pstr) {
return *this;
}

// /*********************************************/
// /* concat */
// /*********************************************/
/*********************************************/
/* concat */
/*********************************************/

bool String::concat(const String &s) {
// Special case if we're concatting ourself (s += s;) since we may end up
Expand Down Expand Up @@ -388,12 +372,14 @@ bool String::concat(char c) {

bool 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));
}

bool 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));
}

bool String::concat(unsigned int num) {
Expand All @@ -404,7 +390,8 @@ bool String::concat(unsigned int num) {

bool 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));
}

bool String::concat(unsigned long num) {
Expand All @@ -413,6 +400,18 @@ bool String::concat(unsigned long num) {
return concat(buf, strlen(buf));
}

bool String::concat(long long num) {
char buf[2 + 3 * sizeof(long long)];
lltoa(num, buf, 10);
return concat(buf, strlen(buf));
}

bool String::concat(unsigned long long num) {
char buf[1 + 3 * sizeof(unsigned long long)];
ulltoa(num, buf, 10);
return concat(buf, strlen(buf));
}

bool String::concat(float num) {
char buf[20];
char* string = dtostrf(num, 4, 2, buf);
Expand All @@ -425,17 +424,6 @@ bool String::concat(double num) {
return concat(string, strlen(string));
}

bool 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?
}

bool String::concat(unsigned long long num) {
char buf[1 + 3 * sizeof(unsigned long long)];
ulltoa(num, buf, 10);
return concat(buf, strlen(buf));
}

bool String::concat(const __FlashStringHelper * str) {
if (!str)
return false;
Expand Down Expand Up @@ -546,9 +534,9 @@ StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHel
return a;
}

// /*********************************************/
// /* Comparison */
// /*********************************************/
/*********************************************/
/* Comparison */
/*********************************************/

int String::compareTo(const String &s) const {
if(!buffer() || !s.buffer()) {
Expand Down Expand Up @@ -650,9 +638,9 @@ bool 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);
Expand Down Expand Up @@ -692,9 +680,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);
Expand All @@ -703,7 +691,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();
Expand Down Expand Up @@ -773,9 +761,9 @@ String String::substring(unsigned int left, unsigned int right) const {
return out;
}

// /*********************************************/
// /* Modification */
// /*********************************************/
/*********************************************/
/* Modification */
/*********************************************/

void String::replace(char find, char replace) {
if(!buffer())
Expand All @@ -786,7 +774,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();
Expand Down Expand Up @@ -892,9 +880,9 @@ void String::trim(void) {
wbuffer()[newlen] = 0;
}

// /*********************************************/
// /* Parsing / Conversion */
// /*********************************************/
/*********************************************/
/* Parsing / Conversion */
/*********************************************/

long String::toInt(void) const {
if (buffer())
Expand All @@ -908,8 +896,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;
Expand Down
15 changes: 8 additions & 7 deletions cores/esp32/WString.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,24 @@
#define String_class_h
#ifdef __cplusplus

#include <pgmspace.h>

#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <ctype.h>
#include <pgmspace.h>
#include <stdint.h>

// An inherited class for holding the result of a concatenation. These
// result objects are assumed to be writable by subsequent concatenations.
class StringSumHelper;

// an abstract class used as a means to proide a unique pointer type
// but really has no body
class __FlashStringHelper;
#define FPSTR(pstr_pointer) (reinterpret_cast<const __FlashStringHelper *>(pstr_pointer))
#define F(string_literal) (FPSTR(PSTR(string_literal)))

// An inherited class for holding the result of a concatenation. These
// result objects are assumed to be writable by subsequent concatenations.
class StringSumHelper;

// The string class
class String {
// use a function pointer to allow for "if (s)" without the
Expand Down Expand Up @@ -107,7 +109,7 @@ class String {
String & operator =(StringSumHelper &&rval);
#endif

// concatenate (works w/ built-in types)
// concatenate (works w/ built-in types, same as assignment)

// returns true on success, false on failure (in which case, the string
// is left unchanged). if the argument is null or invalid, the
Expand Down Expand Up @@ -265,7 +267,6 @@ class String {
String substring(unsigned int beginIndex) const {
return substring(beginIndex, len());
}
;
String substring(unsigned int beginIndex, unsigned int endIndex) const;

// modification
Expand Down