8000 Merge pull request #147 from mcspr/bools · Cesar-Urteaga/ArduinoCore-API@438172b · GitHub
[go: up one dir, main page]

Skip to content
< 8000 header class="HeaderMktg header-logged-out js-details-container js-header Details f4 py-3" role="banner" data-is-top="true" data-color-mode=light data-light-theme=light data-dark-theme=dark>

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 438172b

Browse files
authored
Merge pull request arduino#147 from mcspr/bools
Bool as return value for logical operations
2 parents 42f8e11 + a010db7 commit 438172b

File tree

2 files changed

+83
-83
lines changed

2 files changed

+83
-83
lines changed

api/String.cpp

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -165,25 +165,25 @@ void String::invalidate(void)
165165
capacity = len = 0;
166166
}
167167

168-
unsigned char String::reserve(unsigned int size)
168+
bool String::reserve(unsigned int size)
169169
{
170170
if (buffer && capacity >= size) return 1;
171171
if (changeBuffer(size)) {
172172
if (len == 0) buffer[0] = 0;
173-
return 1;
173+
return true;
174174
}
175-
return 0;
175+
return false;
176176
}
177177

178-
unsigned char String::changeBuffer(unsigned int maxStrLen)
178+
bool String::changeBuffer(unsigned int maxStrLen)
179179
{
180180
char *newbuffer = (char *)realloc(buffer, maxStrLen + 1);
181181
if (newbuffer) {
182182
buffer = newbuffer;
183183
capacity = maxStrLen;
184-
return 1;
184+
return true;
185185
}
186-
return 0;
186+
return false;
187187
}
188188

189189
/*********************************************/
@@ -269,93 +269,93 @@ String & String::operator = (const __FlashStringHelper *pstr)
269269
/* concat */
270270
/*********************************************/
271271

272-
unsigned char String::concat(const String &s)
272+
bool String::concat(const String &s)
273273
{
274274
return concat(s.buffer, s.len);
275275
}
276276

277-
unsigned char String::concat(const char *cstr, unsigned int length)
277+
bool String::concat(const char *cstr, unsigned int length)
278278
{
279279
unsigned int newlen = len + length;
280-
if (!cstr) return 0;
281-
if (length == 0) return 1;
282-
if (!reserve(newlen)) return 0;
280+
if (!cstr) return false;
281+
if (length == 0) return true;
282+
if (!reserve(newlen)) return false;
283283
memcpy(buffer + len, cstr, length);
284284
len = newlen;
285285
buffer[len] = '\0';
286-
return 1;
286+
return true;
287287
}
288288

289-
unsigned char String::concat(const char *cstr)
289+
bool String::concat(const char *cstr)
290290
{
291-
if (!cstr) return 0;
291+
if (!cstr) return false;
292292
return concat(cstr, strlen(cstr));
293293
}
294294

295-
unsigned char String::concat(char c)
295+
bool String::concat(char c)
296296
{
297297
return concat(&c, 1);
298298
}
299299

300-
unsigned char String::concat(unsigned char num)
300+
bool String::concat(unsigned char num)
301301
{
302302
char buf[1 + 3 * sizeof(unsigned char)];
303303
itoa(num, buf, 10);
304304
return concat(buf);
305305
}
306306

307-
unsigned char String::concat(int num)
307+
bool String::concat(int num)
308308
{
309309
char buf[2 + 3 * sizeof(int)];
310310
itoa(num, buf, 10);
311311
return concat(buf);
312312
}
313313

314-
unsigned char String::concat(unsigned int num)
314+
bool String::concat(unsigned int num)
315315
{
316316
char buf[1 + 3 * sizeof(unsigned int)];
317317
utoa(num, buf, 10);
318318
return concat(buf);
319319
}
320320

321-
unsigned char String::concat(long num)
321+
bool String::concat(long num)
322322
{
323323
char buf[2 + 3 * sizeof(long)];
324324
ltoa(num, buf, 10);
325325
return concat(buf);
326326
}
327327

328-
unsigned char String::concat(unsigned long num)
328+
bool String::concat(unsigned long num)
329329
{
330330
char buf[1 + 3 * sizeof(unsigned long)];
331331
ultoa(num, buf, 10);
332332
return concat(buf);
333333
}
334334

335-
unsigned char String::concat(float num)
335+
bool String::concat(float num)
336336
{
337337
char buf[20];
338338
char* string = dtostrf(num, 4, 2, buf);
339339
return concat(string);
340340
}
341341

342-
unsigned char String::concat(double num)
342+
bool String::concat(double num)
343343
{
344344
char buf[20];
345345
char* string = dtostrf(num, 4, 2, buf);
346346
return concat(string);
347347
}
348348

