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

Slices and printfs #37

Open
zhiayang opened this issue Oct 20, 2019 · 1 comment
Open

Slices and printfs #37

zhiayang opened this issue Oct 20, 2019 · 1 comment

Comments

@zhiayang
Copy link
Member

Currently the compiler automatically converts a str to its pointer when passing it to a C-style vararg function (eg. printf) (see

// auto-convert strings and char slices into char* when passing to va_args
)

Of course this kinda defeats the purpose, since slices are not necessarily null-terminated. The proposed alternative (which I'm proposing now) is to pass the entire struct to printf, which will cause it to get both the length and the pointer.

(aside: the layout of str is like this: struct str { len: i64; ptr: &i8 })

Then, we would need to call printf with %.*s instead of just %s.

@zhiayang
Copy link
Member Author

zhiayang commented Oct 20, 2019

I lied, the layout of a slice currently is struct str { ptr: &i8; len: i64 } which needs to change.

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

No branches or pull requests

1 participant