Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bool as return value for logical operations #147

Merged
merged 1 commit into from
May 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 41 additions & 41 deletions api/String.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,25 +166,25 @@ void String::invalidate(void)
capacity = len = 0;
}

unsigned char String::reserve(unsigned int size)
bool String::reserve(unsigned int size)
{
if (buffer && capacity >= size) return 1;
if (changeBuffer(size)) {
if (len == 0) buffer[0] = 0;
return 1;
return true;
}
return 0;
return false;
}

unsigned char String::changeBuffer(unsigned int maxStrLen)
bool String::changeBuffer(unsigned int maxStrLen)
{
char *newbuffer = (char *)realloc(buffer, maxStrLen + 1);
if (newbuffer) {
buffer = newbuffer;
capacity = maxStrLen;
return 1;
return true;
}
return 0;
return false;
}

/*********************************************/
Expand Down Expand Up @@ -281,93 +281,93 @@ String & String::operator = (const __FlashStringHelper *pstr)
/* concat */
/*********************************************/

unsigned char String::concat(const String &s)
bool String::concat(const String &s)
{
return concat(s.buffer, s.len);
}

unsigned char String::concat(const char *cstr, unsigned int length)
bool String::concat(const char *cstr, unsigned int length)
{
unsigned int newlen = len + length;
if (!cstr) return 0;
if (length == 0) return 1;
if (!reserve(newlen)) return 0;
if (!cstr) return false;
if (length == 0) return true;
if (!reserve(newlen)) return false;
memcpy(buffer + len, cstr, length);
len = newlen;
buffer[len] = '\0';
return 1;
return true;
}

unsigned char String::concat(const char *cstr)
bool String::concat(const char *cstr)
{
if (!cstr) return 0;
if (!cstr) return false;
return concat(cstr, strlen(cstr));
}

unsigned char String::concat(char c)
bool String::concat(char c)
{
return concat(&c, 1);
}

unsigned char String::concat(unsigned char num)
bool String::concat(unsigned char num)
{
char buf[1 + 3 * sizeof(unsigned char)];
itoa(num, buf, 10);
return concat(buf);
}

unsigned char String::concat(int num)
bool String::concat(int num)
{
char buf[2 + 3 * sizeof(int)];
itoa(num, buf, 10);
return concat(buf);
}

unsigned char String::concat(unsigned int num)
bool String::concat(unsigned int num)
{
char buf[1 + 3 * sizeof(unsigned int)];
utoa(num, buf, 10);
return concat(buf);
}

unsigned char String::concat(long num)
bool String::concat(long num)
{
char buf[2 + 3 * sizeof(long)];
ltoa(num, buf, 10);
return concat(buf);
}

unsigned char String::concat(unsigned long num)
bool String::concat(unsigned long num)
{
char buf[1 + 3 * sizeof(unsigned long)];
ultoa(num, buf, 10);
return concat(buf);
}

unsigned char String::concat(float num)
bool String::concat(float num)
{
char buf[20];
char* string = dtostrf(num, 4, 2, buf);
return concat(string);
}

unsigned char String::concat(double num)
bool String::concat(double num)
{
char buf[20];
char* string = dtostrf(num, 4, 2, buf);
return concat(string);
}

unsigned char String::concat(const __FlashStringHelper * str)
bool String::concat(const __FlashStringHelper * str)
{
if (!str) return 0;
if (!str) return false;
int length = strlen_P((const char *) str);
if (length == 0) return 1;
if (length == 0) return true;
unsigned int newlen = len + length;
if (!reserve(newlen)) return 0;
if (!reserve(newlen)) return false;
strcpy_P(buffer + len, (const char *) str);
len = newlen;
return 1;
return true;
}

/*********************************************/
Expand Down Expand Up @@ -475,46 +475,46 @@ int String::compareTo(const char *cstr) const
return strcmp(buffer, cstr);
}

unsigned char String::equals(const String &s2) const
bool String::equals(const String &s2) const
{
return (len == s2.len && compareTo(s2) == 0);
}

unsigned char String::equals(const char *cstr) const
bool String::equals(const char *cstr) const
{
if (len == 0) return (cstr == NULL || *cstr == 0);
if (cstr == NULL) return buffer[0] == 0;
return strcmp(buffer, cstr) == 0;
}

unsigned char String::equalsIgnoreCase( const String &s2 ) const
bool String::equalsIgnoreCase( const String &s2 ) const
{
if (this == &s2) return 1;
if (len != s2.len) return 0;
if (len == 0) return 1;
if (this == &s2) return true;
if (len != s2.len) return false;
if (len == 0) return true;
const char *p1 = buffer;
const char *p2 = s2.buffer;
while (*p1) {
if (tolower(*p1++) != tolower(*p2++)) return 0;
if (tolower(*p1++) != tolower(*p2++)) return false;
}
return 1;
return true;
}

unsigned char String::startsWith( const String &s2 ) const
bool String::startsWith( const String &s2 ) const
{
if (len < s2.len) return 0;
if (len < s2.len) return false;
return startsWith(s2, 0);
}

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

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

Expand Down
84 changes: 42 additions & 42 deletions api/String.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class String
// return true on success, false on failure (in which case, the string
// is left unchanged). reserve(0), if successful, will validate an
// invalid string (i.e., "if (s)" will be true afterwards)
unsigned char reserve(unsigned int size);
bool reserve(unsigned int size);
inline unsigned int length(void) const {return len;}

