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

.trans different behavior for Str Pairs versus List Pairs #5635

Open
p6rt opened this issue Sep 2, 2016 · 4 comments
Open

.trans different behavior for Str Pairs versus List Pairs #5635

p6rt opened this issue Sep 2, 2016 · 4 comments
Labels
RFC Request For Comments

Comments

@p6rt
Copy link

p6rt commented Sep 2, 2016

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

Searchable as RT129172$

@p6rt
Copy link
Author

p6rt commented Sep 2, 2016

From @zostay

From #perl6 today​:

21​:42​:01 <zostay> m​: say "test.foo".trans('.' => '​::');
21​:42​:01 <+camelia> rakudo-moar d2b115​: OUTPUT«test​:foo␤»
21​:42​:11 <zostay> m​: say "test.foo".trans(['.'] => ['​::']);
21​:42​:12 <+camelia> rakudo-moar d2b115​: OUTPUT«test​::foo␤»
21​:42​:16 <zostay> that seems like a bug

This appears to be documented as a feature in the spec tests, but is not
really documented on docs.perl6.org.

Assuming my quick read of the spec is correct, I propose either of the
following solutions, with the first getting my vote​:

1. Change the spec and make the behavior between the two calling styles
consistent so I don't have to remember this bit minutia about a subroutine
I use every once in a long while.

2. Fix the documentation on docs.perl6.org AND modify the Str => Str
variety of .trans so that any attempt to use multiple chars results in a
warning or you risk getting this bug report again by others in the future.
--
Sterling Hanenkamp
http://sterling.hanenkamp.com/stfl/
785-370-4454

@p6rt
Copy link
Author

p6rt commented Sep 2, 2016

From @lizmat

I would suggest 2, as the [] => [] way exists exactly for the reason that you think it’s a bug​: when you want to translate a single char into zero or more chars.

On 02 Sep 2016, at 04​:59, hanenkamp (via RT) <perl6-bugs-followup@​perl.org> wrote​:

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

From #perl6 today​:

21​:42​:01 <zostay> m​: say "test.foo".trans('.' => '​::');
21​:42​:01 <+camelia> rakudo-moar d2b115​: OUTPUT«test​:foo␤»
21​:42​:11 <zostay> m​: say "test.foo".trans(['.'] => ['​::']);
21​:42​:12 <+camelia> rakudo-moar d2b115​: OUTPUT«test​::foo␤»
21​:42​:16 <zostay> that seems like a bug

This appears to be documented as a feature in the spec tests, but is not
really documented on docs.perl6.org.

Assuming my quick read of the spec is correct, I propose either of the
following solutions, with the first getting my vote​:

1. Change the spec and make the behavior between the two calling styles
consistent so I don't have to remember this bit minutia about a subroutine
I use every once in a long while.

2. Fix the documentation on docs.perl6.org AND modify the Str => Str
variety of .trans so that any attempt to use multiple chars results in a
warning or you risk getting this bug report again by others in the future.
--
Sterling Hanenkamp
http://sterling.hanenkamp.com/stfl/
785-370-4454

@p6rt
Copy link
Author

p6rt commented Sep 2, 2016

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

@p6rt
Copy link
Author

p6rt commented Oct 9, 2016

From @skids

Turns out there is actually a (possibly bug) difference in behavior here​:

(02​:58​:53 AM) skids​: m​: my $str = "abcdabcd"; $str.trans("abc" => "AB").say; $str.say;
(02​:58​:54 AM) camelia​: rakudo-moar 6977b8​: OUTPUT«ABAdABAd␤abcdabcd␤»
(02​:59​:07 AM) skids​: m​: my $str = "abcdabcd"; $str.trans("abc" => ("A","B")).say; $str.say;
(02​:59​:07 AM) camelia​: rakudo-moar 6977b8​: OUTPUT«ABBdABBd␤abcdabcd␤»

Other things I noted while looking at roast to see what was tested behavior​:
these tests are either not doing what they are supposed to or not saying what
they are doing​:

is("abcde".trans( ('a..e' => 'A'..'E') ), "ABCDE",
  "Using string range on one side and array reference on the other");

is(" <>&".trans( ([' ', '<', '>', '&'] =>
  [' ', '<', '>', '&' ])),
  " <>&","The array version can map one characters to one-or-more characters");

is(" <>&".trans( ([' ', ' <', '<', '>', '&'] =>
  [' ', 'AB', '<', '>', '&' ])),
  "AB>&",
  "The array version can map one characters to one-or-more characters, using leftmost longest match");

As to the original ticket subject matter, I'm not sure warning is a good idea for two reasons​:

1) it's complicated logic to figure out if the target Str contains ranges longer than needed
2) it's possibly useful behavior, say for something like​:
  $thing.trans($/[]>>.Str, "0123456789") # when $/[].elems is unknown

@p6rt p6rt added the RFC Request For Comments label Jan 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
RFC Request For Comments
Projects
None yet
Development

No branches or pull requests

1 participant