Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unify behaviour of nqp::substr on all backends #533

Open
usev6 opened this issue Apr 27, 2019 · 0 comments
Open

Unify behaviour of nqp::substr on all backends #533

usev6 opened this issue Apr 27, 2019 · 0 comments

Comments

@usev6
Copy link
Contributor

usev6 commented Apr 27, 2019

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.

Another for the record: This is the current implementation on MoarVM: https://github.com/MoarVM/MoarVM/blob/423a9cc7d9/src/strings/ops.c#L706

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant