Skip to content

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

Closed
@mahkoh

Description

@mahkoh
  • 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())
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions