Skip to content
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

Support printing constant strings containing nulls #5747

Closed
wants to merge 2 commits into from

Conversation

eric-wieser
Copy link
Contributor

@eric-wieser eric-wieser commented Dec 22, 2016

What's changed

  1. Serial.print("Hello""\0""World")
    • Before: prints Hello
    • After: prints Hello\0World
  2. Serial.print("Hello")
    • Before: invokes strlen
    • After: does not, is faster. Code size should be unchanged
  3. Serial.println("Hello")
    • Before: invokes strlen
    • After: does not, is faster. Slightly increased code size, due to inlining

Thoughts?

This only changes the behaviour of code like `Serial.print("Hello""\0""World")`, where writing the null byte is clearly intended.

This also gives a speed boost for all calls with constant strings
@matthijskooijman
Copy link
Collaborator

I'm not so sure if this is really a good idea, since now:

write("foo\0bar");

and

char *ptr = "foo\0bar";
write(ptr);

Will have different results and I'm not sure that's really a good idea.

Also, I think that:

char buf[10];
buf[0] = 'X';
buf[1] = '\0';
write(buf);

will now print 10 bytes, even though only 2 is intended. Since this sort of thing is likely to occur in sketches, this breaks compatibility.

Regarding handling of strings with embedded zeroes, you might be interested in #1936 which improves the String class to better handle them.

@PaulStoffregen
Copy link
Contributor

Thoughts?

Isn't this what write(buf, length) is supposed to do?

Ideally, both write() functions ought to take (const void *) type, but whether even that can be considered changeable at this late stage in Arduino's development is a good question?

@eric-wieser
Copy link
Contributor Author

I think that [...] will now print 10 bytes, even though only 2 is intended. Since this sort of thing is likely to occur in sketches, this breaks compatibility.

Yep, you're right - this is not an acceptable change

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants