topazproject / topaz

A high performance ruby, written in RPython
topazruby.com
BSD 3-Clause "New" or "Revised" License
1k stars 84 forks source link

String#tr causes AssertionError #860

Closed mame closed 7 years ago

mame commented 8 years ago

Hello,

String#tr fails with AssertionError when the second argument is "=".

$ ~/opt/topaz/bin/topaz -v -e 'p "x".tr("x", "-")'
topaz (ruby-1.9.3p125) (git rev 019daf0) [x86_64-linux]
RPython traceback:
  File "topaz_objects_functionobject.c", line 1053, in W_BuiltinFunction_call
  File "topaz_gateway_1.c", line 54303, in method_tr
  File "topaz_objects_stringobject.c", line 11715, in W_StringObject_method_tr
  File "topaz_objects_stringobject.c", line 31235, in W_StringObject_tr_trans
  File "topaz_objects_stringobject.c", line 38563, in create_trans_table
  File "topaz_objects_stringobject.c", line 40141, in expand_trans_str
Fatal RPython error: AssertionError
Aborted (core dumped)

MRI works like:

$ ruby -ve 'p "x".tr("x", "-")'
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-linux]
"-"

Thank you,

refi64 commented 8 years ago

Looking at expand_trans_str, would changing the for loop to:

if i < len(source):
            char = source[i]
        # Moved the assertion to this conditional:
        if char == "-" and 0 < i < len(source) - 1:
            # expand the range
            range_beg = ord(source[i - 1])
            range_end = ord(source[i + 1])
            for j in range(range_beg + 1, range_end - 1):
                expanded_source.append(chr(j))
        elif char:
            expanded_source.append(char[0])

fix this?