SafeString  4.1.27
SafeString is a safe, robust and debuggable replacement for string processing in Arduino
SafeString Class Reference

To create SafeStrings use one of the four (4) macros createSafeString or cSF, createSafeStringFromCharArray or cSFA, createSafeStringFromCharPtr or cSFP, createSafeStringFromCharPtrWithSize or cSFPS see the detailed description. More...

#include <SafeString.h>

Inherits Printable, and Print.

Inherited by SafeStringReader, and SerialComs.

Classes

class  DebugPrint
 
class  noDebugPrint
 

Public Member Functions

 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, createSafeStringFromCharArray or cSFA, createSafeStringFromCharPtr or cSFP, createSafeStringFromCharPtrWithSize or cSFPS. More...
 
unsigned char hasError ()
 Returns non-zero if any error detected for this SafeString, each call clears the internal flag. More...
 
const char * debug (bool verbose=true)
 Output the details about the this SafeString to the output specified by setOutput(). More...
 
const char * debug (const char *title, bool verbose=true)
 Output the details about the this SafeString to the output specified by setOutput(). More...
 
const char * debug (const __FlashStringHelper *title, bool verbose=true)
 Output the details about the this SafeString to the output specified by setOutput(). More...
 
const char * debug (SafeString &stitle, bool verbose=true)
 Output the details about the this SafeString to the output specified by setOutput(). More...
 
virtual size_t write (uint8_t b)
 Write (concatinate) a byte to this SafeString, from Print class. More...
 
virtual size_t write (const uint8_t *buffer, size_t length)
 Write (concatinate) bytes to this SafeString, from Print class. More...
 
size_t printTo (Print &p) const
 Implements the Printable interface. More...
 
unsigned char reserve (unsigned int size)
 Checks there is enough free space in this SafeString for the current operation. More...
 
unsigned int length (void)
 Number of characters current in the SafeString, excluding the terminating '\0'. More...
 
unsigned int capacity (void)
 The maximum number of characters this SafeString can hold, excluding the terminating '\0'. More...
 
unsigned char isFull (void)
 Returns non-zero if the SafeString is full. More...
 
unsigned char isEmpty (void)
 Returns non-zero if the SafeString is empty. More...
 
int availableForWrite (void)
 Returns the number chars that can be added to this SafeString before it is full. More...
 
SafeStringclear (void)
 Empties this SafeString. More...
 
size_t print (unsigned char, int=DEC)
 
size_t print (int, int=DEC)
 
size_t print (unsigned int, int=DEC)
 
size_t print (long, int=DEC)
 
size_t print (unsigned long, int=DEC)
 
size_t print (double, int=2)
 
size_t print (const __FlashStringHelper *)
 
size_t print (const char *)
 
size_t print (char)
 
size_t print (SafeString &str)
 
size_t println (unsigned char, int=DEC)
 
size_t println (int, int=DEC)
 
size_t println (unsigned int, int=DEC)
 
size_t println (long, int=DEC)
 
size_t println (unsigned long, int=DEC)
 
size_t println (double, int=2)
 
size_t println (const __FlashStringHelper *)
 
size_t println (const char *)
 
size_t println (char)
 
size_t println (SafeString &str)
 
size_t println (void)
 
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 the specified width and adds a trailing CR NL. More...
 
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 the specified width. More...
 
SafeStringoperator= (char c)
 Clears this SafeString and concatinates a single char. More...
 
SafeStringoperator= (unsigned char num)
 Clears this SafeString and concatinates the text version of the argument. More...
 
SafeStringoperator= (int num)
 Clears this SafeString and concatinates the text version of the argument. More...
 
SafeStringoperator= (unsigned int num)
 Clears this SafeString and concatinates the text version of the argument. More...
 
SafeStringoperator= (long num)
 Clears this SafeString and concatinates the text version of the argument. More...
 
SafeStringoperator= (unsigned long num)
 Clears this SafeString and concatinates the text version of the argument. More...
 
SafeStringoperator= (float num)
 Clears this SafeString and concatinates the text version of the argument. More...
 
SafeStringoperator= (double num)
 Clears this SafeString and concatinates the text version of the argument. More...
 
SafeStringoperator= (SafeString &sfStr)
 Clears this SafeString and copies and concatinates the contents of the argument. More...
 
SafeStringoperator= (const char *cstr)
 Clears this SafeString and copies and concatinates the contents of the argument. More...
 
SafeStringoperator= (const __FlashStringHelper *pstr)
 Clears this SafeString and copies and concatinates the contents of the argument. More...
 
SafeStringprefix (SafeString &s)
 prefix methods add to the front of the current SafeString. More...
 
SafeStringprefix (const char *cstr)
 
SafeStringprefix (char c)
 
SafeStringprefix (unsigned char c)
 
SafeStringprefix (int num)
 
SafeStringprefix (unsigned int num)
 
SafeStringprefix (long num)
 
SafeStringprefix (unsigned long num)
 
SafeStringprefix (float num)
 
SafeStringprefix (double num)
 
SafeStringprefix (const __FlashStringHelper *str)
 
SafeStringprefix (const char *cstr, size_t length)
 
SafeStringprefix (const __FlashStringHelper *str, size_t length)
 
SafeStringconcat (SafeString &str)
 concat methods add to the end of the current SafeString. More...
 
SafeStringconcat (const char *cstr)
 
SafeStringconcat (char c)
 
SafeStringconcat (unsigned char c)
 
SafeStringconcat (int num)
 
SafeStringconcat (unsigned int num)
 
SafeStringconcat (long num)
 
SafeStringconcat (unsigned long num)
 
SafeStringconcat (float num)
 
SafeStringconcat (double num)
 
SafeStringconcat (const __FlashStringHelper *str)
 
SafeStringconcat (const char *cstr, size_t length)
 
SafeStringconcat (const __FlashStringHelper *str, size_t length)
 
SafeStringnewline ()
 Adds \r\n to this SafeString. More...
 
SafeStringoperator-= (SafeString &rhs)
 -= operator prefixes the SafeString. More...
 
SafeStringoperator-= (const char *cstr)
 
SafeStringoperator-= (char c)
 
SafeStringoperator-= (unsigned char num)
 
SafeStringoperator-= (int num)
 
SafeStringoperator-= (unsigned int num)
 
SafeStringoperator-= (long num)
 
SafeStringoperator-= (unsigned long num)
 
SafeStringoperator-= (float num)
 
SafeStringoperator-= (double num)
 
SafeStringoperator-= (const __FlashStringHelper *str)
 
SafeStringoperator+= (SafeString &rhs)
 += operator concatinate to the SafeString. More...
 
SafeStringoperator+= (const char *cstr)
 
SafeStringoperator+= (char c)
 
SafeStringoperator+= (unsigned char num)
 
SafeStringoperator+= (int num)
 
SafeStringoperator+= (unsigned int num)
 
SafeStringoperator+= (long num)
 
SafeStringoperator+= (unsigned long num)
 
SafeStringoperator+= (float num)
 
SafeStringoperator+= (double num)
 
SafeStringoperator+= (const __FlashStringHelper *str)
 
int compareTo (SafeString &s)
 returns -1 if this SafeString is < s, 0 if this SafeString == s and +1 if this SafeString > s More...
 
int compareTo (const char *cstr)
 returns -1 if this SafeString is < cstr, 0 if this SafeString == cstr and +1 if this SafeString > cst More...
 
unsigned char equals (SafeString &s)
 
unsigned char equals (const char *cstr)
 
unsigned char equals (const char c)
 
unsigned char operator== (SafeString &rhs)
 
unsigned char operator== (const char *cstr)
 
unsigned char operator== (const char c)
 
unsigned char operator!= (SafeString &rhs)
 
unsigned char operator!= (const char *cstr)
 
unsigned char operator!= (const char c)
 
unsigned char operator< (SafeString &rhs)
 
unsigned char operator> (SafeString &rhs)
 
unsigned char operator<= (SafeString &rhs)
 
unsigned char operator>= (SafeString &rhs)
 
unsigned char operator< (const char *rhs)
 
unsigned char operator> (const char *rhs)
 
unsigned char operator<= (const char *rhs)
 
unsigned char operator>= (const char *rhs)
 
unsigned char equalsIgnoreCase (SafeString &s)
 
unsigned char equalsIgnoreCase (const char *str2)
 
unsigned char equalsConstantTime (SafeString &s)
 
unsigned char startsWith (const char c, unsigned int fromIndex=0)
 returns non-zero of this SafeString starts this argument looking from fromIndex onwards. More...
 
unsigned char startsWith (const char *str2, unsigned int fromIndex=0)
 returns non-zero of this SafeString starts this argument looking from fromIndex onwards. More...
 
unsigned char startsWith (SafeString &s2, unsigned int fromIndex=0)
 returns non-zero of this SafeString starts this argument looking from fromIndex onwards. More...
 
unsigned char startsWithIgnoreCase (const char c, unsigned int fromIndex=0)
 returns non-zero of this SafeString starts this argument, ignoring case, looking from fromIndex onwards. More...
 
unsigned char startsWithIgnoreCase (const char *str2, unsigned int fromIndex=0)
 returns non-zero of this SafeString starts this argument, ignoring case, looking from fromIndex onwards. More...
 
unsigned char startsWithIgnoreCase (SafeString &s2, unsigned int fromIndex=0)
 returns non-zero of this SafeString starts this argument, ignoring case, looking from fromIndex onwards. More...
 
unsigned char endsWith (const char c)
 returns non-zero of this SafeString ends with the argument More...
 
unsigned char endsWith (SafeString &suffix)
 returns non-zero of this SafeString ends with the argument More...
 
unsigned char endsWith (const char *suffix)
 returns non-zero of this SafeString ends with the argument More...
 
unsigned char endsWithCharFrom (SafeString &suffix)
 returns non-zero of this SafeString ends any one of the chars in the argument More...
 
unsigned char endsWithCharFrom (const char *suffix)
 returns non-zero of this SafeString ends any one of the chars in the argument More...
 
char charAt (unsigned int index)
 returns the char at that location in this SafeString. More...
 
char operator[] (unsigned int index)
 returns the char at that location in this SafeString. More...
 
void setCharAt (unsigned int index, char c)
 sets the char at that location in this SafeString. More...
 
const char * c_str ()
 returns a const char* to the underlying char[ ] in this SafeString. More...
 
int indexOf (char ch, unsigned int fromIndex=0)
 returns the index of the char, searching from fromIndex. More...
 
int indexOf (const char *str, unsigned int fromIndex=0)
 returns the index of the string, searching from fromIndex. More...
 
int indexOf (SafeString &str, unsigned int fromIndex=0)
 returns the index of the SafeString, searching from fromIndex. More...
 
int lastIndexOf (char ch)
 returns the last index of the char, searching backwards from fromIndex (inclusive). More...
 
int lastIndexOf (char ch, unsigned int fromIndex)
 returns the last index of the char, searching backwards from fromIndex (inclusive). More...
 
int lastIndexOf (SafeString &str)
 returns the last index of the arguement, searching backwards from fromIndex (inclusive). More...
 
int lastIndexOf (SafeString &str, unsigned int fromIndex)
 returns the last index of the char, searching backwards from fromIndex (inclusive). More...
 
int lastIndexOf (const char *cstr)
 returns the last index of the arguement, searching backwards from fromIndex (inclusive). More...
 
int lastIndexOf (const char *cstr, unsigned int fromIndex)
 returns the last index of the char, searching backwards from fromIndex (inclusive). More...
 
