Skip to content
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

WString: unify numeric conversion and fix assignments #8526

Merged
merged 9 commits into from
Apr 5, 2022
214 changes: 129 additions & 85 deletions cores/esp8266/WString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,111 +32,169 @@
#define STR(x) __STRHELPER(x) // stringifier

/*********************************************/
/* Constructors */
/* Conversion helpers */
/*********************************************/

String::String(const char *cstr) {
init();
if (cstr)
copy(cstr, strlen_P(cstr));
}

String::String(const String &value) {
init();
*this = value;
}
static String toString(unsigned char value, unsigned char base) {
String out;

String::String(const __FlashStringHelper *pstr) {
init();
*this = pstr; // see operator =
}
char buf[1 + 8 * sizeof(unsigned char)];
out = utoa(value, buf, base);

String::String(String &&rval) noexcept {
init();
move(rval);
return out;
}

String::String(unsigned char value, unsigned char base) {
init();
char buf[1 + 8 * sizeof(unsigned char)];
utoa(value, buf, base);
*this = buf;
}
static String toString(int value, unsigned char base) {
String out;

String::String(int value, unsigned char base) {
init();
char buf[2 + 8 * sizeof(int)];
if (base == 10) {
sprintf(buf, "%d", value);
out.concat(buf, sprintf(buf, "%d", value));
} else {
itoa(value, buf, base);
out = itoa(value, buf, base);
}
*this = buf;

return out;
}

String::String(unsigned int value, unsigned char base) {
init();
static String toString(unsigned int value, unsigned char base) {
String out;

char buf[1 + 8 * sizeof(unsigned int)];
utoa(value, buf, base);
*this = buf;
out = utoa(value, buf, base);

return out;
}

String::String(long value, unsigned char base) {
init();
static String toString(long value, unsigned char base) {
String out;

char buf[2 + 8 * sizeof(long)];
if (base == 10) {
sprintf(buf, "%ld", value);
out.concat(buf, sprintf(buf, "%ld", value));
} else {
ltoa(value, buf, base);
out = ltoa(value, buf, base);
}
*this = buf;

return out;
}

String::String(unsigned long value, unsigned char base) {
init();
static String toString(unsigned long value, unsigned char base) {
String out;

char buf[1 + 8 * sizeof(unsigned long)];
ultoa(value, buf, base);
*this = buf;
out = ultoa(value, buf, base);

return out;
}

String::String(long long value) {
init();
// TODO: {u,}lltoa don't guarantee that the buffer is usable directly, one should always use the returned pointer

static String toString(long long value, unsigned char base) {
String out;

char buf[2 + 8 * sizeof(long long)];
sprintf(buf, "%lld", value);
*this = buf;
if (base == 10) {
out.concat(buf, sprintf(buf, "%lld", value));
} else {
out = lltoa(value, buf, sizeof(buf), base);
}

return out;
}

String::String(unsigned long long value) {
init();
static String toString(unsigned long long value, unsigned char base) {
String out;

char buf[1 + 8 * sizeof(unsigned long long)];
sprintf(buf, "%llu", value);
*this = buf;
if (base == 10) {
out.concat(buf, sprintf(buf, "%llu", value));
} else {
out = ulltoa(value, buf, sizeof(buf), base);
}

return out;
}

String::String(long long value, unsigned char base) {
static String toString(float value, unsigned char decimalPlaces) {
String out;

char buf[33];
out = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);

return out;
}

static String toString(double value, unsigned char decimalPlaces) {
String out;

char buf[33];
out = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);

return out;
}

/*********************************************/
/* Constructors */
/*********************************************/

String::String(const char *cstr) {
init();
char buf[2 + 8 * sizeof(long long)];
*this = lltoa(value, buf, sizeof(buf), base);
if (cstr)
copy(cstr, strlen_P(cstr));
}

String::String(unsigned long long value, unsigned char base) {
String::String(const String &value) {
init();
char buf[1 + 8 * sizeof(unsigned long long)];
*this = ulltoa(value, buf, sizeof(buf), base);
*this = value;
}

String::String(float value, unsigned char decimalPlaces) {
String::String(const __FlashStringHelper *pstr) {
init();
char buf[33];
*this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
*this = pstr; // see operator =
}

String::String(double value, unsigned char decimalPlaces) {
String::String(String &&rval) noexcept {
init();
char buf[33];
*this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
move(rval);
}

String::String(unsigned char value, unsigned char base) :
String(toString(value, base))
{}

String::String(int value, unsigned char base) :
String(toString(value, base))
{}

String::String(unsigned int value, unsigned char base) :
String(toString(value, base))
{}

String::String(long value, unsigned char base) :
String(toString(value, base))
{}

String::String(unsigned long value, unsigned char base) :
String(toString(value, base))
{}

String::String(long long value, unsigned char base) :
String(toString(value, base))
{}

String::String(unsigned long long value, unsigned char base) :
String(toString(value, base))
{}

String::String(float value, unsigned char decimalPlaces) :
String(toString(value, decimalPlaces))
{}

String::String(double value, unsigned char decimalPlaces) :
String(toString(value, decimalPlaces))
{}

/*********************************************/
/* Memory Management */
/*********************************************/
Expand Down Expand Up @@ -279,7 +337,6 @@ String &String::operator =(char c) {
return *this;
}


/*********************************************/
/* concat */
/*********************************************/
Expand Down Expand Up @@ -329,52 +386,39 @@ 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));
return concat(String(num));
}

bool String::concat(int num) {
char buf[2 + 3 * sizeof(int)];
return concat(buf, sprintf(buf, "%d", num));
return concat(String(num));
}

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

bool String::concat(long num) {
char buf[2 + 3 * sizeof(long)];
return concat(buf, sprintf(buf, "%ld", num));
return concat(String(num));
}

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

bool String::concat(long long num) {
char buf[2 + 3 * sizeof(long long)];
return concat(buf, sprintf(buf, "%lld", num));
return concat(String(num));
}

bool String::concat(unsigned long long num) {
char buf[1 + 3 * sizeof(unsigned long long)];
return concat(buf, sprintf(buf, "%llu", num));
return concat(String(num));
}

bool String::concat(float num) {
char buf[20];
char *string = dtostrf(num, 4, 2, buf);
return concat(string, strlen(string));
return concat(String(num));
}

bool String::concat(double num) {
char buf[20];
char *string = dtostrf(num, 4, 2, buf);
return concat(string, strlen(string));
return concat(String(num));
}

bool String::concat(const __FlashStringHelper *str) {
Expand Down
Loading