Skip to content

Commit

Permalink
Improve String support for 64 bit integers
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulStoffregen committed Mar 29, 2023
1 parent c26cb0f commit 1bf479e
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 6 deletions.
36 changes: 33 additions & 3 deletions teensy3/WString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ String::String(unsigned char c)
String::String(const int value, unsigned char base)
{
init();
char buf[18];
char buf[34];
itoa(value, buf, base);
*this = buf;
}

String::String(unsigned int value, unsigned char base)
{
init();
char buf[17];
char buf[33];
utoa(value, buf, base);
*this = buf;
}
Expand All @@ -103,6 +103,22 @@ String::String(unsigned long value, unsigned char base)
*this = buf;
}

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

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

String::String(float num, unsigned char digits)
{
init();
Expand Down Expand Up @@ -306,7 +322,7 @@ String & String::append(unsigned long num)

String & String::append(long long num)
{
char buf[21];
char buf[22];
lltoa(num, buf, 10);
append(buf, strlen(buf));
return *this;
Expand Down Expand Up @@ -397,6 +413,20 @@ StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num)
return a;
}

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

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

StringSumHelper & operator + (const StringSumHelper &lhs, float num)
{
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
Expand Down
6 changes: 6 additions & 0 deletions teensy3/WString.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,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);
String & concat(const String &str) {return append(str);}
Expand All @@ -139,6 +141,8 @@ class String
String & concat(unsigned int num) {return append(num);}
String & concat(long num) {return append(num);}
String & concat(unsigned long num) {return append(num);}
String & concat(long long num) {return append(num);}
String & concat(unsigned long long num) {return append(num);}
String & concat(float num) {return append(num);}
String & concat(double num) {return append(num);}

Expand Down Expand Up @@ -236,6 +240,8 @@ class StringSumHelper : public String
StringSumHelper(unsigned int num) : String(num, 10) {}
StringSumHelper(long num) : String(num, 10) {}
StringSumHelper(unsigned long num) : String(num, 10) {}
StringSumHelper(long long num) : String(num, 10) {}
StringSumHelper(unsigned long long num) : String(num, 10) {}
};

#endif // __cplusplus
Expand Down
36 changes: 33 additions & 3 deletions teensy4/WString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ String::String(unsigned char c)
String::String(const int value, unsigned char base)
{
init();
char buf[18];
char buf[34];
itoa(value, buf, base);
*this = buf;
}

String::String(unsigned int value, unsigned char base)
{
init();
char buf[17];
char buf[33];
utoa(value, buf, base);
*this = buf;
}
Expand All @@ -103,6 +103,22 @@ String::String(unsigned long value, unsigned char base)
*this = buf;
}

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

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

String::String(float num, unsigned char digits)
{
init();
Expand Down Expand Up @@ -306,7 +322,7 @@ String & String::append(unsigned long num)

String & String::append(long long num)
{
char buf[21];
char buf[22];
lltoa(num, buf, 10);
append(buf, strlen(buf));
return *this;
Expand Down Expand Up @@ -397,6 +413,20 @@ StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num)
return a;
}

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

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

StringSumHelper & operator + (const StringSumHelper &lhs, float num)
{
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
Expand Down
6 changes: 6 additions & 0 deletions teensy4/WString.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,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);
String & concat(const String &str) {return append(str);}
Expand All @@ -139,6 +141,8 @@ class String
String & concat(unsigned int num) {return append(num);}
String & concat(long num) {return append(num);}
String & concat(unsigned long num) {return append(num);}
String & concat(long long num) {return append(num);}
String & concat(unsigned long long num) {return append(num);}
String & concat(float num) {return append(num);}
String & concat(double num) {return append(num);}

Expand Down Expand Up @@ -236,6 +240,8 @@ class StringSumHelper : public String
StringSumHelper(unsigned int num) : String(num, 10) {}
StringSumHelper(long num) : String(num, 10) {}
StringSumHelper(unsigned long num) : String(num, 10) {}
StringSumHelper(long long num) : String(num, 10) {}
StringSumHelper(unsigned long long num) : String(num, 10) {}
};

#endif // __cplusplus
Expand Down

0 comments on commit 1bf479e

Please sign in to comment.