// creates a copy of the assigned value. if the value is null or
Expand All @@ -109,19 +109,19 @@ class String
// returns true on success, false on failure (in which case, the string
// is left unchanged). if the argument is null or invalid, the
// concatenation is considered unsucessful.
unsigned char concat(const String &str);
unsigned char concat(const char *cstr);
unsigned char concat(const char *cstr, unsigned int length);
unsigned char concat(const uint8_t *cstr, unsigned int length) {return concat((const char*)cstr, length);}
unsigned char concat(char c);
unsigned char concat(unsigned char num);
unsigned char concat(int num);
unsigned char concat(unsigned int num);
unsigned char concat(long num);
unsigned char concat(unsigned long num);
unsigned char concat(float num);
unsigned char concat(double num);
unsigned char concat(const __FlashStringHelper * str);
bool concat(const String &str);
bool concat(const char *cstr);
bool concat(const char *cstr, unsigned int length);
bool concat(const uint8_t *cstr, unsigned int length) {return concat((const char*)cstr, length);}
bool concat(char c);
bool concat(unsigned char num);
bool concat(int num);
bool concat(unsigned int num);
bool concat(long num);
bool concat(unsigned long num);
bool concat(float num);
bool concat(double num);
bool concat(const __FlashStringHelper * str);

// if there's not enough memory for the concatenated value, the string
// will be left unchanged (but this isn't signalled in any way)
Expand Down Expand Up @@ -153,33 +153,33 @@ class String
operator StringIfHelperType() const { return buffer ? &String::StringIfHelper : 0; }
int compareTo(const String &s) const;
int compareTo(const char *cstr) const;
unsigned char equals(const String &s) const;
unsigned char equals(const char *cstr) const;

friend unsigned char operator == (const String &a, const String &b) { return a.equals(b); }
friend unsigned char operator == (const String &a, const char *b) { return a.equals(b); }
friend unsigned char operator == (const char *a, const String &b) { return b == a; }
friend unsigned char operator < (const String &a, const String &b) { return a.compareTo(b) < 0; }
friend unsigned char operator < (const String &a, const char *b) { return a.compareTo(b) < 0; }
friend unsigned char operator < (const char *a, const String &b) { return b.compareTo(a) > 0; }

friend unsigned char operator != (const String &a, const String &b) { return !(a == b); }
friend unsigned char operator != (const String &a, const char *b) { return !(a == b); }
friend unsigned char operator != (const char *a, const String &b) { return !(a == b); }
friend unsigned char operator > (const String &a, const String &b) { return b < a; }
friend unsigned char operator > (const String &a, const char *b) { return b < a; }
friend unsigned char operator > (const char *a, const String &b) { return b < a; }
friend unsigned char operator <= (const String &a, const String &b) { return !(b < a); }
friend unsigned char operator <= (const String &a, const char *b) { return !(b < a); }
friend unsigned char operator <= (const char *a, const String &b) { return !(b < a); }
friend unsigned char operator >= (const String &a, const String &b) { return !(a < b); }
friend unsigned char operator >= (const String &a, const char *b) { return !(a < b); }
friend unsigned char operator >= (const char *a, const String &b) { return !(a < b); }

unsigned char equalsIgnoreCase(const String &s) const;
unsigned char startsWith( const String &prefix) const;
unsigned char startsWith(const String &prefix, unsigned int offset) const;
unsigned char endsWith(const String &suffix) const;
bool equals(const String &s) const;
bool equals(const char *cstr) const;

friend bool operator == (const String &a, const String &b) { return a.equals(b); }
friend bool operator == (const String &a, const char *b) { return a.equals(b); }
friend bool operator == (const char *a, const String &b) { return b == a; }
friend bool operator < (const String &a, const String &b) { return a.compareTo(b) < 0; }
friend bool operator < (const String &a, const char *b) { return a.compareTo(b) < 0; }
friend bool operator < (const char *a, const String &b) { return b.compareTo(a) > 0; }

friend bool operator != (const String &a, const String &b) { return !(a == b); }
friend bool operator != (const String &a, const char *b) { return !(a == b); }
friend bool operator != (const char *a, const String &b) { return !(a == b); }
friend bool operator > (const String &a, const String &b) { return b < a; }
friend bool operator > (const String &a, const char *b) { return b < a; }
friend bool operator > (const char *a, const String &b) { return b < a; }
friend bool operator <= (const String &a, const String &b) { return !(b < a); }
friend bool operator <= (const String &a, const char *b) { return !(b < a); }
friend bool operator <= (const char *a, const String &b) { return !(b < a); }
friend bool operator >= (const String &a, const String &b) { return !(a < b); }
friend bool operator >= (const String &a, const char *b) { return !(a < b); }
friend bool operator >= (const char *a, const String &b) { return !(a < b); }

bool equalsIgnoreCase(const String &s) const;
bool startsWith( const String &prefix) const;
bool startsWith(const String &prefix, unsigned int offset) const;
bool endsWith(const String &suffix) const;

// character acccess
char charAt(unsigned int index) const;
Expand Down Expand Up @@ -228,7 +228,7 @@ class String
protected:
void init(void);
void invalidate(void);
unsigned char changeBuffer(unsigned int maxStrLen);
bool changeBuffer(unsigned int maxStrLen);

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