int indexOfCharFrom (SafeString &str, unsigned int fromIndex=0)
 returns the first index of any char from the argument. More...
 
int indexOfCharFrom (const char *chars, unsigned int fromIndex=0)
 returns the first index of any char from the argument More...
 
SafeStringsubstring (SafeString &result, unsigned int beginIdx)
 The result is the substring from the beginIdx to the end of the SafeString. More...
 
SafeStringsubstring (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 included. More...
 
void replace (char findChar, char replaceChar)
 replace the findChar with the replaceChar More...
 
void replace (const char findChar, const char *replaceStr)
 replace the findChar with the replace string More...
 
void replace (const char findChar, SafeString &sfReplace)
 replace the findChar with the sfReplace SafeString contents More...
 
void replace (const char *findStr, const char *replaceStr)
 replace the findStr string with the replace string More...
 
void replace (SafeString &sfFind, SafeString &sfReplace)
 replace the occurances of the sfFind string, with the sfReplace SafeString contents More...
 
void removeFrom (unsigned int startIndex)
 remove all chars from startIndex to the end of the SafeString (inclusive) More...
 
void removeBefore (unsigned int startIndex)
 remove all chars from 0 to startIndex (exclusive), that is the char at startIndex is NOT removed More...
 
void remove (unsigned int index)
 remove all chars from index to the end of the SafeString (inclusive) More...
 
void remove (unsigned int index, unsigned int count)
 remove count chars starting from index More...
 
void removeLast (unsigned int count)
 remove the last count chars More...
 
void keepLast (unsigned int count)
 keep the last count chars and remove the rest More...
 
void toLowerCase (void)
 convert this SafeString to all lower case More...
 
void toUpperCase (void)
 convert this SafeString to all lower case More...
 
void trim (void)
 remove all white space from the front and back of this SafeString. More...
 
void processBackspaces (void)
 recursively remove backspaces, '\b' and the preceeding char. More...
 
unsigned char toInt (int &i)
 convert the SafeString to an int. More...
 
unsigned char toLong (long &l)
 convert the SafeString to a long. More...
 
unsigned char binToLong (long &l)
 convert the SafeString to a long assuming the SafeString in binary (0/1). More...
 
unsigned char octToLong (long &l)
 convert the SafeString to a long assuming the SafeString in octal (0 to 7). More...
 
unsigned char hexToLong (long &l)
 convert the SafeString to a long assuming the SafeString in HEX (0 to f or 0 to F). More...
 
unsigned char toUnsignedLong (unsigned long &l)
 convert the SafeString to an unsigned long. More...
 
unsigned char binToUnsignedLong (unsigned long &l)
 convert the SafeString to an unsigned long assuming the SafeString in binary (0/1). More...
 
unsigned char octToUnsignedLong (unsigned long &l)
 convert the SafeString to an unsigned long assuming the SafeString in octal (0 to 7). More...
 
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). More...
 
unsigned char toFloat (float &f)
 convert the SafeString to a float assuming the SafeString in the decimal format (not scientific) More...
 
unsigned char toDouble (double &d)
 convert the SafeString to a float assuming the SafeString in the decimal format (not scientific) More...
 
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 delimiter More...
 
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 delimiter More...
 
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 delimiter More...
 
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 and returns it in the token argument
by default a leading delimiter is stepped over before scanning for a delimited token when nextToken() is called
this firstToken() method overrides this and returns true and an empty token if the first char is a delimiter More...
 
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 and returns it in the token argument
by default a leading delimiter is stepped over before scanning for a delimited token, the firstToken argument can override this
setting the firstToken arguemnent = true, suppresses this so that an empty first token can be returned if the first char is a delimiter AND if returnEmptyFields is true More...
 
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 and returns it in the token argument
by default a leading delimiter is stepped over before scanning for a delimited token when nextToken() is called
this firstToken() method overrides this and returns true and an empty token if the first char is a delimiter More...
 
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 and returns it in the token argument
by default a leading delimiter is stepped over before scanning for a delimited token, the firstToken argument can override this
setting the firstToken arguemnent = true, suppresses this so that an empty first token can be returned if the first char is a delimiter AND if returnEmptyFields is true More...
 
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 and returns it in the token argument
by default a leading delimiter is stepped over before scanning for a delimited token when nextToken() is called
this firstToken() method overrides this and returns true and an empty token if the first char is a delimiter More...
 
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 and returns it in the token argument
by default a leading delimiter is stepped over before scanning for a delimited token, the firstToken argument can override this
setting the firstToken arguemnent = true, suppresses this so that an empty first token can be returned if the first char is a delimiter AND if returnEmptyFields is true More...
 
unsigned int readFrom (SafeString &sfInput, unsigned int startIdx=0)
 reads from the SafeString argument, starting at startIdx, into this SafeString. More...
 
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. More...
 
unsigned int writeTo (SafeString &output, unsigned int startIdx=0)
 writes from this SafeString, starting from startIdx, into the SafeString output arguement. More...
 
unsigned char read (Stream &input)
 reads from the Stream (if chars available) into the SafeString. More...
 
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 chars available
returns true if a delimiter found or SafeString is full, else false
if a delimiter is found it is returned at the end of the SafeString
Only at most one delimiter is added per call
Multiple sucessive delimiters require multiple calls to read them More...
 
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 chars available
returns true if a delimiter found or SafeString is full, else false
if a delimiter is found it is returned at the end of the SafeString
Only at most one delimiter is added per call
Multiple sucessive delimiters require multiple calls to read them More...
 
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 chars available
returns true if a delimiter found or SafeString is full, else false
if a delimiter is found it is returned at the end of the SafeString
Only at most one delimiter is added per call
Multiple sucessive delimiters require multiple calls to read them More...
 
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 this SafeString's capacity will return true with a non-empty token. 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 this SafeString's capacity will return true with a non-empty token. More...
 
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 this SafeString's capacity will return true with a non-empty token. More...
 
size_t getLastReadCount ()
 returns the number of chars read on previous calls to read, readUntil or readUntilToken (includes '\0' read if any). More...
 

Static Public Member Functions

static void setOutput (Print &debugOut, bool verbose=true)
 Turns on Error msgs and debug( ) output for all SafeStrings. More...
 
static void turnOutputOff (void)
 Turns off all debugging messages, both error messages AND debug() method output. More...
 
static void setVerbose (bool verbose)
 Controls size of error messages, setOutput sets verbose to true. More...
 
static unsigned char errorDetected ()
 Returns non-zero if any SafeString has detected and error, each call clears the internal global static flag. More...
 

Static Public Attributes

static SafeString::DebugPrint Output
 

Protected Member Functions

SafeStringconcatln (const __FlashStringHelper *pstr)
 
SafeStringconcatln (char c)
 
SafeStringconcatln (const char *cstr, size_t length)
 
void outputName () const
 
SafeStringconcatInternal (const char *cstr, size_t length, bool assignOp=false)
 
SafeStringconcatInternal (const __FlashStringHelper *str, size_t length, bool assignOp=false)
 
SafeStringconcatInternal (const char *cstr, bool assignOp=false)
 
SafeStringconcatInternal (char c, bool assignOp=false)
 
SafeStringconcatInternal (const __FlashStringHelper *str, bool assignOp=false)
 
size_t printInternal (long, int=DEC, bool assignOp=false)
 
size_t printInternal (unsigned long, int=DEC, bool assignOp=false)
 
size_t printInternal (double, int=2, bool assignOp=false)
 
void setError ()
 
void printlnErr () const
 
void debugInternalMsg (bool _fullDebug) const
 
size_t limitedStrLen (const char *p, size_t limit)
 
size_t printInt (double d, int decs, int width, bool forceSign, bool addNL)
 

Protected Attributes

char * buffer
 
size_t _capacity
 
size_t len
 

Static Protected Attributes

static Print * debugPtr
 
static bool fullDebug
 
static SafeString::noDebugPrint emptyPrint
 
static Print * currentOutput
 

Detailed Description

To create SafeStrings use one of the four (4) macros createSafeString or cSF, createSafeStringFromCharArray or cSFA, createSafeStringFromCharPtr or cSFP, createSafeStringFromCharPtrWithSize or cSFPS see the detailed description.

There are four (4) macros used to create SafeStrings.
createSafeString(name, size); creates a char[size+1] for you and wraps it in a SafeString called name.
createSafeStringFromCharArray(name, char[]); wraps an existing char[] in a SafeString called name.
createSafeStringFromCharPtr(name, char*); wraps an existing char[], pointed to by char*, in a SafeString called name. The capacity of the SafeString is limited to the initial strlen(char*) and cannot be increased
createSafeStringFromCharPtrWithSize(name, char*, size); wraps an existing char[size], pointed to by char*, in a SafeString called name.

Each of thise macros has a short hand
cSF(name,size) for createSafeString(name, size);
cSFA(name,char[]) for createSafeStringFromCharArray(name, char[]);
cSFP(name,char*) for createSafeStringFromCharPtr(name, char*);
cSFPS(name,char*,size) for createSafeStringFromCharPtrWithSize(name, char*, size);

createSafeString and cSF


createSafeString(name, size) or cSF(name,size)
and
createSafeString(name, size, "initialText") or cSF(name,size,"initialText")
are utility macros to create a SafeString of a given name and size and optionally, an initial value

createSafeString(str, 40); or cSF(str, 40); expands in the pre-processor to
char str_SAFEBUFFER[40+1];
SafeString str(sizeof(str_SAFEBUFFER),str_SAFEBUFFER,"","str");

createSafeString(str, 40, "test"); or cSF(str, 40, "test"); expands in the pre-processor to
char str_SAFEBUFFER[40+1];
SafeString str(sizeof(str_SAFEBUFFER),str_SAFEBUFFER,"test","str");

createSafeStringFromCharArray and cSFA


createSafeStringFromCharArray(name, char[]); or cSFA(name, char[]); wraps an existing char[] in a SafeString of the given name. e.g.
char charBuffer[15];
createSafeStringFromCharArray(str,charBuffer); or cSFA(str,charBuffer);
expands in the pre-processor to
SafeString str(sizeof(charBuffer),charBuffer, charBuffer, "str", true);
The capacity of the SafeString is set to 14, from the sizeof(charBuffer)-1 to allow for the terminating '\0'.

createSafeStringFromCharPtr and cSFP


createSafeStringFromCharPtr(name, char*); or cSFP(name, char*); wraps an existing char[] pointed to by char* in a SafeString of the given name
The capacity of the SafeString is set to strlen(charBuffer) and cannot be increased. e.g.
char charBuffer[15] = "1234567890";
char *bufPtr = charBuffer;
createSafeStringFromCharPtr(str,bufPtr); or cSFP(str,bufPtr);
expands in the pre-processor to
SafeString str((unsigned int)-1,charBuffer, charBuffer, "str", true);
The capacity of the SafeString is set to 10, from the strlen(bufPtr)

createSafeStringFromCharPtrWithSize and cSFPS


createSafeStringFromCharPtrWithSize(name, char*, unsigned int size); or cSFPS(name, char*, unsigned int size); wraps an existing char[], pointed to by char*, in a SafeString of the given name
and sets the capacity to the given unsigned int size e.g.
char charBuffer[15]="1234567890";
char *bufPtr = charBuffer;
createSafeStringFromCharPtrWithSize(str,bufPtr, 15); or cSFPS(str,bufPtr, 15);
expands in the pre-processor to
SafeString str(15,charBuffer, charBuffer, "str", true);
The capacity of the SafeString is set to 14 from size-1 to allow for the terminating '\0'.

See The SafeString alternative to Arduino Strings for Beginners for a tutorial.

Also see the example sketches included with the library
SafeString_ConstructorAndDebugging.ino, SafeStringFromCharArray.ino, SafeStringFromCharPtr.ino and SafeStringFromCharPtrWithSize.ino

Definition at line 307 of file SafeString.h.

Constructor & Destructor Documentation

◆ SafeString()

SafeString::SafeString ( unsigned int  maxLen,
char *  buf,
const char *  cstr,
const char *  _name = NULL,
bool  _fromBuffer = false,
bool  _fromPtr = true 
)
explicit

SafeString Constructor called from the four (4) macros createSafeString or cSF, createSafeStringFromCharArray or cSFA, createSafeStringFromCharPtr or cSFP, createSafeStringFromCharPtrWithSize or cSFPS.

This constructor is not designed to called directly by your code. See the detailed SafeString class description above.

Parameters
maxLen- the number of chars that can stored not including the terminating '\0'
buf- the fixed sized char buffer to hold the data
cstr- the initial data to be loaded into this SafeString
_name- the code variable name of this SafeString, default NULL
_fromBuffer- true if this SafeString is wrapping an existing char[]
_fromPtr- true if this SafeString's buf parameter was a char* rather then a char[], _fromPtr is not checked unless _fromBuffer is true

Member Function Documentation

◆ availableForWrite()

int SafeString::availableForWrite ( void  )

Returns the number chars that can be added to this SafeString before it is full.

◆ binToLong()

unsigned char SafeString::binToLong ( long &  l)

convert the SafeString to a long assuming the SafeString in binary (0/1).

Parameters
l– long reference, where the result is stored. l is only updated if the conversion is successful
Returns
– 0 if the SafeString is not a valid int, else return non-zero
leading and trailing white space is allowed around a valid int

◆ binToUnsignedLong()

unsigned char SafeString::binToUnsignedLong ( unsigned long &  l)

convert the SafeString to an unsigned long assuming the SafeString in binary (0/1).

Parameters
l– unsigned long reference, where the result is stored. l is only updated if the conversion is successful
Returns
– 0 if the SafeString is not a valid int, else return non-zero
leading and trailing white space is allowed around a valid int

◆ c_str()

const char* SafeString::c_str ( )

returns a const char* to the underlying char[ ] in this SafeString.

Do not make changes this char[ ]

◆ capacity()

unsigned int SafeString::capacity ( void  )

The maximum number of characters this SafeString can hold, excluding the terminating '\0'.

◆ charAt()

char SafeString::charAt ( unsigned int  index)

returns the char at that location in this SafeString.

Parameters
index- the zero based index of the char to return.
Returns
the char at that index OR if index >= length() return 0 and raise error

◆ clear()

SafeString& SafeString::clear ( void  )

Empties this SafeString.

returns the current SafeString so you can cascade calls e.g.
sfStr.clear().concat("New output");

◆ compareTo() [1/2]

int SafeString::compareTo ( const char *  cstr)

returns -1 if this SafeString is < cstr, 0 if this SafeString == cstr and +1 if this SafeString > cst

◆ compareTo() [2/2]

int SafeString::compareTo ( SafeString s)

returns -1 if this SafeString is < s, 0 if this SafeString == s and +1 if this SafeString > s

◆ concat() [1/13]

SafeString& SafeString::concat ( char  c)

◆ concat() [2/13]

SafeString& SafeString::concat ( const __FlashStringHelper *  str)

◆ concat() [3/13]

SafeString& SafeString::concat ( const __FlashStringHelper *  str,
size_t  length 
)

◆ concat() [4/13]

SafeString& SafeString::concat ( const char *  cstr)

◆ concat() [5/13]

SafeString& SafeString::concat ( const char *  cstr,
size_t  length 
)

◆ concat() [6/13]

SafeString& SafeString::concat ( double  num)

◆ concat() [7/13]

SafeString& SafeString::concat ( float  num)

◆ concat() [8/13]

SafeString& SafeString::concat ( int  num)

◆ concat() [9/13]

SafeString& SafeString::concat ( long  num)

◆ concat() [10/13]

SafeString& SafeString::concat ( SafeString str)

concat methods add to the end of the current SafeString.

returns the current SafeString so you can cascade calls e.g.
sfStr.concat(1).concat("second");
If there's not enough memory for the concatenated value, the SafeString will be left unchanged.
If the argument is null or invalid, the concatenation is considered unsucessful.

◆ concat() [11/13]

SafeString& SafeString::concat ( unsigned char  c)

◆ concat() [12/13]

SafeString& SafeString::concat ( unsigned int  num)

◆ concat() [13/13]

SafeString& SafeString::concat ( unsigned long  num)

◆ concatInternal() [1/5]

SafeString& SafeString::concatInternal ( char  c,
bool  assignOp = false 
)
protected

◆ concatInternal() [2/5]

SafeString& SafeString::concatInternal ( const __FlashStringHelper *  str,
bool  assignOp = false 
)
protected

◆ concatInternal() [3/5]

SafeString& SafeString::concatInternal ( const __FlashStringHelper *  str,
size_t  length,
bool  assignOp = false 
)
protected

◆ concatInternal() [4/5]

SafeString& SafeString::concatInternal ( const char *  cstr,
bool  assignOp = false 
)
protected

◆ concatInternal() [5/5]

SafeString& SafeString::concatInternal ( const char *  cstr,
size_t  length,
bool  assignOp = false 
)
protected

◆ concatln() [1/3]

SafeString& SafeString::concatln ( char  c)
protected

◆ concatln() [2/3]

SafeString& SafeString::concatln ( const __FlashStringHelper *  pstr)
protected

◆ concatln() [3/3]

SafeString& SafeString::concatln ( const char *  cstr,
size_t  length 
)
protected

◆ debug() [1/4]

const char* SafeString::debug ( bool  verbose = true)

Output the details about the this SafeString to the output specified by setOutput().

Parameters
verbose- true for detailed output including current contents

◆ debug() [2/4]

const char* SafeString::debug ( const __FlashStringHelper *  title,
bool  verbose = true 
)

Output the details about the this SafeString to the output specified by setOutput().

Parameters
title- the title to preceed the debug output, a space between this and the debug output
verbose- true for detailed output including current contents

◆ debug() [3/4]

const char* SafeString::debug ( const char *  title,
bool  verbose = true 
)

Output the details about the this SafeString to the output specified by setOutput().

Parameters
title- the title to preceed the debug output, a space between this and the debug output
verbose- true for detailed output including current contents

◆ debug() [4/4]

const char* SafeString::debug ( SafeString stitle,
bool  verbose = true 
)

Output the details about the this SafeString to the output specified by setOutput().

Parameters
title- the title to preceed the debug output, a space between this and the debug output
verbose- true for detailed output including current contents

◆ debugInternalMsg()

void SafeString::debugInternalMsg ( bool  _fullDebug) const
protected

◆ endsWith() [1/3]

unsigned char SafeString::endsWith ( const char *  suffix)

returns non-zero of this SafeString ends with the argument

Parameters
suffix– string to check for

◆ endsWith() [2/3]

unsigned char SafeString::endsWith ( const char  c)

returns non-zero of this SafeString ends with the argument

Parameters
c– the char to check for

◆ endsWith() [3/3]

unsigned char SafeString::endsWith ( SafeString suffix)

returns non-zero of this SafeString ends with the argument

Parameters
suffixSafeString to check for

◆ endsWithCharFrom() [1/2]

unsigned char SafeString::endsWithCharFrom ( const char *  suffix)

returns non-zero of this SafeString ends any one of the chars in the argument

Parameters
suffix– the string containing the chars to check for

◆ endsWithCharFrom() [2/2]

unsigned char SafeString::endsWithCharFrom ( SafeString suffix)

returns non-zero of this SafeString ends any one of the chars in the argument

Parameters
suffix– the SafeString containing the chars to check for

◆ equals() [1/3]

unsigned char SafeString::equals ( const char *  cstr)

◆ equals() [2/3]

unsigned char SafeString::equals ( const char  c)

◆ equals() [3/3]

unsigned char SafeString::equals ( SafeString s)

◆ equalsConstantTime()

unsigned char SafeString::equalsConstantTime ( SafeString s)

◆ equalsIgnoreCase() [1/2]

unsigned char SafeString::equalsIgnoreCase ( const char *  str2)

◆ equalsIgnoreCase() [2/2]

unsigned char SafeString::equalsIgnoreCase ( SafeString s)

◆ errorDetected()

static unsigned char SafeString::errorDetected ( )
static

Returns non-zero if any SafeString has detected and error, each call clears the internal global static flag.

◆ firstToken() [1/3]

unsigned char SafeString::firstToken ( SafeString token,
char  delimiter,
bool  returnLastNonDelimitedToken = true 
)
inline

returns true if a delimited token is found, removes the first delimited token from this SafeString and returns it in the token argument
by default a leading delimiter is stepped over before scanning for a delimited token when nextToken() is called
this firstToken() method overrides this and returns true and an empty token if the first char is a delimiter

The delimiter is not returned and remains in the SafeString so you can test which delimiter terminated the token, provided this SafeString is not empty!
The token argument is always cleared at the start of the nexttToken().
IMPORTANT!! Changed V4.0.4 Now by default un-delimited tokens at the end of the SafeString are returned
To leave partial un-delimited tokens on the end of the SafeString, set returnLastNonDelimitedToken = false.
Setting returnLastNonDelimitedToken = false allows the SafeString to hold partial tokens when reading from an input stream one char at a time.

Parameters
token- the SafeString to return the token in, it is always cleared first and will be empty if no delimited token is found or if there are errors
The token's capacity should be >= this SafeString's capacity incase the entire SafeString needs to be returned.
If the token's capacity is < the next token, then nextToken() returns true, but the returned token argument is empty and an error messages printed if debug is enabled.
In this case to next token is still removed from the SafeString so that the program will not be stuck in an infinite loop calling nextToken()
delimiter- the char which can delimit a token
returnLastNonDelimitedToken– default true, will return last part of SafeString even if not delimited. If set false, will keep it for further input to be added to this SafeString NOTE: since the last delimiter is left in the SafeString, you should only use firstToken() for the first call.
Returns
– true if firstToken() finds a token in this SafeString that is terminated by one of the delimiters, else false
If the return is true, but hasError() is true then the SafeString token argument did not have the capacity to hold the next token.
in this case to next token is still removed from the SafeString so that the program will not be stuck in an infinite loop calling nextToken()
while being consistent with the SafeString's all or nothing insertion rule
Input argument errors return false and an empty token and hasError() is set on both this SafeString and the token SafeString.

Definition at line 1528 of file SafeString.h.

◆ firstToken() [2/3]

unsigned char SafeString::firstToken ( SafeString token,
const char *  delimiters,
bool  returnLastNonDelimitedToken = true 
)
inline

returns true if a delimited token is found, removes the first delimited token from this SafeString and returns it in the token argument
by default a leading delimiter is stepped over before scanning for a delimited token when nextToken() is called
this firstToken() method overrides this and returns true and an empty token if the first char is a delimiter

The delimiter is not returned and remains in the SafeString so you can test which delimiter terminated the token, provided this SafeString is not empty!
The token argument is always cleared at the start of the nexttToken().
IMPORTANT!! Changed V4.0.4 Now by default un-delimited tokens at the end of the SafeString are returned
To leave partial un-delimited tokens on the end of the SafeString, set returnLastNonDelimitedToken = false.
Setting returnLastNonDelimitedToken = false allows the SafeString to hold partial tokens when reading from an input stream one char at a time.

Parameters
token- the SafeString to return the token in, it is always cleared first and will be empty if no delimited token is found or if there are errors
The token's capacity should be >= this SafeString's capacity incase the entire SafeString needs to be returned.
If the token's capacity is < the next token, then firstToken() returns true, but the returned token argument is empty and an error messages printed if debug is enabled.
In this case to next token is still removed from the SafeString so that the program will not be stuck in an infinite loop calling nextToken()
delimiters- the string containing the delimiting characters, any one of which can delimit a token
returnLastNonDelimitedToken– default true, will return last part of SafeString even if not delimited. If set false, will keep it for further input to be added to this SafeString NOTE: since the last delimiter is left in the SafeString, you should only use firstToken() for the first call.
Returns
– true if firstToken() finds a token in this SafeString that is terminated by one of the delimiters, else false
If the return is true, but hasError() is true then the SafeString token argument did not have the capacity to hold the next token.
in this case to next token is still removed from the SafeString so that the program will not be stuck in an infinite loop calling nextToken()
while being consistent with the SafeString's all or nothing insertion rule
Input argument errors return false and an empty token and hasError() is set on both this SafeString and the token SafeString.

Definition at line 1650 of file SafeString.h.

◆ firstToken() [3/3]

unsigned char SafeString::firstToken ( SafeString token,
SafeString  delimiters,
bool  returnLastNonDelimitedToken = true 
)
inline

returns true if a delimited token is found, removes the first delimited token from this SafeString and returns it in the token argument
by default a leading delimiter is stepped over before scanning for a delimited token when nextToken() is called
this firstToken() method overrides this and returns true and an empty token if the first char is a delimiter

The delimiter is not returned and remains in the SafeString so you can test which delimiter terminated the token, provided this SafeString is not empty!
The token argument is always cleared at the start of the nexttToken().
IMPORTANT!! Changed V4.0.4 Now by default un-delimited tokens at the end of the SafeString are returned
To leave partial un-delimited tokens on the end of the SafeString, set returnLastNonDelimitedToken = false.
Setting returnLastNonDelimitedToken = false allows the SafeString to hold partial tokens when reading from an input stream one char at a time.

Parameters
token- the SafeString to return the token in, it is always cleared first and will be empty if no delimited token is found or if there are errors
The token's capacity should be >= this SafeString's capacity incase the entire SafeString needs to be returned.
If the token's capacity is < the next token, then nextToken() returns true, but the returned token argument is empty and an error messages printed if debug is enabled.
In this case to next token is still removed from the SafeString so that the program will not be stuck in an infinite loop calling nextToken()
delimiters- the SafeString containing the delimiting characters, any one of which can delimit a token
returnLastNonDelimitedToken– default true, will return last part of SafeString even if not delimited. If set false, will keep it for further input to be added to this SafeString NOTE: since the last delimiter is left in the SafeString, you should only use firstToken() for the first call.
Returns
– true if firstToken() finds a token in this SafeString that is terminated by one of the delimiters, else false
If the return is true, but hasError() is true then the SafeString token argument did not have the capacity to hold the next token.
in this case to next token is still removed from the SafeString so that the program will not be stuck in an infinite loop calling nextToken()
while being consistent with the SafeString's all or nothing insertion rule
Input argument errors return false and an empty token and hasError() is set on both this SafeString and the token SafeString.

Definition at line 1589 of file SafeString.h.

◆ getLastReadCount()

size_t SafeString::getLastReadCount ( )

returns the number of chars read on previous calls to read, readUntil or readUntilToken (includes '\0' read if any).

Each call read, readUntil, readUntilToken first clears this count

Returns
- the char count read since the last call to this method.

◆ hasError()

unsigned char SafeString::hasError ( )

Returns non-zero if any error detected for this SafeString, each call clears the internal flag.

◆ hexToLong()

unsigned char SafeString::hexToLong ( long &  l)

convert the SafeString to a long assuming the SafeString in HEX (0 to f or 0 to F).

Parameters
l– long reference, where the result is stored. l is only updated if the conversion is successful
Returns
– 0 if the SafeString is not a valid int, else return non-zero
leading and trailing white space is allowed around a valid int

◆ hexToUnsignedLong()

unsigned char SafeString::hexToUnsignedLong ( unsigned long &  l)

convert the SafeString to an unsigned long assuming the SafeString in HEX (0 to f or 0 to F).

Parameters
l– unsigned long reference, where the result is stored. l is only updated if the conversion is successful
Returns
– 0 if the SafeString is not a valid int, else return non-zero
leading and trailing white space is allowed around a valid int

◆ indexOf() [1/3]

int SafeString::indexOf ( char  ch,
unsigned int  fromIndex = 0 
)

returns the index of the char, searching from fromIndex.

Parameters
ch- the char to search for
fromIndex- where to start the search from, default 0, that is from begining
if fromIndex > length() raise an error
if fromIndex == -1 OR fromIndex == length(), return -1 without error
Returns
-1 if not found, else the index in the range 0 to length()-1

◆ indexOf() [2/3]

int SafeString::indexOf ( const char *  str,
unsigned int  fromIndex = 0 
)

returns the index of the string, searching from fromIndex.

Parameters
str- the string to search for
fromIndex- where to start the search from, default 0, that is from begining
if fromIndex > length() raise an error
if fromIndex == -1 OR fromIndex == length(), return -1 without error
Returns
-1 if not found, else the index in the range 0 to length()-1

◆ indexOf() [3/3]

int SafeString::indexOf ( SafeString str,
unsigned int  fromIndex = 0 
)

returns the index of the SafeString, searching from fromIndex.

Parameters
str- the SafeString to search for
fromIndex- where to start the search from, default 0, that is from begining
if fromIndex > length() raise an error
if fromIndex == -1 OR fromIndex == length(), return -1 without error
Returns
-1 if not found, else the index in the range 0 to length()-1

◆ indexOfCharFrom() [1/2]

int SafeString::indexOfCharFrom ( const char *  chars,
unsigned int  fromIndex = 0 
)

returns the first index of any char from the argument

Parameters
chars- the string containing the chars to search for
fromIndex- where to start searching backwards from,
if fromIndex > length() raise an error
if fromIndex == -1 OR fromIndex == length(), return -1 without error
Returns
-1 if not found, else the index in the range 0 to length()-1

◆ indexOfCharFrom() [2/2]

int SafeString::indexOfCharFrom ( SafeString str,
unsigned int  fromIndex = 0 
)

returns the first index of any char from the argument.

Parameters
str- the SafeString containing the chars to search for
fromIndex- where to start searching from,
if fromIndex > length() raise an error
if fromIndex == -1 OR fromIndex == length(), return -1 without error
Returns
-1 if not found, else the index in the range 0 to length()-1

◆ isEmpty()

unsigned char SafeString::isEmpty ( void  )

Returns non-zero if the SafeString is empty.

◆ isFull()

unsigned char SafeString::isFull ( void  )

Returns non-zero if the SafeString is full.

◆ keepLast()

void SafeString::keepLast ( unsigned int  count)

keep the last count chars and remove the rest

Parameters
count- the number of chars to keep
0 to length() is valid for count
count == 0 clears the SafeString
count > length() raises and error and leaves the SafeString unchanged

◆ lastIndexOf() [1/6]

int SafeString::lastIndexOf ( char  ch)

returns the last index of the char, searching backwards from fromIndex (inclusive).

Parameters
c- the char to search for
Returns
-1 if not found, else the index in the range 0 to length()-1

◆ lastIndexOf() [2/6]

int SafeString::lastIndexOf ( char  ch,
unsigned int  fromIndex 
)

returns the last index of the char, searching backwards from fromIndex (inclusive).

Parameters
c- the char to search for
fromIndex- where to start searching backwards from,
if fromIndex > length() raise an error
if fromIndex == -1 OR fromIndex == length(), return -1 without error
Returns
-1 if not found, else the index in the range 0 to length()-1

◆ lastIndexOf() [3/6]

int SafeString::lastIndexOf ( const char *  cstr)

returns the last index of the arguement, searching backwards from fromIndex (inclusive).

Parameters
cstr- the string to search for
Returns
-1 if not found, else the index in the range 0 to length()-1

◆ lastIndexOf() [4/6]

int SafeString::lastIndexOf ( const char *  cstr,
unsigned int  fromIndex 
)

returns the last index of the char, searching backwards from fromIndex (inclusive).

Parameters
cstr- the string to search for
fromIndex- where to start searching backwards from,
if fromIndex > length() raise an error
if fromIndex == -1 OR fromIndex == length(), return -1 without error
Returns
-1 if not found, else the index in the range 0 to length()-1

◆ lastIndexOf() [5/6]

int SafeString::lastIndexOf ( SafeString str)

returns the last index of the arguement, searching backwards from fromIndex (inclusive).

Parameters
str- the SafeString to search for
Returns
-1 if not found, else the index in the range 0 to length()-1

◆ lastIndexOf() [6/6]

int SafeString::lastIndexOf ( SafeString str,
unsigned int  fromIndex 
)

returns the last index of the char, searching backwards from fromIndex (inclusive).

Parameters
str- the SafeString to search for
fromIndex- where to start searching backwards from,
if fromIndex > length() raise an error
if fromIndex == -1 OR fromIndex == length(), return -1 without error
Returns
-1 if not found, else the index in the range 0 to length()-1

◆ length()

unsigned int SafeString::length ( void  )

Number of characters current in the SafeString, excluding the terminating '\0'.

◆ limitedStrLen()

size_t SafeString::limitedStrLen ( const char *  p,
size_t  limit 
)
protected

◆ newline()

SafeString& SafeString::newline ( )

Adds \r\n to this SafeString.

returns the current SafeString so you can cascade calls e.g.
sfStr.newline().concat("next line");

◆ nextToken() [1/3]

unsigned char SafeString::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 and returns it in the token argument
by default a leading delimiter is stepped over before scanning for a delimited token, the firstToken argument can override this
setting the firstToken arguemnent = true, suppresses this so that an empty first token can be returned if the first char is a delimiter AND if returnEmptyFields is true

The delimiter is not returned and remains in the SafeString so you can test which delimiter terminated the token, provided this SafeString is not empty!
The token argument is always cleared at the start of the nexttToken().
IMPORTANT!! Changed V4.0.4 Now by default un-delimited tokens at the end of the SafeString are returned
To leave partial un-delimited tokens on the end of the SafeString, set returnLastNonDelimitedToken = false.
Setting returnLastNonDelimitedToken = false allows the SafeString to hold partial tokens when reading from an input stream one char at a time.
Setting firstToken = true suppressed skipping over a leading delimiter so that an empty first field can be returned if the first char is a delimiter

Parameters
token- the SafeString to return the token in, it is always cleared first and will be empty if no delimited token is found or if there are errors
The token's capacity should be >= this SafeString's capacity incase the entire SafeString needs to be returned.
If the token's capacity is < the next token, then firstToken() returns true, but the returned token argument is empty and an error messages printed if debug is enabled.
In this case to next token is still removed from the SafeString so that the program will not be stuck in an infinite loop calling nextToken()
delimiter- the char which can delimit a token
returnEmptyFields– default false, if true, nextToken() will return true, and an empty token for each consecutive delimiters
returnLastNonDelimitedToken– default true, will return last part of SafeString even if not delimited. If set false, will keep it for further input to be added to this SafeString
firstToken– default false, a leading delimiter will be stepped over before looking for a delimited token
if set to true, a leading delimiter will delimit an empty token which will be returned only if returnEmptyFields is true otherwise it is skipped over.
NOTE: if returnEmptyFields == false this firstToken argument has no effect.
NOTE: since the last delimiter is left in the SafeString, you must set firstToken to be false (or omit it) after the first call.
Returns
– true if nextToken() finds a token in this SafeString that is terminated by one of the delimiters, else false
If the return is true, but hasError() is true then the SafeString token argument did not have the capacity to hold the next token.
in this case to next token is still removed from the SafeString so that the program will not be stuck in an infinite loop calling nextToken()
while being consistent with the SafeString's all or nothing insertion rule
Input argument errors return false and an empty token and hasError() is set on both this SafeString and the token SafeString.

◆ nextToken() [2/3]

unsigned char SafeString::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 and returns it in the token argument
by default a leading delimiter is stepped over before scanning for a delimited token, the firstToken argument can override this
setting the firstToken arguemnent = true, suppresses this so that an empty first token can be returned if the first char is a delimiter AND if returnEmptyFields is true

The delimiter is not returned and remains in the SafeString so you can test which delimiter terminated the token, provided this SafeString is not empty!
The token argument is always cleared at the start of the nexttToken().
IMPORTANT!! Changed V4.0.4 Now by default un-delimited tokens at the end of the SafeString are returned
To leave partial un-delimited tokens on the end of the SafeString, set returnLastNonDelimitedToken = false.
Setting returnLastNonDelimitedToken = false allows the SafeString to hold partial tokens when reading from an input stream one char at a time.
Setting firstToken = true suppressed skipping over a leading delimiter so that an empty first field can be returned if the first char is a delimiter

Parameters
token- the SafeString to return the token in, it is always cleared first and will be empty if no delimited token is found or if there are errors
The token's capacity should be >= this SafeString's capacity incase the entire SafeString needs to be returned.
If the token's capacity is < the next token, then nextToken() returns true, but the returned token argument is empty and an error messages printed if debug is enabled.
In this case to next token is still removed from the SafeString so that the program will not be stuck in an infinite loop calling nextToken()
delimiters- the string containing the delimiting characters, any one of which can delimit a token
returnEmptyFields– default false, if true, nextToken() will return true, and an empty token for each consecutive delimiters
returnLastNonDelimitedToken– default true, will return last part of SafeString even if not delimited. If set false, will keep it for further input to be added to this SafeString
firstToken– default false, a leading delimiter will be stepped over before looking for a delimited token
if set to true, a leading delimiter will delimit an empty token which will be returned only if returnEmptyFields is true otherwise it is skipped over.
NOTE: if returnEmptyFields == false this firstToken argument has no effect.
NOTE: since the last delimiter is left in the SafeString, you must set firstToken to be false (or omit it) after the first call.
Returns
– true if nextToken() finds a token in this SafeString that is terminated by one of the delimiters, else false
If the return is true, but hasError() is true then the SafeString token argument did not have the capacity to hold the next token.
in this case to next token is still removed from the SafeString so that the program will not be stuck in an infinite loop calling nextToken()
while being consistent with the SafeString's all or nothing insertion rule
Input argument errors return false and an empty token and hasError() is set on both this SafeString and the token SafeString.

◆ nextToken() [3/3]

unsigned char SafeString::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 and returns it in the token argument
by default a leading delimiter is stepped over before scanning for a delimited token, the firstToken argument can override this
setting the firstToken arguemnent = true, suppresses this so that an empty first token can be returned if the first char is a delimiter AND if returnEmptyFields is true

The delimiter is not returned and remains in the SafeString so you can test which delimiter terminated the token, provided this SafeString is not empty!
The token argument is always cleared at the start of the nexttToken().
IMPORTANT!! Changed V4.0.4 Now by default un-delimited tokens at the end of the SafeString are returned
To leave partial un-delimited tokens on the end of the SafeString, set returnLastNonDelimitedToken = false.
Setting returnLastNonDelimitedToken = false allows the SafeString to hold partial tokens when reading from an input stream one char at a time.
Setting firstToken = true suppressed skipping over a leading delimiter so that an empty first field can be returned if the first char is a delimiter

Parameters
token- the SafeString to return the token in, it is always cleared first and will be empty if no delimited token is found or if there are errors
The token's capacity should be >= this SafeString's capacity incase the entire SafeString needs to be returned.
If the token's capacity is < the next token, then firstToken() returns true, but the returned token argument is empty and an error messages printed if debug is enabled.
In this case to next token is still removed from the SafeString so that the program will not be stuck in an infinite loop calling nextToken()
delimiters- the SafeString containing the delimiting characters, any one of which can delimit a token
returnEmptyFields– default false, if true, nextToken() will return true, and an empty token for each consecutive delimiters
returnLastNonDelimitedToken– default true, will return last part of SafeString even if not delimited. If set false, will keep it for further input to be added to this SafeString
firstToken– default false, a leading delimiter will be stepped over before looking for a delimited token
if set to true, a leading delimiter will delimit an empty token which will be returned only if returnEmptyFields is true otherwise it is skipped over.
NOTE: if returnEmptyFields == false this firstToken argument has no effect.
NOTE: since the last delimiter is left in the SafeString, you must set firstToken to be false (or omit it) after the first call.
Returns
– true if nextToken() finds a token in this SafeString that is terminated by one of the delimiters, else false
If the return is true, but hasError() is true then the SafeString token argument did not have the capacity to hold the next token.
in this case to next token is still removed from the SafeString so that the program will not be stuck in an infinite loop calling nextToken()
while being consistent with the SafeString's all or nothing insertion rule
Input argument errors return false and an empty token and hasError() is set on both this SafeString and the token SafeString.

◆ octToLong()

unsigned char SafeString::octToLong ( long &  l)

convert the SafeString to a long assuming the SafeString in octal (0 to 7).

Parameters
l– long reference, where the result is stored. l is only updated if the conversion is successful
Returns
– 0 if the SafeString is not a valid int, else return non-zero
leading and trailing white space is allowed around a valid int

◆ octToUnsignedLong()

unsigned char SafeString::octToUnsignedLong ( unsigned long &  l)

convert the SafeString to an unsigned long assuming the SafeString in octal (0 to 7).

Parameters
l– unsigned long reference, where the result is stored. l is only updated if the conversion is successful
Returns
– 0 if the SafeString is not a valid int, else return non-zero
leading and trailing white space is allowed around a valid int

◆ operator!=() [1/3]

unsigned char SafeString::operator!= ( const char *  cstr)
inline

Definition at line 828 of file SafeString.h.

◆ operator!=() [2/3]

unsigned char SafeString::operator!= ( const char  c)
inline

Definition at line 831 of file SafeString.h.

◆ operator!=() [3/3]

unsigned char SafeString::operator!= ( SafeString rhs)
inline

Definition at line 825 of file SafeString.h.

◆ operator+=() [1/11]

SafeString& SafeString::operator+= ( char  c)
inline

Definition at line 771 of file SafeString.h.

◆ operator+=() [2/11]

SafeString& SafeString::operator+= ( const __FlashStringHelper *  str)
inline

Definition at line 795 of file SafeString.h.

◆ operator+=() [3/11]

SafeString& SafeString::operator+= ( const char *  cstr)
inline

Definition at line 768 of file SafeString.h.

◆ operator+=() [4/11]

SafeString& SafeString::operator+= ( double  num)
inline

Definition at line 792 of file SafeString.h.

◆ operator+=() [5/11]

SafeString& SafeString::operator+= ( float  num)
inline

Definition at line 789 of file SafeString.h.

◆ operator+=() [6/11]

SafeString& SafeString::operator+= ( int  num)
inline

Definition at line 777 of file SafeString.h.

◆ operator+=() [7/11]

SafeString& SafeString::operator+= ( long  num)
inline

Definition at line 783 of file SafeString.h.

◆ operator+=() [8/11]

SafeString& SafeString::operator+= ( SafeString rhs)
inline

+= operator concatinate to the SafeString.

e.g.
sfStr += "end";
gives the same SafeString as
sfStr.concat("end");

Definition at line 765 of file SafeString.h.

◆ operator+=() [9/11]

SafeString& SafeString::operator+= ( unsigned char  num)
inline

Definition at line 774 of file SafeString.h.

◆ operator+=() [10/11]

SafeString& SafeString::operator+= ( unsigned int  num)
inline

Definition at line 780 of file SafeString.h.

◆ operator+=() [11/11]

SafeString& SafeString::operator+= ( unsigned long  num)
inline

Definition at line 786 of file SafeString.h.

◆ operator-=() [1/11]

SafeString& SafeString::operator-= ( char  c)
inline

Definition at line 724 of file SafeString.h.

◆ operator-=() [2/11]

SafeString& SafeString::operator-= ( const __FlashStringHelper *  str)
inline

Definition at line 748 of file SafeString.h.

◆ operator-=() [3/11]

SafeString& SafeString::operator-= ( const char *  cstr)
inline

Definition at line 721 of file SafeString.h.

◆ operator-=() [4/11]

SafeString& SafeString::operator-= ( double  num)
inline

Definition at line 745 of file SafeString.h.

◆ operator-=() [5/11]

SafeString& SafeString::operator-= ( float  num)
inline

Definition at line 742 of file SafeString.h.

◆ operator-=() [6/11]

SafeString& SafeString::operator-= ( int  num)
inline

Definition at line 730 of file SafeString.h.

◆ operator-=() [7/11]

SafeString& SafeString::operator-= ( long  num)
inline

Definition at line 736 of file SafeString.h.

◆ operator-=() [8/11]

SafeString& SafeString::operator-= ( SafeString rhs)
inline

-= operator prefixes the SafeString.

e.g.
sfStr -= "top";
gives the same SafeString as
sfStr.prefix("top");

Definition at line 718 of file SafeString.h.

◆ operator-=() [9/11]

SafeString& SafeString::operator-= ( unsigned char  num)
inline

Definition at line 727 of file SafeString.h.

◆ operator-=() [10/11]

SafeString& SafeString::operator-= ( unsigned int  num)
inline

Definition at line 733 of file SafeString.h.

◆ operator-=() [11/11]

SafeString& SafeString::operator-= ( unsigned long  num)
inline

Definition at line 739 of file SafeString.h.

◆ operator<() [1/2]

unsigned char SafeString::operator< ( const char *  rhs)
inline

Definition at line 846 of file SafeString.h.

◆ operator<() [2/2]

unsigned char SafeString::operator< ( SafeString rhs)
inline

Definition at line 834 of file SafeString.h.

◆ operator<=() [1/2]

unsigned char SafeString::operator<= ( const char *  rhs)
inline

Definition at line 852 of file SafeString.h.

◆ operator<=() [2/2]

unsigned char SafeString::operator<= ( SafeString rhs)
inline

Definition at line 840 of file SafeString.h.

◆ operator=() [1/11]

SafeString& SafeString::operator= ( char  c)

Clears this SafeString and concatinates a single char.

If the value is null or invalid, or too large to be fit in the string's internal buffer the resulting SafeString will be empty

Parameters
c- the char

◆ operator=() [2/11]

SafeString& SafeString::operator= ( const __FlashStringHelper *  pstr)

Clears this SafeString and copies and concatinates the contents of the argument.

If the value is null or invalid, or too large to be fit in the string's internal buffer the resulting SafeString will be empty

Parameters
pstr- the '\0' terminated F("..") string to assign

