Skip to content

Commit 53a4f56

Browse files
committed
fix string bool operator
1 parent 1c59947 commit 53a4f56

File tree

2 files changed

+115
-117
lines changed

2 files changed

+115
-117
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2121
- `ci_config.rb` now returns empty arrays (instead of nil) for undefined config keys
2222
- `pgmspace.h` explictly includes `<string.h>`
2323
- `__FlashStringHelper` should now be properly mocked for compilation
24+
- `WString.h` bool operator now works and is simpler
2425

2526
### Security
2627

cpp/arduino/WString.h

Lines changed: 114 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,10 @@ class __FlashStringHelper;
1515
// Compatibility with string class
1616
class String: public string
1717
{
18-
public:
19-
20-
// allow "string s; if (s) {}"
21-
// http://www.artima.com/cppsource/safebool.html
22-
typedef void (String::*TTDNSCstring)() const;
23-
void ttdnsc() const {}
24-
operator TTDNSCstring() const { return &String::ttdnsc; }
25-
2618
private:
27-
static const char* digit(int val) {
28-
static const char* bank = "0123456789ABCDEF";
19+
static const char *digit(int val)
20+
{
21+
static const char *bank = "0123456789ABCDEF";
2922
return bank + val;
3023
}
3124

@@ -51,114 +44,118 @@ class String: public string
5144
return mytoas(val, 10) + "." + mytoa(abs(val - (long)val) * pow(10, decimalPlaces), 10);
5245
}
5346

54-
public:
55-
~String(void) {}
56-
String(const __FlashStringHelper *str): string((const char *)str) {}
57-
String(const char *cstr = ""): string(cstr) {}
58-
String(const string &str): string(str) {}
59-
String(const String &str): string(str) {}
60-
explicit String(char c): string(1, c) {}
61-
62-
explicit String(unsigned char val, unsigned char base=10): string(mytoa(val, base)) {}
63-
explicit String(int val, unsigned char base=10): string(mytoas(val, base)) {}
64-
explicit String(unsigned int val , unsigned char base=10): string(mytoa(val, base)) {}
65-
explicit String(long val, unsigned char base=10): string(mytoas(val, base)) {}
66-
explicit String(unsigned long val, unsigned char base=10): string(mytoa(val, base)) {}
67-
68-
explicit String(float val, unsigned char decimalPlaces=2): string(dtoas(val, decimalPlaces)) {}
69-
explicit String(double val, unsigned char decimalPlaces=2): string(dtoas(val, decimalPlaces)) {}
70-
71-
String & operator = (const String &rhs) { assign(rhs); return *this; }
72-
String & operator = (const string &rhs) { assign(rhs); return *this; }
73-
String & operator = (const char *cstr) { assign(cstr); return *this; }
74-
String & operator = (const char c) { assign(1, c); return *this; }
75-
76-
unsigned char concat(const __FlashStringHelper *str) { append((const char *)str); return 1; }
77-
unsigned char concat(const String &str) { append(str); return 1; }
78-
unsigned char concat(const char *cstr) { append(cstr); return 1; }
79-
unsigned char concat(char c) { append(1, c); return 1; }
80-
unsigned char concat(unsigned char c) { append(1, c); return 1; }
81-
unsigned char concat(int num) { append(String(num)); return 1; }
82-
unsigned char concat(unsigned int num) { append(String(num)); return 1; }
83-
unsigned char concat(long num) { append(String(num)); return 1; }
84-
unsigned char concat(unsigned long num) { append(String(num)); return 1; }
85-
unsigned char concat(float num) { append(String(num)); return 1; }
86-
unsigned char concat(double num) { append(String(num)); return 1; }
87-
88-
String & operator += (const __FlashStringHelper *rhs) { concat(rhs); return *this; }
89-
String & operator += (const String &rhs) { concat(rhs); return *this; }
90-
String & operator += (const char *cstr) { concat(cstr); return *this; }
91-
String & operator += (char c) { concat(c); return *this; }
92-
String & operator += (unsigned char num) { concat(num); return *this; }
93-
String & operator += (int num) { concat(num); return *this; }
94-
String & operator += (unsigned int num) { concat(num); return *this; }
95-
String & operator += (long num) { concat(num); return *this; }
96-
String & operator += (unsigned long num) { concat(num); return *this; }
97-
String & operator += (float num) { concat(num); return *this; }
98-
String & operator += (double num) { concat(num); return *this; }
99-
100-
101-
int compareTo(const String &s) const { return compare(s); }
102-
unsigned char equals(const String &s) const { return compareTo(s) == 0; }
103-
unsigned char equals(const char *cstr) const { return compareTo(String(cstr)) == 0; }
104-
unsigned char equal(const String &s) const { return equals(s); }
105-
unsigned char equal(const char *cstr) const { return equals(cstr); }
106-
unsigned char equalsIgnoreCase(const String &s) const {
107-
String a = String(*this);
108-
String b = String(s);
109-
a.toUpperCase();
110-
b.toUpperCase();
111-
return a.compare(b) == 0;
112-
}
113-
114-
unsigned char startsWith(const String &prefix) const { return find(prefix) == 0; }
115-
unsigned char startsWith(const String &prefix, unsigned int offset) const { return find(prefix, offset) == offset; }
116-
unsigned char endsWith(const String &suffix) const { return rfind(suffix) == length() - suffix.length(); }
117-
118-
char charAt(unsigned int index) const { return operator[](index); }
119-
void setCharAt(unsigned int index, char c) { (*this)[index] = c; }
120-
121-
void getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index=0) const { copy((char*)buf, bufsize, index); }
122-
void toCharArray(char *buf, unsigned int bufsize, unsigned int index=0) const
123-
{ getBytes((unsigned char *)buf, bufsize, index); }
124-
125-
int indexOf( char ch ) const { return find(ch); }
126-
int indexOf( char ch, unsigned int fromIndex ) const { return find(ch, fromIndex); }
127-
int indexOf( const String &str ) const { return find(str); }
128-
int indexOf( const String &str, unsigned int fromIndex ) const { return find(str, fromIndex); }
129-
int lastIndexOf( char ch ) const { return rfind(ch); }
130-
int lastIndexOf( char ch, unsigned int fromIndex ) const { return rfind(ch, fromIndex); }
131-
int lastIndexOf( const String &str ) const { return rfind(str); }
132-
int lastIndexOf( const String &str, unsigned int fromIndex ) const { return rfind(str, fromIndex); }
133-
String substring( unsigned int beginIndex ) const { return String(substr(beginIndex)); }
134-
String substring( unsigned int beginIndex, unsigned int endIndex ) const { return String(substr(beginIndex, endIndex)); }
135-
136-
void replace(const String& target, const String& repl) {
137-
int i = 0;
138-
while ((i = find(target, i)) != npos) {
139-
assign(substr(0, i) + repl + substr(i + target.length()));
140-
i += repl.length();
47+
public:
48+
~String(void) {}
49+
String(const __FlashStringHelper *str): string((const char *)str) {}
50+
String(const char *cstr = ""): string(cstr) {}
51+
String(const string &str): string(str) {}
52+
String(const String &str): string(str) {}
53+
explicit String(char c): string(1, c) {}
54+
55+
explicit String(unsigned char val, unsigned char base=10): string(mytoa(val, base)) {}
56+
explicit String(int val, unsigned char base=10): string(mytoas(val, base)) {}
57+
explicit String(unsigned int val , unsigned char base=10): string(mytoa(val, base)) {}
58+
explicit String(long val, unsigned char base=10): string(mytoas(val, base)) {}
59+
explicit String(unsigned long val, unsigned char base=10): string(mytoa(val, base)) {}
60+
61+
explicit String(float val, unsigned char decimalPlaces=2): string(dtoas(val, decimalPlaces)) {}
62+
explicit String(double val, unsigned char decimalPlaces=2): string(dtoas(val, decimalPlaces)) {}
63+
64+
operator bool() const {
65+
return true;
66+
}
67+
68+
String & operator = (const String &rhs) { assign(rhs); return *this; }
69+
String & operator = (const string &rhs) { assign(rhs); return *this; }
70+
String & operator = (const char *cstr) { assign(cstr); return *this; }
71+
String & operator = (const char c) { assign(1, c); return *this; }
72+
73+
unsigned char concat(const __FlashStringHelper *str) { append((const char *)str); return 1; }
74+
unsigned char concat(const String &str) { append(str); return 1; }
75+
unsigned char concat(const char *cstr) { append(cstr); return 1; }
76+
unsigned char concat(char c) { append(1, c); return 1; }
77+
unsigned char concat(unsigned char c) { append(1, c); return 1; }
78+
unsigned char concat(int num) { append(String(num)); return 1; }
79+
unsigned char concat(unsigned int num) { append(String(num)); return 1; }
80+
unsigned char concat(long num) { append(String(num)); return 1; }
81+
unsigned char concat(unsigned long num) { append(String(num)); return 1; }
82+
unsigned char concat(float num) { append(String(num)); return 1; }
83+
unsigned char concat(double num) { append(String(num)); return 1; }
84+
85+
String & operator += (const __FlashStringHelper *rhs) { concat(rhs); return *this; }
86+
String & operator += (const String &rhs) { concat(rhs); return *this; }
87+
String & operator += (const char *cstr) { concat(cstr); return *this; }
88+
String & operator += (char c) { concat(c); return *this; }
89+
String & operator += (unsigned char num) { concat(num); return *this; }
90+
String & operator += (int num) { concat(num); return *this; }
91+
String & operator += (unsigned int num) { concat(num); return *this; }
92+
String & operator += (long num) { concat(num); return *this; }
93+
String & operator += (unsigned long num) { concat(num); return *this; }
94+
String & operator += (float num) { concat(num); return *this; }
95+
String & operator += (double num) { concat(num); return *this; }
96+
97+
98+
int compareTo(const String &s) const { return compare(s); }
99+
unsigned char equals(const String &s) const { return compareTo(s) == 0; }
100+
unsigned char equals(const char *cstr) const { return compareTo(String(cstr)) == 0; }
101+
unsigned char equal(const String &s) const { return equals(s); }
102+
unsigned char equal(const char *cstr) const { return equals(cstr); }
103+
unsigned char equalsIgnoreCase(const String &s) const {
104+
String a = String(*this);
105+
String b = String(s);
106+
a.toUpperCase();
107+
b.toUpperCase();
108+
return a.compare(b) == 0;
141109
}
142-
}
143-
void replace(char target, char repl) {
144-
replace(String(target), String(repl));
145-
}
146-
void remove(unsigned int index) { assign(substr(0, index)); }
147-
void remove(unsigned int index, unsigned int count) { assign(substr(0, index) + substr(min(length(), index + count), count)); }
148-
void toLowerCase(void) { std::transform(begin(), end(), begin(), ::tolower); }
149-
void toUpperCase(void) { std::transform(begin(), end(), begin(), ::toupper); }
150-
151-
void trim(void) {
152-
int b;
153-
int e;
154-
for (b = 0; b < length() && isSpace(charAt(b)); ++b);
155-
for (e = length() - 1; e > b && isSpace(charAt(e)); --e);
156-
assign(substr(b, e - b + 1));
157-
}
158-
159-
long toInt(void) const { return std::stol(*this); }
160-
float toFloat(void) const { return std::stof(*this); }
161-
double toDouble(void) const { return std::stod(*this); }
110+
111+
unsigned char startsWith(const String &prefix) const { return find(prefix) == 0; }
112+
unsigned char startsWith(const String &prefix, unsigned int offset) const { return find(prefix, offset) == offset; }
113+
unsigned char endsWith(const String &suffix) const { return rfind(suffix) == length() - suffix.length(); }
114+
115+
char charAt(unsigned int index) const { return operator[](index); }
116+
void setCharAt(unsigned int index, char c) { (*this)[index] = c; }
117+
118+
void getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index=0) const { copy((char*)buf, bufsize, index); }
119+
void toCharArray(char *buf, unsigned int bufsize, unsigned int index=0) const
120+
{ getBytes((unsigned char *)buf, bufsize, index); }
121+
122+
int indexOf( char ch ) const { return find(ch); }
123+
int indexOf( char ch, unsigned int fromIndex ) const { return find(ch, fromIndex); }
124+
int indexOf( const String &str ) const { return find(str); }
125+
int indexOf( const String &str, unsigned int fromIndex ) const { return find(str, fromIndex); }
126+
int lastIndexOf( char ch ) const { return rfind(ch); }
127+
int lastIndexOf( char ch, unsigned int fromIndex ) const { return rfind(ch, fromIndex); }
128+
int lastIndexOf( const String &str ) const { return rfind(str); }
129+
int lastIndexOf( const String &str, unsigned int fromIndex ) const { return rfind(str, fromIndex); }
130+
String substring( unsigned int beginIndex ) const { return String(substr(beginIndex)); }
131+
String substring( unsigned int beginIndex, unsigned int endIndex ) const { return String(substr(beginIndex, endIndex)); }
132+
133+
void replace(const String& target, const String& repl) {
134+
int i = 0;
135+
while ((i = find(target, i)) != npos) {
136+
assign(substr(0, i) + repl + substr(i + target.length()));
137+
i += repl.length();
138+
}
139+
}
140+
void replace(char target, char repl) {
141+
replace(String(target), String(repl));
142+
}
143+
void remove(unsigned int index) { assign(substr(0, index)); }
144+
void remove(unsigned int index, unsigned int count) { assign(substr(0, index) + substr(min(length(), index + count), count)); }
145+
void toLowerCase(void) { std::transform(begin(), end(), begin(), ::tolower); }
146+
void toUpperCase(void) { std::transform(begin(), end(), begin(), ::toupper); }
147+
148+
void trim(void) {
149+
int b;
150+
int e;
151+
for (b = 0; b < length() && isSpace(charAt(b)); ++b);
152+
for (e = length() - 1; e > b && isSpace(charAt(e)); --e);
153+
assign(substr(b, e - b + 1));
154+
}
155+
156+
long toInt(void) const { return std::stol(*this); }
157+
float toFloat(void) const { return std::stof(*this); }
158+
double toDouble(void) const { return std::stod(*this); }
162159

163160
};
164161

0 commit comments

Comments
 (0)