-
-
Notifications
You must be signed in to change notification settings - Fork 7k
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
Conversation
This seems like a useful addition. But is Since the function essentially updates the length to match the content, assuming a nul-terminated string, |
As far as I can see void setup() {
String result;
result.reserve(32);
result.c_str()[0] = 'A';
Serial.print(result);
}
void loop() {
} gives an:
|
@cmaglie, that's correct, but simply doing So, I can see that this new method can be useful in some cases (though probably only in unusual cases, that touch the edges of the String object interface), but I still do not believe |
Can we discuss some names, I'll gladly change it to have this feature in. This finds its usefulness when some task needs to add data to a string. It can use up the available buffer in an efficient single step. For instance, but not limited to passing the String to functions that only accept c-string whether it be strconcat, or a function to read an interface.
As for the name, Validate is a verb describing an action. Whereas 'reinitialized' or 'validated' is past tense. As in, "Please validate my String" as opposed to "I need the reinitialized Length". As a validity test, I'd expect to see something like
If you use And there are three ways identified in this issue where a null can be entered in the middle of a string.
Cheers |
Just had another question where this would be very beneficial. Without a feature like this, the String library still needs to be used in conjunction with char arrays, and kind of defeats its 'easy to use' functionality. |
IMHO, using Regarding the name, I agree that However, I do not think the concept of "validating" matches the operation here. Validating, to me, is to check whether something is valid. Validating by itself doesn't change anything about the subject. Even if you'd assume validating means making the string valid, I'm not convinced that the string is invalid when there is an embedded NUL and only valid when the length matches the position of the first NUL. And even if you'd accept that definition of validity and that |
Looking at this again, I still think that changing the string buffer through the Moreover, how will you check for boundaries? c_str() returns the pointer to the String buffer but not the current buffer size that may arbitrarily change (depending on the String class implementation). |
It is a relatively common method for doing efficient bulk inserts. And allows a
When using Besides, if String s = "A String!";
char *c = &s[0]; In the code above, there is no trickery as the pointer is not char arr[] = "A String\0Another String";
char *first = &arr[0];
char *second = &arr[8]; Essentially the Take a very common example, i.e. A beginner wants to use something that works with c-strings, not Strings: If a user is required to modify code they have found, simply so they can use it with their existing code (String use) then it significantly lessens the value of the Arduino API, and essentially, what it was designed for. This is especially important when considering the target demographic: From the introduction
Now I understand that adding an additional function is an increase in complexity. However, it does not have to be used, and will come with documentation and examples where users expect to find them (Arduino reference). This is opposed to forcing the requirement to learn about the very topics the users avoided by utilizing the String class in the first place. And is not covered in the Arduino reference. |
As pointed out in the comments, the constness of the pointer returned by @Chris--A I'd close the issue here, if you still want to see this in the core please reopen in https://github.com/arduino/ArduinoCore-api , thanks! |
As people can access the internal buffer using various provided methods:
str.c_str()
,&str[0]
It gives reason to believe that someone will modify the string externally, or provide these pointers to functions that do. It makes sense as a user can reserve memory usingstr.reserve()
, or simply overwrite existing data already in the pointer.Once this happens the String can reflect incorrect values despite having valid data inside it.
Take for example, someone buffering a string to receive data provided by some source external to the library.
This problem is solved with this PR:
The function simply searches for the null character. There is one parameter called
remainder
which if set to:true
: the function only searches the unused buffer (capacity - len).false
: the entire buffer is re-validated.The default is to validate the entire buffer.
This seems important as the Print library uses the string's internal data to determine length, not the actual null character. If someone was to simply cut a string in half (`str[5] = '\0';) then functions using the null will perform differently to what is seen in debugging (may hinder debugging as Serial.print() is affected).