◆ operator=() [3/11]

SafeString& SafeString::operator= ( const char *  cstr)

Clears this SafeString and copies and concatinates the contents of the argument.

If the value is null or invalid, or too large to be fit in the string's internal buffer the resulting SafeString will be empty

Parameters
cstr- the '\0' terminated char string to assign

◆ operator=() [4/11]

SafeString& SafeString::operator= ( double  num)

Clears this SafeString and concatinates the text version of the argument.

If the value is null or invalid, or too large to be fit in the string's internal buffer the resulting SafeString will be empty

Parameters
num- the number to convert to text

◆ operator=() [5/11]

SafeString& SafeString::operator= ( float  num)

Clears this SafeString and concatinates the text version of the argument.

If the value is null or invalid, or too large to be fit in the string's internal buffer the resulting SafeString will be empty

Parameters
num- the number to convert to text

◆ operator=() [6/11]

SafeString& SafeString::operator= ( int  num)

Clears this SafeString and concatinates the text version of the argument.

If the value is null or invalid, or too large to be fit in the string's internal buffer the resulting SafeString will be empty

Parameters
num- the number to convert to text

◆ operator=() [7/11]

SafeString& SafeString::operator= ( long  num)

Clears this SafeString and concatinates the text version of the argument.

If the value is null or invalid, or too large to be fit in the string's internal buffer the resulting SafeString will be empty

Parameters
num- the number to convert to text

◆ operator=() [8/11]

SafeString& SafeString::operator= ( SafeString sfStr)

Clears this SafeString and copies and concatinates the contents of the argument.

If the value is null or invalid, or too large to be fit in the string's internal buffer the resulting SafeString will be empty

Parameters
sfStr- the SafeString to assign

◆ operator=() [9/11]

SafeString& SafeString::operator= ( unsigned char  num)

Clears this SafeString and concatinates the text version of the argument.

If the value is null or invalid, or too large to be fit in the string's internal buffer the resulting SafeString will be empty

Parameters
num- the number to convert to text

◆ operator=() [10/11]

SafeString& SafeString::operator= ( unsigned int  num)

Clears this SafeString and concatinates the text version of the argument.

If the value is null or invalid, or too large to be fit in the string's internal buffer the resulting SafeString will be empty

Parameters
num- the number to convert to text

◆ operator=() [11/11]

SafeString& SafeString::operator= ( unsigned long  num)

Clears this SafeString and concatinates the text version of the argument.

If the value is null or invalid, or too large to be fit in the string's internal buffer the resulting SafeString will be empty

Parameters
num- the number to convert to text

◆ operator==() [1/3]

unsigned char SafeString::operator== ( const char *  cstr)
inline

Definition at line 819 of file SafeString.h.

◆ operator==() [2/3]

unsigned char SafeString::operator== ( const char  c)
inline

Definition at line 822 of file SafeString.h.

◆ operator==() [3/3]

unsigned char SafeString::operator== ( SafeString rhs)
inline

Definition at line 816 of file SafeString.h.

◆ operator>() [1/2]

unsigned char SafeString::operator> ( const char *  rhs)
inline

Definition at line 849 of file SafeString.h.

◆ operator>() [2/2]

unsigned char SafeString::operator> ( SafeString rhs)
inline

Definition at line 837 of file SafeString.h.

◆ operator>=() [1/2]

unsigned char SafeString::operator>= ( const char *  rhs)
inline

Definition at line 855 of file SafeString.h.

◆ operator>=() [2/2]

unsigned char SafeString::operator>= ( SafeString rhs)
inline

Definition at line 843 of file SafeString.h.

◆ operator[]()

char SafeString::operator[] ( unsigned int  index)

returns the char at that location in this SafeString.

Parameters
index- the zero based index of the char to return.
Returns
the char at that index OR if index >= length() return 0 and raise error

◆ outputName()

void SafeString::outputName ( ) const
protected

◆ prefix() [1/13]

SafeString& SafeString::prefix ( char  c)

◆ prefix() [2/13]

SafeString& SafeString::prefix ( const __FlashStringHelper *  str)

◆ prefix() [3/13]

SafeString& SafeString::prefix ( const __FlashStringHelper *  str,
size_t  length 
)

◆ prefix() [4/13]

SafeString& SafeString::prefix ( const char *  cstr)

◆ prefix() [5/13]

SafeString& SafeString::prefix ( const char *  cstr,
size_t  length 
)

◆ prefix() [6/13]

SafeString& SafeString::prefix ( double  num)

◆ prefix() [7/13]

SafeString& SafeString::prefix ( float  num)

◆ prefix() [8/13]

SafeString& SafeString::prefix ( int  num)

◆ prefix() [9/13]

SafeString& SafeString::prefix ( long  num)

◆ prefix() [10/13]

SafeString& SafeString::prefix ( SafeString s)

prefix methods add to the front of the current SafeString.

returns the current SafeString so you can cascade calls, e.g.
sfStr.prefix(2).prefix("first");
If there's not enough memory for the to add prefix, the SafeString will be left unchanged
If the argument is null or invalid, the prefix is considered unsucessful.

◆ prefix() [11/13]

SafeString& SafeString::prefix ( unsigned char  c)

◆ prefix() [12/13]

SafeString& SafeString::prefix ( unsigned int  num)

◆ prefix() [13/13]

SafeString& SafeString::prefix ( unsigned long  num)

◆ print() [1/11]

size_t SafeString::print ( char  )

◆ print() [2/11]

size_t SafeString::print ( const __FlashStringHelper *  )

◆ print() [3/11]

size_t SafeString::print ( const char *  )

◆ print() [4/11]

size_t SafeString::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 the specified width.

This methods can also be used for ints and longs by passing 0 for the decs.

Parameters
d- the double to convert to text
decs- the preferred number of decimial places to output(limited to <7). This will be reduced automatically to fit the fixed width
width- fixed width the output is to padded/limited to
forceSign- optional, defaults to false, if true the + sign is added for +ve numbers

◆ print() [5/11]

size_t SafeString::print ( double  ,
int  = 2 
)

◆ print() [6/11]

size_t SafeString::print ( int  ,
int  = DEC 
)

◆ print() [7/11]

size_t SafeString::print ( long  ,
int  = DEC 
)

◆ print() [8/11]

size_t SafeString::print ( SafeString str)

◆ print() [9/11]

size_t SafeString::print ( unsigned char  ,
int  = DEC 
)

◆ print() [10/11]

size_t SafeString::print ( unsigned int  ,
int  = DEC 
)

◆ print() [11/11]

size_t SafeString::print ( unsigned long  ,
int  = DEC 
)

◆ printInt()

size_t SafeString::printInt ( double  d,
int  decs,
int  width,
bool  forceSign,
bool  addNL 
)
protected

◆ printInternal() [1/3]

size_t SafeString::printInternal ( double  ,
int  = 2,
bool  assignOp = false 
)
protected

◆ printInternal() [2/3]

size_t SafeString::printInternal ( long  ,
int  = DEC,
bool  assignOp = false 
)
protected

◆ printInternal() [3/3]

size_t SafeString::printInternal ( unsigned long  ,
int  = DEC,
bool  assignOp = false 
)
protected

◆ println() [1/12]

size_t SafeString::println ( char  )

◆ println() [2/12]

size_t SafeString::println ( const __FlashStringHelper *  )

◆ println() [3/12]

size_t SafeString::println ( const char *  )

◆ println() [4/12]

size_t SafeString::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 the specified width and adds a trailing CR NL.

This methods can also be used for ints and longs by passing 0 for the decs.

Parameters
d- the double to convert to text
decs- the preferred number of decimial places to output (limited to <7). This will be reduced automatically to fit the fixed width
width- fixed width the output is to padded/limited to
forceSign- optional, defaults to false, if true the + sign is added for +ve numbers

◆ println() [5/12]

size_t SafeString::println ( double  ,
int  = 2 
)

◆ println() [6/12]

size_t SafeString::println ( int  ,
int  = DEC 
)

◆ println() [7/12]

size_t SafeString::println ( long  ,
int  = DEC 
)

◆ println() [8/12]

size_t SafeString::println ( SafeString str)

◆ println() [9/12]

size_t SafeString::println ( unsigned char  ,
int  = DEC 
)

◆ println() [10/12]

size_t SafeString::println ( unsigned int  ,
int  = DEC 
)

◆ println() [11/12]

size_t SafeString::println ( unsigned long  ,
int  = DEC 
)

◆ println() [12/12]

size_t SafeString::println ( void  )

◆ printlnErr()

void SafeString::printlnErr ( ) const
protected

◆ printTo()

size_t SafeString::printTo ( Print &  p) const

Implements the Printable interface.

Parameters
p- where to print to

◆ processBackspaces()

void SafeString::processBackspaces ( void  )

recursively remove backspaces, '\b' and the preceeding char.

useful for processing inputs from terminal (Telent) connections

◆ read()

unsigned char SafeString::read ( Stream &  input)

reads from the Stream (if chars available) into the SafeString.

The is NON-BLOCKING and returns immediately if nothing available to be read Note: if the SafeString is already full, then nothing will be read and false will be returned

Parameters
-the Stream reference to read from
Returns
true if something added to this SafeString, else false

◆ readFrom() [1/2]

unsigned int SafeString::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.

This lets you read from a char* into a SafeString without errors if the strlen(char*) or maxCharsToRead are larger than the SafeString capacity Use sfResult.clear(); to empty the SafeString first and then sfResult.readFrom(strPtr); to read a much as you can The read stops at first '\0' or the calling SafeString is full or when maxCharsToRead have been read. Note: if the SafeString is already full, then nothing will be read

Parameters
strPtr- pointer char array to read from
maxCharsToRead– the maximum chars to read into the SafeString, defaults to ((unsigned int)-1) i.e. max unsigned int.
Returns
- the number of chars read

◆ readFrom() [2/2]

unsigned int SafeString::readFrom ( SafeString sfInput,
unsigned int  startIdx = 0 
)

reads from the SafeString argument, starting at startIdx, into this SafeString.

The read stops when the end of the SafeString argument is reached or the calling SafeString is full Note: if the SafeString is already full, then nothing will be read and startIdx will be returned

Note: to limit the number of chars read in from sfInput (starting at 0), use
sfStr.readFrom(sfInput.c_str(),maxCharsToRead);

Parameters
sfInput- the SafeString to read from
startIdx- where to start reading from, defaults to 0, if startIdx >= sfInput.length(), nothing read and sfInput.length() returned
Returns
- the new startIdx

◆ readUntil() [1/3]

unsigned char SafeString::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 chars available
returns true if a delimiter found or SafeString is full, else false
if a delimiter is found it is returned at the end of the SafeString
Only at most one delimiter is added per call
Multiple sucessive delimiters require multiple calls to read them

Parameters
input- the Stream reference to read from
delimiters- the string containing the characters any one of which can be a delimieter
Returns
true if SafeString is full or a delimiter is read, else false

◆ readUntil() [2/3]

unsigned char SafeString::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 chars available
returns true if a delimiter found or SafeString is full, else false
if a delimiter is found it is returned at the end of the SafeString
Only at most one delimiter is added per call
Multiple sucessive delimiters require multiple calls to read them

Parameters
input- the Stream reference to read from
delimiter- the char which is the delimieter
Returns
true if SafeString is full or a delimiter is read, else false

◆ readUntil() [3/3]

unsigned char SafeString::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 chars available
returns true if a delimiter found or SafeString is full, else false
if a delimiter is found it is returned at the end of the SafeString
Only at most one delimiter is added per call
Multiple sucessive delimiters require multiple calls to read them

