comparison src/luan/modules/StringLuan.java @ 1333:25746915a241

merge Luan and LuanState
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 12 Feb 2019 22:33:40 -0700
parents 9fa8b8389578
children c88b486a9511
comparison
equal deleted inserted replaced
1332:11b7e11f9ed5 1333:25746915a241
2 2
3 import java.util.Arrays; 3 import java.util.Arrays;
4 import java.util.regex.Pattern; 4 import java.util.regex.Pattern;
5 import java.util.regex.Matcher; 5 import java.util.regex.Matcher;
6 import luan.Luan; 6 import luan.Luan;
7 import luan.LuanState;
8 import luan.LuanTable; 7 import luan.LuanTable;
9 import luan.LuanFunction; 8 import luan.LuanFunction;
10 import luan.LuanException; 9 import luan.LuanException;
11 import luan.LuanMethod; 10 import luan.LuanMethod;
12 11
129 128
130 public static LuanFunction gmatch(String s,String pattern) throws LuanException { 129 public static LuanFunction gmatch(String s,String pattern) throws LuanException {
131 Utils.checkNotNull(s); 130 Utils.checkNotNull(s);
132 final Matcher m = Pattern.compile(pattern).matcher(s); 131 final Matcher m = Pattern.compile(pattern).matcher(s);
133 return new LuanFunction() { 132 return new LuanFunction() {
134 @Override public Object call(LuanState luan,Object[] args) { 133 @Override public Object call(Luan luan,Object[] args) {
135 if( !m.find() ) 134 if( !m.find() )
136 return null; 135 return null;
137 final int n = m.groupCount(); 136 final int n = m.groupCount();
138 if( n == 0 ) 137 if( n == 0 )
139 return m.group(); 138 return m.group();
144 return rtn; 143 return rtn;
145 } 144 }
146 }; 145 };
147 } 146 }
148 147
149 @LuanMethod public static Object[] gsub(LuanState luan,String s,String pattern,Object repl,Integer n) throws LuanException { 148 @LuanMethod public static Object[] gsub(Luan luan,String s,String pattern,Object repl,Integer n) throws LuanException {
150 Utils.checkNotNull(s); 149 Utils.checkNotNull(s);
151 int max = n==null ? Integer.MAX_VALUE : n; 150 int max = n==null ? Integer.MAX_VALUE : n;
152 final Matcher m = Pattern.compile(pattern).matcher(s); 151 final Matcher m = Pattern.compile(pattern).matcher(s);
153 if( repl instanceof String ) { 152 if( repl instanceof String ) {
154 String replacement = (String)repl; 153 String replacement = (String)repl;
208 // note - String.format() is too stupid to convert between ints and floats. 207 // note - String.format() is too stupid to convert between ints and floats.
209 public static String format(String format,Object... args) { 208 public static String format(String format,Object... args) {
210 return String.format(format,args); 209 return String.format(format,args);
211 } 210 }
212 211
213 public static String concat(LuanState luan,Object... args) throws LuanException { 212 public static String concat(Luan luan,Object... args) throws LuanException {
214 StringBuilder sb = new StringBuilder(); 213 StringBuilder sb = new StringBuilder();
215 for( Object arg : args ) { 214 for( Object arg : args ) {
216 sb.append( luan.toString(arg) ); 215 sb.append( luan.toString(arg) );
217 } 216 }
218 return sb.toString(); 217 return sb.toString();