view core/src/luan/modules/BinaryLuan.java @ 576:4723d22062ce

remove LuanBit
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 13 Jul 2015 20:38:26 -0600
parents 473e456444ff
children cdc70de628b5
line wrap: on
line source

package luan.modules;

import luan.Luan;
import luan.LuanState;
import luan.LuanTable;
import luan.LuanFunction;
import luan.LuanException;
import luan.LuanMethod;


public final class BinaryLuan {

	static int start(byte[] binary,int i) {
		int len = binary.length;
		return i==0 ? 0 : i > 0 ? Math.min(i-1,len) : Math.max(len+i,0);
	}

	static int start(byte[] binary,Integer i,int dflt) {
		return i==null ? dflt : start(binary,i);
	}

	static int end(byte[] binary,int i) {
		int len = binary.length;
		return i==0 ? 0 : i > 0 ? Math.min(i,len) : Math.max(len+i+1,0);
	}

	static int end(byte[] binary,Integer i,int dflt) {
		return i==null ? dflt : end(binary,i);
	}

	@LuanMethod public static Byte[] byte_(LuanState luan,byte[] binary,Integer i,Integer j) throws LuanException {
		Utils.checkNotNull(luan,binary);
		int start = start(binary,i,1);
		int end = end(binary,j,start+1);
		Byte[] bytes = new Byte[end-start];
		for( int k=0; k<bytes.length; k++ ) {
			bytes[k] = binary[start+k];
		}
		return bytes;
	}

	@LuanMethod public static byte[] binary(byte... bytes) {
		return bytes;
	}

	public static String to_string(byte[] binary) {
		return new String(binary);
	}

}