Mercurial Hosting > luan
comparison core/src/luan/modules/BinaryLuan.java @ 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 | 2f8938fc518c |
children | cbb94a7c7a9e |
comparison
equal
deleted
inserted
replaced
304:03e9cda4748d | 305:5e7450ac27f2 |
---|---|
8 import luan.LuanMethod; | 8 import luan.LuanMethod; |
9 | 9 |
10 | 10 |
11 public final class BinaryLuan { | 11 public final class BinaryLuan { |
12 | 12 |
13 @LuanMethod public static byte[] pack(byte... bytes) { | 13 public static Object __index(LuanState luan,final byte[] binary,Object key) throws LuanException { |
14 return bytes; | 14 LuanTable mod = (LuanTable)PackageLuan.loaded(luan).get("luan:Binary"); |
15 if( mod!=null ) { | |
16 Object obj = mod.get(key); | |
17 if( obj instanceof LuanFunction ) { | |
18 final LuanFunction fn = (LuanFunction)obj; | |
19 return new LuanFunction() { | |
20 @Override public Object call(LuanState luan,Object[] args) throws LuanException { | |
21 Object[] a = new Object[args.length+1]; | |
22 a[0] = binary; | |
23 System.arraycopy(args,0,a,1,args.length); | |
24 return fn.call(luan,a); | |
25 } | |
26 }; | |
27 } | |
28 } | |
29 return null; | |
15 } | 30 } |
16 | 31 |
17 @LuanMethod public static Byte[] unpack(byte[] binary) { | 32 static int start(byte[] binary,int i) { |
18 Byte[] bytes = new Byte[binary.length]; | 33 int len = binary.length; |
19 for( int i=0; i<binary.length; i++ ) { | 34 return i==0 ? 0 : i > 0 ? Math.min(i-1,len) : Math.max(len+i,0); |
20 bytes[i] = binary[i]; | 35 } |
36 | |
37 static int start(byte[] binary,Integer i,int dflt) { | |
38 return i==null ? dflt : start(binary,i); | |
39 } | |
40 | |
41 static int end(byte[] binary,int i) { | |
42 int len = binary.length; | |
43 return i==0 ? 0 : i > 0 ? Math.min(i,len) : Math.max(len+i+1,0); | |
44 } | |
45 | |
46 static int end(byte[] binary,Integer i,int dflt) { | |
47 return i==null ? dflt : end(binary,i); | |
48 } | |
49 | |
50 @LuanMethod public static Byte[] byte_(LuanState luan,byte[] binary,Integer i,Integer j) throws LuanException { | |
51 Utils.checkNotNull(luan,binary); | |
52 int start = start(binary,i,1); | |
53 int end = end(binary,j,start+1); | |
54 Byte[] bytes = new Byte[end-start]; | |
55 for( int k=0; k<bytes.length; k++ ) { | |
56 bytes[k] = binary[start+k]; | |
21 } | 57 } |
22 return bytes; | 58 return bytes; |
23 } | 59 } |
24 | 60 |
61 @LuanMethod public static byte[] binary(byte... bytes) { | |
62 return bytes; | |
63 } | |
64 | |
25 } | 65 } |