Skip to content

Commit

Permalink
Extends String to print 64-bit integers (#6768)
Browse files Browse the repository at this point in the history
  • Loading branch information
SuGlider authored May 18, 2022
1 parent ba6e82c commit 722c464
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 0 deletions.
43 changes: 43 additions & 0 deletions cores/esp32/WString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,24 @@ 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);
}
*this = buf;
}

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

String::~String() {
invalidate();
}
Expand Down Expand Up @@ -408,6 +426,17 @@ 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);
Expand Down Expand Up @@ -493,6 +522,20 @@ StringSumHelper & operator +(const StringSumHelper &lhs, double num) {
return a;
}

StringSumHelper & operator +(const StringSumHelper &lhs, long long num) {
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
if(!a.concat(num))
a.invalidate();
return a;
}

StringSumHelper & operator +(const StringSumHelper &lhs, unsigned long long num) {
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
if(!a.concat(num))
a.invalidate();
return a;
}

StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHelper *rhs)
{
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
Expand Down
20 changes: 20 additions & 0 deletions cores/esp32/WString.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ class String {
explicit String(unsigned long, unsigned char base = 10);
explicit String(float, unsigned int decimalPlaces = 2);
explicit String(double, unsigned int decimalPlaces = 2);
explicit String(long long, unsigned char base = 10);
explicit String(unsigned long long, unsigned char base = 10);
~String(void);

// memory management
Expand Down Expand Up @@ -122,6 +124,8 @@ class String {
unsigned char concat(unsigned long num);
unsigned char concat(float num);
unsigned char concat(double num);
unsigned char concat(long long num);
unsigned char concat(unsigned long long num);
unsigned char concat(const __FlashStringHelper * str);

// if there's not enough memory for the concatenated value, the string
Expand Down Expand Up @@ -166,6 +170,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 += (const __FlashStringHelper *str){
concat(str);
return (*this);
Expand All @@ -182,6 +194,8 @@ class String {
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);
friend StringSumHelper & operator +(const StringSumHelper &lhs, long long num);
friend StringSumHelper & operator +(const StringSumHelper &lhs, unsigned long long num);

// comparison (only works w/ Strings and "strings")
operator StringIfHelperType() const {
Expand Down Expand Up @@ -373,6 +387,12 @@ class StringSumHelper: public String {
StringSumHelper(double num) :
String(num) {
}
StringSumHelper(long long num) :
String(num) {
}
StringSumHelper(unsigned long long num) :
String(num) {
}
};

extern const String emptyString;
Expand Down
48 changes: 48 additions & 0 deletions cores/esp32/stdlib_noniso.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,31 @@ char* ltoa(long value, char* result, int base) {
return result;
}

char* lltoa (long long val, char* result, int base) {
if(base < 2 || base > 16) {
*result = 0;
return result;
}

char* out = result;
long long quotient = val > 0 ? val : -val;

do {
const long long tmp = quotient / base;
*out = "0123456789abcdef"[quotient - (tmp * base)];
++out;
quotient = tmp;
} while(quotient);

// Apply negative sign
if(val < 0)
*out++ = '-';

reverse(result, out);
*out = 0;
return result;
}

char* ultoa(unsigned long value, char* result, int base) {
if(base < 2 || base > 16) {
*result = 0;
Expand All @@ -88,6 +113,27 @@ char* ultoa(unsigned long value, char* result, int base) {
return result;
}

char* ulltoa (unsigned long long val, char* result, int base) {
if(base < 2 || base > 16) {
*result = 0;
return result;
}

char* out = result;
unsigned long long quotient = val;

do {
const unsigned long long tmp = quotient / base;
*out = "0123456789abcdef"[quotient - (tmp * base)];
++out;
quotient = tmp;
} while(quotient);

reverse(result, out);
*out = 0;
return result;
}

char * dtostrf(double number, signed int width, unsigned int prec, char *s) {
bool negative = false;

Expand Down Expand Up @@ -160,3 +206,5 @@ char * dtostrf(double number, signed int width, unsigned int prec, char *s) {
*out = 0;
return s;
}


4 changes: 4 additions & 0 deletions cores/esp32/stdlib_noniso.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,14 @@ char* itoa (int val, char *s, int radix);

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

char* lltoa (long long val, char* s, int 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* s, int radix);

char* dtostrf (double val, signed int width, unsigned int prec, char *s);

#ifdef __cplusplus
Expand Down

0 comments on commit 722c464

Please sign in to comment.