comparison core/src/luan/modules/StringLuan.java @ 646:cdc70de628b5

simplify LuanException
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 29 Mar 2016 19:58:39 -0600
parents 859c0dedc8b6
children b21d82ee5756
comparison
equal deleted inserted replaced
645:859c0dedc8b6 646:cdc70de628b5
29 29
30 static int end(String s,Integer i,int dflt) { 30 static int end(String s,Integer i,int dflt) {
31 return i==null ? dflt : end(s,i); 31 return i==null ? dflt : end(s,i);
32 } 32 }
33 33
34 @LuanMethod public static Integer[] unicode(LuanState luan,String s,Integer i,Integer j) throws LuanException { 34 @LuanMethod public static Integer[] unicode(String s,Integer i,Integer j) throws LuanException {
35 Utils.checkNotNull(luan,s); 35 Utils.checkNotNull(s);
36 int start = start(s,i,1); 36 int start = start(s,i,1);
37 int end = end(s,j,start+1); 37 int end = end(s,j,start+1);
38 Integer[] chars = new Integer[end-start]; 38 Integer[] chars = new Integer[end-start];
39 for( int k=0; k<chars.length; k++ ) { 39 for( int k=0; k<chars.length; k++ ) {
40 chars[k] = (int)s.charAt(start+k); 40 chars[k] = (int)s.charAt(start+k);
52 52
53 @LuanMethod public static byte[] to_binary(String s) { 53 @LuanMethod public static byte[] to_binary(String s) {
54 return s.getBytes(); 54 return s.getBytes();
55 } 55 }
56 56
57 public static String lower(LuanState luan,String s) throws LuanException { 57 public static String lower(String s) throws LuanException {
58 Utils.checkNotNull(luan,s); 58 Utils.checkNotNull(s);
59 return s.toLowerCase(); 59 return s.toLowerCase();
60 } 60 }
61 61
62 public static String upper(LuanState luan,String s) throws LuanException { 62 public static String upper(String s) throws LuanException {
63 Utils.checkNotNull(luan,s); 63 Utils.checkNotNull(s);
64 return s.toUpperCase(); 64 return s.toUpperCase();
65 } 65 }
66 66
67 public static String trim(LuanState luan,String s) throws LuanException { 67 public static String trim(String s) throws LuanException {
68 Utils.checkNotNull(luan,s); 68 Utils.checkNotNull(s);
69 return s.trim(); 69 return s.trim();
70 } 70 }
71 71
72 public static String reverse(LuanState luan,String s) throws LuanException { 72 public static String reverse(String s) throws LuanException {
73 Utils.checkNotNull(luan,s); 73 Utils.checkNotNull(s);
74 return new StringBuilder(s).reverse().toString(); 74 return new StringBuilder(s).reverse().toString();
75 } 75 }
76 76
77 public static String rep(String s,int n,String sep) { 77 public static String rep(String s,int n,String sep) {
78 if( n < 1 ) 78 if( n < 1 )
84 buf.append(s); 84 buf.append(s);
85 } 85 }
86 return buf.toString(); 86 return buf.toString();
87 } 87 }
88 88
89 public static String sub(LuanState luan,String s,int i,Integer j) throws LuanException { 89 public static String sub(String s,int i,Integer j) throws LuanException {
90 Utils.checkNotNull(luan,s); 90 Utils.checkNotNull(s);
91 int start = start(s,i); 91 int start = start(s,i);
92 int end = end(s,j,s.length()); 92 int end = end(s,j,s.length());
93 return s.substring(start,end); 93 return s.substring(start,end);
94 } 94 }
95 95
125 rtn[i] = m.group(i+1); 125 rtn[i] = m.group(i+1);
126 } 126 }
127 return rtn; 127 return rtn;
128 } 128 }
129 129
130 public static LuanFunction gmatch(LuanState luan,String s,String pattern) throws LuanException { 130 public static LuanFunction gmatch(String s,String pattern) throws LuanException {
131 Utils.checkNotNull(luan,s); 131 Utils.checkNotNull(s);
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 @Override 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 null; 136 return null;
145 } 145 }
146 }; 146 };
147 } 147 }
148 148
149 @LuanMethod public static Object[] gsub(LuanState luan,String s,String pattern,Object repl,Integer n) throws LuanException { 149 @LuanMethod public static Object[] gsub(LuanState luan,String s,String pattern,Object repl,Integer n) throws LuanException {
150 Utils.checkNotNull(luan,s); 150 Utils.checkNotNull(s);
151 int max = n==null ? Integer.MAX_VALUE : n; 151 int max = n==null ? Integer.MAX_VALUE : n;
152 final Matcher m = Pattern.compile(pattern).matcher(s); 152 final Matcher m = Pattern.compile(pattern).matcher(s);
153 if( repl instanceof String ) { 153 if( repl instanceof String ) {
154 String replacement = (String)repl; 154 String replacement = (String)repl;
155 int i = 0; 155 int i = 0;
200 i++; 200 i++;
201 } 201 }
202 m.appendTail(sb); 202 m.appendTail(sb);
203 return new Object[]{ sb.toString(), i }; 203 return new Object[]{ sb.toString(), i };
204 } 204 }
205 throw new LuanException(luan, "bad argument #3 to 'gsub' (string/function/table expected)" ); 205 throw new LuanException( "bad argument #3 to 'gsub' (string/function/table expected)" );
206 } 206 }
207 207
208 // note - String.format() is too stupid to convert between ints and floats. 208 // note - String.format() is too stupid to convert between ints and floats.
209 public static String format(String format,Object... args) { 209 public static String format(String format,Object... args) {
210 return String.format(format,args); 210 return String.format(format,args);
220 220
221 public static String encode(String s) { 221 public static String encode(String s) {
222 return Luan.stringEncode(s); 222 return Luan.stringEncode(s);
223 } 223 }
224 224
225 public static Number to_number(LuanState luan,String s,Integer base) throws LuanException { 225 public static Number to_number(String s,Integer base) throws LuanException {
226 Utils.checkNotNull(luan,s); 226 Utils.checkNotNull(s);
227 try { 227 try {
228 if( base == null ) { 228 if( base == null ) {
229 return Double.valueOf(s); 229 return Double.valueOf(s);
230 } else { 230 } else {
231 return Long.valueOf(s,base); 231 return Long.valueOf(s,base);