Skip to content

Commit 722c464

Browse files
authored
Extends String to print 64-bit integers (espressif#6768)
1 parent ba6e82c commit 722c464

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-0
lines changed

Diff for: cores/esp32/WString.cpp

+43
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,24 @@ String::String(double value, unsigned int decimalPlaces) {
137137
}
138138
}
139139

140+
String::String(long long value, unsigned char base) {
141+
init();
142+
char buf[2 + 8 * sizeof(long long)];
143+
if (base==10) {
144+
sprintf(buf, "%lld", value); // NOT SURE - NewLib Nano ... does it support %lld?
145+
} else {
146+
lltoa(value, buf, base);
147+
}
148+
*this = buf;
149+
}
150+
151+
String::String(unsigned long long value, unsigned char base) {
152+
init();
153+
char buf[1 + 8 * sizeof(unsigned long long)];
154+
ulltoa(value, buf, base);
155+
*this = buf;
156+
}
157+
140158
String::~String() {
141159
invalidate();
142160
}
@@ -408,6 +426,17 @@ unsigned char String::concat(double num) {
408426
return concat(string, strlen(string));
409427
}
410428

429+
unsigned char String::concat(long long num) {
430+
char buf[2 + 3 * sizeof(long long)];
431+
return concat(buf, sprintf(buf, "%lld", num)); // NOT SURE - NewLib Nano ... does it support %lld?
432+
}
433+
434+
unsigned char String::concat(unsigned long long num) {
435+
char buf[1 + 3 * sizeof(unsigned long long)];
436+
ulltoa(num, buf, 10);
437+
return concat(buf, strlen(buf));
438+
}
439+
411440
unsigned char String::concat(const __FlashStringHelper * str) {
412441
if (!str) return 0;
413442
int length = strlen_P((PGM_P)str);
@@ -493,6 +522,20 @@ StringSumHelper & operator +(const StringSumHelper &lhs, double num) {
493522
return a;
494523
}
495524

525+
StringSumHelper & operator +(const StringSumHelper &lhs, long long num) {
526+
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
527+
if(!a.concat(num))
528+
a.invalidate();
529+
return a;
530+
}
531+
532+
StringSumHelper & operator +(const StringSumHelper &lhs, unsigned long long num) {
533+
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
534+
if(!a.concat(num))
535+
a.invalidate();
536+
return a;
537+
}
538+
496539
StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHelper *rhs)
497540
{
498541
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);

Diff for: cores/esp32/WString.h

+20
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ class String {
7373
explicit String(unsigned long, unsigned char base = 10);
7474
explicit String(float, unsigned int decimalPlaces = 2);
7575
explicit String(double, unsigned int decimalPlaces = 2);
76+
explicit String(long long, unsigned char base = 10);
77+
explicit String(unsigned long long, unsigned char base = 10);
7678
~String(void);
7779

7880
// memory management
@@ -122,6 +124,8 @@ class String {
122124
unsigned char concat(unsigned long num);
123125
unsigned char concat(float num);
124126
unsigned char concat(double num);
127+
unsigned char concat(long long num);
128+
unsigned char concat(unsigned long long num);
125129
unsigned char concat(const __FlashStringHelper * str);
126130

127131
// if there's not enough memory for the concatenated value, the string
@@ -166,6 +170,14 @@ class String {
166170
concat(num);
167171
return (*this);
168172
}
173+
String & operator +=(long long num) {
174+
concat(num);
175+
return (*this);
176+
}
177+
String & operator +=(unsigned long long num) {
178+
concat(num);
179+
return (*this);
180+
}
169181
String & operator += (const __FlashStringHelper *str){
170182
concat(str);
171183
return (*this);
@@ -182,6 +194,8 @@ class String {
182194
friend StringSumHelper & operator +(const StringSumHelper &lhs, float num);
183195
friend StringSumHelper & operator +(const StringSumHelper &lhs, double num);
184196
friend StringSumHelper & operator +(const StringSumHelper &lhs, const __FlashStringHelper *rhs);
197+
friend StringSumHelper & operator +(const StringSumHelper &lhs, long long num);
198+
friend StringSumHelper & operator +(const StringSumHelper &lhs, unsigned long long num);
185199

186200
// comparison (only works w/ Strings and "strings")
187201
operator StringIfHelperType() const {
@@ -373,6 +387,12 @@ class StringSumHelper: public String {
373387
StringSumHelper(double num) :
374388
String(num) {
375389
}
390+
StringSumHelper(long long num) :
391+
String(num) {
392+
}
393+
StringSumHelper(unsigned long long num) :
394+
String(num) {
395+
}
376396
};
377397

378398
extern const String emptyString;

Diff for: cores/esp32/stdlib_noniso.c

+48
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,31 @@ char* ltoa(long value, char* result, int base) {
6767
return result;
6868
}
6969

70+
char* lltoa (long long val, char* result, int base) {
71+
if(base < 2 || base > 16) {
72+
*result = 0;
73+
return result;
74+
}
75+
76+
char* out = result;
77+
long long quotient = val > 0 ? val : -val;
78+
79+
do {
80+
const long long tmp = quotient / base;
81+
*out = "0123456789abcdef"[quotient - (tmp * base)];
82+
++out;
83+
quotient = tmp;
84+
} while(quotient);
85+
86+
// Apply negative sign
87+
if(val < 0)
88+
*out++ = '-';
89+
90+
reverse(result, out);
91+
*out = 0;
92+
return result;
93+
}
94+
7095
char* ultoa(unsigned long value, char* result, int base) {
7196
if(base < 2 || base > 16) {
7297
*result = 0;
@@ -88,6 +113,27 @@ char* ultoa(unsigned long value, char* result, int base) {
88113
return result;
89114
}
90115

116+
char* ulltoa (unsigned long long val, char* result, int base) {
117+
if(base < 2 || base > 16) {
118+
*result = 0;
119+
return result;
120+
}
121+
122+
char* out = result;
123+
unsigned long long quotient = val;
124+
125+
do {
126+
const unsigned long long tmp = quotient / base;
127+
*out = "0123456789abcdef"[quotient - (tmp * base)];
128+
++out;
129+
quotient = tmp;
130+
} while(quotient);
131+
132+
reverse(result, out);
133+
*out = 0;
134+
return result;
135+
}
136+
91137
char * dtostrf(double number, signed int width, unsigned int prec, char *s) {
92138
bool negative = false;
93139

@@ -160,3 +206,5 @@ char * dtostrf(double number, signed int width, unsigned int prec, char *s) {
160206
*out = 0;
161207
return s;
162208
}
209+
210+

Diff for: cores/esp32/stdlib_noniso.h

+4
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,14 @@ char* itoa (int val, char *s, int radix);
3535

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

38+
char* lltoa (long long val, char* s, int radix);
39+
3840
char* utoa (unsigned int val, char *s, int radix);
3941

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

44+
char* ulltoa (unsigned long long val, char* s, int radix);
45+
4246
char* dtostrf (double val, signed int width, unsigned int prec, char *s);
4347

4448
#ifdef __cplusplus

0 commit comments

Comments
 (0)