Skip to content

Commit 0fa6672

Browse files
WString Return bool (#7774)
1 parent d03217a commit 0fa6672

File tree

2 files changed

+92
-90
lines changed

2 files changed

+92
-90
lines changed

cores/esp32/WString.cpp

+57-55
Original file line numberDiff line numberDiff line change
@@ -176,26 +176,25 @@ void String::invalidate(void) {
176176
init();
177177
}
178178

179-
unsigned char String::reserve(unsigned int size) {
179+
bool String::reserve(unsigned int size) {
180180
if(buffer() && capacity() >= size)
181-
return 1;
181+
return true;
182182
if(changeBuffer(size)) {
183183
if(len() == 0)
184184
wbuffer()[0] = 0;
185-
return 1;
185+
return true;
186186
}
187-
return 0;
187+
return false;
188188
}
189189

190-
unsigned char String::changeBuffer(unsigned int maxStrLen) {
190+
bool String::changeBuffer(unsigned int maxStrLen) {
191191
// Can we use SSO here to avoid allocation?
192192
if (maxStrLen < sizeof(sso.buff) - 1) {
193193
if (isSSO() || !buffer()) {
194194
// Already using SSO, nothing to do
195-
uint16_t oldLen = len();
195+
uint16_t oldLen = len();
196196
setSSO(true);
197197
setLen(oldLen);
198-
return 1;
199198
} else { // if bufptr && !isSSO()
200199
// Using bufptr, need to shrink into sso.buff
201200
char temp[sizeof(sso.buff)];
@@ -205,8 +204,8 @@ unsigned char String::changeBuffer(unsigned int maxStrLen) {
205204
setSSO(true);
206205
memcpy(wbuffer(), temp, maxStrLen);
207206
setLen(oldLen);
208-
return 1;
209207
}
208+
return true;
210209
}
211210
// Fallthrough to normal allocator
212211
size_t newSize = (maxStrLen + 16) & (~0xf);
@@ -230,9 +229,9 @@ unsigned char String::changeBuffer(unsigned int maxStrLen) {
230229
setCapacity(newSize - 1);
231230
setBuffer(newbuffer);
232231
setLen(oldLen); // Needed in case of SSO where len() never existed
233-
return 1;
232+
return true;
234233
}
235-
return 0;
234+
return false;
236235
}
237236

238237
// /*********************************************/
@@ -338,114 +337,117 @@ String & String::operator =(const __FlashStringHelper *pstr) {
338337
// /* concat */
339338
// /*********************************************/
340339

341-
unsigned char String::concat(const String &s) {
340+
bool String::concat(const String &s) {
342341
// Special case if we're concatting ourself (s += s;) since we may end up
343342
// realloc'ing the buffer and moving s.buffer in the method called
344343
if (&s == this) {
345344
unsigned int newlen = 2 * len();
346345
if (!s.buffer())
347-
return 0;
346+
return false;
348347
if (s.len() == 0)
349-
return 1;
348+
return true;
350349
if (!reserve(newlen))
351-
return 0;
350+
return false;
352351
memmove(wbuffer() + len(), buffer(), len());
353352
setLen(newlen);
354353
wbuffer()[len()] = 0;
355-
return 1;
354+
return true;
356355
} else {
357356
return concat(s.buffer(), s.len());
358357
}
359358
}
360359

361-
unsigned char String::concat(const char *cstr, unsigned int length) {
360+
bool String::concat(const char *cstr, unsigned int length) {
362361
unsigned int newlen = len() + length;
363362
if(!cstr)
364-
return 0;
363+
return false;
365364
if(length == 0)
366-
return 1;
365+
return true;
367366
if(!reserve(newlen))
368-
return 0;
367+
return false;
369368
if (cstr >= wbuffer() && cstr < wbuffer() + len())
370369
// compatible with SSO in ram #6155 (case "x += x.c_str()")
371370
memmove(wbuffer() + len(), cstr, length + 1);
372371
else
373372
// compatible with source in flash #6367
374373
memcpy_P(wbuffer() + len(), cstr, length + 1);
375374
setLen(newlen);
376-
return 1;
375+
return true;
377376
}
378377

379-
unsigned char String::concat(const char *cstr) {
378+
bool String::concat(const char *cstr) {
380379
if(!cstr)
381-
return 0;
380+
return false;
382381
return concat(cstr, strlen(cstr));
383382
}
384383

385-
unsigned char String::concat(char c) {
384+
bool String::concat(char c) {
386385
char buf[] = { c, '\0' };
387386
return concat(buf, 1);
388387
}
389388

390-
unsigned char String::concat(unsigned char num) {
389+
bool String::concat(unsigned char num) {
391390
char buf[1 + 3 * sizeof(unsigned char)];
392391
return concat(buf, sprintf(buf, "%d", num));
393392
}
394393

395-
unsigned char String::concat(int num) {
394+
bool String::concat(int num) {
396395
char buf[2 + 3 * sizeof(int)];
397396
return concat(buf, sprintf(buf, "%d", num));
398397
}
399398

400-
unsigned char String::concat(unsigned int num) {
399+
bool String::concat(unsigned int num) {
401400
char buf[1 + 3 * sizeof(unsigned int)];
402401
utoa(num, buf, 10);
403402
return concat(buf, strlen(buf));
404403
}
405404

406-
unsigned char String::concat(long num) {
405+
bool String::concat(long num) {
407406
char buf[2 + 3 * sizeof(long)];
408407
return concat(buf, sprintf(buf, "%ld", num));
409408
}
410409

411-
unsigned char String::concat(unsigned long num) {
410+
bool String::concat(unsigned long num) {
412411
char buf[1 + 3 * sizeof(unsigned long)];
413412
ultoa(num, buf, 10);
414413
return concat(buf, strlen(buf));
415414
}
416415

417-
unsigned char String::concat(float num) {
416+
bool String::concat(float num) {
418417
char buf[20];
419418
char* string = dtostrf(num, 4, 2, buf);
420419
return concat(string, strlen(string));
421420
}
422421

423-
unsigned char String::concat(double num) {
422+
bool String::concat(double num) {
424423
char buf[20];
425424
char* string = dtostrf(num, 4, 2, buf);
426425
return concat(string, strlen(string));
427426
}
428427

429-
unsigned char String::concat(long long num) {
428+
bool String::concat(long long num) {
430429
char buf[2 + 3 * sizeof(long long)];
431430
return concat(buf, sprintf(buf, "%lld", num)); // NOT SURE - NewLib Nano ... does it support %lld?
432431
}
433432

434-
unsigned char String::concat(unsigned long long num) {
433+
bool String::concat(unsigned long long num) {
435434
char buf[1 + 3 * sizeof(unsigned long long)];
436435
ulltoa(num, buf, 10);
437436
return concat(buf, strlen(buf));
438437
}
439438

440-
unsigned char String::concat(const __FlashStringHelper * str) {
441-
if (!str) return 0;
439+
bool String::concat(const __FlashStringHelper * str) {
440+
if (!str)
441+
return false;
442442
int length = strlen_P((PGM_P)str);
443-
if (length == 0) return 1;
443+
if (length == 0)
444+
return true;
444445
unsigned int newlen = len() + length;
445-
if (!reserve(newlen)) return 0;
446+
if (!reserve(newlen))
447+
return false;
446448
memcpy_P(wbuffer() + len(), (PGM_P)str, length + 1);
447449
setLen(newlen);
448-
return 1;
450+
return true;
449451
}
450452

451453
/*********************************************/
@@ -559,48 +561,48 @@ int String::compareTo(const String &s) const {
559561
return strcmp(buffer(), s.buffer());
560562
}
561563

562-
unsigned char String::equals(const String &s2) const {
564+
bool String::equals(const String &s2) const {
563565
return (len() == s2.len() && compareTo(s2) == 0);
564566
}
565567

566-
unsigned char String::equals(const char *cstr) const {
568+
bool String::equals(const char *cstr) const {
567569
if(len() == 0)
568570
return (cstr == NULL || *cstr == 0);
569571
if(cstr == NULL)
570572
return buffer()[0] == 0;
571573
return strcmp(buffer(), cstr) == 0;
572574
}
573575

574-
unsigned char String::operator<(const String &rhs) const {
576+
bool String::operator<(const String &rhs) const {
575577
return compareTo(rhs) < 0;
576578
}
577579

578-
unsigned char String::operator>(const String &rhs) const {
580+
bool String::operator>(const String &rhs) const {
579581
return compareTo(rhs) > 0;
580582
}
581583

582-
unsigned char String::operator<=(const String &rhs) const {
584+
bool String::operator<=(const String &rhs) const {
583585
return compareTo(rhs) <= 0;
584586
}
585587

586-
unsigned char String::operator>=(const String &rhs) const {
588+
bool String::operator>=(const String &rhs) const {
587589
return compareTo(rhs) >= 0;
588590
}
589591

590-
unsigned char String::equalsIgnoreCase(const String &s2) const {
592+
bool String::equalsIgnoreCase(const String &s2) const {
591593
if(this == &s2)
592-
return 1;
594+
return true;
593595
if(len() != s2.len())
594-
return 0;
596+
return false;
595597
if(len() == 0)
596-
return 1;
598+
return true;
597599
const char *p1 = buffer();
598600
const char *p2 = s2.buffer();
599601
while(*p1) {
600602
if(tolower(*p1++) != tolower(*p2++))
601-
return 0;
603+
return false;
602604
}
603-
return 1;
605+
return true;
604606
}
605607

606608
unsigned char String::equalsConstantTime(const String &s2) const {
@@ -630,21 +632,21 @@ unsigned char String::equalsConstantTime(const String &s2) const {
630632
return (equalcond & diffcond); //bitwise AND
631633
}
632634

633-
unsigned char String::startsWith(const String &s2) const {
635+
bool String::startsWith(const String &s2) const {
634636
if(len() < s2.len())
635-
return 0;
637+
return false;
636638
return startsWith(s2, 0);
637639
}
638640

639-
unsigned char String::startsWith(const String &s2, unsigned int offset) const {
641+
bool String::startsWith(const String &s2, unsigned int offset) const {
640642
if(offset > (unsigned)(len() - s2.len()) || !buffer() || !s2.buffer())
641-
return 0;
643+
return false;
642644
return strncmp(&buffer()[offset], s2.buffer(), s2.len()) == 0;
643645
}
644646

645-
unsigned char String::endsWith(const String &s2) const {
647+
bool String::endsWith(const String &s2) const {
646648
if(len() < s2.len() || !buffer() || !s2.buffer())
647-
return 0;
649+
return false;
648650
return strcmp(&buffer()[len() - s2.len()], s2.buffer()) == 0;
649651
}
650652

0 commit comments

Comments
 (0)