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

String: compatibility with 64 bits scalars #7863

Merged
merged 9 commits into from
Feb 13, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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
36 changes: 36 additions & 0 deletions cores/esp8266/WString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,32 @@ String::String(unsigned long value, unsigned char base) {
*this = buf;
}

String::String(long long value) {
init();
char buf[2 + 8 * sizeof(long long)];
sprintf(buf, "%lld", value);
*this = buf;
}

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

String::String(unsigned long long value) {
init();
char buf[1 + 8 * sizeof(unsigned long long)];
sprintf(buf, "%llu", value);
*this = buf;
}

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

String::String(float value, unsigned char decimalPlaces) {
init();
char buf[33];
Expand Down Expand Up @@ -313,6 +339,16 @@ 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)];
return concat(buf, sprintf(buf, "%lld", num));
}

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

d-a-v marked this conversation as resolved.
Show resolved Hide resolved
unsigned char String::concat(float num) {
char buf[20];
char *string = dtostrf(num, 4, 2, buf);
Expand Down
22 changes: 22 additions & 0 deletions cores/esp8266/WString.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ class String {
explicit String(unsigned int, unsigned char base = 10);
explicit String(long, unsigned char base = 10);
explicit String(unsigned long, unsigned char base = 10);
explicit String(long long /* base 10 */);
explicit String(long long, unsigned char base);
explicit String(unsigned long long /* base 10 */);
explicit String(unsigned long long, unsigned char base);
explicit String(float, unsigned char decimalPlaces = 2);
explicit String(double, unsigned char decimalPlaces = 2);
~String() {
Expand Down Expand Up @@ -117,6 +121,8 @@ class String {
unsigned char concat(unsigned int num);
unsigned char concat(long num);
unsigned char concat(unsigned long num);
unsigned char concat(long long num);
unsigned char concat(unsigned long long num);
unsigned char concat(float num);
unsigned char concat(double num);
unsigned char concat(const __FlashStringHelper *str);
Expand Down Expand Up @@ -156,6 +162,14 @@ class String {
concat(num);
return *this;
}
String &operator +=(long long num) {
concat(num);
return *this;
}
String &operator +=(unsigned long long num) {
concat(num);
return *this;
}
String &operator +=(float num) {
concat(num);
return *this;
Expand All @@ -177,6 +191,8 @@ class String {
friend StringSumHelper &operator +(const StringSumHelper &lhs, unsigned int num);
friend StringSumHelper &operator +(const StringSumHelper &lhs, long num);
friend StringSumHelper &operator +(const StringSumHelper &lhs, unsigned long num);
friend StringSumHelper &operator +(const StringSumHelper &lhs, long long num);
friend StringSumHelper &operator +(const StringSumHelper &lhs, unsigned long long num);
friend StringSumHelper &operator +(const StringSumHelper &lhs, float num);
friend StringSumHelper &operator +(const StringSumHelper &lhs, double num);
friend StringSumHelper &operator +(const StringSumHelper &lhs, const __FlashStringHelper *rhs);
Expand Down Expand Up @@ -375,6 +391,12 @@ class StringSumHelper: public String {
StringSumHelper(unsigned long num) :
String(num) {
}
StringSumHelper(long long num) :
String(num) {
}
StringSumHelper(unsigned long long num) :
String(num) {
}
StringSumHelper(float num) :
String(num) {
}
Expand Down
2 changes: 1 addition & 1 deletion cores/esp8266/debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void hexdump(const void *mem, uint32_t len, uint8_t cols)
while (len > 0)
{
uint32_t linesize = cols > len ? len : cols;
os_printf("\n[%p] 0x%04x: ", src, src - (const char*)mem);
os_printf("\n[%p] 0x%04x: ", src, (int)(src - (const char*)mem));
for (uint32_t i = 0; i < linesize; i++)
{
os_printf("%02x ", *(src + i));
Expand Down
64 changes: 64 additions & 0 deletions cores/esp8266/stdlib_noniso.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
stdlib_noniso.h - nonstandard (but usefull) conversion functions

Copyright (c) 2021 David Gauchard. All rights reserved.
This file is part of the esp8266 core for Arduino environment.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/


#include "stdlib_noniso.h"

// fill backwards
char* ulltoa(unsigned long long val, char* str, int slen, unsigned long long radix)
{
str += --slen;
*str = 0;
do
{
auto mod = val % radix;
val /= radix;
*--str = mod + ((mod > 9) ? ('a' - 10) : '0');
} while (--slen && val);
return val? nullptr: str;
}

char* lltoa (long long val, char* str, int slen, long long radix)
{
bool neg;
if (radix < 0)
{
radix = -radix;
val = -val;
}
if (val < 0)
{
val = -val;
neg = true;
}
else
{
neg = false;
}
char* ret = ulltoa(val, str, slen, radix);
if (neg)
{
if (ret == str)
return nullptr;
*--ret = '-';
}
return ret;
}
10 changes: 7 additions & 3 deletions cores/esp8266/stdlib_noniso.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/*
/*
stdlib_noniso.h - nonstandard (but usefull) conversion functions

Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
This file is part of the esp8266 core for Arduino environment.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
Expand Down Expand Up @@ -36,10 +36,14 @@ char* itoa (int val, char *s, int radix);

char* ltoa (long val, char *s, int radix);

char* lltoa (long long val, char* str, int slen, long long radix);

char* utoa (unsigned int val, char *s, int radix);

char* ultoa (unsigned long val, char *s, int radix);


char* ulltoa (unsigned long long val, char* str, int slen, unsigned long long radix);

d-a-v marked this conversation as resolved.
Show resolved Hide resolved
char* dtostrf (double val, signed char width, unsigned char prec, char *s);

void reverse(char* begin, char* end);
Expand Down
1 change: 1 addition & 0 deletions tests/host/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ CORE_CPP_FILES := \
Stream.cpp \
WString.cpp \
Print.cpp \
stdlib_noniso.cpp \
FS.cpp \
spiffs_api.cpp \
MD5Builder.cpp \
Expand Down
10 changes: 10 additions & 0 deletions tests/host/core/test_string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,16 @@ TEST_CASE("String concantenation", "[core][String]")
REQUIRE(str == "abcdeabcde9872147483647-2147483648691969-123321-1.01");
str += (double)1.01;
REQUIRE(str == "abcdeabcde9872147483647-2147483648691969-123321-1.011.01");
str += LLONG_MIN;
REQUIRE(str == "abcdeabcde9872147483647-2147483648691969-123321-1.011.01-9223372036854775808");
str += String(LLONG_MIN, 10);
REQUIRE(str == "abcdeabcde9872147483647-2147483648691969-123321-1.011.01-9223372036854775808-9223372036854775808");
str += LLONG_MAX;
REQUIRE(str == "abcdeabcde9872147483647-2147483648691969-123321-1.011.01-9223372036854775808-92233720368547758089223372036854775807");
str += ULLONG_MAX;
REQUIRE(str == "abcdeabcde9872147483647-2147483648691969-123321-1.011.01-9223372036854775808-9223372036854775808922337203685477580718446744073709551615");
str += String(ULLONG_MAX, 16);
REQUIRE(str == "abcdeabcde9872147483647-2147483648691969-123321-1.011.01-9223372036854775808-9223372036854775808922337203685477580718446744073709551615ffffffffffffffff");
str = "clean";
REQUIRE(str.concat(str) == true);
REQUIRE(str == "cleanclean");
Expand Down