349-
unsigned char String::concat(const __FlashStringHelper * str)
349+
bool String::concat(const __FlashStringHelper * str)
350350
{
351-
if (!str) return 0;
351+
if (!str) return false;
352352
int length = strlen_P((const char *) str);
353-
if (length == 0) return 1;
353+
if (length == 0) return true;
354354
unsigned int newlen = len + length;
355-
if (!reserve(newlen)) return 0;
355+
if (!reserve(newlen)) return false;
356356
strcpy_P(buffer + len, (const char *) str);
357357
len = newlen;
358-
return 1;
358+
return true;
359359
}
360360

361361
/*********************************************/
@@ -463,46 +463,46 @@ int String::compareTo(const char *cstr) const
463463
return strcmp(buffer, cstr);
464464
}
465465

466-
unsigned char String::equals(const String &s2) const
466+
bool String::equals(const String &s2) const
467467
{
468468
return (len == s2.len && compareTo(s2) == 0);
469469
}
470470

471-
unsigned char String::equals(const char *cstr) const
471+
bool String::equals(const char *cstr) const
472472
{
473473
if (len == 0) return (cstr == NULL || *cstr == 0);
474474
if (cstr == NULL) return buffer[0] == 0;
475475
return strcmp(buffer, cstr) == 0;
476476
}
477477

478-
unsigned char String::equalsIgnoreCase( const String &s2 ) const
478+
bool String::equalsIgnoreCase( const String &s2 ) const
479479
{
480-
if (this == &s2) return 1;
481-
if (len != s2.len) return 0;
482-
if (len == 0) return 1;
480+
if (this == &s2) return true;
481+
if (len != s2.len) return false;
482+
if (len == 0) return true;
483483
const char *p1 = buffer;
484484
const char *p2 = s2.buffer;
485485
while (*p1) {
486-
if (tolower(*p1++) != tolower(*p2++)) return 0;
486+
if (tolower(*p1++) != tolower(*p2++)) return false;
487487
}
488-
return 1;
488+
return true;
489489
}
490490

491-
unsigned char String::startsWith( const String &s2 ) const
491+
bool String::startsWith( const String &s2 ) const
492492
{
493-
if (len < s2.len) return 0;
493+
if (len < s2.len) return false;
494494
return startsWith(s2, 0);
495495
}
496496

497-
unsigned char String::startsWith( const String &s2, unsigned int offset ) const
497+
bool String::startsWith( const String &s2, unsigned int offset ) const
498498
{
499-
if (offset > len - s2.len || !buffer || !s2.buffer) return 0;
499+
if (offset > len - s2.len || !buffer || !s2.buffer) return false;
500500
return strncmp( &buffer[offset], s2.buffer, s2.len ) == 0;
501501
}
502502

503-
unsigned char String::endsWith( const String &s2 ) const
503+
bool String::endsWith( const String &s2 ) const
504504
{
505-
if ( len < s2.len || !buffer || !s2.buffer) return 0;
505+
if ( len < s2.len || !buffer || !s2.buffer) return false;
506506
return strcmp(&buffer[len - s2.len], s2.buffer) == 0;
507507
}
508508

api/String.h

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class String
8989
// return true on success, false on failure (in which case, the string
9090
// is left unchanged). reserve(0), if successful, will validate an
9191
// invalid string (i.e., "if (s)" will be true afterwards)
92-
unsigned char reserve(unsigned int size);
92+
bool reserve(unsigned int size);
9393
inline unsigned int length(void) const {return len;}
9494

9595
// creates a copy of the assigned value. if the value is null or
@@ -107,19 +107,19 @@ class String
107107
// returns true on success, false on failure (in which case, the string
108108
// is left unchanged). if the argument is null or invalid, the
109109
// concatenation is considered unsucessful.
110-
unsigned char concat(const String &str);
111-
unsigned char concat(const char *cstr);
112-
unsigned char concat(const char *cstr, unsigned int length);
113-
unsigned char concat(const uint8_t *cstr, unsigned int length) {return concat((const char*)cstr, length);}
114-
unsigned char concat(char c);
115-
unsigned char concat(unsigned char num);
116-
unsigned char concat(int num);
117-
unsigned char concat(unsigned int num);
118-
unsigned char concat(long num);
119-
unsigned char concat(unsigned long num);
120-
unsigned char concat(float num);
121-
unsigned char concat(double num);
122-
unsigned char concat(const __FlashStringHelper * str);
110+
bool concat(const String &str);
111+
bool concat(const char *cstr);
112+
bool concat(const char *cstr, unsigned int length);
113+
bool concat(const uint8_t *cstr, unsigned int length) {return concat((const char*)cstr, length);}
114+
bool concat(char c);
115+
bool concat(unsigned char num);
116+
bool concat(int num);
117+
bool concat(unsigned int num);
118+
bool concat(long num);
119+
bool concat(unsigned long num);
120+
bool concat(float num);
121+
bool concat(double num);
122+
bool concat(const __FlashStringHelper * str);
123123

124124
// if there's not enough memory for the concatenated value, the string
125125
// will be left unchanged (but this isn't signalled in any way)
@@ -151,33 +151,33 @@ class String
151151
operator StringIfHelperType() const { return buffer ? &String::StringIfHelper : 0; }
152152
int compareTo(const String &s) const;
153153
int compareTo(const char *cstr) const;
154-
unsigned char equals(const String &s) const;
155-
unsigned char equals(const char *cstr) const;
156-
157-
friend unsigned char operator == (const String &a, const String &b) { return a.equals(b); }
158-
friend unsigned char operator == (const String &a, const char *b) { return a.equals(b); }
159-
friend unsigned char operator == (const char *a, const String &b) { return b == a; }
160-
friend unsigned char operator < (const String &a, const String &b) { return a.compareTo(b) < 0; }
161-
friend unsigned char operator < (const String &a, const char *b) { return a.compareTo(b) < 0; }
162-
friend unsigned char operator < (const char *a, const String &b) { return b.compareTo(a) > 0; }
163-
164-
friend unsigned char operator != (const String &a, const String &b) { return !(a == b); }
165-
friend unsigned char operator != (const String &a, const char *b) { return !(a == b); }
166-
friend unsigned char operator != (const char *a, const String &b) { return !(a == b); }
167-
friend unsigned char operator > (const String &a, const String &b) { return b < a; }
168-
friend unsigned char operator > (const String &a, const char *b) { return b < a; }
169-
friend unsigned char operator > (const char *a, const String &b) { return b < a; }
170-
friend unsigned char operator <= (const String &a, const String &b) { return !(b < a); }
171-
friend unsigned char operator <= (const String &a, const char *b) { return !(b < a); }
172-
friend unsigned char operator <= (const char *a, const String &b) { return !(b < a); }
173-
friend unsigned char operator >= (const String &a, const String &b) { return !(a < b); }
174-
friend unsigned char operator >= (const String &a, const char *b) { return !(a < b); }
175-
friend unsigned char operator >= (const char *a, const String &b) { return !(a < b); }
176-
177-
unsigned char equalsIgnoreCase(const String &s) const;
178-
unsigned char startsWith( const String &prefix) const;
179-
unsigned char startsWith(const String &prefix, unsigned int offset) const;
180-
unsigned char endsWith(const String &suffix) const;
154+
bool equals(const String &s) const;
155+
bool equals(const char *cstr) const;
156+
157+
friend bool operator == (const String &a, const String &b) { return a.equals(b); }
158+
friend bool operator == (const String &a, const char *b) { return a.equals(b); }
159+
friend bool operator == (const char *a, const String &b) { return b == a; }
160+
friend bool operator < (const String &a, const String &b) { return a.compareTo(b) < 0; }
161+
friend bool operator < (const String &a, const char *b) { return a.compareTo(b) < 0; }
162+
friend bool operator < (const char *a, const String &b) { return b.compareTo(a) > 0; }
163+
164+
friend bool operator != (const String &a, const String &b) { return !(a == b); }
165+
friend bool operator != (const String &a, const char *b) { return !(a == b); }
166+
friend bool operator != (const char *a, const String &b) { return !(a == b); }
167+
friend bool operator > (const String &a, const String &b) { return b < a; }
168+
friend bool operator > (const String &a, const char *b) { return b < a; }
169+
friend bool operator > (const char *a, const String &b) { return b < a; }
170+
friend bool operator <= (const String &a, const String &b) { return !(b < a); }
171+
friend bool operator <= (const String &a, const char *b) { return !(b < a); }
172+
friend bool operator <= (const char *a, const String &b) { return !(b < a); }
173+
friend bool operator >= (const String &a, const String &b) { return !(a < b); }
174+
friend bool operator >= (const String &a, const char *b) { return !(a < b); }
175+
friend bool operator >= (const char *a, const String &b) { return !(a < b); }
176+
177+
bool equalsIgnoreCase(const String &s) const;
178+
bool startsWith( const String &prefix) const;
179+
bool startsWith(const String &prefix, unsigned int offset) const;
180+
bool endsWith(const String &suffix) const;
181181

182182
// character acccess
183183
char charAt(unsigned int index) const;
@@ -226,7 +226,7 @@ class String
226226
protected:
227227
void init(void);
228228
void invalidate(void);
229-
unsigned char changeBuffer(unsigned int maxStrLen);
229+
bool changeBuffer(unsigned int maxStrLen);
230230

231231
// copy and move
232232
String & copy(const char *cstr, unsigned int length);

0 commit comments

Comments
 (0)
0