comparison core/src/luan/modules/StringLuan.java @ 297:899253043270

remove PackageLuan.load_lib() git-svn-id: https://luan-java.googlecode.com/svn/trunk@298 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Tue, 16 Dec 2014 03:26:43 +0000
parents ae7ae2755b48
children 2f8938fc518c
comparison
equal deleted inserted replaced
296:7ea6dfdf81ba 297:899253043270
7 import luan.LuanTable; 7 import luan.LuanTable;
8 import luan.LuanFunction; 8 import luan.LuanFunction;
9 import luan.LuanJavaFunction; 9 import luan.LuanJavaFunction;
10 import luan.LuanElement; 10 import luan.LuanElement;
11 import luan.LuanException; 11 import luan.LuanException;
12 import luan.LuanMethod;
12 13
13 14
14 public final class StringLuan { 15 public final class StringLuan {
15
16 public static final LuanFunction LOADER = new LuanFunction() {
17 @Override public Object call(LuanState luan,Object[] args) {
18 LuanTable module = Luan.newTable();
19 try {
20 add( module, "to_binary", String.class );
21 module.put( "byte", new LuanJavaFunction(StringLuan.class.getMethod( "byte_", String.class ),null) );
22 module.put( "char", new LuanJavaFunction(StringLuan.class.getMethod( "char_", new int[0].getClass() ),null) );
23 add( module, "concat", LuanState.class, new Object[0].getClass() );
24 add( module, "find", String.class, String.class, Integer.class, Boolean.class );
25 add( module, "format", String.class, new Object[0].getClass() );
26 add( module, "gmatch", LuanState.class, String.class, String.class );
27 add( module, "gsub", LuanState.class, String.class, String.class, Object.class, Integer.class );
28 add( module, "len", LuanState.class, String.class );
29 add( module, "lower", LuanState.class, String.class );
30 add( module, "match", String.class, String.class, Integer.class );
31 add( module, "rep", String.class, Integer.TYPE, String.class );
32 add( module, "reverse", LuanState.class, String.class );
33 add( module, "sub", LuanState.class, String.class, Integer.TYPE, Integer.class );
34 add( module, "trim", LuanState.class, String.class );
35 add( module, "upper", LuanState.class, String.class );
36 } catch(NoSuchMethodException e) {
37 throw new RuntimeException(e);
38 }
39 return module;
40 }
41 };
42
43 private static void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException {
44 t.put( method, new LuanJavaFunction(StringLuan.class.getMethod(method,parameterTypes),null) );
45 }
46 16
47 public static Object __index(LuanState luan,final String s,Object key) throws LuanException { 17 public static Object __index(LuanState luan,final String s,Object key) throws LuanException {
48 LuanTable mod = (LuanTable)PackageLuan.loaded(luan).get("luan:String"); 18 LuanTable mod = (LuanTable)PackageLuan.loaded(luan).get("luan:String");
49 if( mod!=null ) { 19 if( mod!=null ) {
50 Object obj = mod.get(key); 20 Object obj = mod.get(key);
154 } 124 }
155 Matcher m = Pattern.compile(pattern).matcher(s); 125 Matcher m = Pattern.compile(pattern).matcher(s);
156 return m.find(start) ? new int[]{m.start()+1,m.end()} : null; 126 return m.find(start) ? new int[]{m.start()+1,m.end()} : null;
157 } 127 }
158 128
159 public static String[] match(String s,String pattern,Integer init) { 129 @LuanMethod public static String[] match(String s,String pattern,Integer init) {
160 int start = start(s,init,0); 130 int start = start(s,init,0);
161 Matcher m = Pattern.compile(pattern).matcher(s); 131 Matcher m = Pattern.compile(pattern).matcher(s);
162 if( !m.find(start) ) 132 if( !m.find(start) )
163 return null; 133 return null;
164 final int n = m.groupCount(); 134 final int n = m.groupCount();
188 return rtn; 158 return rtn;
189 } 159 }
190 }; 160 };
191 } 161 }
192 162
193 public static Object[] gsub(LuanState luan,String s,String pattern,Object repl,Integer n) throws LuanException { 163 @LuanMethod public static Object[] gsub(LuanState luan,String s,String pattern,Object repl,Integer n) throws LuanException {
194 int max = n==null ? Integer.MAX_VALUE : n; 164 int max = n==null ? Integer.MAX_VALUE : n;
195 final Matcher m = Pattern.compile(pattern).matcher(s); 165 final Matcher m = Pattern.compile(pattern).matcher(s);
196 if( repl instanceof String ) { 166 if( repl instanceof String ) {
197 String replacement = (String)repl; 167 String replacement = (String)repl;
198 int i = 0; 168 int i = 0;