Parameters
input- the Stream reference to read from
delimiters- the SafeString containing the characters any one of which can be a delimieter
Returns
true if SafeString is full or a delimiter is read, else false

◆ readUntilToken() [1/3]

unsigned char SafeString::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 this SafeString's capacity will return true with a non-empty token.


Streams of chars that overflow this SafeString's capacity are ignored and return an empty token on the next delimiter
A timeout can be specified to return the last un-delimited token after chars stop arriving.
The input read can be echoed back to the input Stream
You can force a skip to the next delimiter to discard partial tokens

That is this SafeString's capacity should be at least 1 more then the largest expected token.
If this SafeString OR the SafeString& token return argument is too small to hold the result, the token is returned empty and an error message output if debugging is enabled.
The delimiter is NOT included in the SafeString& token return. It will the first char of the this SafeString when readUntilToken returns true
It is recommended that the capacity of the SafeString& token argument be >= this SafeString's capacity
Each call to this method removes any leading delimiters so if you need to check the delimiter do it BEFORE the next call to readUntilToken()

Parameters
input- the Stream reference to read from
token- the SafeString to return the token found if any, this always cleared at the start of this method
delimiters- the string containing the characters any one of which can be a delimieter
skipToDelimiter- a bool reference variable to hold the skipToDelimiter state between calls
If this is true all chars upto the next delimiter (or timeout) will be discarded and false returned with an empty token
echoInput- defaults to false, pass non-zero (true) to echo the chars read back to the input Stream
timeout_ms- defaults to never timeout, pass a non-zero ms to auto-terminate the last token if no new chars received for that time.
Returns
- true if a delimited series of chars found that fit in this SafeString else false
If this SafeString OR the SafeString& token argument is too small to hold the result, the token is returned empty
If a delimited token is found that fits in this SafeString but is too large for the token then true is returned and an empty token returned and an error raised on both this SafeString and the token
The delimiter is NOT included in the SafeString& token return. It will the first char of the this SafeString when readUntilToken returns true

◆ readUntilToken() [2/3]

unsigned char SafeString::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 this SafeString's capacity will return true with a non-empty token.


Streams of chars that overflow this SafeString's capacity are ignored and return an empty token on the next delimiter
A timeout can be specified to return the last un-delimited token after chars stop arriving.
The input read can be echoed back to the input Stream
You can force a skip to the next delimiter to discard partial tokens

That is this SafeString's capacity should be at least 1 more then the largest expected token.
If this SafeString OR the SafeString& token return argument is too small to hold the result, the token is returned empty and an error message output if debugging is enabled.
The delimiter is NOT included in the SafeString& token return. It will the first char of the this SafeString when readUntilToken returns true
It is recommended that the capacity of the SafeString& token argument be >= this SafeString's capacity
Each call to this method removes any leading delimiters so if you need to check the delimiter do it BEFORE the next call to readUntilToken()

Parameters
input- the Stream reference to read from
token- the SafeString to return the token found if any, this always cleared at the start of this method
delimiter- the char which is the delimieter
skipToDelimiter- a bool reference variable to hold the skipToDelimiter state between calls
If this is true all chars upto the next delimiter (or timeout) will be discarded and false returned with an empty token
echoInput- defaults to false, pass non-zero (true) to echo the chars read back to the input Stream
timeout_ms- defaults to never timeout, pass a non-zero ms to auto-terminate the last token if no new chars received for that time.
Returns
- true if a delimited series of chars found that fit in this SafeString else false
If this SafeString OR the SafeString& token argument is too small to hold the result, the token is returned empty
If a delimited token is found that fits in this SafeString but is too large for the token then true is returned and an empty token returned and an error raised on both this SafeString and the token
The delimiter is NOT included in the SafeString& token return. It will the first char of the this SafeString when readUntilToken returns true

◆ readUntilToken() [3/3]

unsigned char SafeString::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 this SafeString's capacity will return true with a non-empty token.


Streams of chars that overflow this SafeString's capacity are ignored and return an empty token on the next delimiter
A timeout can be specified to return the last un-delimited token after chars stop arriving.
The input read can be echoed back to the input Stream
You can force a skip to the next delimiter to discard partial tokens

That is this SafeString's capacity should be at least 1 more then the largest expected token.
If this SafeString OR the SafeString& token return argument is too small to hold the result, the token is returned empty and an error message output if debugging is enabled.
The delimiter is NOT included in the SafeString& token return. It will the first char of the this SafeString when readUntilToken returns true
It is recommended that the capacity of the SafeString& token argument be >= this SafeString's capacity
Each call to this method removes any leading delimiters so if you need to check the delimiter do it BEFORE the next call to readUntilToken()

Parameters
input- the Stream reference to read from
token- the SafeString to return the token found if any, this always cleared at the start of this method
delimiters- the SafeString containing the characters any one of which can be a delimieter
skipToDelimiter- a bool reference variable to hold the skipToDelimiter state between calls
If this is true all chars upto the next delimiter (or timeout) will be discarded and false returned with an empty token
echoInput- defaults to false, pass non-zero (true) to echo the chars read back to the input Stream
timeout_ms- defaults to never timeout, pass a non-zero ms to auto-terminate the last token if no new chars received for that time.
Returns
- true if a delimited series of chars found that fit in this SafeString else false
If this SafeString OR the SafeString& token argument is too small to hold the result, the token is returned empty
If a delimited token is found that fits in this SafeString but is too large for the token then true is returned and an empty token returned and an error raised on both this SafeString and the token
The delimiter is NOT included in the SafeString& token return. It will the first char of the this SafeString when readUntilToken returns true

◆ remove() [1/2]

void SafeString::remove ( unsigned int  index)

remove all chars from index to the end of the SafeString (inclusive)

Parameters
index- the index of the first char to be removed
Valid index is 0 to length(), and -1 which is treated as length()
Other index raise an error.

◆ remove() [2/2]

void SafeString::remove ( unsigned int  index,
unsigned int  count 
)

remove count chars starting from index

Parameters
index- the index of the first char to be removed
Valid index is 0 to length(), and -1 which is treated as length()
Other index raise an error.
count- the number of chars to remove
if count > length() - index, count is set to length() - index, and a error raised.

◆ removeBefore()

void SafeString::removeBefore ( unsigned int  startIndex)

remove all chars from 0 to startIndex (exclusive), that is the char at startIndex is NOT removed

Parameters
startIndex- the index of the first char NOT to be removed
Valid startIndex is 0 to length(), and -1 which is treated as length()
Other startIndex raise an error.

◆ removeFrom()

void SafeString::removeFrom ( unsigned int  startIndex)

remove all chars from startIndex to the end of the SafeString (inclusive)

Parameters
startIndex- the index of the first char to be removed
Valid startIndex is 0 to length(), and -1 which is treated as length()
Other startIndex raise an error.

◆ removeLast()

void SafeString::removeLast ( unsigned int  count)

remove the last count chars

Parameters
count- the number of chars to remove
0 to length() is valid for count
count >= length() clears the SafeString
count > length() raises and error

◆ replace() [1/5]

void SafeString::replace ( char  findChar,
char  replaceChar 
)

replace the findChar with the replaceChar

Parameters
findChar- the char to be replaced
replaceChar- the char to replace it

◆ replace() [2/5]

void SafeString::replace ( const char *  findStr,
const char *  replaceStr 
)

replace the findStr string with the replace string

Parameters
findStr- the string to be replaced
replaceStr- the string to replace it

◆ replace() [3/5]

void SafeString::replace ( const char  findChar,
const char *  replaceStr 
)

replace the findChar with the replace string

Parameters
findChar- the char to be replaced
replaceStr- the string to replace it

◆ replace() [4/5]

void SafeString::replace ( const char  findChar,
SafeString sfReplace 
)

replace the findChar with the sfReplace SafeString contents

Parameters
findChar- the char to be replaced
sfReplace- the SafeString whose contents will replace it

◆ replace() [5/5]

void SafeString::replace ( SafeString sfFind,
SafeString sfReplace 
)

replace the occurances of the sfFind string, with the sfReplace SafeString contents

Parameters
sfFind- the SafeString containing the string of chars to be replaced
sfReplace- the SafeString whose contents will replace it

◆ reserve()

unsigned char SafeString::reserve ( unsigned int  size)

Checks there is enough free space in this SafeString for the current operation.

Parameters
size- total number of chars that will be in the SafeString if the operation completes

◆ setCharAt()

void SafeString::setCharAt ( unsigned int  index,
char  c 
)

sets the char at that location in this SafeString.

Parameters
index- the zero based index of the char to return.
c- the char to set at that index. '\0' chars are ignored and an error raised.

◆ setError()

void SafeString::setError ( )
protected

◆ setOutput()

static void SafeString::setOutput ( Print &  debugOut,
bool  verbose = true 
)
static

Turns on Error msgs and debug( ) output for all SafeStrings.

This also sets output for the debug( ) method.

Parameters
debugOut- where to send the messages to, usually Serial, e.g. SafeString::setOutput(Serial);
verbose- optional, if missing defaults to true, use false for compact error messages or call setVerbose(false)

◆ setVerbose()

static void SafeString::setVerbose ( bool  verbose)
static

Controls size of error messages, setOutput sets verbose to true.

Parameters
verbose- true for detailed error messages, else short error messages.

◆ startsWith() [1/3]

unsigned char SafeString::startsWith ( const char *  str2,
unsigned int  fromIndex = 0 
)

returns non-zero of this SafeString starts this argument looking from fromIndex onwards.

Parameters
str– string to check for
fromIndex– where in the SafeString to start looking, default 0, that is start from beginning

◆ startsWith() [2/3]

unsigned char SafeString::startsWith ( const char  c,
unsigned int  fromIndex = 0 
)

returns non-zero of this SafeString starts this argument looking from fromIndex onwards.

Parameters
c– char to check for
fromIndex– where in the SafeString to start looking, default 0, that is start from beginning

◆ startsWith() [3/3]

unsigned char SafeString::startsWith ( SafeString s2,
unsigned int  fromIndex = 0 
)

returns non-zero of this SafeString starts this argument looking from fromIndex onwards.

Parameters
s– the SafeString to check for
fromIndex– where in the SafeString to start looking, default 0, that is start from beginning

◆ startsWithIgnoreCase() [1/3]

unsigned char SafeString::startsWithIgnoreCase ( const char *  str2,
unsigned int  fromIndex = 0 
)

returns non-zero of this SafeString starts this argument, ignoring case, looking from fromIndex onwards.

Parameters
str– string to check for, ignoring case
fromIndex– where in the SafeString to start looking, default 0, that is start from beginning

◆ startsWithIgnoreCase() [2/3]

unsigned char SafeString::startsWithIgnoreCase ( const char  c,
unsigned int  fromIndex = 0 
)

returns non-zero of this SafeString starts this argument, ignoring case, looking from fromIndex onwards.

Parameters
c– char to check for, ignoring case
fromIndex– where in the SafeString to start looking, default 0, that is start from beginning

◆ startsWithIgnoreCase() [3/3]

unsigned char SafeString::startsWithIgnoreCase ( SafeString s2,
unsigned int  fromIndex = 0 
)

returns non-zero of this SafeString starts this argument, ignoring case, looking from fromIndex onwards.

Parameters
sSafeString to check for, ignoring case
fromIndex– where in the SafeString to start looking, default 0, that is start from beginning

◆ stoken() [1/3]

int SafeString::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 delimiter

