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

\0 in a byte string literal does not encode NUL #348

Open
mneumann opened this issue Sep 8, 2022 · 5 comments
Open

\0 in a byte string literal does not encode NUL #348

mneumann opened this issue Sep 8, 2022 · 5 comments
Labels
complexity 1: simple This should be quite easy to pull off. kind: feature: language Adding a new language feature

Comments

@mneumann
Copy link
Contributor

mneumann commented Sep 8, 2022

Currently:

assert: b"foo\0_bar\0" == b"foo\x5c\x30_bar\x5c\x30"

Expected:

assert: b"foo\0_bar\0" == b"foo\x00_bar\x00"

Maybe there is good reason for this.

@mneumann
Copy link
Contributor Author

mneumann commented Sep 8, 2022

Might also apply to Strings.

@jemc
Copy link
Contributor

jemc commented Sep 8, 2022

Maybe there is good reason for this.

No, there's no reason apart from me just not knowing that \0 was a common escape sequence that people expect to be able to use.

I've never used the escape sequence \0 in another language - just to avoid me needing to search around a lot for what you already know, what language(s) are you used to using this escape sequence in?

I have no problem with us adding this escape sequence if it's common enough in other languages that people expect it.

@mneumann
Copy link
Contributor Author

mneumann commented Sep 8, 2022

\0 is used in quite some languages: C, C++, Ruby, Python, Zig, ...

You can roughly say, if \n is an escape sequence in that language, then \0 is.

I don't need \0 as escape sequence, as it's use is rather rare! But, I'd make everything that follows \ special, and if it is unknown, I'd rather emit a compiler error than encode it as is. Otherwise, some people will use \0 (like I did :) and if they don't have tests will wonder why they get two characters \ and 0 instead of NUL.

@jemc
Copy link
Contributor

jemc commented Sep 8, 2022

Given the large number of languages where it's present, I'm definitely okay with us adding \0 if it is desired.

I also agree with you that we should issue a compilation error when an unknown escape sequence is encountered, in a String or Bytes literal. I'll prepare a quick PR to add that as a compilation error.

jemc added a commit that referenced this issue Sep 8, 2022
This helps to prevent bugs wherein the user believes they have
correctly written an escape code.

Prior to this commit, invalid escape code were treated as if
they were literal characters, including the backslash character.

See discussion in #348
@jemc
Copy link
Contributor

jemc commented Sep 8, 2022

PR #353 adds a nice compilation error on invalid escape characters.

I'll leave this ticket open in case you want to do a PR to add \0 as a valid escape sequence.

@jemc jemc added kind: feature: language Adding a new language feature complexity 1: simple This should be quite easy to pull off. labels Sep 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
complexity 1: simple This should be quite easy to pull off. kind: feature: language Adding a new language feature
Projects
None yet
Development

No branches or pull requests

2 participants