comparison src/luan/modules/StringLuan.java @ 1721:5c69d2e8bd75

no regex in String.find and String.split
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 26 Jul 2022 12:44:52 -0600
parents 5603ee8e2a71
children 7d2ab44f7a59
comparison
equal deleted inserted replaced
1720:2f4c99c02436 1721:5c69d2e8bd75
2 2
3 import java.security.NoSuchAlgorithmException; 3 import java.security.NoSuchAlgorithmException;
4 import java.util.Arrays; 4 import java.util.Arrays;
5 import java.util.regex.Pattern; 5 import java.util.regex.Pattern;
6 import java.util.regex.Matcher; 6 import java.util.regex.Matcher;
7 import goodjava.util.GoodUtils;
7 import luan.Luan; 8 import luan.Luan;
8 import luan.LuanTable; 9 import luan.LuanTable;
9 import luan.LuanFunction; 10 import luan.LuanFunction;
10 import luan.LuanException; 11 import luan.LuanException;
11 12
90 int start = start(s,i); 91 int start = start(s,i);
91 int end = end(s,j,s.length()); 92 int end = end(s,j,s.length());
92 return s.substring(start,end); 93 return s.substring(start,end);
93 } 94 }
94 95
95 public static Object[] find(String s,String pattern,Integer init,Boolean plain) { 96 public static Object[] find(String s,String s2,Integer init) {
96 int start = start(s,init,0); 97 int start = start(s,init,0);
97 if( Boolean.TRUE.equals(plain) ) { 98 int i = s.indexOf(s2,start);
98 int i = s.indexOf(pattern,start); 99 return i == -1 ? null : new Integer[]{i+1,i+s2.length()};
99 return i == -1 ? null : new Integer[]{i+1,i+pattern.length()};
100 }
101 Matcher m = Pattern.compile(pattern).matcher(s);
102 if( !m.find(start) )
103 return null;
104 int n = m.groupCount();
105 Object[] rtn = new Object[2+n];
106 rtn[0] = m.start() + 1;
107 rtn[1] = m.end();
108 for( int i=0; i<n; i++ ) {
109 rtn[2+i] = m.group(i+1);
110 }
111 return rtn;
112 } 100 }
113 101
114 public static String[] match(String s,String pattern,Integer init) { 102 public static String[] match(String s,String pattern,Integer init) {
115 int start = start(s,init,0); 103 int start = start(s,init,0);
116 Matcher m = Pattern.compile(pattern).matcher(s); 104 Matcher m = Pattern.compile(pattern).matcher(s);
229 public static boolean matches(String s,String pattern) throws LuanException { 217 public static boolean matches(String s,String pattern) throws LuanException {
230 Utils.checkNotNull(s); 218 Utils.checkNotNull(s);
231 return Pattern.compile(pattern).matcher(s).find(); 219 return Pattern.compile(pattern).matcher(s).find();
232 } 220 }
233 221
234 public static String[] split(String s,String pattern,Integer limit) throws LuanException { 222 public static String[] split(String s,String s2,Integer limit) throws LuanException {
235 Utils.checkNotNull(s); 223 Utils.checkNotNull(s);
236 Utils.checkNotNull(pattern,2); 224 Utils.checkNotNull(s2,2);
237 int n = limit==null ? -1 : limit; 225 int n = limit==null ? -1 : limit;
238 return s.split(pattern,n); 226 return GoodUtils.split(s,s2,n);
239 } 227 }
240 228
241 public static String digest_message(String algorithm,String input) throws LuanException, NoSuchAlgorithmException { 229 public static String digest_message(String algorithm,String input) throws LuanException, NoSuchAlgorithmException {
242 Utils.checkNotNull(algorithm,1); 230 Utils.checkNotNull(algorithm,1);
243 Utils.checkNotNull(input,2); 231 Utils.checkNotNull(input,2);