diff --git a/hardware/arduino/avr/cores/arduino/WString.cpp b/hardware/arduino/avr/cores/arduino/WString.cpp index dcd469d7dd0..01774baa167 100644 --- a/hardware/arduino/avr/cores/arduino/WString.cpp +++ b/hardware/arduino/avr/cores/arduino/WString.cpp @@ -152,6 +152,13 @@ unsigned char String::reserve(unsigned int size) return 0; } +unsigned int String::validate( bool remainder ) +{ + if( !remainder ) len = 0; + while( len < capacity && buffer[ len ] ) ++len; + return len; +} + unsigned char String::changeBuffer(unsigned int maxStrLen) { char *newbuffer = (char *)realloc(buffer, maxStrLen + 1); diff --git a/hardware/arduino/avr/cores/arduino/WString.h b/hardware/arduino/avr/cores/arduino/WString.h index 74024309278..2832043c275 100644 --- a/hardware/arduino/avr/cores/arduino/WString.h +++ b/hardware/arduino/avr/cores/arduino/WString.h @@ -78,6 +78,15 @@ class String // is left unchanged). reserve(0), if successful, will validate an // invalid string (i.e., "if (s)" will be true afterwards) unsigned char reserve(unsigned int size); + + // If reserved data is modified externally using c_str()/&str[x] + // this function will allow the contents to be validated and ready + // for use with the String functionality. + + // If remainder is true, only the reserved buffer is validated (capacity - len bytes) + // If remainder is false, the entire buffer is re-validated. + unsigned int validate( bool remainder = false ); + inline unsigned int length(void) const {return len;} // creates a copy of the assigned value. if the value is null or diff --git a/hardware/arduino/sam/cores/arduino/WString.cpp b/hardware/arduino/sam/cores/arduino/WString.cpp index 53f16cdc051..e1e0cdab84b 100644 --- a/hardware/arduino/sam/cores/arduino/WString.cpp +++ b/hardware/arduino/sam/cores/arduino/WString.cpp @@ -154,6 +154,13 @@ unsigned char String::reserve(unsigned int size) return 0; } +unsigned int String::validate( bool remainder ) +{ + if( !remainder ) len = 0; + while( len < capacity && buffer[ len ] ) ++len; + return len; +} + unsigned char String::changeBuffer(unsigned int maxStrLen) { char *newbuffer = (char *)realloc(buffer, maxStrLen + 1); diff --git a/hardware/arduino/sam/cores/arduino/WString.h b/hardware/arduino/sam/cores/arduino/WString.h index 74024309278..2832043c275 100644 --- a/hardware/arduino/sam/cores/arduino/WString.h +++ b/hardware/arduino/sam/cores/arduino/WString.h @@ -78,6 +78,15 @@ class String // is left unchanged). reserve(0), if successful, will validate an // invalid string (i.e., "if (s)" will be true afterwards) unsigned char reserve(unsigned int size); + + // If reserved data is modified externally using c_str()/&str[x] + // this function will allow the contents to be validated and ready + // for use with the String functionality. + + // If remainder is true, only the reserved buffer is validated (capacity - len bytes) + // If remainder is false, the entire buffer is re-validated. + unsigned int validate( bool remainder = false ); + inline unsigned int length(void) const {return len;} // creates a copy of the assigned value. if the value is null or