Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

implement bitwise operators for Buf

  • Loading branch information...
commit 09a598fed2522883d3450dec386c5e328077f250 1 parent 7a51746
Moritz Lenz moritz authored
Showing with 25 additions and 5 deletions.
  1. +0 −5 LHF.markdown
  2. +25 −0 src/core/Buf.pm
5 LHF.markdown
View
@@ -15,8 +15,3 @@ so forth. The regex ones are probably also good candidates now.
## Nil assignment
my Int $b = 3; $b = Nil;
-
-## Buf operators
-Implement bitwise operators for Buf, so that t/spec/S03-operators/buf.t
-passes. Much of the implementation can be stolen from the 2011.07
-release (though you need .list instead of .contents)
25 src/core/Buf.pm
View
@@ -67,3 +67,28 @@ multi infix:<~>(Buf:D $a, Buf:D $b) {
nqp::bindattr($r, Buf, '$!buffer', $br);
$r;
}
+multi sub infix:<~&>(Buf:D $a, Buf:D $b) {
+ my $minlen := $a.elems min $b.elems;
+ my @anded-contents = $a.list[^$minlen] >>+&<< $b.list[^$minlen];
+ @anded-contents.push: 0 xx ($a.elems - @anded-contents.elems);
+ @anded-contents.push: 0 xx ($b.elems - @anded-contents.elems);
+ Buf.new(@anded-contents);
+}
+
+
+multi sub infix:<~|>(Buf:D $a, Buf:D $b) {
+ my $minlen = $a.elems min $b.elems;
+ my @ored-contents = $a.list[^$minlen] «+|» $b.list[^$minlen];
+ @ored-contents.push: $a.list[@ored-contents.elems ..^ $a.elems];
+ @ored-contents.push: $b.list[@ored-contents.elems ..^ $b.elems];
+ Buf.new(@ored-contents);
+}
+
+multi sub infix:<~^>(Buf:D $a, Buf:D $b) {
+ my $minlen = $a.elems min $b.elems;
+ my @xored-contents = $a.list[^$minlen] «+^» $b.list[^$minlen];
+ @xored-contents.push: $a.list[@xored-contents.elems ..^ $a.elems];
+ @xored-contents.push: $b.list[@xored-contents.elems ..^ $b.elems];
+ Buf.new(@xored-contents);
+}
+
Please sign in to comment.
Something went wrong with that request. Please try again.