Raku / old-issue-tracker

Tickets from RT
https://github.com/Raku/old-issue-tracker/issues
2 stars 1 forks source link

any-str.pir p5chop implementation #351

Closed p6rt closed 15 years ago

p6rt commented 16 years ago

Migrated from rt.perl.org#59552 (status was 'resolved')

Searchable as RT59552$

p6rt commented 16 years ago

From @azawawi

Hello,

Here is the p5chop patch that makes our tests pass the first 4 tests. To test it, please use the following command​:

./perl6 t/spec/S29-str/p5chop.t 2>&1 | head

Thanks, Ahmad Zawawi

Index​: src/builtins/any-str.pir

--- src/builtins/any-str.pir (revision 31564) +++ src/builtins/any-str.pir (working copy) @​@​ -21,7 +21,7 @​@​ .namespace [] .sub 'onload' :anon :init :load   $P0 = get_hll_namespace ['Any'] - '!EXPORT'('capitalize chop chars index lc lcfirst rindex ord substr uc ucfirst', 'from'=>$P0) + '!EXPORT'('capitalize chop p5chop chars index lc lcfirst rindex ord substr uc ucfirst', 'from'=>$P0) .end

@​@​ -79,7 +79,29 @​@​   .return ($I0) .end

+=item p5chop + + our Char multi P5emul​::Str​::p5chop ( Str $string is rw ) + our Char multi P5emul​::Str​::p5chop ( Str *@​strings = ($+_) is rw ) + + Trims the last character from C\<$string>, and returns it. Called with a + list, it chops each item in turn, and returns the last character + chopped.

+=cut + +.sub 'p5chop' :method :multi(_) + .local string tmps + .local string lastchar + + tmps = self + substr lastchar, tmps, -1 + chopn tmps, 1 + self = tmps + + .return(lastchar) +.end + =item chop

  our Str method Str​::chop ( Str $string​: )

p6rt commented 16 years ago

From @azawawi

Attaching the patch file

p6rt commented 16 years ago

From @azawawi

str_p5chop.patch ```diff Index: src/builtins/any-str.pir =================================================================== --- src/builtins/any-str.pir (revision 31564) +++ src/builtins/any-str.pir (working copy) @@ -21,7 +21,7 @@ .namespace [] .sub 'onload' :anon :init :load $P0 = get_hll_namespace ['Any'] - '!EXPORT'('capitalize chop chars index lc lcfirst rindex ord substr uc ucfirst', 'from'=>$P0) + '!EXPORT'('capitalize chop p5chop chars index lc lcfirst rindex ord substr uc ucfirst', 'from'=>$P0) .end @@ -79,7 +79,29 @@ .return ($I0) .end +=item p5chop + + our Char multi P5emul::Str::p5chop ( Str $string is rw ) + our Char multi P5emul::Str::p5chop ( Str *@strings = ($+_) is rw ) + + Trims the last character from C<$string>, and returns it. Called with a + list, it chops each item in turn, and returns the last character + chopped. +=cut + +.sub 'p5chop' :method :multi(_) + .local string tmps + .local string lastchar + + tmps = self + substr lastchar, tmps, -1 + chopn tmps, 1 + self = tmps + + .return(lastchar) +.end + =item chop our Str method Str::chop ( Str $string: ) ```
p6rt commented 16 years ago

@azawawi - Status changed from 'new' to 'open'

p6rt commented 16 years ago

From @azawawi

Attached is the implementation of p5chop for any-list.pir

Together they make t/spec/S29-str/p5chop.t succeed with all of its tests. Although i think that p5chop.t can be enhanced.

./ahmad &

p6rt commented 16 years ago

From @azawawi

p5chop_lists.patch ```diff Index: src/builtins/any-list.pir =================================================================== --- src/builtins/any-list.pir (revision 31798) +++ src/builtins/any-list.pir (working copy) @@ -21,7 +21,7 @@ .namespace ['Any'] .sub 'onload' :anon :init :load $P0 = get_hll_namespace ['Any'] - '!EXPORT'('abs', 'from'=>$P0) + '!EXPORT'('abs p5chop', 'from'=>$P0) .end @@ -43,7 +43,29 @@ .return ($I0) .end +=item p5chop +=cut +.namespace ['Any'] +.sub 'p5chop' :method :multi(_) + .local string tmps + .local string lastchar + .local pmc it + + $P0 = self.'list'() + it = $P0.'iterator'() + loop: + unless it goto done + $P0 = shift it + tmps = $P0 + substr lastchar, tmps, -1 + chopn tmps, 1 + $P0 = tmps + goto loop + done: + .return(lastchar) +.end + =item join =cut ```
p6rt commented 16 years ago

From @azawawi

Please use the latest any-list.pir patch. It works perfectly. No need for the previous patch.

On Wed Oct 08 14​:03​:03 2008, ahmadz wrote​:

Attached is the implementation of p5chop for any-list.pir

Together they make t/spec/S29-str/p5chop.t succeed with all of its tests. Although i think that p5chop.t can be enhanced.

./ahmad &

p6rt commented 16 years ago

From @moritz

First of all thanks for the patch.

As mentioned on IRC I'm not too happy with applying these patches until Patrick (or somebody else) has sorted out the container/value and assignment/binding issues.

Here's why​:

$ perl -wle 'chop "xy"' Can't modify constant item in chop at -e line 1, at EOF Execution of -e aborted due to compilation errors. $ # with your patch​: $ ../../parrot perl6.pbc -e 'say "abc".p5chop' c $ ../../parrot perl6.pbc -e 'my $x = "abc"; my $y = $x; say $x.p5chop; say $x; say $y' c ab ab

In Perl 6 a string is immutable. The way that string modifying functions work is that they get hold of a the variable that holds the string, create a modified copy of the string, and store that into the variable.

So this patch is basically cheating by modifying an object that should be immutable. That might be a valid optimization, but only if we can ensure that it never leaks to the user land.

Which is why I'll wait with applying these patches until the assignment and binding issues are sorted out. If they (magically) continue to work, and don't erroneously modify copies of the string I'll apply them.

I apologize for not being clearer on these issues before.

Moritz

p6rt commented 15 years ago

From @pmichaud

I'm going to go ahead and mark this ticket as "resolved" for now -- things have changed quite a bit since it was submitted.

I invite new implementations of p5chop and p5chomp, especially if they can be done as methods in the setting (possibly using inline PIR).

Thanks!

Pm

p6rt commented 15 years ago

@pmichaud - Status changed from 'open' to 'resolved'