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

any-str.pir p5chop implementation #351

Closed
p6rt opened this issue Oct 2, 2008 · 10 comments
Closed

any-str.pir p5chop implementation #351

p6rt opened this issue Oct 2, 2008 · 10 comments
Labels

Comments

@p6rt
Copy link

p6rt commented Oct 2, 2008

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

Searchable as RT59552$

@p6rt
Copy link
Author

p6rt commented Oct 2, 2008

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
Copy link
Author

p6rt commented Oct 2, 2008

From @azawawi

Attaching the patch file

@p6rt
Copy link
Author

p6rt commented Oct 2, 2008

From @azawawi

str_p5chop.patch
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
Copy link
Author

p6rt commented Oct 2, 2008

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

@p6rt
Copy link
Author

p6rt commented Oct 8, 2008

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
Copy link
Author

p6rt commented Oct 8, 2008

From @azawawi

p5chop_lists.patch
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
Copy link
Author

p6rt commented Oct 8, 2008

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
Copy link
Author

p6rt commented Oct 10, 2008

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
Copy link
Author

p6rt commented Mar 10, 2009

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
Copy link
Author

p6rt commented Mar 10, 2009

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

@p6rt p6rt closed this as completed Mar 10, 2009
@p6rt p6rt added the patch label Jan 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant