comparison src/luan/modules/StringLuan.java @ 1715:ad44e849c60c

add String.regex_compile
author Franklin Schmidt <fschmidt@gmail.com>
date Sat, 23 Jul 2022 21:53:04 -0600
parents ea56e3b04c03
children b82767112d8e
comparison
equal deleted inserted replaced
1714:31a82b0d0a87 1715:ad44e849c60c
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,String pattern,Integer init,Boolean plain) { 95 public static Object[] find(String s,Object pattern,Integer init,Boolean plain) throws LuanException {
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 int i = s.indexOf(pattern,start); 98 String ptn = (String)pattern;
99 return i == -1 ? null : new Integer[]{i+1,i+pattern.length()}; 99 int i = s.indexOf(ptn,start);
100 } 100 return i == -1 ? null : new Integer[]{i+1,i+ptn.length()};
101 Matcher m = Pattern.compile(pattern).matcher(s); 101 }
102 Matcher m = pattern(pattern).matcher(s);
102 if( !m.find(start) ) 103 if( !m.find(start) )
103 return null; 104 return null;
104 int n = m.groupCount(); 105 int n = m.groupCount();
105 Object[] rtn = new Object[2+n]; 106 Object[] rtn = new Object[2+n];
106 rtn[0] = m.start() + 1; 107 rtn[0] = m.start() + 1;
109 rtn[2+i] = m.group(i+1); 110 rtn[2+i] = m.group(i+1);
110 } 111 }
111 return rtn; 112 return rtn;
112 } 113 }
113 114
114 public static String[] match(String s,String pattern,Integer init) { 115 public static String[] match(String s,Object pattern,Integer init) throws LuanException {
115 int start = start(s,init,0); 116 int start = start(s,init,0);
116 Matcher m = Pattern.compile(pattern).matcher(s); 117 Matcher m = pattern(pattern).matcher(s);
117 if( !m.find(start) ) 118 if( !m.find(start) )
118 return null; 119 return null;
119 int n = m.groupCount(); 120 int n = m.groupCount();
120 if( n == 0 ) 121 if( n == 0 )
121 return new String[]{m.group()}; 122 return new String[]{m.group()};
124 rtn[i] = m.group(i+1); 125 rtn[i] = m.group(i+1);
125 } 126 }
126 return rtn; 127 return rtn;
127 } 128 }
128 129
129 public static LuanFunction gmatch(String s,String pattern) throws LuanException { 130 public static LuanFunction gmatch(String s,Object pattern) throws LuanException {
130 Utils.checkNotNull(s); 131 Utils.checkNotNull(s);
131 Utils.checkNotNull(pattern,2); 132 final Matcher m = pattern(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(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,String pattern,Object repl,Integer n) throws LuanException { 149 public static Object[] gsub(Luan luan,String s,Object 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.compile(pattern).matcher(s); 152 final Matcher m = pattern(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 public static boolean matches(String s,String pattern) throws LuanException { 229 private static Pattern pattern(Object pattern) throws LuanException {
230 Utils.checkNotNull(s); 230 if( pattern instanceof Pattern ) {
231 return Pattern.compile(pattern).matcher(s).find(); 231 return (Pattern)pattern;
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();
232 } 242 }
233 243
234 public static String[] split(String s,String pattern,Integer limit) throws LuanException { 244 public static String[] split(String s,String pattern,Integer limit) throws LuanException {
235 Utils.checkNotNull(s); 245 Utils.checkNotNull(s);
236 Utils.checkNotNull(pattern,1); 246 Utils.checkNotNull(pattern,1);