comparison src/luan/lib/StringLib.java @ 112:f5af13062b10

fix previous rev git-svn-id: https://luan-java.googlecode.com/svn/trunk@113 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Fri, 23 May 2014 22:52:39 +0000
parents 3c404a296995
children 8c706d6eb5dc
comparison
equal deleted inserted replaced
111:2428ecfed375 112:f5af13062b10
14 public final class StringLib { 14 public final class StringLib {
15 15
16 public static final String NAME = "String"; 16 public static final String NAME = "String";
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 module.put( "byte", new LuanJavaFunction(StringLib.class.getMethod("byte_",String.class,Integer.class,Integer.class),null) );
23 module.put( "char", new LuanJavaFunction(StringLib.class.getMethod("char_",new byte[0].getClass()),null) ); 23 module.put( "char", new LuanJavaFunction(StringLib.class.getMethod("char_",new byte[0].getClass()),null) );
24 add( module, "find", String.class, String.class, Integer.class, Boolean.class ); 24 add( module, "find", String.class, String.class, Integer.class, Boolean.class );
33 add( module, "sub", String.class, Integer.TYPE, Integer.class ); 33 add( module, "sub", String.class, Integer.TYPE, Integer.class );
34 add( module, "upper", String.class ); 34 add( module, "upper", String.class );
35 } catch(NoSuchMethodException e) { 35 } catch(NoSuchMethodException e) {
36 throw new RuntimeException(e); 36 throw new RuntimeException(e);
37 } 37 }
38 return new Object[]{module}; 38 return module;
39 } 39 }
40 }; 40 };
41 41
42 private static void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException { 42 private static void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException {
43 t.put( method, new LuanJavaFunction(StringLib.class.getMethod(method,parameterTypes),null) ); 43 t.put( method, new LuanJavaFunction(StringLib.class.getMethod(method,parameterTypes),null) );
129 } 129 }
130 130
131 public static LuanFunction gmatch(String s,String pattern) { 131 public static LuanFunction gmatch(String s,String pattern) {
132 final Matcher m = Pattern.compile(pattern).matcher(s); 132 final Matcher m = Pattern.compile(pattern).matcher(s);
133 return new LuanFunction() { 133 return new LuanFunction() {
134 public Object[] call(LuanState luan,Object[] args) { 134 @Override public Object call(LuanState luan,Object[] args) {
135 if( !m.find() ) 135 if( !m.find() )
136 return LuanFunction.EMPTY; 136 return null;
137 final int n = m.groupCount(); 137 final int n = m.groupCount();
138 if( n == 0 ) 138 if( n == 0 )
139 return new String[]{m.group()}; 139 return m.group();
140 String[] rtn = new String[n]; 140 String[] rtn = new String[n];
141 for( int i=0; i<n; i++ ) { 141 for( int i=0; i<n; i++ ) {
142 rtn[i] = m.group(i); 142 rtn[i] = m.group(i);
143 } 143 }
144 return rtn; 144 return rtn;