SafeString  4.1.42
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.29
4 
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 
99 #ifdef __cplusplus
100 
101 #include <stdbool.h>
102 #include <stdlib.h>
103 #include <string.h>
104 #include <ctype.h>
105 
106 #if defined(ESP_PLATFORM) || defined(ARDUINO_ARCH_ESP8266)
107 #include <pgmspace.h>
108 #elif defined(ARDUINO_ARDUINO_NANO33BLE) || defined(ARDUINO_ARCH_MBED_RP2040)|| defined(ARDUINO_ARCH_RP2040)|| defined(ARDUINO_ARCH_MBED)
109 #include <api/deprecated-avr-comp/avr/pgmspace.h>
110 #else
111 #include <avr/pgmspace.h>
112 #endif
113 
114 #include <stdint.h>
115 #include <Print.h>
116 #include <Printable.h>
117 
118 // This include handles the rename of Stream for MBED compiles
119 #if defined(ARDUINO_ARDUINO_NANO33BLE) || defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_MBED_RP2040) || defined(ARDUINO_ARCH_RP2040) || defined(ARDUINO_ARCH_MBED)
120 #include <Stream.h>
121 #elif defined( __MBED__ ) || defined( MBED_H )
122 #include <WStream.h>
123 #define Stream WStream
124 #else
125 #include <Stream.h>
126 #endif
127 
128 // handle namespace arduino
130 
131 // removed V4.1.29 -- Add these lines back in if your board does not define the F() macro and the class __FlashStringHelper;
132 //class __FlashStringHelper;
133 //#define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))
134 
135 // to remove all the error messages, comment out
136 #define SSTRING_DEBUG
137 // this saves program bytes and the ram used by the SafeString object names
138 //
139 // Usually just leave as is and use SafeString::setOutput(..) to control the error messages and debug output
140 // there will be no error messages or debug output if SafeString::setOutput(..) has not been called from your sketch
141 //
142 // SafeString.debug() is always available regardless of the SSTRING_DEBUG define setting
143 // but SafeString::setOutput() still needs to be called to set where the output should go.
144 
145 /* ----------------- creating SafeStrings ---------------------------------
146  See the example sketches SafeString_ConstructorAndDebugging.ino and SafeStringFromCharArray.ino
147  and SafeStringFromCharPtr.ino and SafeStringFromCharPtrWithSize.ion
148 
149  createSafeString(name, size) and createSafeString(name, size, "initialText")
150  are utility macros to create an SafeString of a given name and size and optionally, an initial value
151 
152  createSafeString(str, 40); or cSF(str, 40);
153  expands in the pre-processor to
154  char str_SAFEBUFFER[40+1];
155  SafeString str(sizeof(str_SAFEBUFFER),str_SAFEBUFFER,"","str");
156 
157  createSafeString(str, 40, "test"); or cSF(str, 40, "test");
158  expands in the pre-processor to
159  char str_SAFEBUFFER[40+1];
160  SafeString str(sizeof(str_SAFEBUFFER),str_SAFEBUFFER,"test","str");
161 
162  createSafeStringFromCharArray(name, char[]); or cSFA(name, char[]);
163  wraps an existing char[] in a SafeString of the given name
164  e.g.
165  char charBuffer[15];
166  createSafeStringFromCharArray(str,charBuffer); or cSFA(str,charBuffer);
167  expands in the pre-processor to
168  SafeString str(sizeof(charBuffer),charBuffer, charBuffer, "str", true);
169 
170  createSafeStringFromCharPtr(name, char*); or cSFP(name, char*);
171  wraps an existing char[] pointed to by char* in a SafeString of the given name
172  e.g.
173  char charBuffer[15];
174  char *bufPtr = charBuffer;
175  createSafeStringFromCharPtr(str,bufPtr); or cSFP(str,bufPtr);
176  expands in the pre-processor to
177  SafeString str((unsigned int)-1,charBuffer, charBuffer, "str", true);
178  and the capacity of the SafeString is set to strlen(charBuffer) and cannot be increased.
179 
180  createSafeStringFromCharPtrWithSize(name, char*, unsigned int); or cSFPS(name, char*, unsigned int);
181  wraps an existing char[] pointed to by char* in a SafeString of the given name and sets the capacity to the given size -1
182  e.g.
183  char charBuffer[15];
184  char *bufPtr = charBuffer;
185  createSafeStringFromCharPtrWithSize(str,bufPtr, 15); or cSFPS(str,bufPtr, 15);
186  expands in the pre-processor to
187  SafeString str(15,charBuffer, charBuffer, "str", true);
188  The capacity of the SafeString is set to 14.
189 
190 ****************************************************************************************/
191 /* **************************************************
192  If str is a SafeString then
193  str = .. works for signed/unsigned ints, char*, char, F(".."), SafeString float, double etc
194  str.concat(..) and string.prefix(..) also works for those
195  str.stoken(..) can be used to split a string in to tokens
196 
197  SafeStrings created via createSafeString(..) or cSF(..) are never invalid, even if called with invalid arguments.
198  SafeStrings created via createSafeStringFromCharArray(..) or cSFA(..) are valid as long at the underlying char[] is valid
199  Usually the only way the char[] can become invalid is if it exists in a struct that is allocated (via calloc/malloc)
200  and then freed while the SafeString wrapping it is still in use.
201  SafeStrings created via createSafeStringFromCharPtr(..) or cSFP(..) are valid if the char[] pointed to is validly terminated
202  SafeStrings created via createSafeStringFromCharWithSize(..) or cSFPS(..) are valid if the char[] and size specified is valid.
203  For both createSafeStringFromCharPtr() and createSafeStringFromCharWithSize()
204  the SafeStrings created remain valid as long as the underlying char[] is valid.
205  Usually the only way the char[] can become invalid is if it was allocated (via calloc/malloc)
206  and then freed while the SafeString wrapping it is still in use.
207 * ***************************************************/
208 
209 
210 #ifdef SSTRING_DEBUG
211 #define createSafeString(name, size,...) char name ## _SAFEBUFFER[(size)+1]; SafeString name(sizeof(name ## _SAFEBUFFER),name ## _SAFEBUFFER, "" __VA_ARGS__ , #name);
212 #define createSafeStringFromCharArray(name, charArray) SafeString name(sizeof(charArray),charArray, charArray, #name, true, false);
213 #define createSafeStringFromCharPtr(name, charPtr) SafeString name((unsigned int)-1,charPtr, charPtr, #name, true);
214 #define createSafeStringFromCharPtrWithSize(name, charPtr, arraySize) SafeString name((arraySize),charPtr, charPtr, #name, true);
215 #else
216 #define createSafeString(name, size,...) char name ## _SAFEBUFFER[(size)+1]; SafeString name(sizeof(name ## _SAFEBUFFER),name ## _SAFEBUFFER, "" __VA_ARGS__);
217 #define createSafeStringFromCharArray(name,charArray) SafeString name(sizeof(charArray),charArray, charArray, NULL, true, false);
218 #define createSafeStringFromCharPtr(name, charPtr) SafeString name((unsigned int)-1,charPtr, charPtr, NULL, true);
219 #define createSafeStringFromCharPtrWithSize(name, charPtr, arraySize) SafeString name((arraySize),charPtr, charPtr, NULL, true);
220 #endif
221 
222 // define typing shortcuts
223 #define cSF createSafeString
224 #define cSFA createSafeStringFromCharArray
225 #define cSFP createSafeStringFromCharPtr
226 #define cSFPS createSafeStringFromCharPtrWithSize
227 
228 
300 class SafeString : public Printable, public Print {
301 
302  public:
303 
314 // In all cases when maxlen != -1, it is the actual size of the array
315 // if _fromBuffer false (i.e. cSF(sfStr,20); ) then maxLen is the capacity+1 and the macro allocates an char[20+1], (_fromPtr ignored)
316 // 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
317 // 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
318 // if maxLen == -1 then capacity == strlen(char*) i.e. cSFP( )
319 // else capacity == maxLen-1; i.e. cSFPS( )
320  explicit SafeString(unsigned int maxLen, char *buf, const char* cstr, const char* _name = NULL, bool _fromBuffer = false, bool _fromPtr = true);
321  // _fromBuffer true does extra checking before each method execution for SafeStrings created from existing char[] buffers
322  // _fromPtr is not checked unless _fromBuffer is true
323  // _fromPtr true allows for any array size, if false prevents passing char* by checking sizeof(charArray) != sizeof(char*)
324 
325  private: // to force compile errors if function definition of the SafeString argument is not a refernce, i.e. not SafeString&
326  SafeString(const SafeString& other ); // You must declare SafeStrings function arguments as a reference, SafeString&, e.g. void test(SafeString& strIn)
327  // NO other constructors, NO conversion constructors
328 
329  public:
330 
337  static void setOutput(Print& debugOut, bool verbose = true);
338  // static SafeString::DebugPrint Output; // a Print object controlled by setOutput() / turnOutputOff() is defined at the bottom
339 
340 
345  static void turnOutputOff(void); // call this to turn all debugging OFF, both error messages AND debug( ) method output
346 
347  // use this to control error messages verbose output
348 
353  static void setVerbose(bool verbose); // turn verbose error msgs on/off. setOutput( ) sets verbose to true
354 
355  // returns true if error detected, errors are detected even is setOutput has not been called
356  // each call to hasError() clears the errorFlag
357 
360  unsigned char hasError();
361 
362  // returns true if error detected in any SafeString object, errors are detected even is setOutput has not been called
363  // each call to errorDetected() clears the classErrorFlag
364 
367  static unsigned char errorDetected();
368 
369  // these methods print out info on this SafeString object, iff setOutput has been called
370  // setVerbose( ) does NOT effect these methods which have their own verbose argument
371  // Each of these debug( ) methods defaults to outputing the string contents. Set the optional verbose argument to false to suppress outputing string contents
372  // NOTE!! all these debug methods return a pointer to an empty string.
373  // This is so that if you add .debug() to Serial.println(str); i.e. Serial.println(str.debug()) will work as expected
374 
379  const char* debug(bool verbose = true);
380 
381 
387  const char* debug(const char* title, bool verbose = true);
388 
389 
395  const char* debug(const __FlashStringHelper *title, bool verbose = true);
396 
397 
403  const char* debug(SafeString &stitle, bool verbose = true);
404 
405 
412  virtual size_t write(uint8_t b);
413  // writes at most length chars to this SafeString,
414  // NOTE: write(cstr,length) will set hasError and optionally output errorMsg, if strlen(cstr) < length and nothing will be added to the SafeString
415 
416 
424  virtual size_t write(const uint8_t *buffer, size_t length);
425 
426 
431  size_t printTo(Print& p) const;
432 
433  // reserve returns 0 if _capacity < size
434 
439  unsigned char reserve(unsigned int size);
440 
441 
444  unsigned int length(void);
445 
446 
449  unsigned int capacity(void);
450 
451 
454  unsigned char isFull(void);
455 
456 
459  unsigned char isEmpty(void);
460 
461 
464  int availableForWrite(void);
465 
466 
472  SafeString & clear(void);
473 
474  public:
475  // support for print
476  size_t print(unsigned char, int = DEC);
477  size_t print(int, int = DEC);
478  size_t print(unsigned int, int = DEC);
479  size_t print(long, int = DEC);
480  size_t print(unsigned long, int = DEC);
481  size_t print(int64_t, int = DEC);
482  size_t print(double, int = 2);
483  size_t print(const __FlashStringHelper *);
484  size_t print(const char*);
485  size_t print(char);
486  size_t print(SafeString &str);
487 
488  size_t println(unsigned char, int = DEC);
489  size_t println(int, int = DEC);
490  size_t println(unsigned int, int = DEC);
491  size_t println(long, int = DEC);
492  size_t println(unsigned long, int = DEC);
493  size_t println(int64_t, int = DEC);
494  size_t println(double, int = 2);
495  size_t println(const __FlashStringHelper *);
496  size_t println(const char*);
497  size_t println(char);
498  size_t println(SafeString &str);
499  size_t println(void);
500 
501  // ********** special prints padding and formatting doubles (longs) **************
502  // print to SafeString a double (or long) with decs after the decimal point and padd to specified width
503  // width is a signed value, negative for left adjustment, +ve for right padding
504  // by default the + sign is not added, set forceSign argument to true to force the display of the + sign
505  //
506  // If the result exceeds abs(width), reduce the decs after the decmial point to fit into width
507  // If result with decs reduced to 0 is still > abs(width) raise an error and ,optionally, output an error msg
508  //
509  // Note decs is quietly limited in this method to < 7 digit after the decimal point.
510 
511 
521  size_t println(double d, int decs, int width, bool forceSign = false);
522 
523 
533  size_t print(double d, int decs, int width, bool forceSign = false);
534 
535 
536 
537  // Assignment operators **********************************
538  // Set the SafeString to a char version of the assigned value.
539  // For = (const char *) the contents are copied to the SafeString buffer
540  // if the value is null or invalid,
541  // or too large to be fit in the string's internal buffer
542  // the string will be left empty
543 
544 
552 
553 
560  SafeString & operator = (unsigned char num);
561 
562 
570 
571 
578  SafeString & operator = (unsigned int num);
579 
580 
587  SafeString & operator = (long num);
588 
589 
596  SafeString & operator = (unsigned long num);
597 
598 
605  SafeString & operator = (int64_t num);
606 
607 
614  SafeString & operator = (float num);
615 
616 
623  SafeString & operator = (double num);
624 
625 
633 
634 
641  SafeString & operator = (const char *cstr);
642 
643 
650  SafeString & operator = (const __FlashStringHelper *pstr); // handle F(" .. ") values
651 
652 
661  SafeString & prefix(const char *cstr);
662  SafeString & prefix(char c);
663  SafeString & prefix(unsigned char c);
664  SafeString & prefix(int num);
665  SafeString & prefix(unsigned int num);
666  SafeString & prefix(long num);
667  SafeString & prefix(unsigned long num);
668  SafeString & prefix(int64_t num);
669  SafeString & prefix(float num);
670  SafeString & prefix(double num);
671  SafeString & prefix(const __FlashStringHelper * str);
672  SafeString & prefix(const char *cstr, size_t length);
673  SafeString & prefix(const __FlashStringHelper * str, size_t length);
674 
675 
684  SafeString & concat(const char *cstr);
685  SafeString & concat(char c);
686  SafeString & concat(unsigned char c);
687  SafeString & concat(int num);
688  SafeString & concat(unsigned int num);
689  SafeString & concat(long num);
690  SafeString & concat(unsigned long num);
691  SafeString & concat(int64_t num);
692  SafeString & concat(float num);
693  SafeString & concat(double num);
694  SafeString & concat(const __FlashStringHelper * str);
695  // ------------------------------------------------------
696  // no corresponding methods these three (3) in prefix, +=, -+
697 
698  SafeString & concat(const char *cstr, size_t length); // concat at most length chars from cstr
699  // NOTE: concat(cstr,length) will set hasError and optionally output errorMsg, if strlen(cstr) < length and nothing will be concatinated.
700 
701  SafeString & concat(const __FlashStringHelper * str, size_t length); // concat at most length chars
702 
703 
708  SafeString & newline(); // append newline \r\n same as concat("\r\n"); same a println()
709  // e.g. sfStr.concat("test").newline();
710 
711  /* prefix() operator -= ******************
712  Operator version of prefix( )
713  prefix -=
714  To cascade operators use ( )
715  e.g. (sfStr -= 'a') -= 5;
716  **/
717 
725  return prefix(rhs);
726  }
727  SafeString & operator -= (const char *cstr) {
728  return prefix(cstr);
729  }
731  return prefix(c);
732  }
733  SafeString & operator -= (unsigned char num) {
734  return prefix(num);
735  }
737  return prefix(num);
738  }
739  SafeString & operator -= (unsigned int num) {
740  return prefix(num);
741  }
742  SafeString & operator -= (long num) {
743  return prefix(num);
744  }
745  SafeString & operator -= (unsigned long num) {
746  return prefix(num);
747  }
748  SafeString & operator -= (int64_t num) {
749  return prefix(num);
750  }
751  SafeString & operator -= (float num) {
752  return prefix(num);
753  }
754  SafeString & operator -= (double num) {
755  return prefix(num);
756  }
757  SafeString & operator -= (const __FlashStringHelper *str) {
758  return prefix(str);
759  }
760 
761  /* concat() operator += ******************
762  Operator versions of concat( )
763  suffix/append +=
764  To cascade operators use ( )
765  e.g. (sfStr += 'a') += 5;
766  **/
767 
775  return concat(rhs);
776  }
777  SafeString & operator += (const char *cstr) {
778  return concat(cstr);
779  }
781  return concat(c);
782  }
783  SafeString & operator += (unsigned char num) {
784  return concat(num);
785  }
787  return concat(num);
788  }
789  SafeString & operator += (unsigned int num) {
790  return concat(num);
791  }
792  SafeString & operator += (long num) {
793  return concat(num);
794  }
795  SafeString & operator += (unsigned long num) {
796  return concat(num);
797  }
798  SafeString & operator += (int64_t num) {
799  return concat(num);
800  }
801  SafeString & operator += (float num) {
802  return concat(num);
803  }
804  SafeString & operator += (double num) {
805  return concat(num);
806  }
807  SafeString & operator += (const __FlashStringHelper *str) {
808  return concat(str);
809  }
810 
811  /* Comparision methods and operators ******************
812  comparisons only work with SafeStrings and "strings"
813  These methods used to be ... const {
814  but now with createSafeStringFromBuffer( ) the SafeString may be modified by cleanUp()
815  **/
816 
820 
823  int compareTo(const char *cstr) ;
824 
825  unsigned char equals(SafeString &s) ;
826  unsigned char equals(const char *cstr) ;
827  unsigned char equals(const char c) ;
828  unsigned char operator == (SafeString &rhs) {
829  return equals(rhs);
830  }
831  unsigned char operator == (const char *cstr) {
832  return equals(cstr);
833  }
834  unsigned char operator == (const char c) {
835  return equals(c);
836  }
837  unsigned char operator != (SafeString &rhs) {
838  return !equals(rhs);
839  }
840  unsigned char operator != (const char *cstr) {
841  return !equals(cstr);
842  }
843  unsigned char operator != (const char c) {
844  return !equals(c);
845  }
846  unsigned char operator < (SafeString &rhs) {
847  return compareTo(rhs) < 0;
848  }
849  unsigned char operator > (SafeString &rhs) {
850  return compareTo(rhs) > 0;
851  }
852  unsigned char operator <= (SafeString &rhs) {
853  return compareTo(rhs) <= 0;
854  }
855  unsigned char operator >= (SafeString &rhs) {
856  return compareTo(rhs) >= 0;
857  }
858  unsigned char operator < (const char* rhs) {
859  return compareTo(rhs) < 0;
860  }
861  unsigned char operator > (const char* rhs) {
862  return compareTo(rhs) > 0;
863  }
864  unsigned char operator <= (const char* rhs) {
865  return compareTo(rhs) <= 0;
866  }
867  unsigned char operator >= (const char* rhs) {
868  return compareTo(rhs) >= 0;
869  }
870  unsigned char equalsIgnoreCase(SafeString &s) ;
871  unsigned char equalsIgnoreCase(const char *str2) ;
872 
873  unsigned char equalsConstantTime(SafeString &s) ;
874 
875  /* startsWith methods *******************
876  The fromIndex is offset into this SafeString where check is to start
877  0 to length() and (unsigned int)(-1) are valid for fromIndex, if fromIndex == length() or -1 false is returned
878  if the argument is null or fromIndex > length(), an error is flagged and false returned
879  **/
886  unsigned char startsWith(const char c, unsigned int fromIndex = 0);
893  unsigned char startsWith( const char *str2, unsigned int fromIndex = 0) ;
900  unsigned char startsWith(SafeString &s2, unsigned int fromIndex = 0) ;
901 
908  unsigned char startsWithIgnoreCase(const char c, unsigned int fromIndex = 0);
915  unsigned char startsWithIgnoreCase( const char *str2, unsigned int fromIndex = 0) ;
922  unsigned char startsWithIgnoreCase( SafeString &s2, unsigned int fromIndex = 0) ;
923 
924  /* endsWith methods *******************/
930  unsigned char endsWith(const char c);
936  unsigned char endsWith(SafeString &suffix) ;
942  unsigned char endsWith(const char *suffix) ;
948  unsigned char endsWithCharFrom(SafeString &suffix) ;
954  unsigned char endsWithCharFrom(const char *suffix) ;
955 
956  /* character acccess methods *******************
957  NOTE: There is no access to modify the underlying char buffer directly
958  For these methods 0 to length()-1 is valid for index
959  index greater than length() -1 will return 0 and set the error flag and will print errors if debug enabled
960  **/
968  char charAt(unsigned int index) ; // if index >= length() returns 0 and prints a error msg
976  char operator [] (unsigned int index) ; // if index >= length() returns 0 and prints a error msg
977 
978  // setting a char in the SafeString
979  // str[..] = c; is not supported because it allows direct access to modify the underlying char buffer
986  void setCharAt(unsigned int index, char c); //if index >= length() the error flag is set
987  // calls to setCharAt(length(), ..) and setCharAt(.. , '\0') are ignored and error flag is set
988 
989  // returning the underlying buffer
990  // returned as a const and should not be changesdor recast to a non-const
995  const char* c_str();
996 
997 
998  /* search methods *******************
999  Arrays are indexed by a unsigned int variable
1000  See the SafeStringIndexOf.ino example sketch
1001  All indexOf methods return -1 if not found
1002  **********************************************/
1003  // The fromIndex is offset into this SafeString where to start searching (inclusive)
1004  // 0 to length() and -1 is valid for fromIndex
1005  // if fromIndex > length(), than the error flag is set and -1 returned and prints an error if debug enabled
1006  // if fromIndex == (unsigned int)(-1) -1 is returned without error.
1007  /*
1008  returns the index
1009  */
1010  //int indexOf( char ch ) ;
1018  int indexOf( char ch, unsigned int fromIndex = 0) ;
1019  //int indexOf( SafeString & str ) ;
1020  //int indexOf( const char* str ) ;
1027  int indexOf(const char* str , unsigned int fromIndex = 0) ;
1034  int indexOf( SafeString & str, unsigned int fromIndex = 0 ) ;
1035 
1041  int lastIndexOf( char ch ) ;
1042 
1049  int lastIndexOf( char ch, unsigned int fromIndex) ;
1050 
1056  int lastIndexOf( SafeString & str ) ;
1063  int lastIndexOf( SafeString & str, unsigned int fromIndex) ;
1064 
1070  int lastIndexOf( const char *cstr ) ;
1071 
1078  int lastIndexOf(const char* cstr, unsigned int fromIndex);
1079 
1080  // first index of the chars listed in chars string
1081  // loop through chars and look for index of each and return the min index or -1 if none found
1082  //int indexOfCharFrom(SafeString & str);
1083  //int indexOfCharFrom(const char* chars);
1084  // start searching from fromIndex
1091  int indexOfCharFrom(SafeString & str, unsigned int fromIndex = 0);
1092 
1099  int indexOfCharFrom(const char* chars, unsigned int fromIndex = 0);
1100 
1101  /* *** UTF-8 methods ************/
1102  // For endIdx <= length(), utf8index returns an index in the range endIdx-3 to endIdx
1103  // such that using that index for substring will not split a valid UTF-8 code point
1104  // if endIdx > length(), endIdx is set to length(); and the error flag is set
1105  // endIdx == (unsigned int)(-1) is treated as endIdx == length() returns a result without an error
1106  //Code Points 1st-Byte 2nd-Byte 3rd-Byte 4th-Byte
1107  //U+0000..U+007F 00..7F
1108  //U+0080..U+07FF C2..DF 80..BF
1109  //U+0800..U+0FFF E0 A0..BF 80..BF
1110  //U+1000..U+CFFF E1..EC 80..BF 80..BF
1111  //U+D000..U+D7FF ED 80..9F 80..BF
1112  //U+E000..U+FFFF EE..EF 80..BF 80..BF
1113  //U+10000..U+3FFFF F0 90..BF 80..BF 80..BF
1114  //U+40000..U+FFFFF F1..F3 80..BF 80..BF 80..BF
1115  //U+100000..U+10FFFF F4 80..8F 80..BF 80..BF
1116 
1133  int utf8index(unsigned int endIdx);
1134 
1135 
1136  // For startIdx < length(), utf8nextIndex returns an index in the range startIdx+1 to startIdx+4
1137  // such that using that index for substring will not split a valid UTF-8 code point
1138  // if startIdx > length(), (unsigned int)(-1) will be returned and the error flag is set
1139  // if startIdx == (unsigned int)(-1), OR startIdx == length(), (unsigned int)(-1) will be returned with no error
1157  int utf8nextIndex(unsigned int startIdx);
1158 
1159  /* *** substring methods ************/
1160  // substring is from beginIdx to end of string
1161  // The result substring is ALWAYS first cleared by this method so it will be empty on errors
1162  // if beginIdx = length(), an empty result will be returned without error
1163  // if beginIdx > length(), an empty result will be returned with error flag set on both this SafeString and the result SafeString
1164  // beginIdx == (unsigned int)(-1) returns an empty result without an error
1165  // You can take substring of yourself e.g. str.substring(str,3);
1166  // if result does not have the capacity to hold the substring, hasError() is set on both this SafeString and the result SafeString
1167 
1183  SafeString & substring(SafeString & result, unsigned int beginIdx);
1184 
1185  // The result substring is ALWAYS first cleared by this method so it will be empty on errors
1186  // if beginIdx = length(), an empty result will be returned without error
1187  // if beginIdx > length(), an empty result will be returned with error flag set on both this SafeString and the result SafeString
1188  // 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
1189  // if endIdx > length(), endIdx is set to length(); and the error flag is set on both this SafeString and the result SafeString
1190  // endIdx == (unsigned int)(-1) is treated as endIdx == length() returns a result without an error
1191  // substring is from beginIdx to endIdx-1, endIdx is exclusive
1192  // You can take substring of yourself e.g. str.substring(str,3,6);
1193  // 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
1212  SafeString & substring(SafeString & result, unsigned int beginIdx, unsigned int endIdx);
1213 
1214  /* *** SafeString modification methods ************/
1215 
1216  /* *** replace ************/
1222  void replace(char findChar, char replaceChar);
1223 
1229  void replace(const char findChar, const char *replaceStr);
1230 
1236  void replace(const char findChar, SafeString& sfReplace);
1237 
1243  void replace(const char* findStr, const char *replaceStr);
1244 
1250  void replace(SafeString & sfFind, SafeString & sfReplace);
1251 
1252  /* *** remove ************/
1253  // remove from index to end of SafeString
1254  // 0 to length() and (unsigned int)(-1) are valid for index,
1255  // -1 => length() for processing and just returns without error
1262  void removeFrom(unsigned int startIndex);
1263 
1264  // remove from 0 to startIdx (excluding startIdx)
1265  // 0 to length() and (unsigned int)(-1) are valid for index,
1266  // -1 => length() for processing
1273  void removeBefore(unsigned int startIndex);
1274 
1275  // remove from index to end of SafeString
1276  // 0 to length() and (unsigned int)(-1) are valid for index,
1277  // -1 => length() for processing and just returns without error
1284  void remove(unsigned int index);
1285 
1286  // remove count chars starting from index
1287  // 0 to length() and unsigned int(-1) are valid for index
1288  // -1 just returns without error
1289  // 0 to (length()- index) is valid for count, larger values set the error flag and remove from idx to end of string
1298  void remove(unsigned int index, unsigned int count);
1299 
1300  // remove the last 'count' chars
1301  // 0 to length() is valid for count,
1302  // count >= length() clears the SafeString
1303  // count > length() set the error flag
1311  void removeLast(unsigned int count);
1312 
1313  // keep last 'count' number of chars remove the rest
1314  // 0 to length() is valid for count, passing in count == 0 clears the SafeString
1315  // count > length() sets error flag and returns SafeString unchanged
1323  void keepLast(unsigned int count);
1324 
1325 
1326  /* *** change case ************/
1330  void toLowerCase(void);
1334  void toUpperCase(void);
1335 
1336  /* *** remove white space from front and back of SafeString ************/
1337  // the method isspace( ) is used to. For the 'C' local the following are trimmed
1338  // ' ' (0x20) space (SPC)
1339  // '\t' (0x09) horizontal tab (TAB)
1340  // '\n' (0x0a) newline (LF)
1341  // '\v' (0x0b) vertical tab (VT)
1342  // '\f' (0x0c) feed (FF)
1343  // '\r' (0x0d) carriage return (CR)
1355  void trim(void); // trims front and back
1356 
1357  // processBackspaces recursively remove backspaces, '\b' and the preceeding char
1358  // use for processing inputs from terminal (Telent) connections
1364  void processBackspaces(void);
1365 
1366  /* *** numgber parsing/conversion ************/
1367  // convert numbers
1368  // If the SafeString is a valid number update the argument with the result
1369  // else leave the argument unchanged
1370  // SafeString conversions are stricter than the Arduino String version
1371  // trailing chars can only be white space
1378  unsigned char toInt(int & i) ;
1379 
1386  unsigned char toInt64_t(int64_t &l) ;
1387 
1394  unsigned char toLong(long & l) ;
1395 
1402  unsigned char binToLong(long & l) ;
1409  unsigned char octToLong(long & l) ;
1416  unsigned char hexToLong(long & l) ;
1423  unsigned char toUnsignedLong(unsigned long & l) ;
1430  unsigned char binToUnsignedLong(unsigned long & l) ;
1437  unsigned char octToUnsignedLong(unsigned long & l) ;
1444  unsigned char hexToUnsignedLong(unsigned long & l) ;
1451  unsigned char toFloat(float & f) ;
1458  unsigned char toDouble(double & d) ;
1459 
1460  // float toFloat(); possible alternative
1461 
1462  /* Tokenizeing methods, stoken(), nextToken()/firstToken() ************************/
1463  /* Differences between stoken() and nextToken
1464  stoken() leaves the SafeString unchanged, nextToken() removes the token (and leading delimiters) from the SafeString giving space to add more input
1465  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
1466  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
1467  Setting returnLastNonDelimitedToken = false this allows partial tokens to be read from a Stream and kept until the full token and delimiter is read
1468  */
1469  /*
1470  stoken -- The SafeString itself is not changed
1471  stoken breaks into the SafeString into tokens using chars in delimiters string and the end of the SafeString as delimiters.
1472  Any leading delimiters are first stepped over and then the delimited token is return in the token argument (less the delimiter).
1473  The token argument is always cleared at the start of the stoken().
1474  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
1475 
1476  params
1477  token - the SafeString to return the token in, it is cleared if no delimited token found or if there are errors
1478  The found delimited token (less the delimiter) is returned in the token SafeString argument if there is capacity.
1479  The token's capacity should be >= this SafeString's capacity incase the entire SafeString needs to be returned.
1480  If the token's capacity is < the next token, then token is returned empty and an error messages printed if debug is enabled.
1481  In this case the return (nextIndex) is still updated.
1482  fromIndex -- where to start the search from 0 to length() and -1 is valid for fromIndex, -1 => length() for processing
1483  delimiters - the characters that any one of which can delimit a token. The end of the SafeString is always a delimiter.
1484  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
1485  useAsDelimiters - default true, if false then token consists only of chars in the delimiters and any other char terminates the token
1486 
1487  return -- nextIndex, the next index in this SafeString after the end of the token just found, -1 if this is the last token
1488  Use this as the fromIndex for the next call
1489  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
1490  If the token's capacity is < the next token, the token returned is empty and an error messages printed if debug is enabled.
1491  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
1492  while being consistent with the SafeString's all or nothing insertion rule
1493 
1494  Input argument errors return -1 and an empty token and hasError() is set on both this SafeString and the token SafeString.
1495  **/
1496 
1523  int stoken(SafeString & token, unsigned int fromIndex, const char delimiter, bool returnEmptyFields = false, bool useAsDelimiters = true);
1524 
1551  int stoken(SafeString & token, unsigned int fromIndex, const char* delimiters, bool returnEmptyFields = false, bool useAsDelimiters = true);
1552 
1579  int stoken(SafeString & token, unsigned int fromIndex, SafeString & delimiters, bool returnEmptyFields = false, bool useAsDelimiters = true);
1580 
1606  inline unsigned char firstToken(SafeString & token, char delimiter, bool returnLastNonDelimitedToken = true) {
1607  return nextToken(token,delimiter,true,returnLastNonDelimitedToken,true);
1608  }
1609 
1640  unsigned char nextToken(SafeString & token, char delimiter, bool returnEmptyFields = false, bool returnLastNonDelimitedToken = true, bool firstToken = false);
1641 
1667  inline unsigned char firstToken(SafeString & token, SafeString delimiters, bool returnLastNonDelimitedToken = true) {
1668  return nextToken(token,delimiters,true,returnLastNonDelimitedToken,true);
1669  }
1670 
1701  unsigned char nextToken(SafeString & token, SafeString & delimiters, bool returnEmptyFields = false, bool returnLastNonDelimitedToken = true, bool firstToken = false);
1702 
1728  inline unsigned char firstToken(SafeString & token, const char* delimiters, bool returnLastNonDelimitedToken = true) {
1729  return nextToken(token,delimiters,true,returnLastNonDelimitedToken,true);
1730  }
1731 
1762  unsigned char nextToken(SafeString & token, const char* delimiters, bool returnEmptyFields = false, bool returnLastNonDelimitedToken = true, bool firstToken = false);
1763 
1764 
1765  /* *** ReadFrom from SafeString, writeTo SafeString ************************/
1781  unsigned int readFrom(SafeString & sfInput, unsigned int startIdx = 0);
1782 
1796  unsigned int readFrom(const char* strPtr, unsigned int maxCharsToRead = ((unsigned int)-1));
1797 
1810  unsigned int writeTo(SafeString & output, unsigned int startIdx = 0);
1811 
1812  /* *** NON-blocking reads from Stream ************************/
1813 
1823  unsigned char read(Stream & input);
1824 
1836  unsigned char readUntil(Stream & input, const char delimiter);
1848  unsigned char readUntil(Stream & input, const char* delimiters);
1860  unsigned char readUntil(Stream & input, SafeString & delimiters);
1861 
1889  unsigned char readUntilToken(Stream & input, SafeString & token, const char delimiter, bool & skipToDelimiter, uint8_t echoInput = false, unsigned long timeout_ms = 0);
1890 
1918  unsigned char readUntilToken(Stream & input, SafeString & token, const char* delimiters, bool & skipToDelimiter, uint8_t echoInput = false, unsigned long timeout_ms = 0);
1919 
1947  unsigned char readUntilToken(Stream & input, SafeString & token, SafeString & delimiters, bool & skipToDelimiter, uint8_t echoInput = false, unsigned long timeout_ms = 0);
1948 
1955  size_t getLastReadCount();
1956 
1957  /* *** END OF PUBLIC METHODS ************/
1958 
1959  protected:
1960  static Print* debugPtr;
1961  static bool fullDebug;
1962  char *buffer; // the actual char array
1963  size_t _capacity; // the array length minus one (for the '\0')
1964  size_t len; // the SafeString length (not counting the '\0')
1965 
1966  class noDebugPrint : public Print {
1967  public:
1968  inline size_t write(uint8_t b) {
1969  (void)(b);
1970  return 0;
1971  }
1972  inline size_t write(const uint8_t *buffer, size_t length) {
1973  (void)(buffer);
1974  (void)(length);
1975  return 0;
1976  };
1977  void flush() { }
1978  };
1979 
1981 
1982  static Print* currentOutput;// = &emptyPrint;
1983 
1984  class DebugPrint : public Print {
1985  public:
1986  size_t write(uint8_t b) {
1987  return currentOutput->write(b);
1988  }
1989  size_t write(const uint8_t *buffer, size_t length) {
1990  return currentOutput->write(buffer, length);
1991  };
1992  void flush() {
1993 #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)
1994  // ESP32 has no flush in Print!! but ESP8266 has
1995 #else
1996  currentOutput->flush();
1997 #endif
1998  }
1999  };
2000 
2001  public:
2002  static SafeString::DebugPrint Output; // a Print object controlled by setOutput() / turnOutputOff()
2003 
2004  protected:
2005  SafeString & concatln(const __FlashStringHelper * pstr);
2007  SafeString & concatln(const char *cstr, size_t length);
2008  void outputName() const ;
2009  SafeString & concatInternal(const char *cstr, size_t length, bool assignOp = false); // concat at most length chars from cstr
2010  SafeString & concatInternal(const __FlashStringHelper * str, size_t length, bool assignOp = false); // concat at most length chars
2011 
2012  SafeString & concatInternal(const char *cstr, bool assignOp = false);
2013  SafeString & concatInternal(char c, bool assignOp = false);
2014  SafeString & concatInternal(const __FlashStringHelper * str, bool assignOp = false);
2015  size_t printInternal(long, int = DEC, bool assignOp = false);
2016  size_t printInternal(unsigned long, int = DEC, bool assignOp = false);
2017  size_t printInternal(double, int = 2, bool assignOp = false);
2018  size_t printInternal(int64_t num, int base = 2, bool assignOp = false);
2019 
2020  void setError();
2021  void printlnErr()const ;
2022  void debugInternalMsg(bool _fullDebug) const ;
2023  size_t limitedStrLen(const char* p, size_t limit);
2024  size_t printInt(double d, int decs, int width, bool forceSign, bool addNL);
2025 
2026  private:
2027  bool readUntilTokenInternal(Stream & input, SafeString & token, const char* delimitersIn, char delimiterIn, bool & skipToDelimiter, uint8_t echoInput, unsigned long timeout_ms);
2028  bool readUntilInternal(Stream & input, const char* delimitersIn, char delimiterIn);
2029  bool nextTokenInternal(SafeString & token, const char* delimitersIn, char delimiterIn, bool returnEmptyFields, bool returnLastNonDelimitedToken);
2030  int stokenInternal(SafeString &token, unsigned int fromIndex, const char* delimitersIn, char delimiterIn, bool returnEmptyFields, bool useAsDelimiters);
2031  bool fromBuffer; // true if createSafeStringFromBuffer created this object
2032  bool errorFlag; // set to true if error detected, cleared on each call to hasError()
2033  static bool classErrorFlag; // set to true if any error detected in any SafeString, cleared on each call to SafeString::errorDetected()
2034  void cleanUp(); // reterminates buffer at capacity and resets len to current strlen
2035  const char *name;
2036  unsigned long timeoutStart_ms;
2037  bool timeoutRunning;
2038  size_t noCharsRead; // number of char read on last call to readUntilToken
2039  static char nullBufferSafeStringBuffer[1];
2040  static char emptyDebugRtnBuffer[1];
2041  void debugInternal(bool _fullDebug) const ;
2042  void debugInternalResultMsg(bool _fullDebug) const ;
2043  void baseError(const __FlashStringHelper * methodName, int base) const ;
2044  void concatErr()const ;
2045  void concatAssignError() const;
2046  void prefixErr()const ;
2047  void capError(const __FlashStringHelper * methodName, size_t neededCap, const char* cstr, const __FlashStringHelper *pstr = NULL, char c = '\0', size_t length = 0)const ;
2048  void assignError(size_t neededCap, const char* cstr, const __FlashStringHelper *pstr = NULL, char c = '\0', bool numberFlag = false) const;
2049  void errorMethod(const __FlashStringHelper * methodName) const ;
2050  void warningMethod(const __FlashStringHelper * methodName) const ;
2051  void assignErrorMethod() const ;
2052  void outputFromIndexIfFullDebug(unsigned int fromIndex) const ;
2053  int64_t strto_int64_t(const char *nptr, char **endptr, int base);
2054 };
2055 
2056 #include "SafeStringNameSpaceEnd.h"
2057 
2058 #endif // __cplusplus
2059 #endif // SafeString_class_h
size_t write(const uint8_t *buffer, size_t length)
Definition: SafeString.h:1989
size_t write(uint8_t b)
Definition: SafeString.h:1986
size_t write(uint8_t b)
Definition: SafeString.h:1968
size_t write(const uint8_t *buffer, size_t length)
Definition: SafeString.h:1972
To create SafeStrings use one of the four (4) macros createSafeString or cSF, createSafeStringFromCha...
Definition: SafeString.h:300
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:2002
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:724
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:855
size_t println(SafeString &str)
static SafeString::noDebugPrint emptyPrint
Definition: SafeString.h:1980
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:1964
size_t println(long, int=DEC)
static Print * currentOutput
Definition: SafeString.h:1982
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:837
size_t printInternal(int64_t num, int base=2, bool assignOp=false)
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:852
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)
int utf8index(unsigned int endIdx)
For endIdx <= length(), utf8index() returns an index in the range endIdx-3 to endIdx such that using ...
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 println(int64_t, int=DEC)
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:1606
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:849
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
int utf8nextIndex(unsigned int startIdx)
For startIdx < length(), utf8nextIndex() returns an index in the range startIdx+1 to startIdx+4 such ...
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...
unsigned char toInt64_t(int64_t &l)
convert the SafeString to a int64_t (for time_t long long).
size_t print(char)
SafeString & clear(void)
Empties this SafeString.
void toUpperCase(void)
convert this SafeString to all lower case
size_t print(int64_t, int=DEC)
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:774
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:1667
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:1962
SafeString & prefix(unsigned char c)
size_t print(double, int=2)
SafeString & prefix(int64_t num)
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:1728
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:1960
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:1963
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:828
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:846
static bool fullDebug
Definition: SafeString.h:1961
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 & concat(int64_t num)
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.