Mercurial Hosting > luan
changeset 305:5e7450ac27f2
rename String.byte() to String.unicode();
improve Binary;
git-svn-id: https://luan-java.googlecode.com/svn/trunk@306 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Fri, 19 Dec 2014 07:50:46 +0000 |
parents | 03e9cda4748d |
children | 7f38793a99ee |
files | core/src/luan/LuanState.java core/src/luan/modules/Binary.luan core/src/luan/modules/BinaryLuan.java core/src/luan/modules/String.luan core/src/luan/modules/StringLuan.java core/src/luan/modules/Utils.java |
diffstat | 6 files changed, 66 insertions(+), 19 deletions(-) [+] |
line wrap: on
line diff
diff -r 03e9cda4748d -r 5e7450ac27f2 core/src/luan/LuanState.java --- a/core/src/luan/LuanState.java Thu Dec 18 12:06:56 2014 +0000 +++ b/core/src/luan/LuanState.java Fri Dec 19 07:50:46 2014 +0000 @@ -13,6 +13,7 @@ import luan.modules.PackageLuan; import luan.modules.JavaLuan; import luan.modules.StringLuan; +import luan.modules.BinaryLuan; public abstract class LuanState implements DeepCloneable<LuanState> { @@ -120,6 +121,11 @@ if( rtn != null ) return rtn; } + if( obj instanceof byte[] ) { + Object rtn = BinaryLuan.__index(luan,(byte[])obj,key); + if( rtn != null ) + return rtn; + } return JavaLuan.__index(luan,obj,key); }
diff -r 03e9cda4748d -r 5e7450ac27f2 core/src/luan/modules/Binary.luan --- a/core/src/luan/modules/Binary.luan Thu Dec 18 12:06:56 2014 +0000 +++ b/core/src/luan/modules/Binary.luan Fri Dec 19 07:50:46 2014 +0000 @@ -1,5 +1,5 @@ java() import "java:luan.modules.BinaryLuan" -pack = BinaryLuan.pack -unpack = BinaryLuan.unpack +byte = BinaryLuan.byte_ +binary = BinaryLuan.binary
diff -r 03e9cda4748d -r 5e7450ac27f2 core/src/luan/modules/BinaryLuan.java --- a/core/src/luan/modules/BinaryLuan.java Thu Dec 18 12:06:56 2014 +0000 +++ b/core/src/luan/modules/BinaryLuan.java Fri Dec 19 07:50:46 2014 +0000 @@ -10,16 +10,56 @@ public final class BinaryLuan { - @LuanMethod public static byte[] pack(byte... bytes) { - return bytes; + public static Object __index(LuanState luan,final byte[] binary,Object key) throws LuanException { + LuanTable mod = (LuanTable)PackageLuan.loaded(luan).get("luan:Binary"); + if( mod!=null ) { + Object obj = mod.get(key); + if( obj instanceof LuanFunction ) { + final LuanFunction fn = (LuanFunction)obj; + return new LuanFunction() { + @Override public Object call(LuanState luan,Object[] args) throws LuanException { + Object[] a = new Object[args.length+1]; + a[0] = binary; + System.arraycopy(args,0,a,1,args.length); + return fn.call(luan,a); + } + }; + } + } + return null; } - @LuanMethod public static Byte[] unpack(byte[] binary) { - Byte[] bytes = new Byte[binary.length]; - for( int i=0; i<binary.length; i++ ) { - bytes[i] = binary[i]; + 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; + } + }
diff -r 03e9cda4748d -r 5e7450ac27f2 core/src/luan/modules/String.luan --- a/core/src/luan/modules/String.luan Thu Dec 18 12:06:56 2014 +0000 +++ b/core/src/luan/modules/String.luan Fri Dec 19 07:50:46 2014 +0000 @@ -1,7 +1,7 @@ java() import "java:luan.modules.StringLuan" -byte = StringLuan.byte_ +unicode = StringLuan.unicode char = StringLuan.char_ concat = StringLuan.concat find = StringLuan.find
diff -r 03e9cda4748d -r 5e7450ac27f2 core/src/luan/modules/StringLuan.java --- a/core/src/luan/modules/StringLuan.java Thu Dec 18 12:06:56 2014 +0000 +++ b/core/src/luan/modules/StringLuan.java Fri Dec 19 07:50:46 2014 +0000 @@ -51,16 +51,13 @@ return i==null ? dflt : end(s,i); } - @LuanMethod public static Integer[] byte_(String s,Integer i,Integer j) { - if( i== null ) - i = 1; - if( j==null ) - j = i; - s = s.substring(i-1,j); - char[] a = s.toCharArray(); - Integer[] chars = new Integer[a.length]; - for( int k=0; k<a.length; k++ ) { - chars[k] = (int)a[k]; + @LuanMethod public static Integer[] unicode(LuanState luan,String s,Integer i,Integer j) throws LuanException { + Utils.checkNotNull(luan,s); + int start = start(s,i,1); + int end = end(s,j,start+1); + Integer[] chars = new Integer[end-start]; + for( int k=0; k<chars.length; k++ ) { + chars[k] = (int)s.charAt(start+k); } return chars; }
diff -r 03e9cda4748d -r 5e7450ac27f2 core/src/luan/modules/Utils.java --- a/core/src/luan/modules/Utils.java Thu Dec 18 12:06:56 2014 +0000 +++ b/core/src/luan/modules/Utils.java Fri Dec 19 07:50:46 2014 +0000 @@ -27,6 +27,10 @@ checkNotNull(luan,s,"string"); } + public static void checkNotNull(LuanState luan,byte[] b) throws LuanException { + checkNotNull(luan,b,"binary"); + } + public static void checkNotNull(LuanState luan,LuanTable t) throws LuanException { checkNotNull(luan,t,"table"); }