Permalink
Browse files

Start replacing Buf.

Buf is now a role, as per spec. We also provide buf8, buf16, buf32 and
buf64 symbols in the setting, which are instantiations of it. However,
VMArray isn't quite smart enough to pack those as it should yet. Much
previous Buf functionality is still missing; it will be added back
before this branch is merged.
  • Loading branch information...
1 parent 34e8703 commit fa3ec0245c99fd17f11e216b8abb33b996d0b55e @jnthn jnthn committed Jul 19, 2013
Showing with 53 additions and 128 deletions.
  1. +51 −125 src/core/Buf.pm
  2. +0 −1 src/core/Str.pm
  3. +1 −1 tools/build/Makefile-JVM.in
  4. +1 −1 tools/build/Makefile-Parrot.in
View
@@ -1,65 +1,55 @@
-# the real Buf should be something parametric and much more awesome.
-# this is merely a placeholder until people who know their stuff build
-# the Real Thing.
-
-my class X::Buf::AsStr { ... };
-my class X::Buf::Pack { ... };
-my class X::Buf::Pack::NonASCII { ... };
-
-my class Buf does Positional {
- has str $!buffer;
-#?if parrot
- my int $binary_encoding = pir::find_encoding__Is('binary');
- method BUILD() {
- $!buffer = pir::trans_encoding__Ssi('', $binary_encoding);
- 1;
- }
-#?endif
-#?if !parrot
- method BUILD() { die "Buf NYI on JVM backend" }
-#?endif
- method new(*@codes) {
- my $new := self.bless(*);
- $new!set_codes(@codes);
- $new;
+my role Buf[::T = int8] does Positional[T] does Stringy is repr('VMArray') is array_type(T) {
+ proto method new(|) { * }
+ multi method new() {
+ nqp::create(self)
}
- method !set_codes(@codes) {
- my int $bytes = @codes.elems;
- my $rsa := nqp::list_s();
- my int $i = 0;
- while $i < $bytes {
- nqp::bindpos_s($rsa, $i, nqp::chr(nqp::unbox_i(@codes[$i])));
+ multi method new(@values) {
+ my $buf := nqp::create(self);
+ my int $n = @values.elems;
+ my int $i;
+ nqp::setelems($buf, $n);
+ while $i < $n {
+ nqp::bindpos_i($buf, $i, @values.at_pos($i));
$i = $i + 1;
}
- $!buffer = nqp::join('', $rsa);
- self;
+ $buf
}
-
- method at_pos(Buf:D: Int:D $idx) {
- nqp::p6box_i(nqp::ord(nqp::substr($!buffer, nqp::unbox_i($idx), 1)));
+ multi method new(*@values) {
+ self.new(@values)
}
-
+
+ multi method at_pos(Buf:D: $i) {
+ nqp::atpos_i(self, $i.Int)
+ }
+ multi method at_pos(Buf:D: Int $i) {
+ nqp::atpos_i(self, $i)
+ }
+ multi method at_pos(Buf:D: int $i) {
+ nqp::atpos_i(self, $i)
+ }
+
multi method Bool(Buf:D:) {
- nqp::p6bool(nqp::chars($!buffer));
+ nqp::p6bool(nqp::elems(self));
}
method elems(Buf:D:) {
- nqp::p6box_i(nqp::chars($!buffer));
+ nqp::p6box_i(nqp::elems(self));
+ }
+ method bytes(Buf:D:) {
+ self.elems
}
- method bytes(Buf:D:) { self.elems }
method chars() { X::Buf::AsStr.new(method => 'chars').throw }
multi method Str() { X::Buf::AsStr.new(method => 'Str' ).throw }
-
method Numeric { self.elems }
method Int { self.elems }
-
+
method list() {
my @l;
- my int $bytes = nqp::chars($!buffer);
+ my int $n = nqp::elems(self);
my int $i = 0;
- while $i < $bytes {
- @l[$i] = nqp::p6box_i(nqp::ord(nqp::substr($!buffer, $i, 1)));
+ while $i < $n {
+ @l[$i] = nqp::atpos_i(self, $i);
$i = $i + 1;
}
@l;
@@ -71,26 +61,21 @@ my class Buf does Positional {
multi method perl(Buf:D:) {
self.^name ~ '.new(' ~ self.list.join(', ') ~ ')';
}
-
- method decode(Str:D $encoding = 'utf8') {
-#?if parrot
- my $bb := pir::new__Ps('ByteBuffer');
- pir::set__vPs($bb, $!buffer);
- nqp::p6box_s($bb.get_string($encoding eq 'binary' ?? 'binary' !! PARROT_ENCODING($encoding)));
-#?endif
-#?if !parrot
- die "Buf NYI on JVM backend"
-#?endif
- }
-
- method subbuf(Buf:D: $from = 0, $len = self.elems) {
+
+ method subbuf(Buf:D: $from = 0, $len = self.elems - $from) {
my $ret := nqp::create(self);
- nqp::bindattr_s($ret, Buf, '$!buffer',
- nqp::substr($!buffer, nqp::unbox_i($from), nqp::unbox_i($len))
- );
- $ret;
+ my int $llen = $len.Int;
+ nqp::setelems($ret, $llen);
+ my int $i = 0;
+ my int $f = $from.Int;
+ while $i < $llen {
+ nqp::bindpos_i($ret, $i, nqp::atpos_i(self, $f));
+ $i = $i + 1;
+ $f = $f + 1;
+ }
+ $ret
}
-
+
method unpack(Buf:D: $template) {
my @bytes = self.list;
my @fields;
@@ -162,69 +147,10 @@ my class Buf does Positional {
method contents(Buf:D:) { self.list }
}
-multi infix:<eqv>(Buf:D $a, Buf:D $b) {
- $a.WHAT === $b.WHAT && nqp::p6bool(nqp::iseq_s(
- nqp::getattr_s(nqp::decont($a), Buf, '$!buffer'),
- nqp::getattr_s(nqp::decont($b), Buf, '$!buffer')
- ));
-}
-multi prefix:<~^>(Buf:D $a) {
- Buf.new($a.list.map: 255 - *);
-}
-multi infix:<~>(Buf:D $a, Buf:D $b) {
- my Buf $r := nqp::create(Buf);
-
- my str $ba = nqp::getattr_s(nqp::decont($a), Buf, '$!buffer');
- my str $bb = nqp::getattr_s(nqp::decont($b), Buf, '$!buffer');
- nqp::bindattr_s($r, Buf, '$!buffer', nqp::concat($ba, $bb));
- $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);
-}
-
-multi sub infix:<cmp>(Buf:D $a, Buf:D $b) {
- [||] $a.list Z<=> $b.list or $a.elems <=> $b.elems
-}
-multi sub infix:<eq>(Buf:D $a, Buf:D $b) {
- $a.elems == $b.elems && $a.list eq $b.list
-}
-multi sub infix:<ne>(Buf:D $a, Buf:D $b) {
- not $a eq $b;
-}
-multi sub infix:<lt>(Buf:D $a, Buf:D $b) {
- ($a cmp $b) == -1
-}
-multi sub infix:<gt>(Buf:D $a, Buf:D $b) {
- ($a cmp $b) == 1
-}
-multi sub infix:<le>(Buf:D $a, Buf:D $b) {
- ($a cmp $b) != 1
-}
-multi sub infix:<ge>(Buf:D $a, Buf:D $b) {
- ($a cmp $b) != -1
-}
+constant buf8 = Buf[int8];
+constant buf16 = Buf[int16];
+constant buf32 = Buf[int32];
+constant buf64 = Buf[int64];
multi sub pack(Str $template, *@items) {
my @bytes;
View
@@ -1,7 +1,6 @@
my class Cursor {... }
my class Range {... }
my class Match {... }
-my class Buf {... }
my class IO::Path { ... }
my class X::Str::Numeric { ... }
my class X::Str::Match::x { ... }
@@ -133,6 +133,7 @@ CORE_SOURCES = \
src/core/Order.pm \
src/core/UInt64.pm \
src/core/Num.pm \
+ src/core/Buf.pm \
src/core/Str.pm \
src/core/Capture.pm \
src/core/Parcel.pm \
@@ -154,7 +155,6 @@ CORE_SOURCES = \
src/core/PseudoStash.pm \
src/core/Parameter.pm \
src/core/Signature.pm \
- src/core/Buf.pm \
src/core/Rational.pm \
src/core/Rat.pm \
src/core/Complex.pm \
@@ -178,6 +178,7 @@ CORE_SOURCES = \
src/core/Order.pm \
src/core/UInt64.pm \
src/core/Num.pm \
+ src/core/Buf.pm \
src/core/Str.pm \
src/core/Capture.pm \
src/core/Parcel.pm \
@@ -199,7 +200,6 @@ CORE_SOURCES = \
src/core/PseudoStash.pm \
src/core/Parameter.pm \
src/core/Signature.pm \
- src/core/Buf.pm \
src/core/Rational.pm \
src/core/Rat.pm \
src/core/Complex.pm \

0 comments on commit fa3ec02

Please sign in to comment.