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

Str.rindex returns error messages with missing or incorrect information #5659

Open
p6rt opened this issue Sep 11, 2016 · 7 comments
Open

Str.rindex returns error messages with missing or incorrect information #5659

p6rt opened this issue Sep 11, 2016 · 7 comments

Comments

@p6rt
Copy link

p6rt commented Sep 11, 2016

Migrated from rt.perl.org#129248 (status was 'open')

Searchable as RT129248$

@p6rt
Copy link
Author

p6rt commented Sep 11, 2016

From @dogbert17

# Tested with

dogbert@​dogbert-VirtualBox ~/.rakudobrew/moar-nom $ perl6 -v This is Rakudo
version 2016.08.1-155-gccecbfe built on MoarVM version 2016.08-43-g3d04391
implementing Perl 6.c.

# the first problem

dogbert@​dogbert-VirtualBox ~/.rakudobrew/moar-nom $ perl6 -e 'say
"1234".rindex("2", -1)'
Position in rindex out of range. Is​: -1, should be in 0..4
  in block <unit> at -e line 1

Actually thrown at​:
  in block <unit> at -e line 1

# the position given *IS* out of range but the suggestion given, i.e.
"should be in 0..4" is incorrect since the string only contains four chars #
let us test the suggestion and use 4 as the position

dogbert@​dogbert-VirtualBox ~/.rakudobrew/moar-nom $ perl6 -e 'say
"1234".rindex("2", 4)'
index start offset out of range
  in block <unit> at -e line 1

# this also failed but this time we got no extra information, i.e. "Position
in rindex out of range. Bla bla"
# according to timo this could possibly point to a problem in MoarVM

See http://irclog.perlgeek.de/perl6/2016-09-10#i_13185974

/dogbert17

@p6rt
Copy link
Author

p6rt commented Sep 11, 2016

From @lizmat

Oddly enough, index() does *not* have this issue​:

$ 6 'say "1234".index("2", 4)’
Nil

Not sure that’s a bug either.

On 11 Sep 2016, at 16​:58, Jan-Olof Hendig (via RT) <perl6-bugs-followup@​perl.org> wrote​:

# New Ticket Created by Jan-Olof Hendig
# Please include the string​: [perl #​129248]
# in the subject line of all future correspondence about this issue.
# <URL​: https://rt-archive.perl.org/perl6/Ticket/Display.html?id=129248 >

# Tested with

dogbert@​dogbert-VirtualBox ~/.rakudobrew/moar-nom $ perl6 -v This is Rakudo
version 2016.08.1-155-gccecbfe built on MoarVM version 2016.08-43-g3d04391
implementing Perl 6.c.

# the first problem

dogbert@​dogbert-VirtualBox ~/.rakudobrew/moar-nom $ perl6 -e 'say
"1234".rindex("2", -1)'
Position in rindex out of range. Is​: -1, should be in 0..4
in block <unit> at -e line 1

Actually thrown at​:
in block <unit> at -e line 1

# the position given *IS* out of range but the suggestion given, i.e.
"should be in 0..4" is incorrect since the string only contains four chars #
let us test the suggestion and use 4 as the position

dogbert@​dogbert-VirtualBox ~/.rakudobrew/moar-nom $ perl6 -e 'say
"1234".rindex("2", 4)'
index start offset out of range
in block <unit> at -e line 1

# this also failed but this time we got no extra information, i.e. "Position
in rindex out of range. Bla bla"
# according to timo this could possibly point to a problem in MoarVM

See http://irclog.perlgeek.de/perl6/2016-09-10#i_13185974

/dogbert17

@p6rt
Copy link
Author

p6rt commented Sep 11, 2016

The RT System itself - Status changed from 'new' to 'open'

@p6rt
Copy link
Author

p6rt commented Sep 12, 2016

From @dogbert17

They do indeed behave somewhat inconsistently when the position is too high. Consistency, and the off by one error, returns when using -1 though.

dogbert@​dogbert-VirtualBox ~/repos/doc $ perl6 -e 'say "1234".index("2", -1)'
Position in index out of range. Is​: -1, should be in 0..4
  in block <unit> at -e line 1

Actually thrown at​:
  in block <unit> at -e line 1

-----Original Message-----
From​: Elizabeth Mattijsen via RT [mailto​:perl6-bugs-followup@​perl.org]
Sent​: den 11 september 2016 21​:38
To​: jan-olof.hendig@​bredband.net
Subject​: Re​: [perl #​129248] [BUG] Str.rindex returns error messages with missing or incorrect information

Oddly enough, index() does *not* have this issue​:

$ 6 'say "1234".index("2", 4)’
Nil

Not sure that’s a bug either.

On 11 Sep 2016, at 16​:58, Jan-Olof Hendig (via RT) <perl6-bugs-followup@​perl.org> wrote​:

# New Ticket Created by Jan-Olof Hendig # Please include the string​:
[perl #​129248] # in the subject line of all future correspondence
about this issue.
# <URL​: https://rt-archive.perl.org/perl6/Ticket/Display.html?id=129248 >

# Tested with

dogbert@​dogbert-VirtualBox ~/.rakudobrew/moar-nom $ perl6 -v This is
Rakudo version 2016.08.1-155-gccecbfe built on MoarVM version
2016.08-43-g3d04391 implementing Perl 6.c.

# the first problem

dogbert@​dogbert-VirtualBox ~/.rakudobrew/moar-nom $ perl6 -e 'say
"1234".rindex("2", -1)'
Position in rindex out of range. Is​: -1, should be in 0..4 in block
<unit> at -e line 1

Actually thrown at​:
in block <unit> at -e line 1

# the position given *IS* out of range but the suggestion given, i.e.
"should be in 0..4" is incorrect since the string only contains four
chars # let us test the suggestion and use 4 as the position

dogbert@​dogbert-VirtualBox ~/.rakudobrew/moar-nom $ perl6 -e 'say
"1234".rindex("2", 4)'
index start offset out of range
in block <unit> at -e line 1

# this also failed but this time we got no extra information, i.e.
"Position in rindex out of range. Bla bla"
# according to timo this could possibly point to a problem in MoarVM

See http://irclog.perlgeek.de/perl6/2016-09-10#i_13185974

/dogbert17

@p6rt
Copy link
Author

p6rt commented Sep 20, 2016

From @MasterDuke17

On Mon Sep 12 13​:56​:24 2016, jan-olof.hendig@​bredband.net wrote​:

They do indeed behave somewhat inconsistently when the position is too
high. Consistency, and the off by one error, returns when using -1
though.

dogbert@​dogbert-VirtualBox ~/repos/doc $ perl6 -e 'say
"1234".index("2", -1)'
Position in index out of range. Is​: -1, should be in 0..4
in block <unit> at -e line 1

Actually thrown at​:
in block <unit> at -e line 1

-----Original Message-----
From​: Elizabeth Mattijsen via RT [mailto​:perl6-bugs-
followup@​perl.org]
Sent​: den 11 september 2016 21​:38
To​: jan-olof.hendig@​bredband.net
Subject​: Re​: [perl #​129248] [BUG] Str.rindex returns error messages
with missing or incorrect information

Oddly enough, index() does *not* have this issue​:

$ 6 'say "1234".index("2", 4)’
Nil

Not sure that’s a bug either.

On 11 Sep 2016, at 16​:58, Jan-Olof Hendig (via RT) <perl6-bugs-
followup@​perl.org> wrote​:

# New Ticket Created by Jan-Olof Hendig # Please include the string​:
[perl #​129248] # in the subject line of all future correspondence
about this issue.
# <URL​: https://rt-archive.perl.org/perl6/Ticket/Display.html?id=129248 >

# Tested with

dogbert@​dogbert-VirtualBox ~/.rakudobrew/moar-nom $ perl6 -v This is
Rakudo version 2016.08.1-155-gccecbfe built on MoarVM version
2016.08-43-g3d04391 implementing Perl 6.c.

# the first problem

dogbert@​dogbert-VirtualBox ~/.rakudobrew/moar-nom $ perl6 -e 'say
"1234".rindex("2", -1)'
Position in rindex out of range. Is​: -1, should be in 0..4 in block
<unit> at -e line 1

Actually thrown at​:
in block <unit> at -e line 1

# the position given *IS* out of range but the suggestion given, i.e.
"should be in 0..4" is incorrect since the string only contains four
chars # let us test the suggestion and use 4 as the position

dogbert@​dogbert-VirtualBox ~/.rakudobrew/moar-nom $ perl6 -e 'say
"1234".rindex("2", 4)'
index start offset out of range
in block <unit> at -e line 1

# this also failed but this time we got no extra information, i.e.
"Position in rindex out of range. Bla bla"
# according to timo this could possibly point to a problem in MoarVM

See http://irclog.perlgeek.de/perl6/2016-09-10#i_13185974

/dogbert17

Roast has a test for index and rindex that implies the 1..4 range in this bug's example is correct.
In S32-str/index.t is this test​: is(index("Hello", "", 5), 5, "Substr is empty, pos at end of str");
In S32-str/rindex.t is this test​: is(rindex("Hello", "", 5), 5, "Substr is empty, pos at end of str");

To expand on the inconsistency between index and rindex, "1234".index("", 5) and "1234".index("2", 5) both return Nil, and so does "1234".rindex("", 5), but "1234".rindex("2", 5) fails with "index start offset out of range".

This inconsistency is passed through from nqp, where nqp​::index("1234", "", 5) and nqp​::index("1234", "2", 5) both return -1, but nqp​::rindex("1234", "", 5) returns -1 and nqp​::rindex("1234", "2", 5) fails with "index start offset out of range".

And the real source of the inconsistency is in MoarVM like timotimo said in the chat log. MVM_string_index_from_end in src/string.ops.c has​:
if (start < 0 || start >= hgraphs)
  /* maybe return -1 instead? */
  MVM_exception_throw_adhoc(tc, "index start offset out of range");

but MVM_string_index has​:
if (start < 0 || start >= hgraphs)
  return -1;

What should the Perl 6 behavior be? Return Nil when it's a little bit out of range and a Failure if it's a lot out of range? Or return either Nil or Failure if it's at all out of range?

@p6rt
Copy link
Author

p6rt commented Sep 22, 2016

From @MasterDuke17

On Mon Sep 19 23​:30​:20 2016, ddgreen@​gmail.com wrote​:

On Mon Sep 12 13​:56​:24 2016, jan-olof.hendig@​bredband.net wrote​:

They do indeed behave somewhat inconsistently when the position is
too
high. Consistency, and the off by one error, returns when using -1
though.

dogbert@​dogbert-VirtualBox ~/repos/doc $ perl6 -e 'say
"1234".index("2", -1)'
Position in index out of range. Is​: -1, should be in 0..4
in block <unit> at -e line 1

Actually thrown at​:
in block <unit> at -e line 1

-----Original Message-----
From​: Elizabeth Mattijsen via RT [mailto​:perl6-bugs-
followup@​perl.org]
Sent​: den 11 september 2016 21​:38
To​: jan-olof.hendig@​bredband.net
Subject​: Re​: [perl #​129248] [BUG] Str.rindex returns error messages
with missing or incorrect information

Oddly enough, index() does *not* have this issue​:

$ 6 'say "1234".index("2", 4)’
Nil

Not sure that’s a bug either.

On 11 Sep 2016, at 16​:58, Jan-Olof Hendig (via RT) <perl6-bugs-
followup@​perl.org> wrote​:

# New Ticket Created by Jan-Olof Hendig # Please include the
string​:
[perl #​129248] # in the subject line of all future correspondence
about this issue.
# <URL​: https://rt-archive.perl.org/perl6/Ticket/Display.html?id=129248 >

# Tested with

dogbert@​dogbert-VirtualBox ~/.rakudobrew/moar-nom $ perl6 -v This
is
Rakudo version 2016.08.1-155-gccecbfe built on MoarVM version
2016.08-43-g3d04391 implementing Perl 6.c.

# the first problem

dogbert@​dogbert-VirtualBox ~/.rakudobrew/moar-nom $ perl6 -e 'say
"1234".rindex("2", -1)'
Position in rindex out of range. Is​: -1, should be in 0..4 in
block
<unit> at -e line 1

Actually thrown at​:
in block <unit> at -e line 1

# the position given *IS* out of range but the suggestion given,
i.e.
"should be in 0..4" is incorrect since the string only contains
four
chars # let us test the suggestion and use 4 as the position

dogbert@​dogbert-VirtualBox ~/.rakudobrew/moar-nom $ perl6 -e 'say
"1234".rindex("2", 4)'
index start offset out of range
in block <unit> at -e line 1

# this also failed but this time we got no extra information, i.e.
"Position in rindex out of range. Bla bla"
# according to timo this could possibly point to a problem in
MoarVM

See http://irclog.perlgeek.de/perl6/2016-09-10#i_13185974

/dogbert17

Roast has a test for index and rindex that implies the 1..4 range in
this bug's example is correct.
In S32-str/index.t is this test​: is(index("Hello", "", 5), 5, "Substr
is empty, pos at end of str");
In S32-str/rindex.t is this test​: is(rindex("Hello", "", 5), 5,
"Substr is empty, pos at end of str");

To expand on the inconsistency between index and rindex,
"1234".index("", 5) and "1234".index("2", 5) both return Nil, and so
does "1234".rindex("", 5), but "1234".rindex("2", 5) fails with "index
start offset out of range".

This inconsistency is passed through from nqp, where
nqp​::index("1234", "", 5) and nqp​::index("1234", "2", 5) both return
-1, but nqp​::rindex("1234", "", 5) returns -1 and nqp​::rindex("1234",
"2", 5) fails with "index start offset out of range".

And the real source of the inconsistency is in MoarVM like timotimo
said in the chat log. MVM_string_index_from_end in src/string.ops.c
has​:
if (start < 0 || start >= hgraphs)
/* maybe return -1 instead? */
MVM_exception_throw_adhoc(tc, "index start offset out of range");

but MVM_string_index has​:
if (start < 0 || start >= hgraphs)
return -1;

What should the Perl 6 behavior be? Return Nil when it's a little bit
out of range and a Failure if it's a lot out of range? Or return
either Nil or Failure if it's at all out of range?

Adding [@​LARRY] per http://irclog.perlgeek.de/perl6-dev/2016-09-22#i_13254766

@p6rt
Copy link
Author

p6rt commented Sep 28, 2016

From @MasterDuke17

On Thu Sep 22 04​:39​:30 2016, ddgreen@​gmail.com wrote​:

On Mon Sep 19 23​:30​:20 2016, ddgreen@​gmail.com wrote​:

On Mon Sep 12 13​:56​:24 2016, jan-olof.hendig@​bredband.net wrote​:

They do indeed behave somewhat inconsistently when the position is
too
high. Consistency, and the off by one error, returns when using -1
though.

dogbert@​dogbert-VirtualBox ~/repos/doc $ perl6 -e 'say
"1234".index("2", -1)'
Position in index out of range. Is​: -1, should be in 0..4
in block <unit> at -e line 1

Actually thrown at​:
in block <unit> at -e line 1

-----Original Message-----
From​: Elizabeth Mattijsen via RT [mailto​:perl6-bugs-
followup@​perl.org]
Sent​: den 11 september 2016 21​:38
To​: jan-olof.hendig@​bredband.net
Subject​: Re​: [perl #​129248] [BUG] Str.rindex returns error messages
with missing or incorrect information

Oddly enough, index() does *not* have this issue​:

$ 6 'say "1234".index("2", 4)’
Nil

Not sure that’s a bug either.

On 11 Sep 2016, at 16​:58, Jan-Olof Hendig (via RT) <perl6-bugs-
followup@​perl.org> wrote​:

# New Ticket Created by Jan-Olof Hendig # Please include the
string​:
[perl #​129248] # in the subject line of all future correspondence
about this issue.
# <URL​: https://rt-archive.perl.org/perl6/Ticket/Display.html?id=129248 >

# Tested with

dogbert@​dogbert-VirtualBox ~/.rakudobrew/moar-nom $ perl6 -v This
is
Rakudo version 2016.08.1-155-gccecbfe built on MoarVM version
2016.08-43-g3d04391 implementing Perl 6.c.

# the first problem

dogbert@​dogbert-VirtualBox ~/.rakudobrew/moar-nom $ perl6 -e 'say
"1234".rindex("2", -1)'
Position in rindex out of range. Is​: -1, should be in 0..4 in
block
<unit> at -e line 1

Actually thrown at​:
in block <unit> at -e line 1

# the position given *IS* out of range but the suggestion given,
i.e.
"should be in 0..4" is incorrect since the string only contains
four
chars # let us test the suggestion and use 4 as the position

dogbert@​dogbert-VirtualBox ~/.rakudobrew/moar-nom $ perl6 -e 'say
"1234".rindex("2", 4)'
index start offset out of range
in block <unit> at -e line 1

# this also failed but this time we got no extra information,
i.e.
"Position in rindex out of range. Bla bla"
# according to timo this could possibly point to a problem in
MoarVM

See http://irclog.perlgeek.de/perl6/2016-09-10#i_13185974

/dogbert17

Roast has a test for index and rindex that implies the 1..4 range in
this bug's example is correct.
In S32-str/index.t is this test​: is(index("Hello", "", 5), 5, "Substr
is empty, pos at end of str");
In S32-str/rindex.t is this test​: is(rindex("Hello", "", 5), 5,
"Substr is empty, pos at end of str");

To expand on the inconsistency between index and rindex,
"1234".index("", 5) and "1234".index("2", 5) both return Nil, and so
does "1234".rindex("", 5), but "1234".rindex("2", 5) fails with
"index
start offset out of range".

This inconsistency is passed through from nqp, where
nqp​::index("1234", "", 5) and nqp​::index("1234", "2", 5) both return
-1, but nqp​::rindex("1234", "", 5) returns -1 and nqp​::rindex("1234",
"2", 5) fails with "index start offset out of range".

And the real source of the inconsistency is in MoarVM like timotimo
said in the chat log. MVM_string_index_from_end in src/string.ops.c
has​:
if (start < 0 || start >= hgraphs)
/* maybe return -1 instead? */
MVM_exception_throw_adhoc(tc, "index start offset out of range");

but MVM_string_index has​:
if (start < 0 || start >= hgraphs)
return -1;

What should the Perl 6 behavior be? Return Nil when it's a little bit
out of range and a Failure if it's a lot out of range? Or return
either Nil or Failure if it's at all out of range?

Adding [@​LARRY] per http://irclog.perlgeek.de/perl6-dev/2016-09-
22#i_13254766

Adding comments from a conversation with TimToady (http://irclog.perlgeek.de/perl6/2016-09-26#i_13285140)

16​:28 TimToady_ MasterDuke_​: rindex is not symmetrical with index, since positions are measured from the start of the needle, not the end, so I'd say the rindex("1234","2",4) should fail indicating the correct range is 0..3, since the actual allowed range is not 0 .. 4 but rather 0 .. 4 - $needle.chars
16​:29 TimToady_ MasterDuke_​: though I suppose we could just return Nil up through 4 just for convenience
16​:30 TimToady_ MasterDuke_​: or we could say that if they give us a position within the string that is too long to match, we just force it to the first position that could match
16​:31 TimToady_ such that rindex("1234","2",4) would simply return 1, and rindex("1234","2",5) would report failure
16​:31 and then it wouldn't matter if it said 0..3 or 0..4
16​:33 TimToady_ MasterDuke_​: so I think the last thing I said is probably the best approach

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

No branches or pull requests

1 participant