Mercurial Hosting > luan
comparison src/luan/lib/StringLib.java @ 48:64ecb7a3aad7
rename Lua to Luan
git-svn-id: https://luan-java.googlecode.com/svn/trunk@49 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Fri, 28 Dec 2012 03:29:12 +0000 |
parents | a443637829c1 |
children | 8ede219cd111 |
comparison
equal
deleted
inserted
replaced
47:659c7139e903 | 48:64ecb7a3aad7 |
---|---|
1 package luan.lib; | 1 package luan.lib; |
2 | 2 |
3 import java.util.regex.Pattern; | 3 import java.util.regex.Pattern; |
4 import java.util.regex.Matcher; | 4 import java.util.regex.Matcher; |
5 import luan.Lua; | 5 import luan.Luan; |
6 import luan.LuaState; | 6 import luan.LuanState; |
7 import luan.LuaTable; | 7 import luan.LuanTable; |
8 import luan.LuaFunction; | 8 import luan.LuanFunction; |
9 import luan.LuaJavaFunction; | 9 import luan.LuanJavaFunction; |
10 import luan.LuaElement; | 10 import luan.LuanElement; |
11 import luan.LuaException; | 11 import luan.LuanException; |
12 | 12 |
13 | 13 |
14 public final class StringLib { | 14 public final class StringLib { |
15 | 15 |
16 public static void register(LuaState lua) { | 16 public static void register(LuanState lua) { |
17 LuaTable module = new LuaTable(); | 17 LuanTable module = new LuanTable(); |
18 LuaTable global = lua.global(); | 18 LuanTable global = lua.global(); |
19 global.put("string",module); | 19 global.put("string",module); |
20 try { | 20 try { |
21 module.put( "byte", new LuaJavaFunction(StringLib.class.getMethod("byte_",String.class,Integer.class,Integer.class),null) ); | 21 module.put( "byte", new LuanJavaFunction(StringLib.class.getMethod("byte_",String.class,Integer.class,Integer.class),null) ); |
22 module.put( "char", new LuaJavaFunction(StringLib.class.getMethod("char_",new byte[0].getClass()),null) ); | 22 module.put( "char", new LuanJavaFunction(StringLib.class.getMethod("char_",new byte[0].getClass()),null) ); |
23 add( module, "find", String.class, String.class, Integer.class, Boolean.class ); | 23 add( module, "find", String.class, String.class, Integer.class, Boolean.class ); |
24 add( module, "gmatch", String.class, String.class ); | 24 add( module, "gmatch", String.class, String.class ); |
25 add( module, "gsub", LuaState.class, String.class, String.class, Object.class, Integer.class ); | 25 add( module, "gsub", LuanState.class, String.class, String.class, Object.class, Integer.class ); |
26 add( module, "len", String.class ); | 26 add( module, "len", String.class ); |
27 add( module, "lower", String.class ); | 27 add( module, "lower", String.class ); |
28 add( module, "match", String.class, String.class, Integer.class ); | 28 add( module, "match", String.class, String.class, Integer.class ); |
29 add( module, "rep", String.class, Integer.TYPE, String.class ); | 29 add( module, "rep", String.class, Integer.TYPE, String.class ); |
30 add( module, "reverse", String.class ); | 30 add( module, "reverse", String.class ); |
33 } catch(NoSuchMethodException e) { | 33 } catch(NoSuchMethodException e) { |
34 throw new RuntimeException(e); | 34 throw new RuntimeException(e); |
35 } | 35 } |
36 } | 36 } |
37 | 37 |
38 private static void add(LuaTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException { | 38 private static void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException { |
39 t.put( method, new LuaJavaFunction(StringLib.class.getMethod(method,parameterTypes),null) ); | 39 t.put( method, new LuanJavaFunction(StringLib.class.getMethod(method,parameterTypes),null) ); |
40 } | 40 } |
41 | 41 |
42 static int start(String s,int i) { | 42 static int start(String s,int i) { |
43 return i==0 ? 0 : i > 0 ? i - 1 : s.length() + i; | 43 return i==0 ? 0 : i > 0 ? i - 1 : s.length() + i; |
44 } | 44 } |
124 rtn[i] = m.group(i); | 124 rtn[i] = m.group(i); |
125 } | 125 } |
126 return rtn; | 126 return rtn; |
127 } | 127 } |
128 | 128 |
129 public static LuaFunction gmatch(String s,String pattern) { | 129 public static LuanFunction gmatch(String s,String pattern) { |
130 final Matcher m = Pattern.compile(pattern).matcher(s); | 130 final Matcher m = Pattern.compile(pattern).matcher(s); |
131 return new LuaFunction() { | 131 return new LuanFunction() { |
132 public Object[] call(LuaState lua,Object[] args) { | 132 public Object[] call(LuanState lua,Object[] args) { |
133 if( !m.find() ) | 133 if( !m.find() ) |
134 return LuaFunction.EMPTY_RTN; | 134 return LuanFunction.EMPTY_RTN; |
135 final int n = m.groupCount(); | 135 final int n = m.groupCount(); |
136 if( n == 0 ) | 136 if( n == 0 ) |
137 return new String[]{m.group()}; | 137 return new String[]{m.group()}; |
138 String[] rtn = new String[n]; | 138 String[] rtn = new String[n]; |
139 for( int i=0; i<n; i++ ) { | 139 for( int i=0; i<n; i++ ) { |
142 return rtn; | 142 return rtn; |
143 } | 143 } |
144 }; | 144 }; |
145 } | 145 } |
146 | 146 |
147 public static Object[] gsub(LuaState lua,String s,String pattern,Object repl,Integer n) throws LuaException { | 147 public static Object[] gsub(LuanState lua,String s,String pattern,Object repl,Integer n) throws LuanException { |
148 int max = n==null ? Integer.MAX_VALUE : n; | 148 int max = n==null ? Integer.MAX_VALUE : n; |
149 final Matcher m = Pattern.compile(pattern).matcher(s); | 149 final Matcher m = Pattern.compile(pattern).matcher(s); |
150 if( repl instanceof String ) { | 150 if( repl instanceof String ) { |
151 String replacement = (String)repl; | 151 String replacement = (String)repl; |
152 int i = 0; | 152 int i = 0; |
156 i++; | 156 i++; |
157 } | 157 } |
158 m.appendTail(sb); | 158 m.appendTail(sb); |
159 return new Object[]{ sb.toString(), i }; | 159 return new Object[]{ sb.toString(), i }; |
160 } | 160 } |
161 if( repl instanceof LuaTable ) { | 161 if( repl instanceof LuanTable ) { |
162 LuaTable t = (LuaTable)repl; | 162 LuanTable t = (LuanTable)repl; |
163 int i = 0; | 163 int i = 0; |
164 StringBuffer sb = new StringBuffer(); | 164 StringBuffer sb = new StringBuffer(); |
165 while( i<max && m.find() ) { | 165 while( i<max && m.find() ) { |
166 String match = m.groupCount()==0 ? m.group() : m.group(0); | 166 String match = m.groupCount()==0 ? m.group() : m.group(0); |
167 Object val = t.get(match); | 167 Object val = t.get(match); |
168 if( Lua.toBoolean(val) ) { | 168 if( Luan.toBoolean(val) ) { |
169 String replacement = Lua.asString(val); | 169 String replacement = Luan.asString(val); |
170 if( replacement==null ) | 170 if( replacement==null ) |
171 throw new LuaException( lua, LuaElement.JAVA, "invalid replacement value (a "+Lua.type(val)+")" ); | 171 throw new LuanException( lua, LuanElement.JAVA, "invalid replacement value (a "+Luan.type(val)+")" ); |
172 m.appendReplacement(sb,replacement); | 172 m.appendReplacement(sb,replacement); |
173 } | 173 } |
174 i++; | 174 i++; |
175 } | 175 } |
176 m.appendTail(sb); | 176 m.appendTail(sb); |
177 return new Object[]{ sb.toString(), i }; | 177 return new Object[]{ sb.toString(), i }; |
178 } | 178 } |
179 if( repl instanceof LuaFunction ) { | 179 if( repl instanceof LuanFunction ) { |
180 LuaFunction fn = (LuaFunction)repl; | 180 LuanFunction fn = (LuanFunction)repl; |
181 int i = 0; | 181 int i = 0; |
182 StringBuffer sb = new StringBuffer(); | 182 StringBuffer sb = new StringBuffer(); |
183 while( i<max && m.find() ) { | 183 while( i<max && m.find() ) { |
184 Object[] args; | 184 Object[] args; |
185 final int count = m.groupCount(); | 185 final int count = m.groupCount(); |
189 args = new Object[count]; | 189 args = new Object[count]; |
190 for( int j=0; j<count; j++ ) { | 190 for( int j=0; j<count; j++ ) { |
191 args[j] = m.group(j); | 191 args[j] = m.group(j); |
192 } | 192 } |
193 } | 193 } |
194 Object val = Lua.first( lua.call(fn,LuaElement.JAVA,"repl-arg",args) ); | 194 Object val = Luan.first( lua.call(fn,LuanElement.JAVA,"repl-arg",args) ); |
195 if( Lua.toBoolean(val) ) { | 195 if( Luan.toBoolean(val) ) { |
196 String replacement = Lua.asString(val); | 196 String replacement = Luan.asString(val); |
197 if( replacement==null ) | 197 if( replacement==null ) |
198 throw new LuaException( lua, LuaElement.JAVA, "invalid replacement value (a "+Lua.type(val)+")" ); | 198 throw new LuanException( lua, LuanElement.JAVA, "invalid replacement value (a "+Luan.type(val)+")" ); |
199 m.appendReplacement(sb,replacement); | 199 m.appendReplacement(sb,replacement); |
200 } | 200 } |
201 i++; | 201 i++; |
202 } | 202 } |
203 m.appendTail(sb); | 203 m.appendTail(sb); |
204 return new Object[]{ sb.toString(), i }; | 204 return new Object[]{ sb.toString(), i }; |
205 } | 205 } |
206 throw new LuaException( lua, LuaElement.JAVA, "bad argument #3 to 'gsub' (string/function/table expected)" ); | 206 throw new LuanException( lua, LuanElement.JAVA, "bad argument #3 to 'gsub' (string/function/table expected)" ); |
207 } | 207 } |
208 | 208 |
209 } | 209 } |