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

CStr should have a function like from_bytes_with_nul() that reads until the first '\0' #49107

Closed
Boscop opened this issue Mar 17, 2018 · 7 comments
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@Boscop
Copy link

Boscop commented Mar 17, 2018

When dealing with FFI (fixed char buffers in file formats that represent strings and passing fixed char buffers to C code to write chars to them up to a given max len) I often find myself doing this:

let end = s.iter().position(|&b| b == 0).map_or(0, |i| i + 1);
CStr::from_bytes_with_nul(&s[..end])

Because if I pass the whole slice to it, it returns a FromBytesWithNulError.
There should be a function that combines both lines and read until the first '\0' byte in the slice.

Something like:

CStr::from_bytes_until_nul(slice)

And yes, I did write my own function for my use case but I find myself copying these two lines around into different projects dealing with FFI and I think it would make sense to have this (arguably) "basic functionality" in std's CStr :)

@kennytm
Copy link
Member

kennytm commented Mar 17, 2018

This has been proposed before as #20475, and closed because this is already supported by the package c_fixed_string.

@kennytm kennytm added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. C-feature-request Category: A feature request, i.e: not implemented / a PR. labels Mar 17, 2018
@GuillaumeGomez
Copy link
Member

That's a bit sad and problematic that such a basic functionality isn't implemented on CStr directly...

@kanekv
Copy link

kanekv commented Aug 16, 2018

Why does from_bytes_with_nul accept &[u8], whereas char by default is signed?

@czipperz
Copy link
Contributor

@kanekv are you talking about in C? Because in C char's sign is undefined. We use u8 because we treat them as bytes -- which are treated as unsigned 8 bit numbers.

@patrickelectric
Copy link
Contributor

@kennytm is there a possibility to accept such function inside CStr and not rely on a package for that ?
This is really a basic thing to have for any person that is dealing with fixed size char arrays, I totally agree with @GuillaumeGomez

@Boscop
Copy link
Author

Boscop commented Feb 13, 2021

@patrickelectric I agree. The reason that some functionality is offered by a thirdparty crate is not a valid reason to not have it in std.
Many functions in std could theoretically be factored out into other crates, but they are part of std for good reasons!
E.g. the boolinator crate provides a function to map a bool to an Option, but bool::then was still added to std, and for the same reason, the proposed CStr::from_bytes_until_nul should be added to std. :)

@tgross35
Copy link
Contributor

tgross35 commented Aug 3, 2023

cstr_from_bytes_until_nul is now stable and available since 1.69.0: #107429

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

8 participants