From f2906256b12da3201348189d3fa1bf339ea47859 Mon Sep 17 00:00:00 2001 From: Chris--A Date: Thu, 7 May 2015 21:33:51 +1000 Subject: [PATCH 1/2] Add validate function to String (AVR) --- hardware/arduino/avr/cores/arduino/WString.cpp | 7 +++++++ hardware/arduino/avr/cores/arduino/WString.h | 9 +++++++++ 2 files changed, 16 insertions(+) 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 From 8b1b53254693aa60352547f85f479bbb3ee10e77 Mon Sep 17 00:00:00 2001 From: Chris--A Date: Thu, 7 May 2015 21:43:22 +1000 Subject: [PATCH 2/2] Add validate function to String (SAM) --- hardware/arduino/sam/cores/arduino/WString.cpp | 7 +++++++ hardware/arduino/sam/cores/arduino/WString.h | 9 +++++++++ 2 files changed, 16 insertions(+) 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