Skip to content

Commit f290625

Browse files
committed
Add validate function to String (AVR)
1 parent 5a68e34 commit f290625

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

hardware/arduino/avr/cores/arduino/WString.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,13 @@ unsigned char String::reserve(unsigned int size)
152152
return 0;
153153
}
154154

155+
unsigned int String::validate( bool remainder )
156+
{
157+
if( !remainder ) len = 0;
158+
while( len < capacity && buffer[ len ] ) ++len;
159+
return len;
160+
}
161+
155162
unsigned char String::changeBuffer(unsigned int maxStrLen)
156163
{
157164
char *newbuffer = (char *)realloc(buffer, maxStrLen + 1);

hardware/arduino/avr/cores/arduino/WString.h

+9
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,15 @@ class String
7878
// is left unchanged). reserve(0), if successful, will validate an
7979
// invalid string (i.e., "if (s)" will be true afterwards)
8080
unsigned char reserve(unsigned int size);
81+
82+
// If reserved data is modified externally using c_str()/&str[x]
83+
// this function will allow the contents to be validated and ready
84+
// for use with the String functionality.
85+
86+
// If remainder is true, only the reserved buffer is validated (capacity - len bytes)
87+
// If remainder is false, the entire buffer is re-validated.
88+
unsigned int validate( bool remainder = false );
89+
8190
inline unsigned int length(void) const {return len;}
8291

8392
// creates a copy of the assigned value. if the value is null or

0 commit comments

Comments
 (0)