Commit ab309e40 authored by Earle F. Philhower, III's avatar Earle F. Philhower, III Committed by Me No Dev

Copy ESP8266 String w/SSO to ESP32 repo (#2715)

I redid the ESP8266 WString library to enable small string optimization
(SSO) a while back, and think it would be helpful even on the ESP32 with
its higher memory complement.

SSO avoids lots of tiny mallocs() on the heap which cause fragmentation
by using the memory in the class object itself to store the actual
string and only mallocing() for buffers that are larger than what can
fit in thie class object.  Modern C++ std::string implementations have
this optimization as well, but since we're using Arduino strings we had
to roll our own.
parent 932666a0
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
StreamString.cpp StreamString.cpp
Copyright (c) 2015 Markus Sattler. All rights reserved. Copyright (c) 2015 Markus Sattler. All rights reserved.
This file is part of the esp8266 core for Arduino environment.
This library is free software; you can redistribute it and/or This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public modify it under the terms of the GNU Lesser General Public
...@@ -22,31 +23,27 @@ ...@@ -22,31 +23,27 @@
#include <Arduino.h> #include <Arduino.h>
#include "StreamString.h" #include "StreamString.h"
size_t StreamString::write(const uint8_t *data, size_t size) size_t StreamString::write(const uint8_t *data, size_t size) {
{
if(size && data) { if(size && data) {
if(reserve(length() + size + 1)) { if(reserve(length() + size + 1)) {
memcpy((void *) (buffer + len), (const void *) data, size); memcpy((void *) (wbuffer() + len()), (const void *) data, size);
len += size; setLen(len() + size);
*(buffer + len) = 0x00; // add null for string end *(wbuffer() + len()) = 0x00; // add null for string end
return size; return size;
} }
} }
return 0; return 0;
} }
size_t StreamString::write(uint8_t data) size_t StreamString::write(uint8_t data) {
{
return concat((char) data); return concat((char) data);
} }
int StreamString::available() int StreamString::available() {
{
return length(); return length();
} }
int StreamString::read() int StreamString::read() {
{
if(length()) { if(length()) {
char c = charAt(0); char c = charAt(0);
remove(0, 1); remove(0, 1);
...@@ -56,8 +53,7 @@ int StreamString::read() ...@@ -56,8 +53,7 @@ int StreamString::read()
return -1; return -1;
} }
int StreamString::peek() int StreamString::peek() {
{
if(length()) { if(length()) {
char c = charAt(0); char c = charAt(0);
return c; return c;
...@@ -65,7 +61,6 @@ int StreamString::peek() ...@@ -65,7 +61,6 @@ int StreamString::peek()
return -1; return -1;
} }
void StreamString::flush() void StreamString::flush() {
{
} }
...@@ -3,8 +3,8 @@ ...@@ -3,8 +3,8 @@
...mostly rewritten by Paul Stoffregen... ...mostly rewritten by Paul Stoffregen...
Copyright (c) 2009-10 Hernando Barragan. All rights reserved. Copyright (c) 2009-10 Hernando Barragan. All rights reserved.
Copyright 2011, Paul Stoffregen, paul@pjrc.com Copyright 2011, Paul Stoffregen, paul@pjrc.com
Modified by Ivan Grokhotkov, 2014 - ESP31B support Modified by Ivan Grokhotkov, 2014 - esp8266 support
Modified by Michael C. Miller, 2015 - ESP31B progmem support Modified by Michael C. Miller, 2015 - esp8266 progmem support
This library is free software; you can redistribute it and/or This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public modify it under the terms of the GNU Lesser General Public
...@@ -21,47 +21,43 @@ ...@@ -21,47 +21,43 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/ */
#include <Arduino.h>
#include "WString.h" #include "WString.h"
#include "stdlib_noniso.h" #include "stdlib_noniso.h"
#include "esp32-hal-log.h"
//extern "C" {
//#include "esp_common.h"
//}
/*********************************************/ /*********************************************/
/* Constructors */ /* Constructors */
/*********************************************/ /*********************************************/
String::String(const char *cstr) String::String(const char *cstr) {
{
init(); init();
if(cstr) { if(cstr)
copy(cstr, strlen(cstr)); copy(cstr, strlen(cstr));
}
} }
String::String(const String &value) String::String(const String &value) {
{
init(); init();
*this = value; *this = value;
} }
String::String(const __FlashStringHelper *pstr) {
init();
*this = pstr; // see operator =
}
#ifdef __GXX_EXPERIMENTAL_CXX0X__ #ifdef __GXX_EXPERIMENTAL_CXX0X__
String::String(String &&rval) String::String(String &&rval) {
{
init(); init();
move(rval); move(rval);
} }
String::String(StringSumHelper &&rval) String::String(StringSumHelper &&rval) {
{
init(); init();
move(rval); move(rval);
} }
#endif #endif
String::String(char c) String::String(char c) {
{
init(); init();
char buf[2]; char buf[2];
buf[0] = c; buf[0] = c;
...@@ -69,120 +65,134 @@ String::String(char c) ...@@ -69,120 +65,134 @@ String::String(char c)
*this = buf; *this = buf;
} }
String::String(unsigned char value, unsigned char base) String::String(unsigned char value, unsigned char base) {
{
init(); init();
char buf[1 + 8 * sizeof(unsigned char)]; char buf[1 + 8 * sizeof(unsigned char)];
utoa(value, buf, base); utoa(value, buf, base);
*this = buf; *this = buf;
} }
String::String(int value, unsigned char base) String::String(int value, unsigned char base) {
{
init(); init();
char buf[2 + 8 * sizeof(int)]; char buf[2 + 8 * sizeof(int)];
itoa(value, buf, base); if (base == 10) {
sprintf(buf, "%d", value);
} else {
itoa(value, buf, base);
}
*this = buf; *this = buf;
} }
String::String(unsigned int value, unsigned char base) String::String(unsigned int value, unsigned char base) {
{
init(); init();
char buf[1 + 8 * sizeof(unsigned int)]; char buf[1 + 8 * sizeof(unsigned int)];
utoa(value, buf, base); utoa(value, buf, base);
*this = buf; *this = buf;
} }
String::String(long value, unsigned char base) String::String(long value, unsigned char base) {
{
init(); init();
char buf[2 + 8 * sizeof(long)]; char buf[2 + 8 * sizeof(long)];
ltoa(value, buf, base); if (base==10) {
sprintf(buf, "%ld", value);
} else {
ltoa(value, buf, base);
}
*this = buf; *this = buf;
} }
String::String(unsigned long value, unsigned char base) String::String(unsigned long value, unsigned char base) {
{
init(); init();
char buf[1 + 8 * sizeof(unsigned long)]; char buf[1 + 8 * sizeof(unsigned long)];
ultoa(value, buf, base); ultoa(value, buf, base);
*this = buf; *this = buf;
} }
String::String(float value, unsigned char decimalPlaces) String::String(float value, unsigned char decimalPlaces) {
{
init(); init();
char buf[33]; char buf[33];
*this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf); *this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
} }
String::String(double value, unsigned char decimalPlaces) String::String(double value, unsigned char decimalPlaces) {
{
init(); init();
char buf[33]; char buf[33];
*this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf); *this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
} }
String::~String() String::~String() {
{ invalidate();
if(buffer) {
free(buffer);
}
init();
} }
// /*********************************************/ // /*********************************************/
// /* Memory Management */ // /* Memory Management */
// /*********************************************/ // /*********************************************/
inline void String::init(void) inline void String::init(void) {
{ setSSO(false);
buffer = NULL; setCapacity(0);
capacity = 0; setLen(0);
len = 0; setBuffer(nullptr);
} }
void String::invalidate(void) void String::invalidate(void) {
{ if(!sso() && wbuffer())
if(buffer) { free(wbuffer());
free(buffer);
}
init(); init();
} }
unsigned char String::reserve(unsigned int size) unsigned char String::reserve(unsigned int size) {
{ if(buffer() && capacity() >= size)
if(buffer && capacity >= size) {
return 1; return 1;
}
if(changeBuffer(size)) { if(changeBuffer(size)) {
if(len == 0) { if(len() == 0)
buffer[0] = 0; wbuffer()[0] = 0;
}
return 1; return 1;
} }
return 0; return 0;
} }
unsigned char String::changeBuffer(unsigned int maxStrLen) unsigned char String::changeBuffer(unsigned int maxStrLen) {
{ // Can we use SSO here to avoid allocation?
size_t newSize = ((maxStrLen + 16) & (~0xf)) - 1; if (maxStrLen < sizeof(sso_buf)) {
char *newbuffer = (char *) realloc(buffer, newSize+1); if (sso() || !buffer()) {
// Already using SSO, nothing to do
setSSO(true);
return 1;
} else { // if bufptr && !sso()
// Using bufptr, need to shrink into sso_buff
char temp[sizeof(sso_buf)];
memcpy(temp, buffer(), maxStrLen);
free(wbuffer());
setSSO(true);
memcpy(wbuffer(), temp, maxStrLen);
return 1;
}
}
// Fallthrough to normal allocator
size_t newSize = (maxStrLen + 16) & (~0xf);
// Make sure we can fit newsize in the buffer
if (newSize > CAPACITY_MAX) {
return false;
}
uint16_t oldLen = len();
char *newbuffer = (char *) realloc(sso() ? nullptr : wbuffer(), newSize);
if(newbuffer) { if(newbuffer) {
if(newSize > len){ size_t oldSize = capacity() + 1; // include NULL.
if(newSize > capacity){ if (sso()) {
memset(newbuffer+capacity, 0, newSize-capacity); // Copy the SSO buffer into allocated space
} memcpy(newbuffer, sso_buf, sizeof(sso_buf));
} else {
//new buffer can not fit the old len
newbuffer[newSize] = 0;
len = newSize;
} }
capacity = newSize; if (newSize > oldSize)
buffer = newbuffer; {
memset(newbuffer + oldSize, 0, newSize - oldSize);
}
setSSO(false);
setCapacity(newSize - 1);
setLen(oldLen); // Needed in case of SSO where len() never existed
setBuffer(newbuffer);
return 1; return 1;
} }
log_e("realloc failed! Buffer unchanged");
return 0; return 0;
} }
...@@ -190,84 +200,88 @@ unsigned char String::changeBuffer(unsigned int maxStrLen) ...@@ -190,84 +200,88 @@ unsigned char String::changeBuffer(unsigned int maxStrLen)
// /* Copy and Move */ // /* Copy and Move */
// /*********************************************/ // /*********************************************/
String & String::copy(const char *cstr, unsigned int length) String & String::copy(const char *cstr, unsigned int length) {
{
if(!reserve(length)) { if(!reserve(length)) {
invalidate(); invalidate();
return *this; return *this;
} }
len = length; setLen(length);
strcpy(buffer, cstr); strcpy(wbuffer(), cstr);
return *this; return *this;
} }
String & String::copy(const __FlashStringHelper *pstr, unsigned int length) String & String::copy(const __FlashStringHelper *pstr, unsigned int length) {
{ if (!reserve(length)) {
return copy(reinterpret_cast<const char *>(pstr), length); invalidate();
return *this;
}
setLen(length);
strcpy_P(wbuffer(), (PGM_P)pstr);
return *this;
} }
#ifdef __GXX_EXPERIMENTAL_CXX0X__ #ifdef __GXX_EXPERIMENTAL_CXX0X__
void String::move(String &rhs) void String::move(String &rhs) {
{ if(buffer()) {
if(buffer) { if(capacity() >= rhs.len()) {
if(capacity >= rhs.len) { strcpy(wbuffer(), rhs.buffer());
strcpy(buffer, rhs.buffer); setLen(rhs.len());
len = rhs.len; rhs.invalidate();
rhs.len = 0;
return; return;
} else { } else {
free(buffer); if (!sso()) {
free(wbuffer());
setBuffer(nullptr);
}
} }
} }
buffer = rhs.buffer; if (rhs.sso()) {
capacity = rhs.capacity; setSSO(true);
len = rhs.len; memmove(sso_buf, rhs.sso_buf, sizeof(sso_buf));
rhs.buffer = NULL; } else {
rhs.capacity = 0; setSSO(false);
rhs.len = 0; setBuffer(rhs.wbuffer());
}
setCapacity(rhs.capacity());
setLen(rhs.len());
rhs.setSSO(false);
rhs.setCapacity(0);
rhs.setLen(0);
rhs.setBuffer(nullptr);
} }
#endif #endif
String & String::operator =(const String &rhs) String & String::operator =(const String &rhs) {
{ if(this == &rhs)
if(this == &rhs) {
return *this; return *this;
}
if(rhs.buffer) { if(rhs.buffer())
copy(rhs.buffer, rhs.len); copy(rhs.buffer(), rhs.len());
} else { else
invalidate(); invalidate();
}
return *this; return *this;
} }
#ifdef __GXX_EXPERIMENTAL_CXX0X__ #ifdef __GXX_EXPERIMENTAL_CXX0X__
String & String::operator =(String &&rval) String & String::operator =(String &&rval) {
{ if(this != &rval)
if(this != &rval) {
move(rval); move(rval);
}
return *this; return *this;
} }
String & String::operator =(StringSumHelper &&rval) String & String::operator =(StringSumHelper &&rval) {
{ if(this != &rval)
if(this != &rval) {
move(rval); move(rval);
}
return *this; return *this;
} }
#endif #endif
String & String::operator =(const char *cstr) String & String::operator =(const char *cstr) {
{ if(cstr)
if(cstr) {
copy(cstr, strlen(cstr)); copy(cstr, strlen(cstr));
} else { else
invalidate(); invalidate();
}
return *this; return *this;
} }
...@@ -284,196 +298,184 @@ String & String::operator = (const __FlashStringHelper *pstr) ...@@ -284,196 +298,184 @@ String & String::operator = (const __FlashStringHelper *pstr)
// /* concat */ // /* concat */
// /*********************************************/ // /*********************************************/
unsigned char String::concat(const String &s) unsigned char String::concat(const String &s) {
{ // Special case if we're concatting ourself (s += s;) since we may end up
return concat(s.buffer, s.len); // realloc'ing the buffer and moving s.buffer in the method called
if (&s == this) {
unsigned int newlen = 2 * len();
if (!s.buffer())
return 0;
if (s.len() == 0)
return 1;
if (!reserve(newlen))
return 0;
memcpy(wbuffer() + len(), buffer(), len());
setLen(newlen);
wbuffer()[len()] = 0;
return 1;
} else {
return concat(s.buffer(), s.len());
}
} }
unsigned char String::concat(const char *cstr, unsigned int length) unsigned char String::concat(const char *cstr, unsigned int length) {
{ unsigned int newlen = len() + length;
unsigned int newlen = len + length; if(!cstr)
if(!cstr) {
return 0; return 0;
} if(length == 0)
if(length == 0) {
return 1; return 1;
} if(!reserve(newlen))
if(!reserve(newlen)) {
return 0; return 0;
} strcpy(wbuffer() + len(), cstr);
strcpy(buffer + len, cstr); setLen(newlen);
len = newlen;
return 1; return 1;
} }
unsigned char String::concat(const char *cstr) unsigned char String::concat(const char *cstr) {
{ if(!cstr)
if(!cstr) {
return 0; return 0;
}
return concat(cstr, strlen(cstr)); return concat(cstr, strlen(cstr));
} }
unsigned char String::concat(char c) unsigned char String::concat(char c) {
{
char buf[2]; char buf[2];
buf[0] = c; buf[0] = c;
buf[1] = 0; buf[1] = 0;
return concat(buf, 1); return concat(buf, 1);
} }
unsigned char String::concat(unsigned char num) unsigned char String::concat(unsigned char num) {
{
char buf[1 + 3 * sizeof(unsigned char)]; char buf[1 + 3 * sizeof(unsigned char)];
itoa(num, buf, 10); sprintf(buf, "%d", num);
return concat(buf, strlen(buf)); return concat(buf, strlen(buf));
} }
unsigned char String::concat(int num) unsigned char String::concat(int num) {
{
char buf[2 + 3 * sizeof(int)]; char buf[2 + 3 * sizeof(int)];
itoa(num, buf, 10); sprintf(buf, "%d", num);
return concat(buf, strlen(buf)); return concat(buf, strlen(buf));
} }
unsigned char String::concat(unsigned int num) unsigned char String::concat(unsigned int num) {
{
char buf[1 + 3 * sizeof(unsigned int)]; char buf[1 + 3 * sizeof(unsigned int)];
utoa(num, buf, 10); utoa(num, buf, 10);
return concat(buf, strlen(buf)); return concat(buf, strlen(buf));
} }
unsigned char String::concat(long num) unsigned char String::concat(long num) {
{
char buf[2 + 3 * sizeof(long)]; char buf[2 + 3 * sizeof(long)];
ltoa(num, buf, 10); sprintf(buf, "%ld", num);
return concat(buf, strlen(buf)); return concat(buf, strlen(buf));
} }
unsigned char String::concat(unsigned long num) unsigned char String::concat(unsigned long num) {
{
char buf[1 + 3 * sizeof(unsigned long)]; char buf[1 + 3 * sizeof(unsigned long)];
ultoa(num, buf, 10); ultoa(num, buf, 10);
return concat(buf, strlen(buf)); return concat(buf, strlen(buf));
} }
unsigned char String::concat(float num) unsigned char String::concat(float num) {
{
char buf[20]; char buf[20];
char* string = dtostrf(num, 4, 2, buf); char* string = dtostrf(num, 4, 2, buf);
return concat(string, strlen(string)); return concat(string, strlen(string));
} }
unsigned char String::concat(double num) unsigned char String::concat(double num) {
{
char buf[20]; char buf[20];
char* string = dtostrf(num, 4, 2, buf); char* string = dtostrf(num, 4, 2, buf);
return concat(string, strlen(string)); return concat(string, strlen(string));
} }
unsigned char String::concat(const __FlashStringHelper * str) unsigned char String::concat(const __FlashStringHelper * str) {
{ if (!str) return 0;
return concat(reinterpret_cast<const char *>(str)); int length = strlen_P((PGM_P)str);
if (length == 0) return 1;
unsigned int newlen = len() + length;
if (!reserve(newlen)) return 0;
strcpy_P(wbuffer() + len(), (PGM_P)str);
setLen(newlen);
return 1;
} }
/*********************************************/ /*********************************************/
/* Concatenate */ /* Concatenate */
/*********************************************/ /*********************************************/
StringSumHelper & operator +(const StringSumHelper &lhs, const String &rhs) StringSumHelper & operator +(const StringSumHelper &lhs, const String &rhs) {
{
StringSumHelper &a = const_cast<StringSumHelper&>(lhs); StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
if(!a.concat(rhs.buffer, rhs.len)) { if(!a.concat(rhs.buffer(), rhs.len()))
a.invalidate(); a.invalidate();
}
return a; return a;
} }
StringSumHelper & operator +(const StringSumHelper &lhs, const char *cstr) StringSumHelper & operator +(const StringSumHelper &lhs, const char *cstr) {
{
StringSumHelper &a = const_cast<StringSumHelper&>(lhs); StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
if(!cstr || !a.concat(cstr, strlen(cstr))) { if(!cstr || !a.concat(cstr, strlen(cstr)))
a.invalidate(); a.invalidate();
}
return a; return a;
} }
StringSumHelper & operator +(const StringSumHelper &lhs, char c) StringSumHelper & operator +(const StringSumHelper &lhs, char c) {
{
StringSumHelper &a = const_cast<StringSumHelper&>(lhs); StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
if(!a.concat(c)) { if(!a.concat(c))
a.invalidate(); a.invalidate();
}
return a; return a;
} }
StringSumHelper & operator +(const StringSumHelper &lhs, unsigned char num) StringSumHelper & operator +(const StringSumHelper &lhs, unsigned char num) {
{
StringSumHelper &a = const_cast<StringSumHelper&>(lhs); StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
if(!a.concat(num)) { if(!a.concat(num))
a.invalidate(); a.invalidate();
}
return a; return a;
} }
StringSumHelper & operator +(const StringSumHelper &lhs, int num) StringSumHelper & operator +(const StringSumHelper &lhs, int num) {
{
StringSumHelper &a = const_cast<StringSumHelper&>(lhs); StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
if(!a.concat(num)) { if(!a.concat(num))
a.invalidate(); a.invalidate();
}
return a; return a;
} }
StringSumHelper & operator +(const StringSumHelper &lhs, unsigned int num) StringSumHelper & operator +(const StringSumHelper &lhs, unsigned int num) {
{
StringSumHelper &a = const_cast<StringSumHelper&>(lhs); StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
if(!a.concat(num)) { if(!a.concat(num))
a.invalidate(); a.invalidate();
}
return a; return a;
} }
StringSumHelper & operator +(const StringSumHelper &lhs, long num) StringSumHelper & operator +(const StringSumHelper &lhs, long num) {
{
StringSumHelper &a = const_cast<StringSumHelper&>(lhs); StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
if(!a.concat(num)) { if(!a.concat(num))
a.invalidate(); a.invalidate();
}
return a; return a;
} }
StringSumHelper & operator +(const StringSumHelper &lhs, unsigned long num) StringSumHelper & operator +(const StringSumHelper &lhs, unsigned long num) {
{
StringSumHelper &a = const_cast<StringSumHelper&>(lhs); StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
if(!a.concat(num)) { if(!a.concat(num))
a.invalidate(); a.invalidate();
}
return a; return a;
} }
StringSumHelper & operator +(const StringSumHelper &lhs, float num) StringSumHelper & operator +(const StringSumHelper &lhs, float num) {
{
StringSumHelper &a = const_cast<StringSumHelper&>(lhs); StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
if(!a.concat(num)) { if(!a.concat(num))
a.invalidate(); a.invalidate();
}
return a; return a;
} }
StringSumHelper & operator +(const StringSumHelper &lhs, double num) StringSumHelper & operator +(const StringSumHelper &lhs, double num) {
{
StringSumHelper &a = const_cast<StringSumHelper&>(lhs); StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
if(!a.concat(num)) { if(!a.concat(num))
a.invalidate(); a.invalidate();
}
return a; return a;
} }
StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHelper *rhs) StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHelper *rhs)
{ {
StringSumHelper &a = const_cast<StringSumHelper&>(lhs); StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
if (!a.concat(rhs)) a.invalidate(); if (!a.concat(rhs))
a.invalidate();
return a; return a;
} }
...@@ -481,149 +483,145 @@ StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHel ...@@ -481,149 +483,145 @@ StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHel
// /* Comparison */ // /* Comparison */
// /*********************************************/ // /*********************************************/
int String::compareTo(const String &s) const int String::compareTo(const String &s) const {
{ if(!buffer() || !s.buffer()) {
if(!buffer || !s.buffer) { if(s.buffer() && s.len() > 0)
if(s.buffer && s.len > 0) { return 0 - *(unsigned char *) s.buffer();
return 0 - *(unsigned char *) s.buffer; if(buffer() && len() > 0)
} return *(unsigned char *) buffer();
if(buffer && len > 0) {
return *(unsigned char *) buffer;
}
return 0; return 0;
} }
return strcmp(buffer, s.buffer); return strcmp(buffer(), s.buffer());
} }
unsigned char String::equals(const String &s2) const unsigned char String::equals(const String &s2) const {
{ return (len() == s2.len() && compareTo(s2) == 0);
return (len == s2.len && compareTo(s2) == 0);
} }
unsigned char String::equals(const char *cstr) const unsigned char String::equals(const char *cstr) const {
{ if(len() == 0)
if(len == 0) {
return (cstr == NULL || *cstr == 0); return (cstr == NULL || *cstr == 0);
} if(cstr == NULL)
if(cstr == NULL) { return buffer()[0] == 0;
return buffer[0] == 0; return strcmp(buffer(), cstr) == 0;
}
return strcmp(buffer, cstr) == 0;
} }
unsigned char String::operator<(const String &rhs) const unsigned char String::operator<(const String &rhs) const {
{
return compareTo(rhs) < 0; return compareTo(rhs) < 0;
} }
unsigned char String::operator>(const String &rhs) const unsigned char String::operator>(const String &rhs) const {
{
return compareTo(rhs) > 0; return compareTo(rhs) > 0;
} }
unsigned char String::operator<=(const String &rhs) const unsigned char String::operator<=(const String &rhs) const {
{
return compareTo(rhs) <= 0; return compareTo(rhs) <= 0;
} }
unsigned char String::operator>=(const String &rhs) const unsigned char String::operator>=(const String &rhs) const {
{
return compareTo(rhs) >= 0; return compareTo(rhs) >= 0;
} }
unsigned char String::equalsIgnoreCase(const String &s2) const unsigned char String::equalsIgnoreCase(const String &s2) const {
{ if(this == &s2)
if(this == &s2) {
return 1; return 1;
} if(len() != s2.len())
if(len != s2.len) {
return 0; return 0;
} if(len() == 0)
if(len == 0) {
return 1; return 1;
} const char *p1 = buffer();
const char *p1 = buffer; const char *p2 = s2.buffer();
const char *p2 = s2.buffer;
while(*p1) { while(*p1) {
if(tolower(*p1++) != tolower(*p2++)) { if(tolower(*p1++) != tolower(*p2++))
return 0; return 0;
}
} }
return 1; return 1;
} }
unsigned char String::startsWith(const String &s2) const unsigned char String::equalsConstantTime(const String &s2) const {
{ // To avoid possible time-based attacks present function
if(len < s2.len) { // compares given strings in a constant time.
if(len() != s2.len())
return 0; return 0;
//at this point lengths are the same
if(len() == 0)
return 1;
//at this point lenghts are the same and non-zero
const char *p1 = buffer();
const char *p2 = s2.buffer();
unsigned int equalchars = 0;
unsigned int diffchars = 0;
while(*p1) {
if(*p1 == *p2)
++equalchars;
else
++diffchars;
++p1;
++p2;
} }
//the following should force a constant time eval of the condition without a compiler "logical shortcut"
unsigned char equalcond = (equalchars == len());
unsigned char diffcond = (diffchars == 0);
return (equalcond & diffcond); //bitwise AND
}
unsigned char String::startsWith(const String &s2) const {
if(len() < s2.len())
return 0;
return startsWith(s2, 0); return startsWith(s2, 0);
} }
unsigned char String::startsWith(const String &s2, unsigned int offset) const unsigned char String::startsWith(const String &s2, unsigned int offset) const {
{ if(offset > (unsigned)(len() - s2.len()) || !buffer() || !s2.buffer())
if(offset > len - s2.len || !buffer || !s2.buffer) {
return 0; return 0;
} return strncmp(&buffer()[offset], s2.buffer(), s2.len()) == 0;
return strncmp(&buffer[offset], s2.buffer, s2.len) == 0;
} }
unsigned char String::endsWith(const String &s2) const unsigned char String::endsWith(const String &s2) const {
{ if(len() < s2.len() || !buffer() || !s2.buffer())
if(len < s2.len || !buffer || !s2.buffer) {
return 0; return 0;
} return strcmp(&buffer()[len() - s2.len()], s2.buffer()) == 0;
return strcmp(&buffer[len - s2.len], s2.buffer) == 0;
} }
// /*********************************************/ // /*********************************************/
// /* Character Access */ // /* Character Access */
// /*********************************************/ // /*********************************************/
char String::charAt(unsigned int loc) const char String::charAt(unsigned int loc) const {
{
return operator[](loc); return operator[](loc);
} }
void String::setCharAt(unsigned int loc, char c) void String::setCharAt(unsigned int loc, char c) {
{ if(loc < len())
if(loc < len) { wbuffer()[loc] = c;
buffer[loc] = c;
}
} }
char & String::operator[](unsigned int index) char & String::operator[](unsigned int index) {
{
static char dummy_writable_char; static char dummy_writable_char;
if(index >= len || !buffer) { if(index >= len() || !buffer()) {
dummy_writable_char = 0; dummy_writable_char = 0;
return dummy_writable_char; return dummy_writable_char;
} }
return buffer[index]; return wbuffer()[index];
} }
char String::operator[](unsigned int index) const char String::operator[](unsigned int index) const {
{ if(index >= len() || !buffer())
if(index >= len || !buffer) {
return 0; return 0;
} return buffer()[index];
return buffer[index];
} }
void String::getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index) const void String::getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index) const {
{ if(!bufsize || !buf)
if(!bufsize || !buf) {
return; return;
} if(index >= len()) {
if(index >= len) {
buf[0] = 0; buf[0] = 0;
return; return;
} }
unsigned int n = bufsize - 1; unsigned int n = bufsize - 1;
if(n > len - index) { if(n > len() - index)
n = len - index; n = len() - index;
} strncpy((char *) buf, buffer() + index, n);
strncpy((char *) buf, buffer + index, n);
buf[n] = 0; buf[n] = 0;
} }
...@@ -631,104 +629,83 @@ void String::getBytes(unsigned char *buf, unsigned int bufsize, unsigned int ind ...@@ -631,104 +629,83 @@ void String::getBytes(unsigned char *buf, unsigned int bufsize, unsigned int ind
// /* Search */ // /* Search */
// /*********************************************/ // /*********************************************/
int String::indexOf(char c) const int String::indexOf(char c) const {
{
return indexOf(c, 0); return indexOf(c, 0);
} }
int String::indexOf(char ch, unsigned int fromIndex) const int String::indexOf(char ch, unsigned int fromIndex) const {
{ if(fromIndex >= len())
if(fromIndex >= len) {
return -1; return -1;
} const char* temp = strchr(buffer() + fromIndex, ch);
const char* temp = strchr(buffer + fromIndex, ch); if(temp == NULL)
if(temp == NULL) {
return -1; return -1;
} return temp - buffer();
return temp - buffer;
} }
int String::indexOf(const String &s2) const int String::indexOf(const String &s2) const {
{
return indexOf(s2, 0); return indexOf(s2, 0);
} }
int String::indexOf(const String &s2, unsigned int fromIndex) const int String::indexOf(const String &s2, unsigned int fromIndex) const {
{ if(fromIndex >= len())
if(fromIndex >= len) {
return -1; return -1;
} const char *found = strstr(buffer() + fromIndex, s2.buffer());
const char *found = strstr(buffer + fromIndex, s2.buffer); if(found == NULL)
if(found == NULL) {
return -1; return -1;
} return found - buffer();
return found - buffer;
} }
int String::lastIndexOf(char theChar) const int String::lastIndexOf(char theChar) const {
{ return lastIndexOf(theChar, len() - 1);
return lastIndexOf(theChar, len - 1);
} }
int String::lastIndexOf(char ch, unsigned int fromIndex) const int String::lastIndexOf(char ch, unsigned int fromIndex) const {
{ if(fromIndex >= len())
if(fromIndex >= len) {
return -1; return -1;
} char tempchar = buffer()[fromIndex + 1];
char tempchar = buffer[fromIndex + 1]; wbuffer()[fromIndex + 1] = '\0';
buffer[fromIndex + 1] = '\0'; char* temp = strrchr(wbuffer(), ch);
char* temp = strrchr(buffer, ch); wbuffer()[fromIndex + 1] = tempchar;
buffer[fromIndex + 1] = tempchar; if(temp == NULL)
if(temp == NULL) {
return -1; return -1;
} return temp - buffer();
return temp - buffer;
} }
int String::lastIndexOf(const String &s2) const int String::lastIndexOf(const String &s2) const {
{ return lastIndexOf(s2, len() - s2.len());
return lastIndexOf(s2, len - s2.len);
} }
int String::lastIndexOf(const String &s2, unsigned int fromIndex) const int String::lastIndexOf(const String &s2, unsigned int fromIndex) const {
{ if(s2.len() == 0 || len() == 0 || s2.len() > len())
if(s2.len == 0 || len == 0 || s2.len > len) {
return -1; return -1;
} if(fromIndex >= len())
if(fromIndex >= len) { fromIndex = len() - 1;
fromIndex = len - 1;
}
int found = -1; int found = -1;
for(char *p = buffer; p <= buffer + fromIndex; p++) { for(char *p = wbuffer(); p <= wbuffer() + fromIndex; p++) {
p = strstr(p, s2.buffer); p = strstr(p, s2.buffer());
if(!p) { if(!p)
break; break;
} if((unsigned int) (p - wbuffer()) <= fromIndex)
if((unsigned int) (p - buffer) <= fromIndex) { found = p - buffer();
found = p - buffer;
}
} }
return found; return found;
} }
String String::substring(unsigned int left, unsigned int right) const String String::substring(unsigned int left, unsigned int right) const {
{
if(left > right) { if(left > right) {
unsigned int temp = right; unsigned int temp = right;
right = left; right = left;
left = temp; left = temp;
} }
String out; String out;
if(left >= len) { if(left >= len())
return out; return out;
} if(right > len())
if(right > len) { right = len();
right = len; char temp = buffer()[right]; // save the replaced character
} wbuffer()[right] = '\0';
char temp = buffer[right]; // save the replaced character out = wbuffer() + left; // pointer arithmetic
buffer[right] = '\0'; wbuffer()[right] = temp; //restore character
out = buffer + left; // pointer arithmetic
buffer[right] = temp; //restore character
return out; return out;
} }
...@@ -736,182 +713,140 @@ String String::substring(unsigned int left, unsigned int right) const ...@@ -736,182 +713,140 @@ String String::substring(unsigned int left, unsigned int right) const
// /* Modification */ // /* Modification */
// /*********************************************/ // /*********************************************/
void String::replace(char find, char replace) void String::replace(char find, char replace) {
{ if(!buffer())
if(!buffer) {
return; return;
} for(char *p = wbuffer(); *p; p++) {
for(char *p = buffer; *p; p++) { if(*p == find)
if(*p == find) {
*p = replace; *p = replace;
}
} }
} }
void String::replace(const String& find, const String& replace) void String::replace(const String& find, const String& replace) {
{ if(len() == 0 || find.len() == 0)
if(len == 0 || find.len == 0) {
return; return;
} int diff = replace.len() - find.len();
int diff = replace.len - find.len; char *readFrom = wbuffer();
char *readFrom = buffer;
char *foundAt; char *foundAt;
if(diff == 0) { if(diff == 0) {
while((foundAt = strstr(readFrom, find.buffer)) != NULL) { while((foundAt = strstr(readFrom, find.buffer())) != NULL) {
memcpy(foundAt, replace.buffer, replace.len); memmove(foundAt, replace.buffer(), replace.len());
readFrom = foundAt + replace.len; readFrom = foundAt + replace.len();
} }
} else if(diff < 0) { } else if(diff < 0) {
char *writeTo = buffer; char *writeTo = wbuffer();
while((foundAt = strstr(readFrom, find.buffer)) != NULL) { while((foundAt = strstr(readFrom, find.buffer())) != NULL) {
unsigned int n = foundAt - readFrom; unsigned int n = foundAt - readFrom;
memcpy(writeTo, readFrom, n); memmove(writeTo, readFrom, n);
writeTo += n; writeTo += n;
memcpy(writeTo, replace.buffer, replace.len); memmove(writeTo, replace.buffer(), replace.len());
writeTo += replace.len; writeTo += replace.len();
readFrom = foundAt + find.len; readFrom = foundAt + find.len();
len += diff; setLen(len() + diff);
} }
strcpy(writeTo, readFrom); memmove(writeTo, readFrom, strlen(readFrom)+1);
} else { } else {
unsigned int size = len; // compute size needed for result unsigned int size = len(); // compute size needed for result
while((foundAt = strstr(readFrom, find.buffer)) != NULL) { while((foundAt = strstr(readFrom, find.buffer())) != NULL) {
readFrom = foundAt + find.len; readFrom = foundAt + find.len();
size += diff; size += diff;
} }
if(size == len) { if(size == len())
return; return;
} if(size > capacity() && !changeBuffer(size))
if(size > capacity && !changeBuffer(size)) { return; // XXX: tell user!
return; // XXX: tell user! int index = len() - 1;
}
int index = len - 1;
while(index >= 0 && (index = lastIndexOf(find, index)) >= 0) { while(index >= 0 && (index = lastIndexOf(find, index)) >= 0) {
readFrom = buffer + index + find.len; readFrom = wbuffer() + index + find.len();
memmove(readFrom + diff, readFrom, len - (readFrom - buffer)); memmove(readFrom + diff, readFrom, len() - (readFrom - buffer()));
len += diff; int newLen = len() + diff;
buffer[len] = 0; memmove(wbuffer() + index, replace.buffer(), replace.len());
memcpy(buffer + index, replace.buffer, replace.len); setLen(newLen);
wbuffer()[newLen] = 0;
index--; index--;
} }
} }
} }
void String::remove(unsigned int index) void String::remove(unsigned int index) {
{
// Pass the biggest integer as the count. The remove method // Pass the biggest integer as the count. The remove method
// below will take care of truncating it at the end of the // below will take care of truncating it at the end of the
// string. // string.
remove(index, (unsigned int) -1); remove(index, (unsigned int) -1);
} }
void String::remove(unsigned int index, unsigned int count) void String::remove(unsigned int index, unsigned int count) {
{ if(index >= len()) {
if(index >= len) {
return; return;
} }
if(count <= 0) { if(count <= 0) {
return; return;
} }
if(count > len - index) { if(count > len() - index) {
count = len - index; count = len() - index;
} }
char *writeTo = buffer + index; char *writeTo = wbuffer() + index;
len = len - count; unsigned int newlen = len() - count;
strncpy(writeTo, buffer + index + count, len - index); setLen(newlen);
buffer[len] = 0; memmove(writeTo, wbuffer() + index + count, newlen - index);
wbuffer()[newlen] = 0;
} }
void String::toLowerCase(void) void String::toLowerCase(void) {
{ if(!buffer())
if(!buffer) {
return; return;
} for(char *p = wbuffer(); *p; p++) {
for(char *p = buffer; *p; p++) {
*p = tolower(*p); *p = tolower(*p);
} }
} }
void String::toUpperCase(void) void String::toUpperCase(void) {
{ if(!buffer())
if(!buffer) {
return; return;
} for(char *p = wbuffer(); *p; p++) {
for(char *p = buffer; *p; p++) {
*p = toupper(*p); *p = toupper(*p);
} }
} }
void String::trim(void) void String::trim(void) {
{ if(!buffer() || len() == 0)
if(!buffer || len == 0) {
return; return;
} char *begin = wbuffer();
char *begin = buffer; while(isspace(*begin))
while(isspace(*begin)) {
begin++; begin++;
} char *end = wbuffer() + len() - 1;
char *end = buffer + len - 1; while(isspace(*end) && end >= begin)
while(isspace(*end) && end >= begin) {
end--; end--;
} unsigned int newlen = end + 1 - begin;
len = end + 1 - begin; setLen(newlen);
if(begin > buffer) { if(begin > buffer())
memcpy(buffer, begin, len); memmove(wbuffer(), begin, newlen);
} wbuffer()[newlen] = 0;
buffer[len] = 0;
} }
// /*********************************************/ // /*********************************************/
// /* Parsing / Conversion */ // /* Parsing / Conversion */
// /*********************************************/ // /*********************************************/
long String::toInt(void) const long String::toInt(void) const {
{ if (buffer())
if(buffer) { return atol(buffer());
return atol(buffer);
}
return 0; return 0;
} }
float String::toFloat(void) const float String::toFloat(void) const {
{ if (buffer())
if(buffer) { return atof(buffer());
return atof(buffer);
}
return 0; return 0;
} }
double String::toDouble(void) const { double String::toDouble(void) const
if (buffer) { {
return atof(buffer); if (buffer())
} return atof(buffer());
return 0; return 0.0;
} }
unsigned char String::equalsConstantTime(const String &s2) const { // global empty string to allow returning const String& with nothing
// To avoid possible time-based attacks present function
// compares given strings in a constant time. const String emptyString;
if(len != s2.len)
return 0;
//at this point lengths are the same
if(len == 0)
return 1;
//at this point lenghts are the same and non-zero
const char *p1 = buffer;
const char *p2 = s2.buffer;
unsigned int equalchars = 0;
unsigned int diffchars = 0;
while(*p1) {
if(*p1 == *p2)
++equalchars;
else
++diffchars;
++p1;
++p2;
}
//the following should force a constant time eval of the condition without a compiler "logical shortcut"
unsigned char equalcond = (equalchars == len);
unsigned char diffcond = (diffchars == 0);
return (equalcond & diffcond); //bitwise AND
}
...@@ -35,295 +35,292 @@ class StringSumHelper; ...@@ -35,295 +35,292 @@ class StringSumHelper;
// an abstract class used as a means to proide a unique pointer type // an abstract class used as a means to proide a unique pointer type
// but really has no body // but really has no body
class __FlashStringHelper; class __FlashStringHelper;
#define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal))) #define FPSTR(pstr_pointer) (reinterpret_cast<const __FlashStringHelper *>(pstr_pointer))
#define F(string_literal) (FPSTR(PSTR(string_literal)))
// The string class // The string class
class String class String {
{ // use a function pointer to allow for "if (s)" without the
// use a function pointer to allow for "if (s)" without the // complications of an operator bool(). for more information, see:
// complications of an operator bool(). for more information, see: // http://www.artima.com/cppsource/safebool.html
// http://www.artima.com/cppsource/safebool.html typedef void (String::*StringIfHelperType)() const;
typedef void (String::*StringIfHelperType)() const; void StringIfHelper() const {
void StringIfHelper() const }
{
}
public: public:
// constructors // constructors
// creates a copy of the initial value. // creates a copy of the initial value.
// if the initial value is null or invalid, or if memory allocation // if the initial value is null or invalid, or if memory allocation
// fails, the string will be marked as invalid (i.e. "if (s)" will // fails, the string will be marked as invalid (i.e. "if (s)" will
// be false). // be false).
String(const char *cstr = ""); String(const char *cstr = "");
String(const String &str); String(const String &str);
String(const __FlashStringHelper *str) : String(reinterpret_cast<const char *>(str)) {}; String(const __FlashStringHelper *str);
#ifdef __GXX_EXPERIMENTAL_CXX0X__ #ifdef __GXX_EXPERIMENTAL_CXX0X__
String(String &&rval); String(String &&rval);
String(StringSumHelper &&rval); String(StringSumHelper &&rval);
#endif #endif
explicit String(char c); explicit String(char c);
explicit String(unsigned char, unsigned char base = 10); explicit String(unsigned char, unsigned char base = 10);
explicit String(int, unsigned char base = 10); explicit String(int, unsigned char base = 10);
explicit String(unsigned int, unsigned char base = 10); explicit String(unsigned int, unsigned char base = 10);
explicit String(long, unsigned char base = 10); explicit String(long, unsigned char base = 10);
explicit String(unsigned long, unsigned char base = 10); explicit String(unsigned long, unsigned char base = 10);
explicit String(float, unsigned char decimalPlaces = 2); explicit String(float, unsigned char decimalPlaces = 2);
explicit String(double, unsigned char decimalPlaces = 2); explicit String(double, unsigned char decimalPlaces = 2);
~String(void); ~String(void);
// memory management // memory management
// return true on success, false on failure (in which case, the string // return true on success, false on failure (in which case, the string
// is left unchanged). reserve(0), if successful, will validate an // is left unchanged). reserve(0), if successful, will validate an
// invalid string (i.e., "if (s)" will be true afterwards) // invalid string (i.e., "if (s)" will be true afterwards)
unsigned char reserve(unsigned int size); unsigned char reserve(unsigned int size);
inline unsigned int length(void) const inline unsigned int length(void) const {
{ if(buffer()) {
if(buffer) { return len();
return len; } else {
} else { return 0;
return 0; }
} }
}
// creates a copy of the assigned value. if the value is null or // creates a copy of the assigned value. if the value is null or
// invalid, or if the memory allocation fails, the string will be // invalid, or if the memory allocation fails, the string will be
// marked as invalid ("if (s)" will be false). // marked as invalid ("if (s)" will be false).
String & operator =(const String &rhs); String & operator =(const String &rhs);
String & operator =(const char *cstr); String & operator =(const char *cstr);
String & operator = (const __FlashStringHelper *str); String & operator = (const __FlashStringHelper *str);
#ifdef __GXX_EXPERIMENTAL_CXX0X__ #ifdef __GXX_EXPERIMENTAL_CXX0X__
String & operator =(String &&rval); String & operator =(String &&rval);
String & operator =(StringSumHelper &&rval); String & operator =(StringSumHelper &&rval);
#endif #endif
// concatenate (works w/ built-in types) // concatenate (works w/ built-in types)
// 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(char c);
unsigned char concat(unsigned char c);
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);
// if there's not enough memory for the concatenated value, the string
// will be left unchanged (but this isn't signalled in any way)
String & operator +=(const String &rhs) {
concat(rhs);
return (*this);
}
String & operator +=(const char *cstr) {
concat(cstr);
return (*this);
}
String & operator +=(char c) {
concat(c);
return (*this);
}
String & operator +=(unsigned char num) {
concat(num);
return (*this);
}
String & operator +=(int num) {
concat(num);
return (*this);
}
String & operator +=(unsigned int num) {
concat(num);
return (*this);
}
String & operator +=(long num) {
concat(num);
return (*this);
}
String & operator +=(unsigned long num) {
concat(num);
return (*this);
}
String & operator +=(float num) {
concat(num);
return (*this);
}
String & operator +=(double num) {
concat(num);
return (*this);
}
String & operator += (const __FlashStringHelper *str){
concat(str);
return (*this);
}
// returns true on success, false on failure (in which case, the string friend StringSumHelper & operator +(const StringSumHelper &lhs, const String &rhs);
// is left unchanged). if the argument is null or invalid, the friend StringSumHelper & operator +(const StringSumHelper &lhs, const char *cstr);
// concatenation is considered unsucessful. friend StringSumHelper & operator +(const StringSumHelper &lhs, char c);
unsigned char concat(const String &str); friend StringSumHelper & operator +(const StringSumHelper &lhs, unsigned char num);
unsigned char concat(const char *cstr); friend StringSumHelper & operator +(const StringSumHelper &lhs, int num);
unsigned char concat(char c); friend StringSumHelper & operator +(const StringSumHelper &lhs, unsigned int num);
unsigned char concat(unsigned char c); friend StringSumHelper & operator +(const StringSumHelper &lhs, long num);
unsigned char concat(int num); friend StringSumHelper & operator +(const StringSumHelper &lhs, unsigned long num);
unsigned char concat(unsigned int num); friend StringSumHelper & operator +(const StringSumHelper &lhs, float num);
unsigned char concat(long num); friend StringSumHelper & operator +(const StringSumHelper &lhs, double num);
unsigned char concat(unsigned long num); friend StringSumHelper & operator +(const StringSumHelper &lhs, const __FlashStringHelper *rhs);
unsigned char concat(float num);
unsigned char concat(double num);
unsigned char concat(const __FlashStringHelper * str);
// if there's not enough memory for the concatenated value, the string // comparison (only works w/ Strings and "strings")
// will be left unchanged (but this isn't signalled in any way) operator StringIfHelperType() const {
String & operator +=(const String &rhs) return buffer() ? &String::StringIfHelper : 0;
{ }
concat(rhs); int compareTo(const String &s) const;
return (*this); unsigned char equals(const String &s) const;
} unsigned char equals(const char *cstr) const;
String & operator +=(const char *cstr) unsigned char operator ==(const String &rhs) const {
{ return equals(rhs);
concat(cstr); }
return (*this); unsigned char operator ==(const char *cstr) const {
} return equals(cstr);
String & operator +=(char c) }
{ unsigned char operator !=(const String &rhs) const {
concat(c); return !equals(rhs);
return (*this); }
} unsigned char operator !=(const char *cstr) const {
String & operator +=(unsigned char num) return !equals(cstr);
{ }
concat(num); unsigned char operator <(const String &rhs) const;
return (*this); unsigned char operator >(const String &rhs) const;
} unsigned char operator <=(const String &rhs) const;
String & operator +=(int num) unsigned char operator >=(const String &rhs) const;
{ unsigned char equalsIgnoreCase(const String &s) const;
concat(num); unsigned char equalsConstantTime(const String &s) const;
return (*this); unsigned char startsWith(const String &prefix) const;
} unsigned char startsWith(const String &prefix, unsigned int offset) const;
String & operator +=(unsigned int num) unsigned char endsWith(const String &suffix) const;
{
concat(num);
return (*this);
}
String & operator +=(long num)
{
concat(num);
return (*this);
}
String & operator +=(unsigned long num)
{
concat(num);
return (*this);
}
String & operator +=(float num)
{
concat(num);
return (*this);
}
String & operator +=(double num)
{
concat(num);
return (*this);
}
String & operator += (const __FlashStringHelper *str)
{
concat(str);
return (*this);
}
friend StringSumHelper & operator +(const StringSumHelper &lhs, const String &rhs); // character acccess
friend StringSumHelper & operator +(const StringSumHelper &lhs, const char *cstr); char charAt(unsigned int index) const;
friend StringSumHelper & operator +(const StringSumHelper &lhs, char c); void setCharAt(unsigned int index, char c);
friend StringSumHelper & operator +(const StringSumHelper &lhs, unsigned char num); char operator [](unsigned int index) const;
friend StringSumHelper & operator +(const StringSumHelper &lhs, int num); char& operator [](unsigned int index);
friend StringSumHelper & operator +(const StringSumHelper &lhs, unsigned int num); void getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index = 0) const;
friend StringSumHelper & operator +(const StringSumHelper &lhs, long num); void toCharArray(char *buf, unsigned int bufsize, unsigned int index = 0) const {
friend StringSumHelper & operator +(const StringSumHelper &lhs, unsigned long num); getBytes((unsigned char *) buf, bufsize, index);
friend StringSumHelper & operator +(const StringSumHelper &lhs, float num); }
friend StringSumHelper & operator +(const StringSumHelper &lhs, double num); const char* c_str() const { return buffer(); }
friend StringSumHelper & operator +(const StringSumHelper &lhs, const __FlashStringHelper *rhs); char* begin() { return wbuffer(); }
char* end() { return wbuffer() + length(); }
const char* begin() const { return c_str(); }
const char* end() const { return c_str() + length(); }
// comparison (only works w/ Strings and "strings") // search
operator StringIfHelperType() const int indexOf(char ch) const;
{ int indexOf(char ch, unsigned int fromIndex) const;
return buffer ? &String::StringIfHelper : 0; int indexOf(const String &str) const;
} int indexOf(const String &str, unsigned int fromIndex) const;
int compareTo(const String &s) const; int lastIndexOf(char ch) const;
unsigned char equals(const String &s) const; int lastIndexOf(char ch, unsigned int fromIndex) const;
unsigned char equals(const char *cstr) const; int lastIndexOf(const String &str) const;
unsigned char operator ==(const String &rhs) const int lastIndexOf(const String &str, unsigned int fromIndex) const;
{ String substring(unsigned int beginIndex) const {
return equals(rhs); return substring(beginIndex, len());
} }
unsigned char operator ==(const char *cstr) const ;
{ String substring(unsigned int beginIndex, unsigned int endIndex) const;
return equals(cstr);
}
unsigned char operator !=(const String &rhs) const
{
return !equals(rhs);
}
unsigned char operator !=(const char *cstr) const
{
return !equals(cstr);
}
unsigned char operator <(const String &rhs) const;
unsigned char operator >(const String &rhs) const;
unsigned char operator <=(const String &rhs) const;
unsigned char operator >=(const String &rhs) const;
unsigned char equalsIgnoreCase(const String &s) const;
unsigned char equalsConstantTime(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;
// character acccess // modification
char charAt(unsigned int index) const; void replace(char find, char replace);
void setCharAt(unsigned int index, char c); void replace(const String& find, const String& replace);
char operator [](unsigned int index) const; void remove(unsigned int index);
char& operator [](unsigned int index); void remove(unsigned int index, unsigned int count);
void getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index = 0) const; void toLowerCase(void);
void toCharArray(char *buf, unsigned int bufsize, unsigned int index = 0) const void toUpperCase(void);
{ void trim(void);
getBytes((unsigned char *) buf, bufsize, index);
}
const char * c_str() const
{
return buffer;
}
// search // parsing/conversion
int indexOf(char ch) const; long toInt(void) const;
int indexOf(char ch, unsigned int fromIndex) const; float toFloat(void) const;
int indexOf(const String &str) const; double toDouble(void) const;
int indexOf(const String &str, unsigned int fromIndex) const;
int lastIndexOf(char ch) const;
int lastIndexOf(char ch, unsigned int fromIndex) const;
int lastIndexOf(const String &str) const;
int lastIndexOf(const String &str, unsigned int fromIndex) const;
String substring(unsigned int beginIndex) const
{
return substring(beginIndex, len);
}
;
String substring(unsigned int beginIndex, unsigned int endIndex) const;
// modification protected:
void replace(char find, char replace); // Contains the string info when we're not in SSO mode
void replace(const String& find, const String& replace); struct _ptr {
void remove(unsigned int index); char * buff;
void remove(unsigned int index, unsigned int count); uint16_t cap;
void toLowerCase(void); uint16_t len;
void toUpperCase(void); };
void trim(void);
// parsing/conversion // SSO is handled by checking the last byte of sso_buff.
long toInt(void) const; // When not in SSO mode, that byte is set to 0xff, while when in SSO mode it is always 0x00 (so it can serve as the string terminator as well as a flag)
float toFloat(void) const; // This allows strings up up to 12 (11 + \0 termination) without any extra space.
double toDouble(void) const; enum { SSOSIZE = sizeof(struct _ptr) + 4 }; // Characters to allocate space for SSO, must be 12 or more
enum { CAPACITY_MAX = 65535 }; // If size of capacity changed, be sure to update this enum
union {
struct _ptr ptr;
char sso_buf[SSOSIZE];
};
// Accessor functions
inline bool sso() const { return sso_buf[SSOSIZE - 1] == 0; }
inline unsigned int len() const { return sso() ? strlen(sso_buf) : ptr.len; }
inline unsigned int capacity() const { return sso() ? SSOSIZE - 1 : ptr.cap; }
inline void setSSO(bool sso) { sso_buf[SSOSIZE - 1] = sso ? 0x00 : 0xff; }
inline void setLen(int len) { if (!sso()) ptr.len = len; }
inline void setCapacity(int cap) { if (!sso()) ptr.cap = cap; }
inline void setBuffer(char *buff) { if (!sso()) ptr.buff = buff; }
// Buffer accessor functions
inline const char *buffer() const { return (const char *)(sso() ? sso_buf : ptr.buff); }
inline char *wbuffer() const { return sso() ? const_cast<char *>(sso_buf) : ptr.buff; } // Writable version of buffer
protected: protected:
char *buffer; // the actual char array void init(void);
unsigned int capacity; // the array length minus one (for the '\0') void invalidate(void);
unsigned int len; // the String length (not counting the '\0') unsigned char changeBuffer(unsigned int maxStrLen);
protected: unsigned char concat(const char *cstr, unsigned int length);
void init(void);
void invalidate(void);
unsigned char changeBuffer(unsigned int maxStrLen);
unsigned char concat(const char *cstr, unsigned int length);
// copy and move // copy and move
String & copy(const char *cstr, unsigned int length); String & copy(const char *cstr, unsigned int length);
String & copy(const __FlashStringHelper *pstr, unsigned int length); String & copy(const __FlashStringHelper *pstr, unsigned int length);
#ifdef __GXX_EXPERIMENTAL_CXX0X__ #ifdef __GXX_EXPERIMENTAL_CXX0X__
void move(String &rhs); void move(String &rhs);
#endif #endif
}; };
class StringSumHelper: public String class StringSumHelper: public String {
{ public:
public: StringSumHelper(const String &s) :
StringSumHelper(const String &s) : String(s) {
String(s) }
{ StringSumHelper(const char *p) :
} String(p) {
StringSumHelper(const char *p) : }
String(p) StringSumHelper(char c) :
{ String(c) {
} }
StringSumHelper(char c) : StringSumHelper(unsigned char num) :
String(c) String(num) {
{ }
} StringSumHelper(int num) :
StringSumHelper(unsigned char num) : String(num) {
String(num) }
{ StringSumHelper(unsigned int num) :
} String(num) {
StringSumHelper(int num) : }
String(num) StringSumHelper(long num) :
{ String(num) {
} }
StringSumHelper(unsigned int num) : StringSumHelper(unsigned long num) :
String(num) String(num) {
{ }
} StringSumHelper(float num) :
StringSumHelper(long num) : String(num) {
String(num) }
{ StringSumHelper(double num) :
} String(num) {
StringSumHelper(unsigned long num) : }
String(num)
{
}
StringSumHelper(float num) :
String(num)
{
}
StringSumHelper(double num) :
String(num)
{
}
}; };
extern const String emptyString;
#endif // __cplusplus #endif // __cplusplus
#endif // String_class_h #endif // String_class_h
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment