Skip to content

Commit

Permalink
Add binary:part/2,3 BIFs
Browse files Browse the repository at this point in the history
  • Loading branch information
krestenkrab committed Aug 20, 2013
1 parent 50131f5 commit 5d38fc0
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions src/main/java/erjang/m/binary/Native.java
Original file line number Diff line number Diff line change
Expand Up @@ -332,17 +332,50 @@ static boolean looking_at(byte[] haystack, int off, byte[] needle) {
*/
@BIF
public static EBinary part(EObject subject, EObject poslen) {
throw new NotImplemented();
EBinary sub = subject.testBinary();
ETuple2 pl = ETuple2.cast(poslen);
ESmall pos, len;
if (sub == null
|| pl == null
|| (pos=pl.elem1.testSmall()) == null
|| (len=pl.elem2.testSmall()) == null
) {
throw ERT.badarg(subject, poslen);
}

return part(sub, pos.value, len.value, false);
}

/**
* part(Subject, Pos, Len) -> binary()
*/
@BIF
public static EBinary part(EObject subject, EObject pos, EObject len) {
throw new NotImplemented();
public static EBinary part(EObject subject, EObject opos, EObject olen) {
EBinary sub = subject.testBinary();
ESmall pos, len;
if (sub == null
|| (pos=opos.testSmall()) == null
|| (len=olen.testSmall()) == null
) {
throw ERT.badarg(subject, opos, olen);
}

return part(sub, pos.value, len.value, false);
}

private static EBinary part(EBinary sub, int pos, int len, boolean as_guard) {
if (len < 0) {
pos = pos + len;
len = -len;
}

if (pos < 0 || (pos + len) > sub.byteSize()) {
throw ERT.badarg(sub, ERT.box(pos), ERT.box(len));
}

return sub.sub_binary(pos, len);
}

/**
* referenced_byte_size(binary()) -> int()
*/
Expand Down

0 comments on commit 5d38fc0

Please sign in to comment.