SafeString  4.1.27
SafeString is a safe, robust and debuggable replacement for string processing in Arduino
SafeString.h
Go to the documentation of this file.
1 // !!!!!!!!! WARNING in V2 substring endIdx is EXCLUSIVE !!!!!!!!!! change from V1 inclusive
2 /*
3  The SafeString class V4.1.26
4  Note: ESP32 gives warning: "F" redefined which can be ignored
5 
6  ----------------- creating SafeStrings ---------------------------------
7  See the example sketches SafeString_ConstructorAndDebugging.ino and SafeStringFromCharArray.ino
8  and SafeStringFromCharPtr.ino and SafeStringFromCharPtrWithSize.ion
9 
10  createSafeString(name, size) and createSafeString(name, size, "initialText")
11  are utility macros to create an SafeString of a given name and size and optionally, an initial value
12 
13  createSafeString(str, 40); or cSF(str, 40);
14  expands in the pre-processor to
15  char str_SAFEBUFFER[40+1];
16  SafeString str(sizeof(str_SAFEBUFFER),str_SAFEBUFFER,"","str");
17 
18  createSafeString(str, 40, "test"); or cSF(str, 40, "test");
19  expands in the pre-processor to
20  char str_SAFEBUFFER[40+1];
21  SafeString str(sizeof(str_SAFEBUFFER),str_SAFEBUFFER,"test","str");
22 
23  createSafeStringFromCharArray(name, char[]); or cSFA(name, char[]);
24  wraps an existing char[] in a SafeString of the given name
25  e.g.
26  char charBuffer[15];
27  createSafeStringFromCharArray(str,charBuffer); or cSFA(str,charBuffer);
28  expands in the pre-processor to
29  SafeString str(sizeof(charBuffer),charBuffer, charBuffer, "str", true);
30 
31  createSafeStringFromCharPtrWithSize(name, char*, unsigned int); or cSFPS(name, char*, unsigned int);
32  wraps an existing char[] pointed to by char* in a SafeString of the given name and sets the capacity to the given size
33  e.g.
34  char charBuffer[15]; // can hold 14 char + terminating '\0'
35  char *bufPtr = charBuffer;
36  createSafeStringFromCharPtrWithSize(str,bufPtr, 14); or cSFPS(str,bufPtr, 14);
37  expands in the pre-processor to
38  SafeString str(14+1,charBuffer, charBuffer, "str", true);
39  The capacity of the SafeString is set to 14.
40 
41  createSafeStringFromCharPtr(name, char*); or cSFP(name, char*);
42  wraps an existing char[] pointed to by char* in a SafeString of the given name
43  createSafeStringFromCharPtr(name, char* s) is the same as createSafeStringFromCharPtrWithSzie(name, char* s, strlen(s));
44  That is the current strlen() is used to set the SafeString capacity.
45  e.g.
46  char charBuffer[15] = "test";
47  char *bufPtr = charBuffer;
48  createSafeStringFromCharPtr(str,bufPtr); or cSFP(str,bufPtr);
49  expands in the pre-processor to
50  SafeString str(0,charBuffer, charBuffer, "str", true);
51  and the capacity of the SafeString is set to strlen(charBuffer) and cannot be increased.
52 
53 
54  If str is a SafeString then
55  str = .. works for signed/unsigned ints, char*, char, F(".."), SafeString float, double etc
56  str.concat(..) and string.prefix(..) also works for those
57  str.stoken(..) can be used to split a string in to tokens
58 
59  SafeStrings created via createSafeString( ) are never invalid, even if called with invalid arguments.
60  SafeStrings created via createSafeStringFromBuffer( ) are valid as long at the buffer is valid.
61  Usually the only way the buffer can become invalid is if it exists in a struct that is allocated (via calloc/malloc)
62  and then freed while the SafeString wrapping it is still in use.
63 *********************************/
64 
65 /*
66  SafeString.h static memory SafeString library modified by
67  Matthew Ford
68  Mods Copyright(c)2020 Forward Computing and Control Pty. Ltd.
69  All rights reservered subject to the License below
70 
71  modified from
72  WString.h - String library for Wiring & Arduino
73  ...mostly rewritten by Paul Stoffregen...
74  Copyright (c) 2009-10 Hernando Barragan. All right reserved.
75  Copyright 2011, Paul Stoffregen, paul@pjrc.com
76 
77  This library is free software; you can redistribute it and/or
78  modify it under the terms of the GNU Lesser General Public
79  License as published by the Free Software Foundation; either
80  version 2.1 of the License, or (at your option) any later version.
81 
82  This library is distributed in the hope that it will be useful,
83  but WITHOUT ANY WARRANTY; without even the implied warranty of
84  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
85  Lesser General Public License for more details.
86 
87  You should have received a copy of the GNU Lesser General Public
88  License along with this library; if not, write to the Free Software
89  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
90 */
91 // bool versus unsigned char
92 // on UNO, ESP8266 and ESP32 sizeof(bool) == 1 i.e. same size as unsigned char
93 // but bool is safer as bool + 1 does not compile
94 // however Arduino uses unsigned char as return value so...
95 #ifndef SafeString_class_h
96 #define SafeString_class_h
97 
98 #if defined(ARDUINO_ARCH_SAM)
99 #define bool int
100 #endif
101 
102 #ifdef __cplusplus
103 
104 #include <stdlib.h>
105 #include <string.h>
106 #include <ctype.h>
107 
108 
109 
110 #if defined(ESP_PLATFORM) || defined(ARDUINO_ARCH_ESP8266)
111 #include <pgmspace.h>
112 #elif defined(ARDUINO_ARDUINO_NANO33BLE) || defined(ARDUINO_ARCH_MBED_RP2040)|| defined(ARDUINO_ARCH_RP2040)
113 #include <api/deprecated-avr-comp/avr/pgmspace.h>
114 #else
115 #include <avr/pgmspace.h>
116 #endif
117 
118 #include <stdint.h>
119 #include <Print.h>
120 #include <Printable.h>
121 
122 // This include handles the rename of Stream for MBED compiles
123 #if defined(ARDUINO_ARDUINO_NANO33BLE) || defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_MBED_RP2040) || defined(ARDUINO_ARCH_RP2040)
124 #include <Stream.h>
125 #elif defined( __MBED__ ) || defined( MBED_H )
126 #include <WStream.h>
127 #define Stream WStream
128 #else
129 #include <Stream.h>
130 #endif
131 
132 // handle namespace arduino
134 
135 class __FlashStringHelper;
136 //#ifndef F // Arduino IDE 2+ adds namespace around this macro so need to define it here also
137 // ESP32 defines a macro F(s) (s), so will get a redefinition warning which can be ignored
138 #define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))
139 //#endif
140 
141 
142 // to remove all the error messages, comment out
143 #define SSTRING_DEBUG
144 // this saves program bytes and the ram used by the SafeString object names
145 //
146 // Usually just leave as is and use SafeString::setOutput(..) to control the error messages and debug output
147 // there will be no error messages or debug output if SafeString::setOutput(..) has not been called from your sketch
148 //
149 // SafeString.debug() is always available regardless of the SSTRING_DEBUG define setting
150 // but SafeString::setOutput() still needs to be called to set where the output should go.
151 
152 /* ----------------- creating SafeStrings ---------------------------------
153  See the example sketches SafeString_ConstructorAndDebugging.ino and SafeStringFromCharArray.ino
154  and SafeStringFromCharPtr.ino and SafeStringFromCharPtrWithSize.ion
155 
156  createSafeString(name, size) and createSafeString(name, size, "initialText")
157  are utility macros to create an SafeString of a given name and size and optionally, an initial value
158 
159  createSafeString(str, 40); or cSF(str, 40);
160  expands in the pre-processor to
161  char str_SAFEBUFFER[40+1];
162  SafeString str(sizeof(str_SAFEBUFFER),str_SAFEBUFFER,"","str");
163 
164  createSafeString(str, 40, "test"); or cSF(str, 40, "test");
165  expands in the pre-processor to
166  char str_SAFEBUFFER[40+1];
167  SafeString str(sizeof(str_SAFEBUFFER),str_SAFEBUFFER,"test","str");
168 
169  createSafeStringFromCharArray(name, char[]); or cSFA(name, char[]);
170  wraps an existing char[] in a SafeString of the given name
171  e.g.
172  char charBuffer[15];
173  createSafeStringFromCharArray(str,charBuffer); or cSFA(str,charBuffer);
174  expands in the pre-processor to
175  SafeString str(sizeof(charBuffer),charBuffer, charBuffer, "str", true);
176 
177  createSafeStringFromCharPtr(name, char*); or cSFP(name, char*);
178  wraps an existing char[] pointed to by char* in a SafeString of the given name
179  e.g.
180  char charBuffer[15];
181  char *bufPtr = charBuffer;
182  createSafeStringFromCharPtr(str,bufPtr); or cSFP(str,bufPtr);
183  expands in the pre-processor to
184  SafeString str((unsigned int)-1,charBuffer, charBuffer, "str", true);
185  and the capacity of the SafeString is set to strlen(charBuffer) and cannot be increased.
186 
187  createSafeStringFromCharPtrWithSize(name, char*, unsigned int); or cSFPS(name, char*, unsigned int);
188  wraps an existing char[] pointed to by char* in a SafeString of the given name and sets the capacity to the given size -1
189  e.g.
190  char charBuffer[15];
191  char *bufPtr = charBuffer;
192  createSafeStringFromCharPtrWithSize(str,bufPtr, 15); or cSFPS(str,bufPtr, 15);
193  expands in the pre-processor to
194  SafeString str(15,charBuffer, charBuffer, "str", true);
195  The capacity of the SafeString is set to 14.
196 
197 ****************************************************************************************/
198 /* **************************************************
199  If str is a SafeString then
200  str = .. works for signed/unsigned ints, char*, char, F(".."), SafeString float, double etc
201  str.concat(..) and string.prefix(..) also works for those
202  str.stoken(..) can be used to split a string in to tokens
203 
204  SafeStrings created via createSafeString(..) or cSF(..) are never invalid, even if called with invalid arguments.
205  SafeStrings created via createSafeStringFromCharArray(..) or cSFA(..) are valid as long at the underlying char[] is valid
206  Usually the only way the char[] can become invalid is if it exists in a struct that is allocated (via calloc/malloc)
207  and then freed while the SafeString wrapping it is still in use.
208  SafeStrings created via createSafeStringFromCharPtr(..) or cSFP(..) are valid if the char[] pointed to is validly terminated
209  SafeStrings created via createSafeStringFromCharWithSize(..) or cSFPS(..) are valid if the char[] and size specified is valid.
210  For both createSafeStringFromCharPtr() and createSafeStringFromCharWithSize()
211  the SafeStrings created remain valid as long as the underlying char[] is valid.
212  Usually the only way the char[] can become invalid is if it was allocated (via calloc/malloc)
213  and then freed while the SafeString wrapping it is still in use.
214 * ***************************************************/
215 
216 
217 #ifdef SSTRING_DEBUG
218 #define createSafeString(name, size,...) char name ## _SAFEBUFFER[(size)+1]; SafeString name(sizeof(name ## _SAFEBUFFER),name ## _SAFEBUFFER, "" __VA_ARGS__ , #name);
219 #define createSafeStringFromCharArray(name, charArray) SafeString name(sizeof(charArray),charArray, charArray, #name, true, false);
220 #define createSafeStringFromCharPtr(name, charPtr) SafeString name((unsigned int)-1,charPtr, charPtr, #name, true);
221 #define createSafeStringFromCharPtrWithSize(name, charPtr, arraySize) SafeString name((arraySize),charPtr, charPtr, #name, true);
222 #else
223 #define createSafeString(name, size,...) char name ## _SAFEBUFFER[(size)+1]; SafeString name(sizeof(name ## _SAFEBUFFER),name ## _SAFEBUFFER, "" __VA_ARGS__);
224 #define createSafeStringFromCharArray(name,charArray) SafeString name(sizeof(charArray),charArray, charArray, NULL, true, false);
225 #define createSafeStringFromCharPtr(name, charPtr) SafeString name((unsigned int)-1,charPtr, charPtr, NULL, true);
226 #define createSafeStringFromCharPtrWithSize(name, charPtr, arraySize) SafeString name((arraySize),charPtr, charPtr, NULL, true);
227 #endif
228 
229 // define typing shortcuts
230 #define cSF createSafeString
231 #define cSFA createSafeStringFromCharArray
232 #define cSFP createSafeStringFromCharPtr
233 #define cSFPS createSafeStringFromCharPtrWithSize
234 
235 
307 class SafeString : public Printable, public Print {
308 
309  public:
310 
321 // In all cases when maxlen != -1, it is the actual size of the array
322 // if _fromBuffer false (i.e. cSF(sfStr,20); ) then maxLen is the capacity+1 and the macro allocates an char[20+1], (_fromPtr ignored)
323 // if _fromBuffer true and _fromPtr false (i.e. cSFA(sfStr, strArray); ) then maxLen is the sizeof the strArray and the capacity is maxLen-1, _fromPtr is false
324 // if _fromBuffer true and _fromPtr true, then from char*, (i.e. cSFP(sfStr,strPtr) or cSFPS(sfStr,strPtr, maxLen) and maxLen is either -1 cSFP( ) the size of the char Array pointed cSFPS
325 // if maxLen == -1 then capacity == strlen(char*) i.e. cSFP( )
326 // else capacity == maxLen-1; i.e. cSFPS( )
327  explicit SafeString(unsigned int maxLen, char *buf, const char* cstr, const char* _name = NULL, bool _fromBuffer = false, bool _fromPtr = true);
328  // _fromBuffer true does extra checking before each method execution for SafeStrings created from existing char[] buffers
329  // _fromPtr is not checked unless _fromBuffer is true
330  // _fromPtr true allows for any array size, if false prevents passing char* by checking sizeof(charArray) != sizeof(char*)
331 
332  private: // to force compile errors if function definition of the SafeString argument is not a refernce, i.e. not SafeString&
333  SafeString(const SafeString& other ); // You must declare SafeStrings function arguments as a reference, SafeString&, e.g. void test(SafeString& strIn)
334  // NO other constructors, NO conversion constructors
335 
336  public:
337 
344  static void setOutput(Print& debugOut, bool verbose = true);
345  // static SafeString::DebugPrint Output; // a Print object controlled by setOutput() / turnOutputOff() is defined at the bottom
346 
347 
352  static void turnOutputOff(void); // call this to turn all debugging OFF, both error messages AND debug( ) method output
353 
354  // use this to control error messages verbose output
355 
360  static void setVerbose(bool verbose); // turn verbose error msgs on/off. setOutput( ) sets verbose to true
361 
362  // returns true if error detected, errors are detected even is setOutput has not been called
363  // each call to hasError() clears the errorFlag
364 
367  unsigned char hasError();
368 
369  // returns true if error detected in any SafeString object, errors are detected even is setOutput has not been called
370  // each call to errorDetected() clears the classErrorFlag
371 
374  static unsigned char errorDetected();
375 
376  // these methods print out info on this SafeString object, iff setOutput has been called
377  // setVerbose( ) does NOT effect these methods which have their own verbose argument
378  // Each of these debug( ) methods defaults to outputing the string contents. Set the optional verbose argument to false to suppress outputing string contents
379  // NOTE!! all these debug methods return a pointer to an empty string.
380  // This is so that if you add .debug() to Serial.println(str); i.e. Serial.println(str.debug()) will work as expected
381 
386  const char* debug(bool verbose = true);
387 
388 
394  const char* debug(const char* title, bool verbose = true);
395 
396 
402  const char* debug(const __FlashStringHelper *title, bool verbose = true);
403 
404 
410  const char* debug(SafeString &stitle, bool verbose = true);
411 
412 
419  virtual size_t write(uint8_t b);
420  // writes at most length chars to this SafeString,
421  // NOTE: write(cstr,length) will set hasError and optionally output errorMsg, if strlen(cstr) < length and nothing will be added to the SafeString
422 
423 
431  virtual size_t write(const uint8_t *buffer, size_t length);
432 
433 
438  size_t printTo(Print& p) const;
439 
440  // reserve returns 0 if _capacity < size
441 
446  unsigned char reserve(unsigned int size);
447 
448 
451  unsigned int length(void);
452 
453 
456  unsigned int capacity(void);
457 
458 
461  unsigned char isFull(void);
462 
463 
466  unsigned char isEmpty(void);
467 
468 
471  int availableForWrite(void);
472 
473 
479  SafeString & clear(void);
480 
481  public:
482  // support for print
483  size_t print(unsigned char, int = DEC);
484  size_t print(int, int = DEC);
485  size_t print(unsigned int, int = DEC);
486  size_t print(long, int = DEC);
487  size_t print(unsigned long, int = DEC);
488  size_t print(double, int = 2);
489  size_t print(const __FlashStringHelper *);
490  size_t print(const char*);
491  size_t print(char);
492  size_t print(SafeString &str);
493 
494  size_t println(unsigned char, int = DEC);
495  size_t println(int, int = DEC);
496  size_t println(unsigned int, int = DEC);
497  size_t println(long, int = DEC);
498  size_t println(unsigned long, int = DEC);
499  size_t println(double, int = 2);
500  size_t println(const __FlashStringHelper *);
501  size_t println(const char*);
502  size_t println(char);
503  size_t println(SafeString &str);
504  size_t println(void);
505 
506  // ********** special prints padding and formatting doubles (longs) **************
507  // print to SafeString a double (or long) with decs after the decimal point and padd to specified width
508  // width is a signed value, negative for left adjustment, +ve for right padding
509  // by default the + sign is not added, set forceSign argument to true to force the display of the + sign
510  //
511  // If the result exceeds abs(width), reduce the decs after the decmial point to fit into width
512  // If result with decs reduced to 0 is still > abs(width) raise an error and ,optionally, output an error msg
513  //
514  // Note decs is quietly limited in this method to < 7 digit after the decimal point.
515 
516 
526  size_t println(double d, int decs, int width, bool forceSign = false);
527 
528 
538  size_t print(double d, int decs, int width, bool forceSign = false);
539 
540 
541 
542  // Assignment operators **********************************
543  // Set the SafeString to a char version of the assigned value.
544  // For = (const char *) the contents are copied to the SafeString buffer
545  // if the value is null or invalid,
546  // or too large to be fit in the string's internal buffer
547  // the string will be left empty
548 
549 
557 
558 
565  SafeString & operator = (unsigned char num);
566 
567 
575 
576 
583  SafeString & operator = (unsigned int num);
584 
585 
592  SafeString & operator = (long num);
593 
594 
601  SafeString & operator = (unsigned long num);
602 
603 
610  SafeString & operator = (float num);
611 
612 
619  SafeString & operator = (double num);
620 
621 
629 
630 
637  SafeString & operator = (const char *cstr);
638 
639 
646  SafeString & operator = (const __FlashStringHelper *pstr); // handle F(" .. ") values
647 
648 
657  SafeString & prefix(const char *cstr);
658  SafeString & prefix(char c);
659  SafeString & prefix(unsigned char c);
660  SafeString & prefix(int num);
661  SafeString & prefix(unsigned int num);
662  SafeString & prefix(long num);
663  SafeString & prefix(unsigned long num);
664  SafeString & prefix(float num);
665  SafeString & prefix(double num);
666  SafeString & prefix(const __FlashStringHelper * str);
667  SafeString & prefix(const char *cstr, size_t length);
668  SafeString & prefix(const __FlashStringHelper * str, size_t length);
669 
670 
679  SafeString & concat(const char *cstr);
680  SafeString & concat(char c);
681  SafeString & concat(unsigned char c);
682  SafeString & concat(int num);
683  SafeString & concat(unsigned int num);
684  SafeString & concat(long num);
685  SafeString & concat(unsigned long num);
686  SafeString & concat(float num);
687  SafeString & concat(double num);
688  SafeString & concat(const __FlashStringHelper * str);
689  // ------------------------------------------------------
690  // no corresponding methods these three (3) in prefix, +=, -+
691 
692  SafeString & concat(const char *cstr, size_t length); // concat at most length chars from cstr
693  // NOTE: concat(cstr,length) will set hasError and optionally output errorMsg, if strlen(cstr) < length and nothing will be concatinated.
694 
695  SafeString & concat(const __FlashStringHelper * str, size_t length); // concat at most length chars
696 
697 
702  SafeString & newline(); // append newline \r\n same as concat("\r\n"); same a println()
703  // e.g. sfStr.concat("test").newline();
704 
705  /* prefix() operator -= ******************
706  Operator version of prefix( )
707  prefix -=
708  To cascade operators use ( )
709  e.g. (sfStr -= 'a') -= 5;
710  **/
711 
719  return prefix(rhs);
720  }
721  SafeString & operator -= (const char *cstr) {
722  return prefix(cstr);
723  }
725  return prefix(c);
726  }
727  SafeString & operator -= (unsigned char num) {
728  return prefix(num);
729  }
731  return prefix(num);
732  }
733  SafeString & operator -= (unsigned int num) {
734  return prefix(num);
735  }
736  SafeString & operator -= (long num) {
737  return prefix(num);
738  }
739  SafeString & operator -= (unsigned long num) {
740  return prefix(num);
741  }
742  SafeString & operator -= (float num) {
743  return prefix(num);
744  }
745  SafeString & operator -= (double num) {
746  return prefix(num);
747  }
748  SafeString & operator -= (const __FlashStringHelper *str) {
749  return prefix(str);
750  }
751 
752  /* concat() operator += ******************
753  Operator versions of concat( )
754  suffix/append +=
755  To cascade operators use ( )
756  e.g. (sfStr += 'a') += 5;
757  **/
758 
766  return concat(rhs);
767  }
768  SafeString & operator += (const char *cstr) {
769  return concat(cstr);
770  }
772  return concat(c);
773  }
774  SafeString & operator += (unsigned char num) {
775  return concat(num);
776  }
778  return concat(num);
779  }
780  SafeString & operator += (unsigned int num) {
781  return concat(num);
782  }
783  SafeString & operator += (long num) {
784  return concat(num);
785  }
786  SafeString & operator += (unsigned long num) {
787  return concat(num);
788  }
789  SafeString & operator += (float num) {
790  return concat(num);
791  }
792  SafeString & operator += (double num) {
793  return concat(num);
794  }
795  SafeString & operator += (const __FlashStringHelper *str) {
796  return concat(str);
797  }
798 
799  /* Comparision methods and operators ******************
800  comparisons only work with SafeStrings and "strings"
801  These methods used to be ... const {
802  but now with createSafeStringFromBuffer( ) the SafeString may be modified by cleanUp()
803  **/
804 
808 
811  int compareTo(const char *cstr) ;
812 
813  unsigned char equals(SafeString &s) ;
814  unsigned char equals(const char *cstr) ;
815  unsigned char equals(const char c) ;
816  unsigned char operator == (SafeString &rhs) {
817  return equals(rhs);
818  }
819  unsigned char operator == (const char *cstr) {
820  return equals(cstr);
821  }
822  unsigned char operator == (const char c) {
823  return equals(c);
824  }
825  unsigned char operator != (SafeString &rhs) {
826  return !equals(rhs);
827  }
828  unsigned char operator != (const char *cstr) {
829  return !equals(cstr);
830  }
831  unsigned char operator != (const char c) {
832  return !equals(c);
833  }
834  unsigned char operator < (SafeString &rhs) {
835  return compareTo(rhs) < 0;
836  }
837  unsigned char operator > (SafeString &rhs) {
838  return compareTo(rhs) > 0;
839  }
840  unsigned char operator <= (SafeString &rhs) {
841  return compareTo(rhs) <= 0;
842  }
843  unsigned char operator >= (SafeString &rhs) {
844  return compareTo(rhs) >= 0;
845  }
846  unsigned char operator < (const char* rhs) {
847  return compareTo(rhs) < 0;
848  }
849  unsigned char operator > (const char* rhs) {
850  return compareTo(rhs) > 0;
851  }
852  unsigned char operator <= (const char* rhs) {
853  return compareTo(rhs) <= 0;
854  }
855  unsigned char operator >= (const char* rhs) {
856  return compareTo(rhs) >= 0;
857  }
858  unsigned char equalsIgnoreCase(SafeString &s) ;
859  unsigned char equalsIgnoreCase(const char *str2) ;
860 
861  unsigned char equalsConstantTime(SafeString &s) ;
862 
863  /* startsWith methods *******************
864  The fromIndex is offset into this SafeString where check is to start
865  0 to length() and (unsigned int)(-1) are valid for fromIndex, if fromIndex == length() or -1 false is returned
866  if the argument is null or fromIndex > length(), an error is flagged and false returned
867  **/
874  unsigned char startsWith(const char c, unsigned int fromIndex = 0);
881  unsigned char startsWith( const char *str2, unsigned int fromIndex = 0) ;
888  unsigned char startsWith(SafeString &s2, unsigned int fromIndex = 0) ;
889 
896  unsigned char startsWithIgnoreCase(const char c, unsigned int fromIndex = 0);
903  unsigned char startsWithIgnoreCase( const char *str2, unsigned int fromIndex = 0) ;
910  unsigned char startsWithIgnoreCase( SafeString &s2, unsigned int fromIndex = 0) ;
911 
912  /* endsWith methods *******************/
918  unsigned char endsWith(const char c);
924  unsigned char endsWith(SafeString &suffix) ;
930  unsigned char endsWith(const char *suffix) ;
936  unsigned char endsWithCharFrom(SafeString &suffix) ;
942  unsigned char endsWithCharFrom(const char *suffix) ;
943 
944  /* character acccess methods *******************
945  NOTE: There is no access to modify the underlying char buffer directly
946  For these methods 0 to length()-1 is valid for index
947  index greater than length() -1 will return 0 and set the error flag and will print errors if debug enabled
948  **/
956  char charAt(unsigned int index) ; // if index >= length() returns 0 and prints a error msg
964  char operator [] (unsigned int index) ; // if index >= length() returns 0 and prints a error msg
965 
966  // setting a char in the SafeString
967  // str[..] = c; is not supported because it allows direct access to modify the underlying char buffer
974  void setCharAt(unsigned int index, char c); //if index >= length() the error flag is set
975  // calls to setCharAt(length(), ..) and setCharAt(.. , '\0') are ignored and error flag is set
976 
977  // returning the underlying buffer
978  // returned as a const and should not be changesdor recast to a non-const
983  const char* c_str();
984 
985 
986  /* search methods *******************
987  Arrays are indexed by a unsigned int variable
988  See the SafeStringIndexOf.ino example sketch
989  All indexOf methods return -1 if not found
990  **********************************************/
991  // The fromIndex is offset into this SafeString where to start searching (inclusive)
992  // 0 to length() and -1 is valid for fromIndex
993  // if fromIndex > length(), than the error flag is set and -1 returned and prints an error if debug enabled
994  // if fromIndex == (unsigned int)(-1) -1 is returned without error.
995  /*
996  returns the index
997  */
998  //int indexOf( char ch ) ;
1006  int indexOf( char ch, unsigned int fromIndex = 0) ;
1007  //int indexOf( SafeString & str ) ;
1008  //int indexOf( const char* str ) ;
1015  int indexOf(const char* str , unsigned int fromIndex = 0) ;
1022  int indexOf( SafeString & str, unsigned int fromIndex = 0 ) ;
1023 
1029  int lastIndexOf( char ch ) ;
1030 
1037  int lastIndexOf( char ch, unsigned int fromIndex) ;
1038 
1044  int lastIndexOf( SafeString & str ) ;
1051  int lastIndexOf( SafeString & str, unsigned int fromIndex) ;
1052 
1058  int lastIndexOf( const char *cstr ) ;
1059 
1066  int lastIndexOf(const char* cstr, unsigned int fromIndex);
1067 
1068  // first index of the chars listed in chars string
1069  // loop through chars and look for index of each and return the min index or -1 if none found
1070  //int indexOfCharFrom(SafeString & str);
1071  //int indexOfCharFrom(const char* chars);
1072  // start searching from fromIndex
1079  int indexOfCharFrom(SafeString & str, unsigned int fromIndex = 0);
1080 
1087  int indexOfCharFrom(const char* chars, unsigned int fromIndex = 0);
1088 
1089 
1090  /* *** substring methods ************/
1091  // substring is from beginIdx to end of string
1092  // The result substring is ALWAYS first cleared by this method so it will be empty on errors
1093  // if beginIdx = length(), an empty result will be returned without error
1094  // if beginIdx > length(), an empty result will be returned with error flag set on both this SafeString and the result SafeString
1095  // beginIdx == (unsigned int)(-1) returns an empty result without an error
1096  // You can take substring of yourself e.g. str.substring(str,3);
1097  // if result does not have the capacity to hold the substring, hasError() is set on both this SafeString and the result SafeString
1098 
1114  SafeString & substring(SafeString & result, unsigned int beginIdx);
1115 
1116  // The result substring is ALWAYS first cleared by this method so it will be empty on errors
1117  // if beginIdx = length(), an empty result will be returned without error
1118  // if beginIdx > length(), an empty result will be returned with error flag set on both this SafeString and the result SafeString
1119  // 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
1120  // if endIdx > length(), endIdx is set to length(); and the error flag is set on both this SafeString and the result SafeString
1121  // endIdx == (unsigned int)(-1) is treated as endIdx == length() returns a result without an error
1122  // substring is from beginIdx to endIdx-1, endIdx is exclusive
1123  // You can take substring of yourself e.g. str.substring(str,3,6);
1124  // if result does not have the capacity to hold the substring, and empty result is returned and hasError() is set on both this SafeString and the result SafeString
1143  SafeString & substring(SafeString & result, unsigned int beginIdx, unsigned int endIdx);
1144 
1145  /* *** SafeString modification methods ************/
1146 
1147  /* *** replace ************/
1153  void replace(char findChar, char replaceChar);
1154 
1160  void replace(const char findChar, const char *replaceStr);
1161 
1167  void replace(const char findChar, SafeString& sfReplace);
1168 
1174  void replace(const char* findStr, const char *replaceStr);
1175 
1181  void replace(SafeString & sfFind, SafeString & sfReplace);
1182 
1183  /* *** remove ************/
1184  // remove from index to end of SafeString
1185  // 0 to length() and (unsigned int)(-1) are valid for index,
1186  // -1 => length() for processing and just returns without error
1193  void removeFrom(unsigned int startIndex);
1194 
1195  // remove from 0 to startIdx (excluding startIdx)
1196  // 0 to length() and (unsigned int)(-1) are valid for index,
1197  // -1 => length() for processing
1204  void removeBefore(unsigned int startIndex);
1205 
1206  // remove from index to end of SafeString
1207  // 0 to length() and (unsigned int)(-1) are valid for index,
1208  // -1 => length() for processing and just returns without error
1215  void remove(unsigned int index);
1216 
1217  // remove count chars starting from index
1218  // 0 to length() and unsigned int(-1) are valid for index
1219  // -1 just returns without error
1220  // 0 to (length()- index) is valid for count, larger values set the error flag and remove from idx to end of string
1229  void remove(unsigned int index, unsigned int count);
1230 
1231  // remove the last 'count' chars
1232  // 0 to length() is valid for count,
1233  // count >= length() clears the SafeString
1234  // count > length() set the error flag
1242  void removeLast(unsigned int count);
1243 
1244  // keep last 'count' number of chars remove the rest
1245  // 0 to length() is valid for count, passing in count == 0 clears the SafeString
1246  // count > length() sets error flag and returns SafeString unchanged
1254  void keepLast(unsigned int count);
1255 
1256 
1257  /* *** change case ************/
1261  void toLowerCase(void);
1265  void toUpperCase(void);
1266 
1267  /* *** remove white space from front and back of SafeString ************/
1268  // the method isspace( ) is used to. For the 'C' local the following are trimmed
1269  // ' ' (0x20) space (SPC)
1270  // '\t' (0x09) horizontal tab (TAB)
1271  // '\n' (0x0a) newline (LF)
1272  // '\v' (0x0b) vertical tab (VT)
1273  // '\f' (0x0c) feed (FF)
1274  // '\r' (0x0d) carriage return (CR)
1286  void trim(void); // trims front and back
1287 
1288  // processBackspaces recursively remove backspaces, '\b' and the preceeding char
1289  // use for processing inputs from terminal (Telent) connections
1295  void processBackspaces(void);
1296 
1297  /* *** numgber parsing/conversion ************/
1298  // convert numbers
1299  // If the SafeString is a valid number update the argument with the result
1300  // else leave the argument unchanged
1301  // SafeString conversions are stricter than the Arduino String version
1302  // trailing chars can only be white space
1309  unsigned char toInt(int & i) ;
1316  unsigned char toLong(long & l) ;
1317 
1324  unsigned char binToLong(long & l) ;
1331  unsigned char octToLong(long & l) ;
1338  unsigned char hexToLong(long & l) ;
1345  unsigned char toUnsignedLong(unsigned long & l) ;
1352  unsigned char binToUnsignedLong(unsigned long & l) ;
1359  unsigned char octToUnsignedLong(unsigned long & l) ;
1366  unsigned char hexToUnsignedLong(unsigned long & l) ;
1373  unsigned char toFloat(float & f) ;
1380  unsigned char toDouble(double & d) ;
1381 
1382  // float toFloat(); possible alternative
1383 
1384  /* Tokenizeing methods, stoken(), nextToken()/firstToken() ************************/
1385  /* Differences between stoken() and nextToken
1386  stoken() leaves the SafeString unchanged, nextToken() removes the token (and leading delimiters) from the SafeString giving space to add more input
1387  In stoken() the end of the SafeString is always treated as a delimiter, i.e. the last token is returned even if it is not followed by one of the delimiters
1388  In nextToken() the end of the SafeString is a delimiter by default, but setting returnLastNonDelimitedToken = false will leave last token that is not terminated in the SafeString
1389  Setting returnLastNonDelimitedToken = false this allows partial tokens to be read from a Stream and kept until the full token and delimiter is read
1390  */
1391  /*
1392  stoken -- The SafeString itself is not changed
1393  stoken breaks into the SafeString into tokens using chars in delimiters string and the end of the SafeString as delimiters.
1394  Any leading delimiters are first stepped over and then the delimited token is return in the token argument (less the delimiter).
1395  The token argument is always cleared at the start of the stoken().
1396  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
1397 
1398  params
1399  token - the SafeString to return the token in, it is cleared if no delimited token found or if there are errors
1400  The found delimited token (less the delimiter) is returned in the token SafeString argument if there is capacity.
1401  The token's capacity should be >= this SafeString's capacity incase the entire SafeString needs to be returned.
1402  If the token's capacity is < the next token, then token is returned empty and an error messages printed if debug is enabled.
1403  In this case the return (nextIndex) is still updated.
1404  fromIndex -- where to start the search from 0 to length() and -1 is valid for fromIndex, -1 => length() for processing
1405  delimiters - the characters that any one of which can delimit a token. The end of the SafeString is always a delimiter.
1406  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
1407  useAsDelimiters - default true, if false then token consists only of chars in the delimiters and any other char terminates the token
1408 
1409  return -- nextIndex, the next index in this SafeString after the end of the token just found, -1 if this is the last token
1410  Use this as the fromIndex for the next call
1411  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
1412  If the token's capacity is < the next token, the token returned is empty and an error messages printed if debug is enabled.
1413  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
1414  while being consistent with the SafeString's all or nothing insertion rule
1415 
1416  Input argument errors return -1 and an empty token and hasError() is set on both this SafeString and the token SafeString.
1417  **/
1418 
1445  int stoken(SafeString & token, unsigned int fromIndex, const char delimiter, bool returnEmptyFields = false, bool useAsDelimiters = true);
1446 
1473  int stoken(SafeString & token, unsigned int fromIndex, const char* delimiters, bool returnEmptyFields = false, bool useAsDelimiters = true);
1474 
1501  int stoken(SafeString & token, unsigned int fromIndex, SafeString & delimiters, bool returnEmptyFields = false, bool useAsDelimiters = true);
1502 
1528  inline unsigned char firstToken(SafeString & token, char delimiter, bool returnLastNonDelimitedToken = true) {
1529  return nextToken(token,delimiter,true,returnLastNonDelimitedToken,true);
1530  }
1531 
1562  unsigned char nextToken(SafeString & token, char delimiter, bool returnEmptyFields = false, bool returnLastNonDelimitedToken = true, bool firstToken = false);
1563 
1589  inline unsigned char firstToken(SafeString & token, SafeString delimiters, bool returnLastNonDelimitedToken = true) {
1590  return nextToken(token,delimiters,true,returnLastNonDelimitedToken,true);
1591  }
1592 
1623  unsigned char nextToken(SafeString & token, SafeString & delimiters, bool returnEmptyFields = false, bool returnLastNonDelimitedToken = true, bool firstToken = false);
1624 
1650  inline unsigned char firstToken(SafeString & token, const char* delimiters, bool returnLastNonDelimitedToken = true) {
1651  return nextToken(token,delimiters,true,returnLastNonDelimitedToken,true);
1652  }
1653 
1684  unsigned char nextToken(SafeString & token, const char* delimiters, bool returnEmptyFields = false, bool returnLastNonDelimitedToken = true, bool firstToken = false);
1685 
1686 
1687  /* *** ReadFrom from SafeString, writeTo SafeString ************************/
1703  unsigned int readFrom(SafeString & sfInput, unsigned int startIdx = 0);
1704 
1718  unsigned int readFrom(const char* strPtr, unsigned int maxCharsToRead = ((unsigned int)-1));
1719 
1732  unsigned int writeTo(SafeString & output, unsigned int startIdx = 0);
1733 
1734  /* *** NON-blocking reads from Stream ************************/
1735 
1745  unsigned char read(Stream & input);
1746 
1758  unsigned char readUntil(Stream & input, const char delimiter);
1770  unsigned char readUntil(Stream & input, const char* delimiters);
1782  unsigned char readUntil(Stream & input, SafeString & delimiters);
1783 
1811  unsigned char readUntilToken(Stream & input, SafeString & token, const char delimiter, bool & skipToDelimiter, uint8_t echoInput = false, unsigned long timeout_ms = 0);
1812 
1840  unsigned char readUntilToken(Stream & input, SafeString & token, const char* delimiters, bool & skipToDelimiter, uint8_t echoInput = false, unsigned long timeout_ms = 0);
1841 
1869  unsigned char readUntilToken(Stream & input, SafeString & token, SafeString & delimiters, bool & skipToDelimiter, uint8_t echoInput = false, unsigned long timeout_ms = 0);
1870 
1877  size_t getLastReadCount();
1878 
1879  /* *** END OF PUBLIC METHODS ************/
1880 
1881  protected:
1882  static Print* debugPtr;
1883  static bool fullDebug;
1884  char *buffer; // the actual char array
1885  size_t _capacity; // the array length minus one (for the '\0')
1886  size_t len; // the SafeString length (not counting the '\0')
1887 
1888  class noDebugPrint : public Print {
1889  public:
1890  inline size_t write(uint8_t b) {
1891  (void)(b);
1892  return 0;
1893  }
1894  inline size_t write(const uint8_t *buffer, size_t length) {
1895  (void)(buffer);
1896  (void)(length);
1897  return 0;
1898  };
1899  void flush() { }
1900  };
1901 
1903 
1904  static Print* currentOutput;// = &emptyPrint;
1905 
1906  class DebugPrint : public Print {
1907  public:
1908  size_t write(uint8_t b) {
1909  return currentOutput->write(b);
1910  }
1911  size_t write(const uint8_t *buffer, size_t length) {
1912  return currentOutput->write(buffer, length);
1913  };
1914  void flush() {
1915 #if defined(ESP_PLATFORM) || defined(ARDUINO_ARCH_NRF52) || defined(ARDUINO_ARCH_NRF5) || defined(ARDUINO_SAM_DUE) || defined(ARDUINO_ARCH_STM32F1) || defined(ARDUINO_ARCH_STM32F4) || defined(ARDUINO_NRF52832_FEATHER) || defined(MEGATINYCORE_MAJOR)
1916  // ESP32 has no flush in Print!! but ESP8266 has
1917 #else
1918  currentOutput->flush();
1919 #endif
1920  }
1921  };
1922 
1923  public:
1924  static SafeString::DebugPrint Output; // a Print object controlled by setOutput() / turnOutputOff()
1925 
1926  protected:
1927  SafeString & concatln(const __FlashStringHelper * pstr);
1929  SafeString & concatln(const char *cstr, size_t length);
1930  void outputName() const ;
1931  SafeString & concatInternal(const char *cstr, size_t length, bool assignOp = false); // concat at most length chars from cstr
1932  SafeString & concatInternal(const __FlashStringHelper * str, size_t length, bool assignOp = false); // concat at most length chars
1933 
1934  SafeString & concatInternal(const char *cstr, bool assignOp = false);
1935  SafeString & concatInternal(char c, bool assignOp = false);
1936  SafeString & concatInternal(const __FlashStringHelper * str, bool assignOp = false);
1937  size_t printInternal(long, int = DEC, bool assignOp = false);
1938  size_t printInternal(unsigned long, int = DEC, bool assignOp = false);
1939  size_t printInternal(double, int = 2, bool assignOp = false);
1940  void setError();
1941  void printlnErr()const ;
1942  void debugInternalMsg(bool _fullDebug) const ;
1943  size_t limitedStrLen(const char* p, size_t limit);
1944  size_t printInt(double d, int decs, int width, bool forceSign, bool addNL);
1945 
1946  private:
1947  bool readUntilTokenInternal(Stream & input, SafeString & token, const char* delimitersIn, char delimiterIn, bool & skipToDelimiter, uint8_t echoInput, unsigned long timeout_ms);
1948  bool readUntilInternal(Stream & input, const char* delimitersIn, char delimiterIn);
1949  bool nextTokenInternal(SafeString & token, const char* delimitersIn, char delimiterIn, bool returnEmptyFields, bool returnLastNonDelimitedToken);
1950  int stokenInternal(SafeString &token, unsigned int fromIndex, const char* delimitersIn, char delimiterIn, bool returnEmptyFields, bool useAsDelimiters);
1951  bool fromBuffer; // true if createSafeStringFromBuffer created this object
1952  bool errorFlag; // set to true if error detected, cleared on each call to hasError()
1953  static bool classErrorFlag; // set to true if any error detected in any SafeString, cleared on each call to SafeString::errorDetected()
1954  void cleanUp(); // reterminates buffer at capacity and resets len to current strlen
1955  const char *name;
1956  unsigned long timeoutStart_ms;
1957  bool timeoutRunning;
1958  size_t noCharsRead; // number of char read on last call to readUntilToken
1959  static char nullBufferSafeStringBuffer[1];
1960  static char emptyDebugRtnBuffer[1];
1961  void debugInternal(bool _fullDebug) const ;
1962  void debugInternalResultMsg(bool _fullDebug) const ;
1963  void concatErr()const ;
1964  void concatAssignError() const;
1965  void prefixErr()const ;
1966  void capError(const __FlashStringHelper * methodName, size_t neededCap, const char* cstr, const __FlashStringHelper *pstr = NULL, char c = '\0', size_t length = 0)const ;
1967  void assignError(size_t neededCap, const char* cstr, const __FlashStringHelper *pstr = NULL, char c = '\0', bool numberFlag = false) const;
1968  void errorMethod(const __FlashStringHelper * methodName) const ;
1969  void warningMethod(const __FlashStringHelper * methodName) const ;
1970  void assignErrorMethod() const ;
1971  void outputFromIndexIfFullDebug(unsigned int fromIndex) const ;
1972 };
1973 
1974 #include "SafeStringNameSpaceEnd.h"
1975 
1976 #endif // __cplusplus
1977 #endif // SafeString_class_h
size_t write(const uint8_t *buffer, size_t length)
Definition: SafeString.h:1911
size_t write(uint8_t b)
Definition: SafeString.h:1908
size_t write(uint8_t b)
Definition: SafeString.h:1890
size_t write(const uint8_t *buffer, size_t length)
Definition: SafeString.h:1894
To create SafeStrings use one of the four (4) macros createSafeString or cSF, createSafeStringFromCha...
Definition: SafeString.h:307
int stoken(SafeString &token, unsigned int fromIndex, const char delimiter, bool returnEmptyFields=false, bool useAsDelimiters=true)
break into the SafeString into tokens using the char delimiter, the end of the SafeString is always a...
size_t getLastReadCount()
returns the number of chars read on previous calls to read, readUntil or readUntilToken (includes '\0...
size_t print(const __FlashStringHelper *)
int indexOfCharFrom(const char *chars, unsigned int fromIndex=0)
returns the first index of any char from the argument
static void setOutput(Print &debugOut, bool verbose=true)
Turns on Error msgs and debug( ) output for all SafeStrings.
SafeString & operator=(char c)
Clears this SafeString and concatinates a single char.
unsigned char endsWith(SafeString &suffix)
returns non-zero of this SafeString ends with the argument
SafeString & concatln(const __FlashStringHelper *pstr)
const char * debug(SafeString &stitle, bool verbose=true)
Output the details about the this SafeString to the output specified by setOutput().
static unsigned char errorDetected()
Returns non-zero if any SafeString has detected and error, each call clears the internal global stati...
size_t print(unsigned char, int=DEC)
SafeString & prefix(const __FlashStringHelper *str, size_t length)
unsigned char equalsConstantTime(SafeString &s)
int availableForWrite(void)
Returns the number chars that can be added to this SafeString before it is full.
SafeString & prefix(long num)
SafeString & prefix(unsigned long num)
SafeString & prefix(int num)
unsigned char hasError()
Returns non-zero if any error detected for this SafeString, each call clears the internal flag.
void printlnErr() const
SafeString(unsigned int maxLen, char *buf, const char *cstr, const char *_name=NULL, bool _fromBuffer=false, bool _fromPtr=true)
SafeString Constructor called from the four (4) macros createSafeString or cSF, createSafeStringFromC...
void debugInternalMsg(bool _fullDebug) const
unsigned char toUnsignedLong(unsigned long &l)
convert the SafeString to an unsigned long.
unsigned char isFull(void)
Returns non-zero if the SafeString is full.
unsigned char readUntil(Stream &input, const char *delimiters)
reads chars into this SafeString until either it is full OR a delimiter is read OR there are no more ...
SafeString & prefix(double num)
unsigned char read(Stream &input)
reads from the Stream (if chars available) into the SafeString.
static void turnOutputOff(void)
Turns off all debugging messages, both error messages AND debug() method output.
SafeString & concatInternal(const char *cstr, size_t length, bool assignOp=false)
static SafeString::DebugPrint Output
Definition: SafeString.h:1924
void replace(const char *findStr, const char *replaceStr)
replace the findStr string with the replace string
void remove(unsigned int index)
remove all chars from index to the end of the SafeString (inclusive)
unsigned char toDouble(double &d)
convert the SafeString to a float assuming the SafeString in the decimal format (not scientific)
unsigned char equalsIgnoreCase(SafeString &s)
int lastIndexOf(char ch, unsigned int fromIndex)
returns the last index of the char, searching backwards from fromIndex (inclusive).
SafeString & operator-=(SafeString &rhs)
-= operator prefixes the SafeString.
Definition: SafeString.h:718
unsigned char reserve(unsigned int size)
Checks there is enough free space in this SafeString for the current operation.
unsigned char operator>=(SafeString &rhs)
Definition: SafeString.h:843
size_t println(SafeString &str)
static SafeString::noDebugPrint emptyPrint
Definition: SafeString.h:1902
unsigned int readFrom(SafeString &sfInput, unsigned int startIdx=0)
reads from the SafeString argument, starting at startIdx, into this SafeString.
SafeString & prefix(const __FlashStringHelper *str)
unsigned char readUntil(Stream &input, const char delimiter)
reads chars into this SafeString until either it is full OR a delimiter is read OR there are no more ...
size_t println(char)
SafeString & concat(float num)
SafeString & concatln(const char *cstr, size_t length)
unsigned char startsWith(const char *str2, unsigned int fromIndex=0)
returns non-zero of this SafeString starts this argument looking from fromIndex onwards.
unsigned char binToUnsignedLong(unsigned long &l)
convert the SafeString to an unsigned long assuming the SafeString in binary (0/1).
unsigned char startsWithIgnoreCase(const char *str2, unsigned int fromIndex=0)
returns non-zero of this SafeString starts this argument, ignoring case, looking from fromIndex onwar...
size_t len
Definition: SafeString.h:1886
size_t println(long, int=DEC)
static Print * currentOutput
Definition: SafeString.h:1904
const char * debug(bool verbose=true)
Output the details about the this SafeString to the output specified by setOutput().
size_t println(unsigned int, int=DEC)
unsigned char hexToLong(long &l)
convert the SafeString to a long assuming the SafeString in HEX (0 to f or 0 to F).
unsigned char operator!=(SafeString &rhs)
Definition: SafeString.h:825
SafeString & concat(const char *cstr)
unsigned int readFrom(const char *strPtr, unsigned int maxCharsToRead=((unsigned int) -1))
reads from the const char* argument, starting at 0 and read up to maxCharToRead, into this SafeString...
void replace(char findChar, char replaceChar)
replace the findChar with the replaceChar
const char * c_str()
returns a const char* to the underlying char[ ] in this SafeString.
unsigned char octToLong(long &l)
convert the SafeString to a long assuming the SafeString in octal (0 to 7).
size_t println(void)
unsigned char operator<=(SafeString &rhs)
Definition: SafeString.h:840
size_t println(double, int=2)
char charAt(unsigned int index)
returns the char at that location in this SafeString.
size_t printInt(double d, int decs, int width, bool forceSign, bool addNL)
unsigned char nextToken(SafeString &token, SafeString &delimiters, bool returnEmptyFields=false, bool returnLastNonDelimitedToken=true, bool firstToken=false)
returns true if a delimited token is found, removes the first delimited token from this SafeString an...
size_t println(int, int=DEC)
unsigned char hexToUnsignedLong(unsigned long &l)
convert the SafeString to an unsigned long assuming the SafeString in HEX (0 to f or 0 to F).
SafeString & concat(unsigned int num)
int stoken(SafeString &token, unsigned int fromIndex, const char *delimiters, bool returnEmptyFields=false, bool useAsDelimiters=true)
break into the SafeString into tokens using the delimiters, the end of the SafeString is always a del...
void setError()
int stoken(SafeString &token, unsigned int fromIndex, SafeString &delimiters, bool returnEmptyFields=false, bool useAsDelimiters=true)
break into the SafeString into tokens using the delimiters, the end of the SafeString is always a del...
int compareTo(const char *cstr)
returns -1 if this SafeString is < cstr, 0 if this SafeString == cstr and +1 if this SafeString > cst
size_t println(unsigned long, int=DEC)
size_t print(long, int=DEC)
void processBackspaces(void)
recursively remove backspaces, '\b' and the preceeding char.
unsigned char equals(const char *cstr)
int lastIndexOf(const char *cstr, unsigned int fromIndex)
returns the last index of the char, searching backwards from fromIndex (inclusive).
size_t limitedStrLen(const char *p, size_t limit)
size_t print(SafeString &str)
unsigned char readUntilToken(Stream &input, SafeString &token, const char delimiter, bool &skipToDelimiter, uint8_t echoInput=false, unsigned long timeout_ms=0)
returns true if a delimited token is found, else false ONLY delimited tokens of length less than thi...
size_t print(unsigned long, int=DEC)
unsigned char firstToken(SafeString &token, char delimiter, bool returnLastNonDelimitedToken=true)
returns true if a delimited token is found, removes the first delimited token from this SafeString an...
Definition: SafeString.h:1528
void removeFrom(unsigned int startIndex)
remove all chars from startIndex to the end of the SafeString (inclusive)
size_t println(const char *)
unsigned char operator>(SafeString &rhs)
Definition: SafeString.h:837
int indexOfCharFrom(SafeString &str, unsigned int fromIndex=0)
returns the first index of any char from the argument.
unsigned int writeTo(SafeString &output, unsigned int startIdx=0)
writes from this SafeString, starting from startIdx, into the SafeString output arguement.
const char * debug(const __FlashStringHelper *title, bool verbose=true)
Output the details about the this SafeString to the output specified by setOutput().
int indexOf(const char *str, unsigned int fromIndex=0)
returns the index of the string, searching from fromIndex.
virtual size_t write(const uint8_t *buffer, size_t length)
Write (concatinate) bytes to this SafeString, from Print class.
unsigned char endsWithCharFrom(const char *suffix)
returns non-zero of this SafeString ends any one of the chars in the argument
const char * debug(const char *title, bool verbose=true)
Output the details about the this SafeString to the output specified by setOutput().
void toLowerCase(void)
convert this SafeString to all lower case
void outputName() const
unsigned char readUntil(Stream &input, SafeString &delimiters)
reads chars into this SafeString until either it is full OR a delimiter is read OR there are no more ...
unsigned char readUntilToken(Stream &input, SafeString &token, const char *delimiters, bool &skipToDelimiter, uint8_t echoInput=false, unsigned long timeout_ms=0)
returns true if a delimited token is found, else false ONLY delimited tokens of length less than thi...
unsigned char toInt(int &i)
convert the SafeString to an int.
void replace(const char findChar, SafeString &sfReplace)
replace the findChar with the sfReplace SafeString contents
unsigned char equals(const char c)
SafeString & concat(char c)
unsigned char nextToken(SafeString &token, const char *delimiters, bool returnEmptyFields=false, bool returnLastNonDelimitedToken=true, bool firstToken=false)
returns true if a delimited token is found, removes the first delimited token from this SafeString an...
size_t print(char)
SafeString & clear(void)
Empties this SafeString.
void toUpperCase(void)
convert this SafeString to all lower case
unsigned char readUntilToken(Stream &input, SafeString &token, SafeString &delimiters, bool &skipToDelimiter, uint8_t echoInput=false, unsigned long timeout_ms=0)
returns true if a delimited token is found, else false ONLY delimited tokens of length less than thi...
SafeString & operator+=(SafeString &rhs)
+= operator concatinate to the SafeString.
Definition: SafeString.h:765
unsigned char binToLong(long &l)
convert the SafeString to a long assuming the SafeString in binary (0/1).
void replace(const char findChar, const char *replaceStr)
replace the findChar with the replace string
unsigned char startsWithIgnoreCase(SafeString &s2, unsigned int fromIndex=0)
returns non-zero of this SafeString starts this argument, ignoring case, looking from fromIndex onwar...
unsigned char firstToken(SafeString &token, SafeString delimiters, bool returnLastNonDelimitedToken=true)
returns true if a delimited token is found, removes the first delimited token from this SafeString an...
Definition: SafeString.h:1589
void setCharAt(unsigned int index, char c)
sets the char at that location in this SafeString.
int lastIndexOf(SafeString &str, unsigned int fromIndex)
returns the last index of the char, searching backwards from fromIndex (inclusive).
virtual size_t write(uint8_t b)
Write (concatinate) a byte to this SafeString, from Print class.
size_t printInternal(unsigned long, int=DEC, bool assignOp=false)
void removeLast(unsigned int count)
remove the last count chars
SafeString & substring(SafeString &result, unsigned int beginIdx)
The result is the substring from the beginIdx to the end of the SafeString.
SafeString & concat(SafeString &str)
concat methods add to the end of the current SafeString.
SafeString & substring(SafeString &result, unsigned int beginIdx, unsigned int endIdx)
The result is the substring from the beginIdx to endIdx (exclusive), that is the endIdx is NOT includ...
SafeString & prefix(char c)
unsigned char nextToken(SafeString &token, char delimiter, bool returnEmptyFields=false, bool returnLastNonDelimitedToken=true, bool firstToken=false)
returns true if a delimited token is found, removes the first delimited token from this SafeString an...
SafeString & newline()
Adds \r\n to this SafeString.
char * buffer
Definition: SafeString.h:1884
SafeString & prefix(unsigned char c)
size_t print(double, int=2)
unsigned char isEmpty(void)
Returns non-zero if the SafeString is empty.
size_t printInternal(double, int=2, bool assignOp=false)
int lastIndexOf(char ch)
returns the last index of the char, searching backwards from fromIndex (inclusive).
size_t println(const __FlashStringHelper *)
size_t print(unsigned int, int=DEC)
SafeString & concat(const __FlashStringHelper *str, size_t length)
unsigned char firstToken(SafeString &token, const char *delimiters, bool returnLastNonDelimitedToken=true)
returns true if a delimited token is found, removes the first delimited token from this SafeString an...
Definition: SafeString.h:1650
size_t print(double d, int decs, int width, bool forceSign=false)
Prints a double (or long/int) to this SafeString padded with spaces (left or right) and limited to th...
SafeString & concatln(char c)
SafeString & prefix(const char *cstr, size_t length)
SafeString & concat(int num)
SafeString & concat(const char *cstr, size_t length)
SafeString & concatInternal(const __FlashStringHelper *str, bool assignOp=false)
void keepLast(unsigned int count)
keep the last count chars and remove the rest
static void setVerbose(bool verbose)
Controls size of error messages, setOutput sets verbose to true.
void remove(unsigned int index, unsigned int count)
remove count chars starting from index
unsigned char endsWithCharFrom(SafeString &suffix)
returns non-zero of this SafeString ends any one of the chars in the argument
SafeString & prefix(unsigned int num)
SafeString & concat(const __FlashStringHelper *str)
size_t println(unsigned char, int=DEC)
int lastIndexOf(const char *cstr)
returns the last index of the arguement, searching backwards from fromIndex (inclusive).
int lastIndexOf(SafeString &str)
returns the last index of the arguement, searching backwards from fromIndex (inclusive).
unsigned int capacity(void)
The maximum number of characters this SafeString can hold, excluding the terminating '\0'.
unsigned char endsWith(const char *suffix)
returns non-zero of this SafeString ends with the argument
size_t printTo(Print &p) const
Implements the Printable interface.
static Print * debugPtr
Definition: SafeString.h:1882
SafeString & prefix(float num)
unsigned char equalsIgnoreCase(const char *str2)
int compareTo(SafeString &s)
returns -1 if this SafeString is < s, 0 if this SafeString == s and +1 if this SafeString > s
size_t _capacity
Definition: SafeString.h:1885
unsigned char toLong(long &l)
convert the SafeString to a long.
SafeString & prefix(SafeString &s)
prefix methods add to the front of the current SafeString.
size_t printInternal(long, int=DEC, bool assignOp=false)
unsigned char equals(SafeString &s)
SafeString & concat(long num)
void trim(void)
remove all white space from the front and back of this SafeString.
unsigned char startsWithIgnoreCase(const char c, unsigned int fromIndex=0)
returns non-zero of this SafeString starts this argument, ignoring case, looking from fromIndex onwar...
unsigned char octToUnsignedLong(unsigned long &l)
convert the SafeString to an unsigned long assuming the SafeString in octal (0 to 7).
size_t print(const char *)
SafeString & concat(unsigned long num)
SafeString & concat(unsigned char c)
unsigned char startsWith(SafeString &s2, unsigned int fromIndex=0)
returns non-zero of this SafeString starts this argument looking from fromIndex onwards.
size_t print(int, int=DEC)
SafeString & concat(double num)
unsigned char endsWith(const char c)
returns non-zero of this SafeString ends with the argument
SafeString & prefix(const char *cstr)
void replace(SafeString &sfFind, SafeString &sfReplace)
replace the occurances of the sfFind string, with the sfReplace SafeString contents
unsigned char operator==(SafeString &rhs)
Definition: SafeString.h:816
SafeString & concatInternal(const char *cstr, bool assignOp=false)
size_t println(double d, int decs, int width, bool forceSign=false)
Prints a double (or long/int) to this SafeString padded with spaces (left or right) and limited to th...
unsigned char toFloat(float &f)
convert the SafeString to a float assuming the SafeString in the decimal format (not scientific)
unsigned char operator<(SafeString &rhs)
Definition: SafeString.h:834
static bool fullDebug
Definition: SafeString.h:1883
SafeString & concatInternal(const __FlashStringHelper *str, size_t length, bool assignOp=false)
unsigned int length(void)
Number of characters current in the SafeString, excluding the terminating '\0'.
unsigned char startsWith(const char c, unsigned int fromIndex=0)
returns non-zero of this SafeString starts this argument looking from fromIndex onwards.
SafeString & concatInternal(char c, bool assignOp=false)
void removeBefore(unsigned int startIndex)
remove all chars from 0 to startIndex (exclusive), that is the char at startIndex is NOT removed
char operator[](unsigned int index)
returns the char at that location in this SafeString.
int indexOf(SafeString &str, unsigned int fromIndex=0)
returns the index of the SafeString, searching from fromIndex.
int indexOf(char ch, unsigned int fromIndex=0)
returns the index of the char, searching from fromIndex.