comparison src/luan/lib/StringLib.java @ 49:8ede219cd111

add WebShell git-svn-id: https://luan-java.googlecode.com/svn/trunk@50 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Fri, 28 Dec 2012 19:35:04 +0000
parents 64ecb7a3aad7
children f86e4f77ef32
comparison
equal deleted inserted replaced
48:64ecb7a3aad7 49:8ede219cd111
11 import luan.LuanException; 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(LuanState lua) { 16 public static void register(LuanState luan) {
17 LuanTable module = new LuanTable(); 17 LuanTable module = new LuanTable();
18 LuanTable global = lua.global(); 18 LuanTable global = luan.global();
19 global.put("string",module); 19 global.put("string",module);
20 try { 20 try {
21 module.put( "byte", new LuanJavaFunction(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 LuanJavaFunction(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 );
127 } 127 }
128 128
129 public static LuanFunction 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 LuanFunction() { 131 return new LuanFunction() {
132 public Object[] call(LuanState lua,Object[] args) { 132 public Object[] call(LuanState luan,Object[] args) {
133 if( !m.find() ) 133 if( !m.find() )
134 return LuanFunction.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()};
142 return rtn; 142 return rtn;
143 } 143 }
144 }; 144 };
145 } 145 }
146 146
147 public static Object[] gsub(LuanState lua,String s,String pattern,Object repl,Integer n) throws LuanException { 147 public static Object[] gsub(LuanState luan,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;
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( Luan.toBoolean(val) ) { 168 if( Luan.toBoolean(val) ) {
169 String replacement = Luan.asString(val); 169 String replacement = Luan.asString(val);
170 if( replacement==null ) 170 if( replacement==null )
171 throw new LuanException( lua, LuanElement.JAVA, "invalid replacement value (a "+Luan.type(val)+")" ); 171 throw new LuanException( luan, 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);
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 = Luan.first( lua.call(fn,LuanElement.JAVA,"repl-arg",args) ); 194 Object val = Luan.first( luan.call(fn,LuanElement.JAVA,"repl-arg",args) );
195 if( Luan.toBoolean(val) ) { 195 if( Luan.toBoolean(val) ) {
196 String replacement = Luan.asString(val); 196 String replacement = Luan.asString(val);
197 if( replacement==null ) 197 if( replacement==null )
198 throw new LuanException( lua, LuanElement.JAVA, "invalid replacement value (a "+Luan.type(val)+")" ); 198 throw new LuanException( luan, 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 LuanException( lua, LuanElement.JAVA, "bad argument #3 to 'gsub' (string/function/table expected)" ); 206 throw new LuanException( luan, LuanElement.JAVA, "bad argument #3 to 'gsub' (string/function/table expected)" );
207 } 207 }
208 208
209 } 209 }