Skip to content

Commit 8b1b532

Browse files
committed
Add validate function to String (SAM)
1 parent f290625 commit 8b1b532

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

hardware/arduino/sam/cores/arduino/WString.cpp

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

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

hardware/arduino/sam/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)