2016-10-06 13:21:30 +02:00
|
|
|
/*
|
|
|
|
WString.h - String library for Wiring & Arduino
|
|
|
|
...mostly rewritten by Paul Stoffregen...
|
|
|
|
Copyright (c) 2009-10 Hernando Barragan. All right reserved.
|
|
|
|
Copyright 2011, Paul Stoffregen, paul@pjrc.com
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef String_class_h
|
|
|
|
#define String_class_h
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
2016-10-07 22:56:50 +02:00
|
|
|
#include <pgmspace.h>
|
2019-08-20 15:39:31 +02:00
|
|
|
#include <stdint.h>
|
2016-10-06 13:21:30 +02:00
|
|
|
|
|
|
|
// An inherited class for holding the result of a concatenation. These
|
|
|
|
// result objects are assumed to be writable by subsequent concatenations.
|
|
|
|
class StringSumHelper;
|
|
|
|
|
|
|
|
// an abstract class used as a means to proide a unique pointer type
|
|
|
|
// but really has no body
|
2017-02-08 18:57:26 +01:00
|
|
|
class __FlashStringHelper;
|
2019-07-17 09:16:47 +02:00
|
|
|
#define FPSTR(pstr_pointer) (reinterpret_cast<const __FlashStringHelper *>(pstr_pointer))
|
|
|
|
#define F(string_literal) (FPSTR(PSTR(string_literal)))
|
2016-10-06 13:21:30 +02:00
|
|
|
|
|
|
|
// The string class
|
2019-04-26 18:41:42 +02:00
|
|
|
class String {
|
|
|
|
// use a function pointer to allow for "if (s)" without the
|
|
|
|
// complications of an operator bool(). for more information, see:
|
|
|
|
// http://www.artima.com/cppsource/safebool.html
|
|
|
|
typedef void (String::*StringIfHelperType)() const;
|
|
|
|
void StringIfHelper() const {
|
|
|
|
}
|
2016-10-06 13:21:30 +02:00
|
|
|
|
2019-04-26 18:41:42 +02:00
|
|
|
public:
|
|
|
|
// constructors
|
|
|
|
// creates a copy of the initial value.
|
|
|
|
// 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
|
|
|
|
// be false).
|
|
|
|
String(const char *cstr = "");
|
|
|
|
String(const String &str);
|
|
|
|
String(const __FlashStringHelper *str);
|
2016-10-06 13:21:30 +02:00
|
|
|
#ifdef __GXX_EXPERIMENTAL_CXX0X__
|
2019-04-26 18:41:42 +02:00
|
|
|
String(String &&rval);
|
|
|
|
String(StringSumHelper &&rval);
|
2016-10-06 13:21:30 +02:00
|
|
|
#endif
|
2019-04-26 18:41:42 +02:00
|
|
|
explicit String(char c);
|
|
|
|
explicit String(unsigned char, unsigned char base = 10);
|
|
|
|
explicit String(int, unsigned char base = 10);
|
|
|
|
explicit String(unsigned int, unsigned char base = 10);
|
|
|
|
explicit String(long, unsigned char base = 10);
|
|
|
|
explicit String(unsigned long, unsigned char base = 10);
|
|
|
|
explicit String(float, unsigned char decimalPlaces = 2);
|
|
|
|
explicit String(double, unsigned char decimalPlaces = 2);
|
|
|
|
~String(void);
|
2016-10-06 13:21:30 +02:00
|
|
|
|
2019-04-26 18:41:42 +02:00
|
|
|
// memory management
|
|
|
|
// 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);
|
|
|
|
inline unsigned int length(void) const {
|
|
|
|
if(buffer()) {
|
|
|
|
return len();
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2016-10-06 13:21:30 +02:00
|
|
|
}
|
2019-08-27 19:05:36 +02:00
|
|
|
inline void clear(void) {
|
|
|
|
setLen(0);
|
|
|
|
}
|
|
|
|
inline bool isEmpty(void) const {
|
|
|
|
return length() == 0;
|
|
|
|
}
|
2016-10-06 13:21:30 +02:00
|
|
|
|
2019-04-26 18:41:42 +02:00
|
|
|
// creates a copy of the assigned value. if the value is null or
|
|
|
|
// invalid, or if the memory allocation fails, the string will be
|
|
|
|
// marked as invalid ("if (s)" will be false).
|
|
|
|
String & operator =(const String &rhs);
|
|
|
|
String & operator =(const char *cstr);
|
|
|
|
String & operator = (const __FlashStringHelper *str);
|
2016-10-06 13:21:30 +02:00
|
|
|
#ifdef __GXX_EXPERIMENTAL_CXX0X__
|
2019-04-26 18:41:42 +02:00
|
|
|
String & operator =(String &&rval);
|
|
|
|
String & operator =(StringSumHelper &&rval);
|
2016-10-06 13:21:30 +02:00
|
|
|
#endif
|
|
|
|
|
2019-04-26 18:41:42 +02:00
|
|
|
// 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
|
2019-08-27 19:05:36 +02:00
|
|
|
// concatenation is considered unsuccessful.
|
2019-04-26 18:41:42 +02:00
|
|
|
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);
|
|
|
|
}
|
2016-10-06 13:21:30 +02:00
|
|
|
|
2019-04-26 18:41:42 +02:00
|
|
|
friend StringSumHelper & operator +(const StringSumHelper &lhs, const String &rhs);
|
|
|
|
friend StringSumHelper & operator +(const StringSumHelper &lhs, const char *cstr);
|
|
|
|
friend StringSumHelper & operator +(const StringSumHelper &lhs, char c);
|
|
|
|
friend StringSumHelper & operator +(const StringSumHelper &lhs, unsigned char num);
|
|
|
|
friend StringSumHelper & operator +(const StringSumHelper &lhs, int num);
|
|
|
|
friend StringSumHelper & operator +(const StringSumHelper &lhs, unsigned int num);
|
|
|
|
friend StringSumHelper & operator +(const StringSumHelper &lhs, long num);
|
|
|
|
friend StringSumHelper & operator +(const StringSumHelper &lhs, unsigned long num);
|
|
|
|
friend StringSumHelper & operator +(const StringSumHelper &lhs, float num);
|
|
|
|
friend StringSumHelper & operator +(const StringSumHelper &lhs, double num);
|
|
|
|
friend StringSumHelper & operator +(const StringSumHelper &lhs, const __FlashStringHelper *rhs);
|
2016-10-06 13:21:30 +02:00
|
|
|
|
2019-04-26 18:41:42 +02:00
|
|
|
// comparison (only works w/ Strings and "strings")
|
|
|
|
operator StringIfHelperType() const {
|
|
|
|
return buffer() ? &String::StringIfHelper : 0;
|
|
|
|
}
|
|
|
|
int compareTo(const String &s) const;
|
|
|
|
unsigned char equals(const String &s) const;
|
|
|
|
unsigned char equals(const char *cstr) const;
|
|
|
|
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 {
|
|
|
|
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;
|
2016-10-06 13:21:30 +02:00
|
|
|
|
2019-08-27 19:05:36 +02:00
|
|
|
// character access
|
2019-04-26 18:41:42 +02:00
|
|
|
char charAt(unsigned int index) const;
|
|
|
|
void setCharAt(unsigned int index, char c);
|
|
|
|
char operator [](unsigned int index) const;
|
|
|
|
char& operator [](unsigned int index);
|
|
|
|
void getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index = 0) const;
|
|
|
|
void toCharArray(char *buf, unsigned int bufsize, unsigned int index = 0) const {
|
|
|
|
getBytes((unsigned char *) buf, bufsize, index);
|
|
|
|
}
|
|
|
|
const char* c_str() const { return buffer(); }
|
|
|
|
char* begin() { return wbuffer(); }
|
|
|
|
char* end() { return wbuffer() + length(); }
|
|
|
|
const char* begin() const { return c_str(); }
|
|
|
|
const char* end() const { return c_str() + length(); }
|
2016-10-06 13:21:30 +02:00
|
|
|
|
2019-04-26 18:41:42 +02:00
|
|
|
// search
|
|
|
|
int indexOf(char ch) const;
|
|
|
|
int indexOf(char ch, unsigned int fromIndex) const;
|
|
|
|
int indexOf(const String &str) 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;
|
2016-10-06 13:21:30 +02:00
|
|
|
|
2019-04-26 18:41:42 +02:00
|
|
|
// modification
|
|
|
|
void replace(char find, char replace);
|
|
|
|
void replace(const String& find, const String& replace);
|
|
|
|
void remove(unsigned int index);
|
|
|
|
void remove(unsigned int index, unsigned int count);
|
|
|
|
void toLowerCase(void);
|
|
|
|
void toUpperCase(void);
|
|
|
|
void trim(void);
|
2016-10-06 13:21:30 +02:00
|
|
|
|
2019-04-26 18:41:42 +02:00
|
|
|
// parsing/conversion
|
|
|
|
long toInt(void) const;
|
|
|
|
float toFloat(void) const;
|
|
|
|
double toDouble(void) const;
|
2016-10-06 13:21:30 +02:00
|
|
|
|
2019-04-26 18:41:42 +02:00
|
|
|
protected:
|
|
|
|
// Contains the string info when we're not in SSO mode
|
|
|
|
struct _ptr {
|
|
|
|
char * buff;
|
|
|
|
uint16_t cap;
|
|
|
|
uint16_t len;
|
|
|
|
};
|
2019-08-27 19:05:36 +02:00
|
|
|
// This allows strings up up to 11 (10 + \0 termination) without any extra space.
|
|
|
|
enum { SSOSIZE = sizeof(struct _ptr) + 4 - 1 }; // Characters to allocate space for SSO, must be 12 or more
|
|
|
|
struct _sso {
|
|
|
|
char buff[SSOSIZE];
|
|
|
|
unsigned char len : 7; // Ensure only one byte is allocated by GCC for the bitfields
|
|
|
|
unsigned char isSSO : 1;
|
|
|
|
} __attribute__((packed)); // Ensure that GCC doesn't expand the flag byte to a 32-bit word for alignment issues
|
|
|
|
enum { CAPACITY_MAX = 65535 }; // If typeof(cap) changed from uint16_t, be sure to update this enum to the max value storable in the type
|
2019-04-26 18:41:42 +02:00
|
|
|
union {
|
|
|
|
struct _ptr ptr;
|
2019-08-27 19:05:36 +02:00
|
|
|
struct _sso sso;
|
2019-04-26 18:41:42 +02:00
|
|
|
};
|
|
|
|
// Accessor functions
|
2019-08-27 19:05:36 +02:00
|
|
|
inline bool isSSO() const { return sso.isSSO; }
|
|
|
|
inline unsigned int len() const { return isSSO() ? sso.len : ptr.len; }
|
|
|
|
inline unsigned int capacity() const { return isSSO() ? (unsigned int)SSOSIZE - 1 : ptr.cap; } // Size of max string not including terminal NUL
|
|
|
|
inline void setSSO(bool set) { sso.isSSO = set; }
|
|
|
|
inline void setLen(int len) { if (isSSO()) sso.len = len; else ptr.len = len; }
|
|
|
|
inline void setCapacity(int cap) { if (!isSSO()) ptr.cap = cap; }
|
|
|
|
inline void setBuffer(char *buff) { if (!isSSO()) ptr.buff = buff; }
|
2019-04-26 18:41:42 +02:00
|
|
|
// Buffer accessor functions
|
2019-08-27 19:05:36 +02:00
|
|
|
inline const char *buffer() const { return (const char *)(isSSO() ? sso.buff : ptr.buff); }
|
|
|
|
inline char *wbuffer() const { return isSSO() ? const_cast<char *>(sso.buff) : ptr.buff; } // Writable version of buffer
|
2016-10-06 13:21:30 +02:00
|
|
|
|
2019-04-26 18:41:42 +02:00
|
|
|
protected:
|
|
|
|
void init(void);
|
|
|
|
void invalidate(void);
|
|
|
|
unsigned char changeBuffer(unsigned int maxStrLen);
|
|
|
|
unsigned char concat(const char *cstr, unsigned int length);
|
2016-10-06 13:21:30 +02:00
|
|
|
|
2019-04-26 18:41:42 +02:00
|
|
|
// copy and move
|
|
|
|
String & copy(const char *cstr, unsigned int length);
|
|
|
|
String & copy(const __FlashStringHelper *pstr, unsigned int length);
|
2016-10-06 13:21:30 +02:00
|
|
|
#ifdef __GXX_EXPERIMENTAL_CXX0X__
|
2019-04-26 18:41:42 +02:00
|
|
|
void move(String &rhs);
|
2016-10-06 13:21:30 +02:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2019-04-26 18:41:42 +02:00
|
|
|
class StringSumHelper: public String {
|
|
|
|
public:
|
|
|
|
StringSumHelper(const String &s) :
|
|
|
|
String(s) {
|
|
|
|
}
|
|
|
|
StringSumHelper(const char *p) :
|
|
|
|
String(p) {
|
|
|
|
}
|
|
|
|
StringSumHelper(char c) :
|
|
|
|
String(c) {
|
|
|
|
}
|
|
|
|
StringSumHelper(unsigned char num) :
|
|
|
|
String(num) {
|
|
|
|
}
|
|
|
|
StringSumHelper(int num) :
|
|
|
|
String(num) {
|
|
|
|
}
|
|
|
|
StringSumHelper(unsigned int num) :
|
|
|
|
String(num) {
|
|
|
|
}
|
|
|
|
StringSumHelper(long num) :
|
|
|
|
String(num) {
|
|
|
|
}
|
|
|
|
StringSumHelper(unsigned long num) :
|
|
|
|
String(num) {
|
|
|
|
}
|
|
|
|
StringSumHelper(float num) :
|
|
|
|
String(num) {
|
|
|
|
}
|
|
|
|
StringSumHelper(double num) :
|
|
|
|
String(num) {
|
|
|
|
}
|
2016-10-06 13:21:30 +02:00
|
|
|
};
|
|
|
|
|
2019-04-26 18:41:42 +02:00
|
|
|
extern const String emptyString;
|
|
|
|
|
2016-10-06 13:21:30 +02:00
|
|
|
#endif // __cplusplus
|
|
|
|
#endif // String_class_h
|