comparison src/luan/modules/StringLuan.java @ 1716:b82767112d8e

add String.regex
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 24 Jul 2022 23:43:03 -0600
parents ad44e849c60c
children c637a2a1023d
comparison
equal deleted inserted replaced
1715:ad44e849c60c 1716:b82767112d8e
90 int start = start(s,i); 90 int start = start(s,i);
91 int end = end(s,j,s.length()); 91 int end = end(s,j,s.length());
92 return s.substring(start,end); 92 return s.substring(start,end);
93 } 93 }
94 94
95 public static Object[] find(String s,Object pattern,Integer init,Boolean plain) throws LuanException { 95 public static Object[] find(String s,String pattern,Integer init,Boolean plain) {
96 int start = start(s,init,0); 96 int start = start(s,init,0);
97 if( Boolean.TRUE.equals(plain) ) { 97 if( Boolean.TRUE.equals(plain) ) {
98 String ptn = (String)pattern; 98 int i = s.indexOf(pattern,start);
99 int i = s.indexOf(ptn,start); 99 return i == -1 ? null : new Integer[]{i+1,i+pattern.length()};
100 return i == -1 ? null : new Integer[]{i+1,i+ptn.length()}; 100 }
101 } 101 Matcher m = Pattern.compile(pattern).matcher(s);
102 Matcher m = pattern(pattern).matcher(s);
103 if( !m.find(start) ) 102 if( !m.find(start) )
104 return null; 103 return null;
105 int n = m.groupCount(); 104 int n = m.groupCount();
106 Object[] rtn = new Object[2+n]; 105 Object[] rtn = new Object[2+n];
107 rtn[0] = m.start() + 1; 106 rtn[0] = m.start() + 1;
110 rtn[2+i] = m.group(i+1); 109 rtn[2+i] = m.group(i+1);
111 } 110 }
112 return rtn; 111 return rtn;
113 } 112 }
114 113
115 public static String[] match(String s,Object pattern,Integer init) throws LuanException { 114 public static String[] match(String s,String pattern,Integer init) {
116 int start = start(s,init,0); 115 int start = start(s,init,0);
117 Matcher m = pattern(pattern).matcher(s); 116 Matcher m = Pattern.compile(pattern).matcher(s);
118 if( !m.find(start) ) 117 if( !m.find(start) )
119 return null; 118 return null;
120 int n = m.groupCount(); 119 int n = m.groupCount();
121 if( n == 0 ) 120 if( n == 0 )
122 return new String[]{m.group()}; 121 return new String[]{m.group()};
125 rtn[i] = m.group(i+1); 124 rtn[i] = m.group(i+1);
126 } 125 }
127 return rtn; 126 return rtn;
128 } 127 }
129 128
130 public static LuanFunction gmatch(String s,Object 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(pattern).matcher(s); 131 Utils.checkNotNull(pattern,2);
132 final Matcher m = Pattern.compile(pattern).matcher(s);
133 return new LuanFunction() { 133 return new LuanFunction() {
134 @Override public Object call(Luan luan,Object[] args) { 134 @Override public Object call(Luan luan,Object[] args) {
135 if( !m.find() ) 135 if( !m.find() )
136 return null; 136 return null;
137 final int n = m.groupCount(); 137 final int n = m.groupCount();
144 return rtn; 144 return rtn;
145 } 145 }
146 }; 146 };
147 } 147 }
148 148
149 public static Object[] gsub(Luan luan,String s,Object pattern,Object repl,Integer n) throws LuanException { 149 public static Object[] gsub(Luan luan,String s,String pattern,Object repl,Integer n) throws LuanException {
150 Utils.checkNotNull(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(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;
156 StringBuffer sb = new StringBuffer(); 156 StringBuffer sb = new StringBuffer();
157 while( i<max && m.find() ) { 157 while( i<max && m.find() ) {
224 } 224 }
225 } catch(NumberFormatException e) {} 225 } catch(NumberFormatException e) {}
226 return null; 226 return null;
227 } 227 }
228 228
229 private static Pattern pattern(Object pattern) throws LuanException { 229 public static boolean matches(String s,String pattern) throws LuanException {
230 if( pattern instanceof Pattern ) { 230 Utils.checkNotNull(s);
231 return (Pattern)pattern; 231 return Pattern.compile(pattern).matcher(s).find();
232 } else if( pattern instanceof String ) {
233 return Pattern.compile((String)pattern);
234 } else {
235 throw new LuanException( "bad argument #2 (string or compiled pattern expected)" );
236 }
237 }
238
239 public static boolean matches(String s,Object pattern) throws LuanException {
240 Utils.checkNotNull(s);
241 return pattern(pattern).matcher(s).find();
242 } 232 }
243 233
244 public static String[] split(String s,String pattern,Integer limit) throws LuanException { 234 public static String[] split(String s,String pattern,Integer limit) throws LuanException {
245 Utils.checkNotNull(s); 235 Utils.checkNotNull(s);
246 Utils.checkNotNull(pattern,1); 236 Utils.checkNotNull(pattern,2);
247 int n = limit==null ? -1 : limit; 237 int n = limit==null ? -1 : limit;
248 return s.split(pattern,n); 238 return s.split(pattern,n);
249 } 239 }
250 240
251 public static String digest_message(String algorithm,String input) throws LuanException, NoSuchAlgorithmException { 241 public static String digest_message(String algorithm,String input) throws LuanException, NoSuchAlgorithmException {
252 Utils.checkNotNull(algorithm,1); 242 Utils.checkNotNull(algorithm,1);
253 Utils.checkNotNull(input,2); 243 Utils.checkNotNull(input,2);
254 return BinaryLuan.to_hex( BinaryLuan.digest_message( algorithm, input.getBytes() ) ); 244 return BinaryLuan.to_hex( BinaryLuan.digest_message( algorithm, input.getBytes() ) );
255 } 245 }
256 246
247 public static boolean contains(String s,String s2) throws LuanException {
248 Utils.checkNotNull(s);
249 Utils.checkNotNull(s2,2);
250 return s.contains(s2);
251 }
252
253 public static boolean starts_with(String s,String s2) throws LuanException {
254 Utils.checkNotNull(s);
255 Utils.checkNotNull(s2,2);
256 return s.startsWith(s2);
257 }
258
259 public static boolean ends_with(String s,String s2) throws LuanException {
260 Utils.checkNotNull(s);
261 Utils.checkNotNull(s2,2);
262 return s.endsWith(s2);
263 }
264
257 } 265 }