diff src/luan/modules/BinaryLuan.java @ 775:1a68fc55a80c

simplify dir structure
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 26 Aug 2016 14:36:40 -0600
parents core/src/luan/modules/BinaryLuan.java@cdc70de628b5
children e38f5869e9df
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/luan/modules/BinaryLuan.java	Fri Aug 26 14:36:40 2016 -0600
@@ -0,0 +1,50 @@
+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_(byte[] binary,Integer i,Integer j) throws LuanException {
+		Utils.checkNotNull(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);
+	}
+
+}