Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Bounds for Blob.at_pos, <0 check for Buf.at_pos
  • Loading branch information
lizmat committed Jan 15, 2015
1 parent a151332 commit fdada00
Showing 1 changed file with 48 additions and 4 deletions.
52 changes: 48 additions & 4 deletions src/core/Buf.pm
Expand Up @@ -32,10 +32,23 @@ my role Blob[::T = uint8] does Positional[T] does Stringy is repr('VMArray') is
}

multi method at_pos(Blob:D: int \pos) {
X::OutOfRange.new(
:what<Index>,
:got(pos),
:range(Range.new(0,nqp::elems(self)-1))
).throw
if nqp::isge_i(pos,nqp::elems(self)) || nqp::islt_i(pos,0);
nqp::atpos_i(self, pos);
}
multi method at_pos(Blob:D: Int:D \pos) {
nqp::atpos_i(self, nqp::unbox_i(pos));
my int $pos = nqp::unbox_i(pos);
X::OutOfRange.new(
:what<Index>,
:got(pos),
:range(Range.new(0,nqp::elems(self)-1))
).throw
if nqp::isge_i($pos,nqp::elems(self)) || nqp::islt_i($pos,0);
nqp::atpos_i(self,$pos);
}

multi method Bool(Blob:D:) {
Expand Down Expand Up @@ -240,13 +253,44 @@ my class utf32 does Blob[uint32] is repr('VMArray') {
}

my role Buf[::T = uint8] does Blob[T] is repr('VMArray') is array_type(T) {
# TODO: override at_pos so we get mutability
#
multi method at_pos(Buf:D: int \pos) {
X::OutOfRange.new(
:what<Index>,
:got(pos),
:range(Range.new(0,Inf))
).throw
if nqp::islt_i(pos,0);
nqp::atpos_i(self, pos);
}
multi method at_pos(Buf:D: Int:D \pos) {
my int $pos = nqp::unbox_i(pos);
X::OutOfRange.new(
:what<Index>,
:got(pos),
:range(Range.new(0,Inf))
).throw
if nqp::islt_i($pos,0);
nqp::atpos_i(self,$pos);
}

multi method assign_pos(Buf:D: int \pos, Mu \assignee) {
X::OutOfRange.new(
:what<Index>,
:got(pos),
:range(Range.new(0,Inf))
).throw
if nqp::islt_i(pos,0);
nqp::bindpos_i(self,\pos,assignee)
}
multi method assign_pos(Buf:D: Int:D \pos, Mu \assignee) is rw {
nqp::bindpos_i(self,nqp::unbox_i(pos),assignee)
my int $pos = nqp::unbox_i(pos);
X::OutOfRange.new(
:what<Index>,
:got(pos),
:range(Range.new(0,Inf))
).throw
if nqp::islt_i($pos,0);
nqp::bindpos_i(self,$pos,assignee)
}
}

Expand Down

0 comments on commit fdada00

Please sign in to comment.