Mercurial Hosting > luan
comparison src/luan/lib/StringLib.java @ 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 | f1bf2890d80f |
comparison
equal
deleted
inserted
replaced
112:f5af13062b10 | 113:8c706d6eb5dc |
---|---|
17 | 17 |
18 public static final LuanFunction LOADER = new LuanFunction() { | 18 public static final LuanFunction LOADER = new LuanFunction() { |
19 @Override public Object call(LuanState luan,Object[] args) { | 19 @Override public Object call(LuanState luan,Object[] args) { |
20 LuanTable module = new LuanTable(); | 20 LuanTable module = new LuanTable(); |
21 try { | 21 try { |
22 module.put( "byte", new LuanJavaFunction(StringLib.class.getMethod("byte_",String.class,Integer.class,Integer.class),null) ); | 22 add( module, "to_binary", String.class ); |
23 module.put( "char", new LuanJavaFunction(StringLib.class.getMethod("char_",new byte[0].getClass()),null) ); | 23 add( module, "to_integers", String.class ); |
24 add( module, "from_integers", new int[0].getClass() ); | |
24 add( module, "find", String.class, String.class, Integer.class, Boolean.class ); | 25 add( module, "find", String.class, String.class, Integer.class, Boolean.class ); |
25 add( module, "format", String.class, new Object[0].getClass() ); | 26 add( module, "format", String.class, new Object[0].getClass() ); |
26 add( module, "gmatch", String.class, String.class ); | 27 add( module, "gmatch", String.class, String.class ); |
27 add( module, "gsub", LuanState.class, String.class, String.class, Object.class, Integer.class ); | 28 add( module, "gsub", LuanState.class, String.class, String.class, Object.class, Integer.class ); |
28 add( module, "len", String.class ); | 29 add( module, "len", String.class ); |
57 | 58 |
58 static int end(String s,Integer i,int dflt) { | 59 static int end(String s,Integer i,int dflt) { |
59 return i==null ? dflt : end(s,i); | 60 return i==null ? dflt : end(s,i); |
60 } | 61 } |
61 | 62 |
62 public static byte[] byte_(String s,Integer i,Integer j) { | 63 public static byte[] to_binary(String s) { |
63 int start = start(s,i,0); | 64 return s.getBytes(); |
64 int end = end(s,j,start+1); | 65 } |
65 return s.substring(start,end).getBytes(); | 66 |
66 } | 67 public static int[] to_integers(String s) { |
67 | 68 char[] a = s.toCharArray(); |
68 public static String char_(byte... bytes) { | 69 int[] chars = new int[a.length]; |
69 return new String(bytes); | 70 for( int i=0; i<a.length; i++ ) { |
71 chars[i] = a[i]; | |
72 } | |
73 return chars; | |
74 } | |
75 | |
76 public static String from_integers(int... chars) { | |
77 char[] a = new char[chars.length]; | |
78 for( int i=0; i<chars.length; i++ ) { | |
79 a[i] = (char)chars[i]; | |
80 } | |
81 return new String(a); | |
70 } | 82 } |
71 | 83 |
72 public static int len(String s) { | 84 public static int len(String s) { |
73 return s.length(); | 85 return s.length(); |
74 } | 86 } |