Mercurial Hosting > luan
comparison src/luan/lib/StringLib.java @ 86:6db8f286fa6c
_ENV is per module, not global
git-svn-id: https://luan-java.googlecode.com/svn/trunk@87 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Wed, 27 Feb 2013 08:03:51 +0000 |
parents | 805929c3c6e1 |
children | eaf37cfa30c2 |
comparison
equal
deleted
inserted
replaced
85:b2551f00bc51 | 86:6db8f286fa6c |
---|---|
4 import java.util.regex.Matcher; | 4 import java.util.regex.Matcher; |
5 import luan.Luan; | 5 import luan.Luan; |
6 import luan.LuanState; | 6 import luan.LuanState; |
7 import luan.LuanTable; | 7 import luan.LuanTable; |
8 import luan.LuanFunction; | 8 import luan.LuanFunction; |
9 import luan.LuanLoader; | |
9 import luan.LuanJavaFunction; | 10 import luan.LuanJavaFunction; |
10 import luan.LuanElement; | 11 import luan.LuanElement; |
11 import luan.LuanException; | 12 import luan.LuanException; |
12 | 13 |
13 | 14 |
14 public final class StringLib { | 15 public final class StringLib { |
15 | 16 |
16 public static final String NAME = "string"; | 17 public static final String NAME = "string"; |
17 | 18 |
18 public static final LuanFunction LOADER = new LuanFunction() { | 19 public static final LuanLoader LOADER = new LuanLoader() { |
19 public Object[] call(LuanState luan,Object[] args) throws LuanException { | 20 @Override protected void load(LuanState luan) { |
20 LuanTable module = new LuanTable(); | 21 LuanTable module = new LuanTable(); |
21 LuanTable global = luan.global(); | |
22 try { | 22 try { |
23 module.put( "byte", new LuanJavaFunction(StringLib.class.getMethod("byte_",String.class,Integer.class,Integer.class),null) ); | 23 module.put( "byte", new LuanJavaFunction(StringLib.class.getMethod("byte_",String.class,Integer.class,Integer.class),null) ); |
24 module.put( "char", new LuanJavaFunction(StringLib.class.getMethod("char_",new byte[0].getClass()),null) ); | 24 module.put( "char", new LuanJavaFunction(StringLib.class.getMethod("char_",new byte[0].getClass()),null) ); |
25 add( module, "find", String.class, String.class, Integer.class, Boolean.class ); | 25 add( module, "find", String.class, String.class, Integer.class, Boolean.class ); |
26 add( module, "format", String.class, new Object[0].getClass() ); | 26 add( module, "format", String.class, new Object[0].getClass() ); |
34 add( module, "sub", String.class, Integer.TYPE, Integer.class ); | 34 add( module, "sub", String.class, Integer.TYPE, Integer.class ); |
35 add( module, "upper", String.class ); | 35 add( module, "upper", String.class ); |
36 } catch(NoSuchMethodException e) { | 36 } catch(NoSuchMethodException e) { |
37 throw new RuntimeException(e); | 37 throw new RuntimeException(e); |
38 } | 38 } |
39 return new Object[]{module}; | 39 luan.loaded().put(NAME,module); |
40 } | 40 } |
41 }; | 41 }; |
42 | 42 |
43 private static void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException { | 43 private static void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException { |
44 t.put( method, new LuanJavaFunction(StringLib.class.getMethod(method,parameterTypes),null) ); | 44 t.put( method, new LuanJavaFunction(StringLib.class.getMethod(method,parameterTypes),null) ); |
132 public static LuanFunction gmatch(String s,String pattern) { | 132 public static LuanFunction gmatch(String s,String pattern) { |
133 final Matcher m = Pattern.compile(pattern).matcher(s); | 133 final Matcher m = Pattern.compile(pattern).matcher(s); |
134 return new LuanFunction() { | 134 return new LuanFunction() { |
135 public Object[] call(LuanState luan,Object[] args) { | 135 public Object[] call(LuanState luan,Object[] args) { |
136 if( !m.find() ) | 136 if( !m.find() ) |
137 return LuanFunction.EMPTY_RTN; | 137 return LuanFunction.EMPTY; |
138 final int n = m.groupCount(); | 138 final int n = m.groupCount(); |
139 if( n == 0 ) | 139 if( n == 0 ) |
140 return new String[]{m.group()}; | 140 return new String[]{m.group()}; |
141 String[] rtn = new String[n]; | 141 String[] rtn = new String[n]; |
142 for( int i=0; i<n; i++ ) { | 142 for( int i=0; i<n; i++ ) { |