You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is the current behaviour of nqp::substr on the different backends with regard to negative values for $position and $length:
my $s := 'foobar'
Code
Result on MoarVM
nqp::substr($s, 4, -1)
'ar'
nqp::substr($s, 4, -2)
dies: Substring length (-2) cannot be negative
nqp::substr($s, -4)
'obar'
nqp::substr($s, -4, 2)
'ob'
nqp::substr($s, -4, -1)
'obar'
nqp::substr($s, -4, -2)
dies: Substring length (-2) cannot be negative
nqp::substr($s, -7, 1)
''
nqp::substr($s, -8, 1)
dies: Substring end (-1) cannot be less than 0
nqp::substr($s, -8)
'foobar'
Code
Result on JVM
nqp::substr($s, 4, -1)
dies: StringIndexOutOfBoundsException: String index out of range: -1
nqp::substr($s, 4, -2)
dies: StringIndexOutOfBoundsException: String index out of range: -2
nqp::substr($s, -4)
'obar'
nqp::substr($s, -4, 2)
dies: StringIndexOutOfBoundsException: String index out of range: -4
nqp::substr($s, -4, -1)
dies: StringIndexOutOfBoundsException: String index out of range: -4
nqp::substr($s, -4, -2)
dies: StringIndexOutOfBoundsException: String index out of range: -4
nqp::substr($s, -7, 1)
dies: StringIndexOutOfBoundsException: String index out of range: -7
nqp::substr($s, -8, 1)
dies: StringIndexOutOfBoundsException: String index out of range: -8
nqp::substr($s, -8)
dies: StringIndexOutOfBoundsException: String index out of range: -2
Code
Result on JS
nqp::substr($s, 4, -1)
''
nqp::substr($s, 4, -2)
''
nqp::substr($s, -4)
'obar'
nqp::substr($s, -4, 2)
'ob'
nqp::substr($s, -4, -1)
''
nqp::substr($s, -4, -2)
''
nqp::substr($s, -7, 1)
'f'
nqp::substr($s, -8, 1)
'f'
nqp::substr($s, -8)
'foobar'
IMHO the behaviour should be identical. This begs the question: What behaviour do we want? For Rakudo we don't allow negative values, but allow '*-2' to index relative to the end.
For the records: This is the output from Perl 5's substr.
Code
Result from Perl 5
substr($s, 4, -1)
'a'
substr($s, 4, -2)
''
substr($s, -4)
'obar'
substr($s, -4, 2)
'ob'
substr($s, -4, -1)
'oba'
substr($s, -4, -2)
'ob'
substr($s, -7, 1)
''
substr($s, -8, 1)
''
substr($s, -8)
'foobar'
If the semantics of nqp::substr are changed for MoarVM, all uses within NQP and Rakudo need to be reviewed. There might be some ecosystem fallout, too.
This is the current behaviour of nqp::substr on the different backends with regard to negative values for
$position
and$length
:my $s := 'foobar'
IMHO the behaviour should be identical. This begs the question: What behaviour do we want? For Rakudo we don't allow negative values, but allow '*-2' to index relative to the end.
For the records: This is the output from Perl 5's substr.
If the semantics of nqp::substr are changed for MoarVM, all uses within NQP and Rakudo need to be reviewed. There might be some ecosystem fallout, too.
Another for the record: This is the current implementation on MoarVM: https://github.com/MoarVM/MoarVM/blob/423a9cc7d9/src/strings/ops.c#L706
The text was updated successfully, but these errors were encountered: