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

Extend FromStr by from_{str,bytes}{,_initial} #16176

Closed
mahkoh opened this Issue Aug 1, 2014 · 3 comments

Comments

Projects
None yet
3 participants
@mahkoh
Copy link
Contributor

mahkoh commented Aug 1, 2014

  • Right now it is impossible to parse strings of the form [0-9]+.+ without practically rewriting the parser or using the correct libc function (strtol).
  • Most of the time the UTF-8 property is irrelevant. Most things parse bytes.
trait FromBytes<T> {
    /// Tries to convert the initial portion of `b` to `T` and returns the number of bytes
    /// consumed.
    fn from_bytes_initial(b: &[u8]) -> Option<(T,uint)>;

    /// Tries to convert `b` to `T`.
    fn from_bytes(b: &[u8]) -> Option<T> {
        match FromBytes::from_bytes_initial(b) {
            Some((t, n)) if n == b.len() => Some(t),
            _ => None,
        }
    }

    /// Tries to convert the initial portion of `s` to `T` and returns the number of bytes
    /// consumed.
    fn from_str_initial(s: &str) -> Option<(T,uint)> {
        FromBytes::from_bytes_initial(s.as_bytes())
    }

    /// Tries to convert `s` to `T`.
    fn from_str(s: &str) -> Option<T> {
        FromBytes::from_bytes(s.as_bytes())
    }
}
@aturon

This comment has been minimized.

Copy link
Member

aturon commented Aug 1, 2014

cc me

@huonw

This comment has been minimized.

Copy link
Member

huonw commented Sep 20, 2014

cc #6220 (at least, this sort of API is mentioned there and would be useful for it).

I don't know if want from_bytes to be the default so deeply, since things that do want to parse unicode are then required to remember to override everything, at the expense of a pile of from_utf8 calls (especially bad for something recursive). Maybe this doesn't matter much in practice.

@huonw huonw added I-ICE A-libs and removed I-ICE labels Sep 20, 2014

@mahkoh

This comment has been minimized.

Copy link
Contributor Author

mahkoh commented Nov 14, 2014

There are several places in the stdlib that already roll their own parsers because of this.

@mahkoh mahkoh closed this Apr 9, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.