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

Bringing OsStr and CStr up to par with str #900

Open
codyps opened this issue Feb 24, 2015 · 8 comments
Open

Bringing OsStr and CStr up to par with str #900

codyps opened this issue Feb 24, 2015 · 8 comments
Labels
T-libs-api Relevant to the library API team, which will review and decide on the RFC.

Comments

@codyps
Copy link

codyps commented Feb 24, 2015

Redirected from rust-lang/rust#22741

Using starts_with() as an example:
Right now one can do this on OsStr by:

  • requiring the OsStr to be utf-8 (and converting it to str/String)
  • Or implimenting a unix version using as_bytes(), and a windows version using encode_wide()

Unfortunately, the lack of a common method for iteration over elements of the OsStr means that any out-of-std implimentation of these will be un-happily platform specific.

@ben0x539
Copy link

ben0x539 commented Apr 23, 2016

It's not clear to me why we can't expose the underlying representation with some sort of .as_bytes(). Is it just because we assume people are gonna assume that on Windows that means it's the active codepage and not WTF-8?

cc @eddyb

@dtolnay dtolnay changed the title OsStr (and related) are missing str/String/slice like methods such as starts_with() Bringing OsStr and CStr up to par with str Nov 17, 2017
@dtolnay
Copy link
Member

dtolnay commented Nov 17, 2017

#1876 discusses some ideas for how slicing could work for CStr.

@dtolnay
Copy link
Member

dtolnay commented Nov 17, 2017

See also:

@Centril Centril added the T-libs-api Relevant to the library API team, which will review and decide on the RFC. label Feb 23, 2018
@dylni
Copy link

dylni commented Nov 29, 2019

For everyone still waiting on this issue, I created OsStr Bytes, which should make OsStr and OsString much easier to use.

@BurntSushi
Copy link
Member

@dylni I realize you're intentionally invoking UB in that crate, but it's still UB and I therefore wouldn't recommend that approach: https://github.com/dylni/os_str_bytes/blob/ff8a9ed5e7d50b9ff63ea20bfd460a7f481340c1/src/windows.rs#L16-L28

bstr does as much as is possible with as little work as possible without breaking abstraction boundaries or invoking UB: https://docs.rs/bstr/0.2.8/bstr/#file-paths-and-os-strings

@dylni
Copy link

dylni commented Dec 4, 2019

@BurntSushi I completely agree and was hoping to hear some opinions on this, which is why I put it on the front page of the documentation. My plan is to come up with something better before a 1.0 release, but I haven't yet decided what the best option would be. However, I don't agree with the approach of not being able to handle some valid arguments on Windows, which is why I created the crate.

I may just end up copying next_code_point() from the standard library, but I haven't decided yet. The problem with this is that bytes could be mangled if the standard library implementation changes, which is why I've avoided it so far.

@dylni
Copy link

dylni commented Dec 7, 2019

@BurntSushi That method is no longer called.

@mzr
Copy link

mzr commented Jun 23, 2022

About starts_with, how about

impl OsStr {
   fn starts_with(&self, prefix: &OsStr) -> bool
}

?

This way I won't have to handle Option like osstr.to_str().map(|x| x.starts_with("prefix")).unwrap_or(false);, but I would be able to do osstr.starts_with(String::from("prefix").into()).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-libs-api Relevant to the library API team, which will review and decide on the RFC.
Projects
None yet
Development

No branches or pull requests

7 participants