Skip to content

New feature: validate function for String class. #3096

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions hardware/arduino/avr/cores/arduino/WString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 9 additions & 0 deletions hardware/arduino/avr/cores/arduino/WString.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions hardware/arduino/sam/cores/arduino/WString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 9 additions & 0 deletions hardware/arduino/sam/cores/arduino/WString.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down