Skip to content

int::from_str("-1") == -253 and other bugs #1102

Closed
@mbrubeck

Description

@mbrubeck

int::from_str returns bad results for strings that start with "-" signs, because it adjust its character position in the wrong direction. Examples:

assert(int::from_str("-1") == -253);
assert(int::from_str("-3") == -253);
assert(int::from_str("-10") == -2531);
assert(int::from_str("-x") == -253);
assert(int::from_str("-0x") == -2530);

It also returns bad results for any strings with non-numeric characters:

assert(int::from_str("x") == 72);

In addition, int::parse_buf is broken for any radix greater than 10:

assert(int::parse_buf(['a' as u8], 16u) == 49);

Most or all of the same bugs apply to the uint module too.

While we're here, would it be better for these methods to return option::t instead of failing on bad input?

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