95 #ifndef SafeString_class_h
96 #define SafeString_class_h
98 #if defined(ARDUINO_ARCH_SAM)
110 #if defined(ESP_PLATFORM) || defined(ARDUINO_ARCH_ESP8266)
111 #include <pgmspace.h>
112 #elif defined(ARDUINO_ARDUINO_NANO33BLE) || defined(ARDUINO_ARCH_MBED_RP2040)|| defined(ARDUINO_ARCH_RP2040)
113 #include <api/deprecated-avr-comp/avr/pgmspace.h>
115 #include <avr/pgmspace.h>
120 #include <Printable.h>
123 #if defined(ARDUINO_ARDUINO_NANO33BLE) || defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_MBED_RP2040) || defined(ARDUINO_ARCH_RP2040)
125 #elif defined( __MBED__ ) || defined( MBED_H )
127 #define Stream WStream
135 class __FlashStringHelper;
138 #define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))
143 #define SSTRING_DEBUG
218 #define createSafeString(name, size,...) char name ## _SAFEBUFFER[(size)+1]; SafeString name(sizeof(name ## _SAFEBUFFER),name ## _SAFEBUFFER, "" __VA_ARGS__ , #name);
219 #define createSafeStringFromCharArray(name, charArray) SafeString name(sizeof(charArray),charArray, charArray, #name, true, false);
220 #define createSafeStringFromCharPtr(name, charPtr) SafeString name((unsigned int)-1,charPtr, charPtr, #name, true);
221 #define createSafeStringFromCharPtrWithSize(name, charPtr, arraySize) SafeString name((arraySize),charPtr, charPtr, #name, true);
223 #define createSafeString(name, size,...) char name ## _SAFEBUFFER[(size)+1]; SafeString name(sizeof(name ## _SAFEBUFFER),name ## _SAFEBUFFER, "" __VA_ARGS__);
224 #define createSafeStringFromCharArray(name,charArray) SafeString name(sizeof(charArray),charArray, charArray, NULL, true, false);
225 #define createSafeStringFromCharPtr(name, charPtr) SafeString name((unsigned int)-1,charPtr, charPtr, NULL, true);
226 #define createSafeStringFromCharPtrWithSize(name, charPtr, arraySize) SafeString name((arraySize),charPtr, charPtr, NULL, true);
230 #define cSF createSafeString
231 #define cSFA createSafeStringFromCharArray
232 #define cSFP createSafeStringFromCharPtr
233 #define cSFPS createSafeStringFromCharPtrWithSize
327 explicit SafeString(
unsigned int maxLen,
char *buf,
const char* cstr,
const char* _name = NULL,
bool _fromBuffer =
false,
bool _fromPtr =
true);
344 static void setOutput(Print& debugOut,
bool verbose =
true);
386 const char*
debug(
bool verbose =
true);
394 const char*
debug(
const char* title,
bool verbose =
true);
402 const char*
debug(
const __FlashStringHelper *title,
bool verbose =
true);
483 size_t print(
unsigned char,
int = DEC);
485 size_t print(
unsigned int,
int = DEC);
487 size_t print(
unsigned long,
int = DEC);
489 size_t print(
const __FlashStringHelper *);
526 size_t println(
double d,
int decs,
int width,
bool forceSign =
false);
538 size_t print(
double d,
int decs,
int width,
bool forceSign =
false);
874 unsigned char startsWith(
const char c,
unsigned int fromIndex = 0);
881 unsigned char startsWith(
const char *str2,
unsigned int fromIndex = 0) ;
1006 int indexOf(
char ch,
unsigned int fromIndex = 0) ;
1015 int indexOf(
const char* str ,
unsigned int fromIndex = 0) ;
1160 void replace(
const char findChar,
const char *replaceStr);
1174 void replace(
const char* findStr,
const char *replaceStr);
1229 void remove(
unsigned int index,
unsigned int count);
1445 int stoken(
SafeString & token,
unsigned int fromIndex,
const char delimiter,
bool returnEmptyFields =
false,
bool useAsDelimiters =
true);
1473 int stoken(
SafeString & token,
unsigned int fromIndex,
const char* delimiters,
bool returnEmptyFields =
false,
bool useAsDelimiters =
true);
1529 return nextToken(token,delimiter,
true,returnLastNonDelimitedToken,
true);
1590 return nextToken(token,delimiters,
true,returnLastNonDelimitedToken,
true);
1650 inline unsigned char firstToken(
SafeString & token,
const char* delimiters,
bool returnLastNonDelimitedToken =
true) {
1651 return nextToken(token,delimiters,
true,returnLastNonDelimitedToken,
true);
1684 unsigned char nextToken(
SafeString & token,
const char* delimiters,
bool returnEmptyFields =
false,
bool returnLastNonDelimitedToken =
true,
bool firstToken =
false);
1718 unsigned int readFrom(
const char* strPtr,
unsigned int maxCharsToRead = ((
unsigned int)-1));
1745 unsigned char read(Stream & input);
1758 unsigned char readUntil(Stream & input,
const char delimiter);
1770 unsigned char readUntil(Stream & input,
const char* delimiters);
1811 unsigned char readUntilToken(Stream & input,
SafeString & token,
const char delimiter,
bool & skipToDelimiter, uint8_t echoInput =
false,
unsigned long timeout_ms = 0);
1840 unsigned char readUntilToken(Stream & input,
SafeString & token,
const char* delimiters,
bool & skipToDelimiter, uint8_t echoInput =
false,
unsigned long timeout_ms = 0);
1915 #if defined(ESP_PLATFORM) || defined(ARDUINO_ARCH_NRF52) || defined(ARDUINO_ARCH_NRF5) || defined(ARDUINO_SAM_DUE) || defined(ARDUINO_ARCH_STM32F1) || defined(ARDUINO_ARCH_STM32F4) || defined(ARDUINO_NRF52832_FEATHER) || defined(MEGATINYCORE_MAJOR)
1944 size_t printInt(
double d,
int decs,
int width,
bool forceSign,
bool addNL);
1947 bool readUntilTokenInternal(Stream & input,
SafeString & token,
const char* delimitersIn,
char delimiterIn,
bool & skipToDelimiter, uint8_t echoInput,
unsigned long timeout_ms);
1948 bool readUntilInternal(Stream & input,
const char* delimitersIn,
char delimiterIn);
1949 bool nextTokenInternal(
SafeString & token,
const char* delimitersIn,
char delimiterIn,
bool returnEmptyFields,
bool returnLastNonDelimitedToken);
1950 int stokenInternal(
SafeString &token,
unsigned int fromIndex,
const char* delimitersIn,
char delimiterIn,
bool returnEmptyFields,
bool useAsDelimiters);
1953 static bool classErrorFlag;
1956 unsigned long timeoutStart_ms;
1957 bool timeoutRunning;
1959 static char nullBufferSafeStringBuffer[1];
1960 static char emptyDebugRtnBuffer[1];
1961 void debugInternal(
bool _fullDebug)
const ;
1962 void debugInternalResultMsg(
bool _fullDebug)
const ;
1963 void concatErr()
const ;
1964 void concatAssignError()
const;
1965 void prefixErr()
const ;
1966 void capError(
const __FlashStringHelper * methodName,
size_t neededCap,
const char* cstr,
const __FlashStringHelper *pstr = NULL,
char c =
'\0',
size_t length = 0)
const ;
1967 void assignError(
size_t neededCap,
const char* cstr,
const __FlashStringHelper *pstr = NULL,
char c =
'\0',
bool numberFlag =
false)
const;
1968 void errorMethod(
const __FlashStringHelper * methodName)
const ;
1969 void warningMethod(
const __FlashStringHelper * methodName)
const ;
1970 void assignErrorMethod()
const ;
1971 void outputFromIndexIfFullDebug(
unsigned int fromIndex)
const ;
size_t write(const uint8_t *buffer, size_t length)
size_t write(const uint8_t *buffer, size_t length)
To create SafeStrings use one of the four (4) macros createSafeString or cSF, createSafeStringFromCha...
int stoken(SafeString &token, unsigned int fromIndex, const char delimiter, bool returnEmptyFields=false, bool useAsDelimiters=true)
break into the SafeString into tokens using the char delimiter, the end of the SafeString is always a...
size_t getLastReadCount()
returns the number of chars read on previous calls to read, readUntil or readUntilToken (includes '\0...
size_t print(const __FlashStringHelper *)
int indexOfCharFrom(const char *chars, unsigned int fromIndex=0)
returns the first index of any char from the argument
static void setOutput(Print &debugOut, bool verbose=true)
Turns on Error msgs and debug( ) output for all SafeStrings.
SafeString & operator=(char c)
Clears this SafeString and concatinates a single char.
unsigned char endsWith(SafeString &suffix)
returns non-zero of this SafeString ends with the argument
SafeString & concatln(const __FlashStringHelper *pstr)
const char * debug(SafeString &stitle, bool verbose=true)
Output the details about the this SafeString to the output specified by setOutput().
static unsigned char errorDetected()
Returns non-zero if any SafeString has detected and error, each call clears the internal global stati...
size_t print(unsigned char, int=DEC)
SafeString & prefix(const __FlashStringHelper *str, size_t length)
unsigned char equalsConstantTime(SafeString &s)
int availableForWrite(void)
Returns the number chars that can be added to this SafeString before it is full.
SafeString & prefix(long num)
SafeString & prefix(unsigned long num)
SafeString & prefix(int num)
unsigned char hasError()
Returns non-zero if any error detected for this SafeString, each call clears the internal flag.
SafeString(unsigned int maxLen, char *buf, const char *cstr, const char *_name=NULL, bool _fromBuffer=false, bool _fromPtr=true)
SafeString Constructor called from the four (4) macros createSafeString or cSF, createSafeStringFromC...
void debugInternalMsg(bool _fullDebug) const
unsigned char toUnsignedLong(unsigned long &l)
convert the SafeString to an unsigned long.
unsigned char isFull(void)
Returns non-zero if the SafeString is full.
unsigned char readUntil(Stream &input, const char *delimiters)
reads chars into this SafeString until either it is full OR a delimiter is read OR there are no more ...
SafeString & prefix(double num)
unsigned char read(Stream &input)
reads from the Stream (if chars available) into the SafeString.
static void turnOutputOff(void)
Turns off all debugging messages, both error messages AND debug() method output.
SafeString & concatInternal(const char *cstr, size_t length, bool assignOp=false)
static SafeString::DebugPrint Output
void replace(const char *findStr, const char *replaceStr)
replace the findStr string with the replace string
void remove(unsigned int index)
remove all chars from index to the end of the SafeString (inclusive)
unsigned char toDouble(double &d)
convert the SafeString to a float assuming the SafeString in the decimal format (not scientific)
unsigned char equalsIgnoreCase(SafeString &s)
int lastIndexOf(char ch, unsigned int fromIndex)
returns the last index of the char, searching backwards from fromIndex (inclusive).
SafeString & operator-=(SafeString &rhs)
-= operator prefixes the SafeString.
unsigned char reserve(unsigned int size)
Checks there is enough free space in this SafeString for the current operation.
unsigned char operator>=(SafeString &rhs)
size_t println(SafeString &str)
static SafeString::noDebugPrint emptyPrint
unsigned int readFrom(SafeString &sfInput, unsigned int startIdx=0)
reads from the SafeString argument, starting at startIdx, into this SafeString.
SafeString & prefix(const __FlashStringHelper *str)
unsigned char readUntil(Stream &input, const char delimiter)
reads chars into this SafeString until either it is full OR a delimiter is read OR there are no more ...
SafeString & concat(float num)
SafeString & concatln(const char *cstr, size_t length)
unsigned char startsWith(const char *str2, unsigned int fromIndex=0)
returns non-zero of this SafeString starts this argument looking from fromIndex onwards.
unsigned char binToUnsignedLong(unsigned long &l)
convert the SafeString to an unsigned long assuming the SafeString in binary (0/1).
unsigned char startsWithIgnoreCase(const char *str2, unsigned int fromIndex=0)
returns non-zero of this SafeString starts this argument, ignoring case, looking from fromIndex onwar...
size_t println(long, int=DEC)
static Print * currentOutput
const char * debug(bool verbose=true)
Output the details about the this SafeString to the output specified by setOutput().
size_t println(unsigned int, int=DEC)
unsigned char hexToLong(long &l)
convert the SafeString to a long assuming the SafeString in HEX (0 to f or 0 to F).
unsigned char operator!=(SafeString &rhs)
SafeString & concat(const char *cstr)
unsigned int readFrom(const char *strPtr, unsigned int maxCharsToRead=((unsigned int) -1))
reads from the const char* argument, starting at 0 and read up to maxCharToRead, into this SafeString...
void replace(char findChar, char replaceChar)
replace the findChar with the replaceChar
const char * c_str()
returns a const char* to the underlying char[ ] in this SafeString.
unsigned char octToLong(long &l)
convert the SafeString to a long assuming the SafeString in octal (0 to 7).
unsigned char operator<=(SafeString &rhs)
size_t println(double, int=2)
char charAt(unsigned int index)
returns the char at that location in this SafeString.
size_t printInt(double d, int decs, int width, bool forceSign, bool addNL)
unsigned char nextToken(SafeString &token, SafeString &delimiters, bool returnEmptyFields=false, bool returnLastNonDelimitedToken=true, bool firstToken=false)
returns true if a delimited token is found, removes the first delimited token from this SafeString an...
size_t println(int, int=DEC)
unsigned char hexToUnsignedLong(unsigned long &l)
convert the SafeString to an unsigned long assuming the SafeString in HEX (0 to f or 0 to F).
SafeString & concat(unsigned int num)
int stoken(SafeString &token, unsigned int fromIndex, const char *delimiters, bool returnEmptyFields=false, bool useAsDelimiters=true)
break into the SafeString into tokens using the delimiters, the end of the SafeString is always a del...
int stoken(SafeString &token, unsigned int fromIndex, SafeString &delimiters, bool returnEmptyFields=false, bool useAsDelimiters=true)
break into the SafeString into tokens using the delimiters, the end of the SafeString is always a del...
int compareTo(const char *cstr)
returns -1 if this SafeString is < cstr, 0 if this SafeString == cstr and +1 if this SafeString > cst
size_t println(unsigned long, int=DEC)
size_t print(long, int=DEC)
void processBackspaces(void)
recursively remove backspaces, '\b' and the preceeding char.
unsigned char equals(const char *cstr)
int lastIndexOf(const char *cstr, unsigned int fromIndex)
returns the last index of the char, searching backwards from fromIndex (inclusive).
size_t limitedStrLen(const char *p, size_t limit)
size_t print(SafeString &str)
unsigned char readUntilToken(Stream &input, SafeString &token, const char delimiter, bool &skipToDelimiter, uint8_t echoInput=false, unsigned long timeout_ms=0)
returns true if a delimited token is found, else false ONLY delimited tokens of length less than thi...
size_t print(unsigned long, int=DEC)
unsigned char firstToken(SafeString &token, char delimiter, bool returnLastNonDelimitedToken=true)
returns true if a delimited token is found, removes the first delimited token from this SafeString an...
void removeFrom(unsigned int startIndex)
remove all chars from startIndex to the end of the SafeString (inclusive)
size_t println(const char *)
unsigned char operator>(SafeString &rhs)
int indexOfCharFrom(SafeString &str, unsigned int fromIndex=0)
returns the first index of any char from the argument.
unsigned int writeTo(SafeString &output, unsigned int startIdx=0)
writes from this SafeString, starting from startIdx, into the SafeString output arguement.
const char * debug(const __FlashStringHelper *title, bool verbose=true)
Output the details about the this SafeString to the output specified by setOutput().
int indexOf(const char *str, unsigned int fromIndex=0)
returns the index of the string, searching from fromIndex.
virtual size_t write(const uint8_t *buffer, size_t length)
Write (concatinate) bytes to this SafeString, from Print class.
unsigned char endsWithCharFrom(const char *suffix)
returns non-zero of this SafeString ends any one of the chars in the argument
const char * debug(const char *title, bool verbose=true)
Output the details about the this SafeString to the output specified by setOutput().
void toLowerCase(void)
convert this SafeString to all lower case
unsigned char readUntil(Stream &input, SafeString &delimiters)
reads chars into this SafeString until either it is full OR a delimiter is read OR there are no more ...
unsigned char readUntilToken(Stream &input, SafeString &token, const char *delimiters, bool &skipToDelimiter, uint8_t echoInput=false, unsigned long timeout_ms=0)
returns true if a delimited token is found, else false ONLY delimited tokens of length less than thi...
unsigned char toInt(int &i)
convert the SafeString to an int.
void replace(const char findChar, SafeString &sfReplace)
replace the findChar with the sfReplace SafeString contents
unsigned char equals(const char c)
SafeString & concat(char c)
unsigned char nextToken(SafeString &token, const char *delimiters, bool returnEmptyFields=false, bool returnLastNonDelimitedToken=true, bool firstToken=false)
returns true if a delimited token is found, removes the first delimited token from this SafeString an...
SafeString & clear(void)
Empties this SafeString.
void toUpperCase(void)
convert this SafeString to all lower case
unsigned char readUntilToken(Stream &input, SafeString &token, SafeString &delimiters, bool &skipToDelimiter, uint8_t echoInput=false, unsigned long timeout_ms=0)
returns true if a delimited token is found, else false ONLY delimited tokens of length less than thi...
SafeString & operator+=(SafeString &rhs)
+= operator concatinate to the SafeString.
unsigned char binToLong(long &l)
convert the SafeString to a long assuming the SafeString in binary (0/1).
void replace(const char findChar, const char *replaceStr)
replace the findChar with the replace string
unsigned char startsWithIgnoreCase(SafeString &s2, unsigned int fromIndex=0)
returns non-zero of this SafeString starts this argument, ignoring case, looking from fromIndex onwar...
unsigned char firstToken(SafeString &token, SafeString delimiters, bool returnLastNonDelimitedToken=true)
returns true if a delimited token is found, removes the first delimited token from this SafeString an...
void setCharAt(unsigned int index, char c)
sets the char at that location in this SafeString.
int lastIndexOf(SafeString &str, unsigned int fromIndex)
returns the last index of the char, searching backwards from fromIndex (inclusive).
virtual size_t write(uint8_t b)
Write (concatinate) a byte to this SafeString, from Print class.
size_t printInternal(unsigned long, int=DEC, bool assignOp=false)
void removeLast(unsigned int count)
remove the last count chars
SafeString & substring(SafeString &result, unsigned int beginIdx)
The result is the substring from the beginIdx to the end of the SafeString.
SafeString & concat(SafeString &str)
concat methods add to the end of the current SafeString.
SafeString & substring(SafeString &result, unsigned int beginIdx, unsigned int endIdx)
The result is the substring from the beginIdx to endIdx (exclusive), that is the endIdx is NOT includ...
SafeString & prefix(char c)
unsigned char nextToken(SafeString &token, char delimiter, bool returnEmptyFields=false, bool returnLastNonDelimitedToken=true, bool firstToken=false)
returns true if a delimited token is found, removes the first delimited token from this SafeString an...
SafeString & newline()
Adds \r\n to this SafeString.
SafeString & prefix(unsigned char c)
size_t print(double, int=2)
unsigned char isEmpty(void)
Returns non-zero if the SafeString is empty.
size_t printInternal(double, int=2, bool assignOp=false)
int lastIndexOf(char ch)
returns the last index of the char, searching backwards from fromIndex (inclusive).
size_t println(const __FlashStringHelper *)
size_t print(unsigned int, int=DEC)
SafeString & concat(const __FlashStringHelper *str, size_t length)
unsigned char firstToken(SafeString &token, const char *delimiters, bool returnLastNonDelimitedToken=true)
returns true if a delimited token is found, removes the first delimited token from this SafeString an...
size_t print(double d, int decs, int width, bool forceSign=false)
Prints a double (or long/int) to this SafeString padded with spaces (left or right) and limited to th...
SafeString & concatln(char c)
SafeString & prefix(const char *cstr, size_t length)
SafeString & concat(int num)
SafeString & concat(const char *cstr, size_t length)
SafeString & concatInternal(const __FlashStringHelper *str, bool assignOp=false)
void keepLast(unsigned int count)
keep the last count chars and remove the rest
static void setVerbose(bool verbose)
Controls size of error messages, setOutput sets verbose to true.
void remove(unsigned int index, unsigned int count)
remove count chars starting from index
unsigned char endsWithCharFrom(SafeString &suffix)
returns non-zero of this SafeString ends any one of the chars in the argument
SafeString & prefix(unsigned int num)
SafeString & concat(const __FlashStringHelper *str)
size_t println(unsigned char, int=DEC)
int lastIndexOf(const char *cstr)
returns the last index of the arguement, searching backwards from fromIndex (inclusive).
int lastIndexOf(SafeString &str)
returns the last index of the arguement, searching backwards from fromIndex (inclusive).
unsigned int capacity(void)
The maximum number of characters this SafeString can hold, excluding the terminating '\0'.
unsigned char endsWith(const char *suffix)
returns non-zero of this SafeString ends with the argument
size_t printTo(Print &p) const
Implements the Printable interface.
SafeString & prefix(float num)
unsigned char equalsIgnoreCase(const char *str2)
int compareTo(SafeString &s)
returns -1 if this SafeString is < s, 0 if this SafeString == s and +1 if this SafeString > s
unsigned char toLong(long &l)
convert the SafeString to a long.
SafeString & prefix(SafeString &s)
prefix methods add to the front of the current SafeString.
size_t printInternal(long, int=DEC, bool assignOp=false)
unsigned char equals(SafeString &s)
SafeString & concat(long num)
void trim(void)
remove all white space from the front and back of this SafeString.
unsigned char startsWithIgnoreCase(const char c, unsigned int fromIndex=0)
returns non-zero of this SafeString starts this argument, ignoring case, looking from fromIndex onwar...
unsigned char octToUnsignedLong(unsigned long &l)
convert the SafeString to an unsigned long assuming the SafeString in octal (0 to 7).
size_t print(const char *)
SafeString & concat(unsigned long num)
SafeString & concat(unsigned char c)
unsigned char startsWith(SafeString &s2, unsigned int fromIndex=0)
returns non-zero of this SafeString starts this argument looking from fromIndex onwards.
size_t print(int, int=DEC)
SafeString & concat(double num)
unsigned char endsWith(const char c)
returns non-zero of this SafeString ends with the argument
SafeString & prefix(const char *cstr)
void replace(SafeString &sfFind, SafeString &sfReplace)
replace the occurances of the sfFind string, with the sfReplace SafeString contents
unsigned char operator==(SafeString &rhs)
SafeString & concatInternal(const char *cstr, bool assignOp=false)
size_t println(double d, int decs, int width, bool forceSign=false)
Prints a double (or long/int) to this SafeString padded with spaces (left or right) and limited to th...
unsigned char toFloat(float &f)
convert the SafeString to a float assuming the SafeString in the decimal format (not scientific)
unsigned char operator<(SafeString &rhs)
SafeString & concatInternal(const __FlashStringHelper *str, size_t length, bool assignOp=false)
unsigned int length(void)
Number of characters current in the SafeString, excluding the terminating '\0'.
unsigned char startsWith(const char c, unsigned int fromIndex=0)
returns non-zero of this SafeString starts this argument looking from fromIndex onwards.
SafeString & concatInternal(char c, bool assignOp=false)
void removeBefore(unsigned int startIndex)
remove all chars from 0 to startIndex (exclusive), that is the char at startIndex is NOT removed
char operator[](unsigned int index)
returns the char at that location in this SafeString.
int indexOf(SafeString &str, unsigned int fromIndex=0)
returns the index of the SafeString, searching from fromIndex.
int indexOf(char ch, unsigned int fromIndex=0)
returns the index of the char, searching from fromIndex.