Any leading delimiters are first stepped over and then the delimited token is return in the token argument (less the delimiter).
The token argument is always cleared at the start of the stoken().
if there are any argument errors or the token does not have the capacity to hold the substring, hasError() is set on both this SafeString and the token SafeString

Parameters
token- the SafeString to return the token in, it is cleared if no delimited token found or if there are errors
The found delimited token (less the delimiter) is returned in the token SafeString argument if there is capacity.
The token's capacity should be >= this SafeString's capacity incase the entire SafeString needs to be returned.
If the token's capacity is < the next token, then token is returned empty and an error messages printed if debug is enabled.
in this case the return (nextIndex) is still updated.
fromIndex- where to start the search from 0 to length() and -1 is valid for fromIndex, -1 => length() for processing
delimiters- the string containing the characters that any one of which can delimit a token. The end of the SafeString is always a delimiter.
returnEmptyFields- default false, if true only skip one leading delimiter after each call.
If the fromIndex is 0 and there is a delimiter at the beginning of the SafeString, an empty token will be returned
useAsDelimiters- default true, if false then token will consists of only chars in the delimiters and any other char terminates the token
Returns
- nextIndex, the next index in this SafeString after the end of the token just found, -1 if this is the last token
Use this as the fromIndex for the next call
NOTE: if there are no delimiters then -1 is returned and the whole SafeString returned in token if the SafeString token argument is large enough
If the token's capacity is < the next token, the token returned is empty and an error messages printed if debug is enabled.
In this case the returned nextIndex is still updated to end of the token just found so that that the program will not be stuck in an infinite loop testing for nextIndex >=0
while being consistent with the SafeString's all or nothing insertion rule
Input argument errors return -1 and an empty token and hasError() is set on both this SafeString and the token SafeString.

◆ stoken() [2/3]

int SafeString::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 delimiter

Any leading delimiters are first stepped over and then the delimited token is return in the token argument (less the delimiter).
The token argument is always cleared at the start of the stoken().
if there are any argument errors or the token does not have the capacity to hold the substring, hasError() is set on both this SafeString and the token SafeString

Parameters
token- the SafeString to return the token in, it is cleared if no delimited token found or if there are errors
The found delimited token (less the delimiter) is returned in the token SafeString argument if there is capacity.
The token's capacity should be >= this SafeString's capacity incase the entire SafeString needs to be returned.
If the token's capacity is < the next token, then token is returned empty and an error messages printed if debug is enabled.
in this case the return (nextIndex) is still updated.
fromIndex- where to start the search from 0 to length() and -1 is valid for fromIndex, -1 => length() for processing
delimiter- the char that delimits a token. The end of the SafeString is always a delimiter.
returnEmptyFields- default false, if true only skip one leading delimiter after each call.
If the fromIndex is 0 and there is a delimiter at the beginning of the SafeString, an empty token will be returned
useAsDelimiters- default true, if false then token will consists of only chars in the delimiters and any other char terminates the token
Returns
- nextIndex, the next index in this SafeString after the end of the token just found, -1 if this is the last token
Use this as the fromIndex for the next call
NOTE: if there are no delimiters then -1 is returned and the whole SafeString returned in token if the SafeString token argument is large enough
If the token's capacity is < the next token, the token returned is empty and an error messages printed if debug is enabled.
In this case the returned nextIndex is still updated to end of the token just found so that that the program will not be stuck in an infinite loop testing for nextIndex >=0
while being consistent with the SafeString's all or nothing insertion rule
Input argument errors return -1 and an empty token and hasError() is set on both this SafeString and the token SafeString.

◆ stoken() [3/3]

int SafeString::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 delimiter

Any leading delimiters are first stepped over and then the delimited token is return in the token argument (less the delimiter).
The token argument is always cleared at the start of the stoken().
if there are any argument errors or the token does not have the capacity to hold the substring, hasError() is set on both this SafeString and the token SafeString

Parameters
token- the SafeString to return the token in, it is cleared if no delimited token found or if there are errors
The found delimited token (less the delimiter) is returned in the token SafeString argument if there is capacity.
The token's capacity should be >= this SafeString's capacity incase the entire SafeString needs to be returned.
If the token's capacity is < the next token, then token is returned empty and an error messages printed if debug is enabled.
in this case the return (nextIndex) is still updated.
fromIndex- where to start the search from 0 to length() and -1 is valid for fromIndex, -1 => length() for processing
delimiters- the SafeString containing the characters that any one of which can delimit a token. The end of the SafeString is always a delimiter.
returnEmptyFields- default false, if true only skip one leading delimiter after each call.
If the fromIndex is 0 and there is a delimiter at the beginning of the SafeString, an empty token will be returned
useAsDelimiters- default true, if false then token will consists of only chars in the delimiters and any other char terminates the token
Returns
- nextIndex, the next index in this SafeString after the end of the token just found, -1 if this is the last token
Use this as the fromIndex for the next call
NOTE: if there are no delimiters then -1 is returned and the whole SafeString returned in token if the SafeString token argument is large enough
If the token's capacity is < the next token, the token returned is empty and an error messages printed if debug is enabled.
In this case the returned nextIndex is still updated to end of the token just found so that that the program will not be stuck in an infinite loop testing for nextIndex >=0
while being consistent with the SafeString's all or nothing insertion rule
Input argument errors return -1 and an empty token and hasError() is set on both this SafeString and the token SafeString.

◆ substring() [1/2]

SafeString& SafeString::substring ( SafeString result,
unsigned int  beginIdx 
)

The result is the substring from the beginIdx to the end of the SafeString.

Parameters
result- the substring
beginIdx- the index of the start of the substring,
if beginIdx == length(), the result is empty (no error)
if beginIdx > length(), the result is empty and an error is raised.
if beginIdx == (unsigned int)(-1),the result is empty (no error).
Returns
result

The result SafeString is ALWAYS cleares to start with and so will be empty if there are any errors.
If the result SafeString does not have the capacity to hold the substring, an empty result is returned and an error raised on both this SafeString and the result.
You can take a substring of yourself e.g.
sfStr.substring(sfStr,3);

◆ substring() [2/2]

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 included.

Parameters
result- the substring
beginIdx- the index of the start of the substring,
if beginIdx == length(), the result is empty (no error)
if beginIdx > length(), the result is empty and an error is raised.
if beginIdx == (unsigned int)(-1),the result is empty (no error).
if beginIdx > endIdx, beginIdx and endIdx will be swapped so that beginIdx <= endIdx and the error flag is set on both this SafeString and the result SafeString
endIdx- the index after the end of the substring, that is endIdx is NOT included
if endIdx > length(), endIdx is set to length(); and the error flag is set on both this SafeString and the result SafeString
if endIdx == (unsigned int)(-1) is treated as endIdx == length() returns a result without an error
Returns
result

The result SafeString is ALWAYS cleares to start with and so will be empty if there are any errors.
If the result SafeString does not have the capacity to hold the substring, an empty result is returned and an error raised on both this SafeString and the result.
You can take a substring of yourself e.g.
sfStr.substring(sfStr,3);

◆ toDouble()

unsigned char SafeString::toDouble ( double &  d)

convert the SafeString to a float assuming the SafeString in the decimal format (not scientific)

Parameters
d– double reference, where the result is stored. l is only updated if the conversion is successful
Returns
– 0 if the SafeString is not a valid int, else return non-zero
leading and trailing white space is allowed around a valid int

◆ toFloat()

unsigned char SafeString::toFloat ( float &  f)

convert the SafeString to a float assuming the SafeString in the decimal format (not scientific)

Parameters
f– float reference, where the result is stored. l is only updated if the conversion is successful
Returns
– 0 if the SafeString is not a valid int, else return non-zero
leading and trailing white space is allowed around a valid int

◆ toInt()

unsigned char SafeString::toInt ( int &  i)

convert the SafeString to an int.

Parameters
i– int reference, where the result is stored. i is only updated if the conversion is successful
Returns
– 0 if the SafeString is not a valid int, else return non-zero
leading and trailing white space is allowed around a valid int

◆ toLong()

unsigned char SafeString::toLong ( long &  l)

convert the SafeString to a long.

Parameters
l– long reference, where the result is stored. l is only updated if the conversion is successful
Returns
– 0 if the SafeString is not a valid int, else return non-zero
leading and trailing white space is allowed around a valid int

◆ toLowerCase()

void SafeString::toLowerCase ( void  )

convert this SafeString to all lower case

◆ toUnsignedLong()

unsigned char SafeString::toUnsignedLong ( unsigned long &  l)

convert the SafeString to an unsigned long.

Parameters
l– long reference, where the result is stored. l is only updated if the conversion is successful
Returns
– 0 if the SafeString is not a valid int, else return non-zero
leading and trailing white space is allowed around a valid int

◆ toUpperCase()

void SafeString::toUpperCase ( void  )

convert this SafeString to all lower case

◆ trim()

void SafeString::trim ( void  )

remove all white space from the front and back of this SafeString.

The C method isspace() is used. For the 'C' local the following are trimmed
' ' (0x20) space (SPC)
'\t' (0x09) horizontal tab (TAB)
'\n' (0x0a) newline (LF)
'\v' (0x0b) vertical tab (VT)
'\f' (0x0c) feed (FF)
'\r' (0x0d) carriage return (CR)

◆ turnOutputOff()

static void SafeString::turnOutputOff ( void  )
static

Turns off all debugging messages, both error messages AND debug() method output.

Errors are still detected and flagged.

◆ write() [1/2]

virtual size_t SafeString::write ( const uint8_t *  buffer,
size_t  length 
)
virtual

Write (concatinate) bytes to this SafeString, from Print class.

'\0' bytes are not allowed and will be skipped over and will raise an error.

Parameters
buffer- bytes to concatinate to this SafeString
length- number of bytes to concatinate

◆ write() [2/2]

virtual size_t SafeString::write ( uint8_t  b)
virtual

Write (concatinate) a byte to this SafeString, from Print class.

'\0' bytes are not allowed and will not be added to the SafeString and will raise an error.

Parameters
b- the byte to concatinate to this SafeString

◆ writeTo()

unsigned int SafeString::writeTo ( SafeString output,
unsigned int  startIdx = 0 
)

writes from this SafeString, starting from startIdx, into the SafeString output arguement.

The write stops when the end if the calling SafeString is reached or the output is full Note: if the output is already full, then nothing will be written and startIdx will be returned unchanged

Parameters
output- the SafeString to write to
startIdx- where to start writing from in the calling SafeString, defaults to 0, if startIdx >= length(), nothing written and length() returned
Returns
new startIdx for next write

Member Data Documentation

◆ _capacity

size_t SafeString::_capacity
protected

Definition at line 1885 of file SafeString.h.

◆ buffer

char* SafeString::buffer
protected

Definition at line 1884 of file SafeString.h.

◆ currentOutput

Print* SafeString::currentOutput
staticprotected

Definition at line 1904 of file SafeString.h.

◆ debugPtr

Print* SafeString::debugPtr
staticprotected

Definition at line 1882 of file SafeString.h.

◆ emptyPrint

SafeString::noDebugPrint SafeString::emptyPrint
staticprotected

Definition at line 1902 of file SafeString.h.

◆ fullDebug

bool SafeString::fullDebug
staticprotected

Definition at line 1883 of file SafeString.h.

◆ len

size_t SafeString::len
protected

Definition at line 1886 of file SafeString.h.

◆ Output

SafeString::DebugPrint SafeString::Output
static

Definition at line 1924 of file SafeString.h.