changeset 113:8c706d6eb5dc

add binary type git-svn-id: https://luan-java.googlecode.com/svn/trunk@114 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Mon, 26 May 2014 05:27:26 +0000
parents f5af13062b10
children c599206448b9
files src/luan/Luan.java src/luan/LuanJavaFunction.java src/luan/LuanState.java src/luan/lib/BinaryLib.java src/luan/lib/StringLib.java
diffstat 5 files changed, 60 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/src/luan/Luan.java	Fri May 23 22:52:39 2014 +0000
+++ b/src/luan/Luan.java	Mon May 26 05:27:26 2014 +0000
@@ -28,6 +28,8 @@
 			return "table";
 		if( obj instanceof LuanFunction )
 			return "function";
+		if( obj instanceof byte[] )
+			return "binary";
 		return "userdata";
 	}
 
@@ -98,6 +100,8 @@
 			LuanException le = (LuanException)obj;
 			return le.getMessage();
 		}
+		if( obj instanceof byte[] )
+			return "binary: " + Integer.toHexString(obj.hashCode());
 		return obj.toString();
 	}
 
--- a/src/luan/LuanJavaFunction.java	Fri May 23 22:52:39 2014 +0000
+++ b/src/luan/LuanJavaFunction.java	Mon May 26 05:27:26 2014 +0000
@@ -195,8 +195,7 @@
 	}
 
 	private static boolean isNumber(Class<?> rtnType) {
-		return rtnType == Byte.TYPE
-			|| rtnType == Short.TYPE
+		return rtnType == Short.TYPE
 			|| rtnType == Integer.TYPE
 			|| rtnType == Long.TYPE
 			|| rtnType == Float.TYPE
--- a/src/luan/LuanState.java	Fri May 23 22:52:39 2014 +0000
+++ b/src/luan/LuanState.java	Mon May 26 05:27:26 2014 +0000
@@ -14,6 +14,7 @@
 import luan.lib.StringLib;
 import luan.lib.TableLib;
 import luan.lib.HtmlLib;
+import luan.lib.BinaryLib;
 
 
 public abstract class LuanState implements DeepCloneable<LuanState> {
@@ -115,6 +116,7 @@
 			luan.load(StringLib.NAME,StringLib.LOADER);
 			luan.load(TableLib.NAME,TableLib.LOADER);
 			luan.load(HtmlLib.NAME,HtmlLib.LOADER);
+			luan.load(BinaryLib.NAME,BinaryLib.LOADER);
 			return luan;
 		} catch(LuanException e) {
 			throw new RuntimeException(e);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/luan/lib/BinaryLib.java	Mon May 26 05:27:26 2014 +0000
@@ -0,0 +1,33 @@
+package luan.lib;
+
+import luan.LuanState;
+import luan.LuanTable;
+import luan.LuanFunction;
+import luan.LuanJavaFunction;
+
+
+public final class BinaryLib {
+
+	public static final String NAME = "Binary";
+
+	public static final LuanFunction LOADER = new LuanFunction() {
+		@Override public Object call(LuanState luan,Object[] args) {
+			LuanTable module = new LuanTable();
+			try {
+				add( module, "to_string", new byte[0].getClass() );
+			} catch(NoSuchMethodException e) {
+				throw new RuntimeException(e);
+			}
+			return module;
+		}
+	};
+
+	private static void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException {
+		t.put( method, new LuanJavaFunction(BinaryLib.class.getMethod(method,parameterTypes),null) );
+	}
+
+	public static String to_string(byte[] bytes) {
+		return new String(bytes);
+	}
+
+}
--- a/src/luan/lib/StringLib.java	Fri May 23 22:52:39 2014 +0000
+++ b/src/luan/lib/StringLib.java	Mon May 26 05:27:26 2014 +0000
@@ -19,8 +19,9 @@
 		@Override public Object call(LuanState luan,Object[] args) {
 			LuanTable module = new LuanTable();
 			try {
-				module.put( "byte", new LuanJavaFunction(StringLib.class.getMethod("byte_",String.class,Integer.class,Integer.class),null) );
-				module.put( "char", new LuanJavaFunction(StringLib.class.getMethod("char_",new byte[0].getClass()),null) );
+				add( module, "to_binary", String.class );
+				add( module, "to_integers", String.class );
+				add( module, "from_integers", new int[0].getClass() );
 				add( module, "find", String.class, String.class, Integer.class, Boolean.class );
 				add( module, "format", String.class, new Object[0].getClass() );
 				add( module, "gmatch", String.class, String.class );
@@ -59,14 +60,25 @@
 		return i==null ? dflt : end(s,i);
 	}
 
-	public static byte[] byte_(String s,Integer i,Integer j) {
-		int start = start(s,i,0);
-		int end = end(s,j,start+1);
-		return s.substring(start,end).getBytes();
+	public static byte[] to_binary(String s) {
+		return s.getBytes();
 	}
 
-	public static String char_(byte... bytes) {
-		return new String(bytes);
+	public static int[] to_integers(String s) {
+		char[] a = s.toCharArray();
+		int[] chars = new int[a.length];
+		for( int i=0; i<a.length; i++ ) {
+			chars[i] = a[i];
+		}
+		return chars;
+	}
+
+	public static String from_integers(int... chars) {
+		char[] a = new char[chars.length];
+		for( int i=0; i<chars.length; i++ ) {
+			a[i] = (char)chars[i];
+		}
+		return new String(a);
 	}
 
 	public static int len(String s) {