Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

implement bitwise operators for Buf

  • Loading branch information...
commit 09a598fed2522883d3450dec386c5e328077f250 1 parent 7a51746
Moritz Lenz moritz authored

Showing 2 changed files with 25 additions and 5 deletions. Show diff stats Hide diff stats

  1. +0 5 LHF.markdown
  2. +25 0 src/core/Buf.pm
5 LHF.markdown
Source Rendered
@@ -15,8 +15,3 @@ so forth. The regex ones are probably also good candidates now.
15 15
16 16 ## Nil assignment
17 17 my Int $b = 3; $b = Nil;
18   -
19   -## Buf operators
20   -Implement bitwise operators for Buf, so that t/spec/S03-operators/buf.t
21   -passes. Much of the implementation can be stolen from the 2011.07
22   -release (though you need .list instead of .contents)
25 src/core/Buf.pm
@@ -67,3 +67,28 @@ multi infix:<~>(Buf:D $a, Buf:D $b) {
67 67 nqp::bindattr($r, Buf, '$!buffer', $br);
68 68 $r;
69 69 }
  70 +multi sub infix:<~&>(Buf:D $a, Buf:D $b) {
  71 + my $minlen := $a.elems min $b.elems;
  72 + my @anded-contents = $a.list[^$minlen] >>+&<< $b.list[^$minlen];
  73 + @anded-contents.push: 0 xx ($a.elems - @anded-contents.elems);
  74 + @anded-contents.push: 0 xx ($b.elems - @anded-contents.elems);
  75 + Buf.new(@anded-contents);
  76 +}
  77 +
  78 +
  79 +multi sub infix:<~|>(Buf:D $a, Buf:D $b) {
  80 + my $minlen = $a.elems min $b.elems;
  81 + my @ored-contents = $a.list[^$minlen] «+|» $b.list[^$minlen];
  82 + @ored-contents.push: $a.list[@ored-contents.elems ..^ $a.elems];
  83 + @ored-contents.push: $b.list[@ored-contents.elems ..^ $b.elems];
  84 + Buf.new(@ored-contents);
  85 +}
  86 +
  87 +multi sub infix:<~^>(Buf:D $a, Buf:D $b) {
  88 + my $minlen = $a.elems min $b.elems;
  89 + my @xored-contents = $a.list[^$minlen] «+^» $b.list[^$minlen];
  90 + @xored-contents.push: $a.list[@xored-contents.elems ..^ $a.elems];
  91 + @xored-contents.push: $b.list[@xored-contents.elems ..^ $b.elems];
  92 + Buf.new(@xored-contents);
  93 +}
  94 +

0 comments on commit 09a598f

Please sign in to comment.
Something went wrong with that request. Please try again.