Exemple #1
0
def string_to_int(s, base=10):
    """Utility to converts a string to an integer.
    If base is 0, the proper base is guessed based on the leading
    characters of 's'.  Raises ParseStringError in case of error.
    Raises ParseStringOverflowError in case the result does not fit.
    """
    from rpython.rlib.rstring import (NumberStringParser,
                                      ParseStringOverflowError, strip_spaces)
    s = literal = strip_spaces(s)
    p = NumberStringParser(s, literal, base, 'int')
    base = p.base
    result = 0
    while True:
        digit = p.next_digit()
        if digit == -1:
            return result

        if p.sign == -1:
            digit = -digit

        try:
            result = ovfcheck(result * base)
            result = ovfcheck(result + digit)
        except OverflowError:
            raise ParseStringOverflowError(p)
Exemple #2
0
def string_to_int(s, base=10):
    """Utility to converts a string to an integer.
    If base is 0, the proper base is guessed based on the leading
    characters of 's'.  Raises ParseStringError in case of error.
    Raises ParseStringOverflowError in case the result does not fit.
    """
    from rpython.rlib.rstring import (
        NumberStringParser, ParseStringOverflowError, strip_spaces)
    s = literal = strip_spaces(s)
    p = NumberStringParser(s, literal, base, 'int')
    base = p.base
    result = 0
    while True:
        digit = p.next_digit()
        if digit == -1:
            return result

        if p.sign == -1:
            digit = -digit

        try:
            result = ovfcheck(result * base)
            result = ovfcheck(result + digit)
        except OverflowError:
            raise ParseStringOverflowError(p)
Exemple #3
0
def string_to_int(s, base=10, allow_underscores=False, no_implicit_octal=False):
    """Utility to converts a string to an integer.
    If base is 0, the proper base is guessed based on the leading
    characters of 's'.  Raises ParseStringError in case of error.
    Raises ParseStringOverflowError in case the result does not fit.
    """
    from rpython.rlib.rstring import (
        NumberStringParser, ParseStringOverflowError)
    p = NumberStringParser(s, s, base, 'int',
                           allow_underscores=allow_underscores,
                           no_implicit_octal=no_implicit_octal)
    base = p.base
    result = 0
    while True:
        digit = p.next_digit()
        if digit == -1:
            return result

        if p.sign == -1:
            digit = -digit

        try:
            result = ovfcheck(result * base)
            result = ovfcheck(result + digit)
        except OverflowError:
            raise ParseStringOverflowError(p)
Exemple #4
0
def string_to_int(s,
                  base=10,
                  allow_underscores=False,
                  no_implicit_octal=False):
    """Utility to converts a string to an integer.
    If base is 0, the proper base is guessed based on the leading
    characters of 's'.  Raises ParseStringError in case of error.
    Raises ParseStringOverflowError in case the result does not fit.
    """
    from rpython.rlib.rstring import (NumberStringParser,
                                      ParseStringOverflowError)

    if base == 10 and 0 < len(s) < OVF_DIGITS:
        # fast path for simple cases, just supporting (+/-)[0-9]* with not too
        # many digits
        start = 0
        sign = 1
        if s[0] == "-":
            start = 1
            sign = -1
        elif s[0] == "+":
            start = 1
        if start != len(s):
            result = 0
            for i in range(start, len(s)):
                char = s[i]
                value = ord(char) - ord('0')
                if 0 <= value <= 9:
                    result = result * 10 + value
                else:
                    # non digit char, let the NumberStringParser do the work
                    break
            else:
                return result * sign

    p = NumberStringParser(s,
                           s,
                           base,
                           'int',
                           allow_underscores=allow_underscores,
                           no_implicit_octal=no_implicit_octal)
    base = p.base
    result = 0
    while True:
        digit = p.next_digit()
        if digit == -1:
            return result

        if p.sign == -1:
            digit = -digit

        try:
            result = ovfcheck(result * base)
            result = ovfcheck(result + digit)
        except OverflowError:
            raise ParseStringOverflowError(p)
Exemple #5
0
 def test_from_numberstring_parser_no_implicit_octal(self):
     from rpython.rlib.rstring import NumberStringParser, ParseStringError
     s = "077777777777777777777777777777"
     parser = NumberStringParser(s, s, 0, "long", no_implicit_octal=True)
     py.test.raises(ParseStringError, rbigint._from_numberstring_parser,
                    parser)
     parser = NumberStringParser("000",
                                 "000",
                                 0,
                                 "long",
                                 no_implicit_octal=True)
     assert rbigint._from_numberstring_parser(parser).tolong() == 0
Exemple #6
0
def parse_integer(string, base=10):
    if base > 36:
        raise ValueError("Not enough digits to base")
    if base < 0:
        raise ValueError("Negative base")
    literal = string
    parser = NumberStringParser(string, literal, base, 'long')
    return Integer(rbigint._from_numberstring_parser(parser))
Exemple #7
0
 def test__from_numberstring_parser_rewind_bug(self):
     from rpython.rlib.rstring import NumberStringParser
     s = "-99"
     p = NumberStringParser(s, s, 10, 'int')
     assert p.sign == -1
     res = p.next_digit()
     assert res == 9
     res = p.next_digit()
     assert res == 9
     res = p.next_digit()
     assert res == -1
     p.rewind()
     res = p.next_digit()
     assert res == 9
     res = p.next_digit()
     assert res == 9
     res = p.next_digit()
     assert res == -1
Exemple #8
0
 def test_from_numberstring_parser(self):
     from rpython.rlib.rstring import NumberStringParser
     parser = NumberStringParser("1231231241", "1231231241", 10, "long")
     assert rbigint._from_numberstring_parser(parser).tolong() == 1231231241