Mercurial Hosting > luan
comparison src/luan/modules/StringLuan.java @ 1722:7d2ab44f7a59
remove String regex fns
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 29 Jul 2022 14:12:01 -0600 |
parents | 5c69d2e8bd75 |
children | e59349d53fec |
comparison
equal
deleted
inserted
replaced
1721:5c69d2e8bd75 | 1722:7d2ab44f7a59 |
---|---|
1 package luan.modules; | 1 package luan.modules; |
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; | |
6 import java.util.regex.Matcher; | |
7 import goodjava.util.GoodUtils; | 5 import goodjava.util.GoodUtils; |
8 import luan.Luan; | 6 import luan.Luan; |
9 import luan.LuanTable; | 7 import luan.LuanTable; |
10 import luan.LuanFunction; | 8 import luan.LuanFunction; |
11 import luan.LuanException; | 9 import luan.LuanException; |
97 int start = start(s,init,0); | 95 int start = start(s,init,0); |
98 int i = s.indexOf(s2,start); | 96 int i = s.indexOf(s2,start); |
99 return i == -1 ? null : new Integer[]{i+1,i+s2.length()}; | 97 return i == -1 ? null : new Integer[]{i+1,i+s2.length()}; |
100 } | 98 } |
101 | 99 |
102 public static String[] match(String s,String pattern,Integer init) { | |
103 int start = start(s,init,0); | |
104 Matcher m = Pattern.compile(pattern).matcher(s); | |
105 if( !m.find(start) ) | |
106 return null; | |
107 int n = m.groupCount(); | |
108 if( n == 0 ) | |
109 return new String[]{m.group()}; | |
110 String[] rtn = new String[n]; | |
111 for( int i=0; i<n; i++ ) { | |
112 rtn[i] = m.group(i+1); | |
113 } | |
114 return rtn; | |
115 } | |
116 | |
117 public static LuanFunction gmatch(String s,String pattern) throws LuanException { | |
118 Utils.checkNotNull(s); | |
119 Utils.checkNotNull(pattern,2); | |
120 final Matcher m = Pattern.compile(pattern).matcher(s); | |
121 return new LuanFunction() { | |
122 @Override public Object call(Luan luan,Object[] args) { | |
123 if( !m.find() ) | |
124 return null; | |
125 final int n = m.groupCount(); | |
126 if( n == 0 ) | |
127 return m.group(); | |
128 String[] rtn = new String[n]; | |
129 for( int i=0; i<n; i++ ) { | |
130 rtn[i] = m.group(i+1); | |
131 } | |
132 return rtn; | |
133 } | |
134 }; | |
135 } | |
136 | |
137 public static Object[] gsub(Luan luan,String s,String pattern,Object repl,Integer n) throws LuanException { | |
138 Utils.checkNotNull(s); | |
139 int max = n==null ? Integer.MAX_VALUE : n; | |
140 final Matcher m = Pattern.compile(pattern).matcher(s); | |
141 if( repl instanceof String ) { | |
142 String replacement = (String)repl; | |
143 int i = 0; | |
144 StringBuffer sb = new StringBuffer(); | |
145 while( i<max && m.find() ) { | |
146 m.appendReplacement(sb,replacement); | |
147 i++; | |
148 } | |
149 m.appendTail(sb); | |
150 return new Object[]{ sb.toString(), i }; | |
151 } | |
152 if( repl instanceof LuanTable ) { | |
153 LuanTable t = (LuanTable)repl; | |
154 int i = 0; | |
155 StringBuffer sb = new StringBuffer(); | |
156 while( i<max && m.find() ) { | |
157 String match = m.groupCount()==0 ? m.group() : m.group(1); | |
158 Object val = t.get(luan,match); | |
159 if( val != null ) { | |
160 String replacement = luan.luanToString(val); | |
161 m.appendReplacement(sb,replacement); | |
162 } | |
163 i++; | |
164 } | |
165 m.appendTail(sb); | |
166 return new Object[]{ sb.toString(), i }; | |
167 } | |
168 if( repl instanceof LuanFunction ) { | |
169 LuanFunction fn = (LuanFunction)repl; | |
170 int i = 0; | |
171 StringBuffer sb = new StringBuffer(); | |
172 while( i<max && m.find() ) { | |
173 Object[] args; | |
174 final int count = m.groupCount(); | |
175 if( count == 0 ) { | |
176 args = new String[]{m.group()}; | |
177 } else { | |
178 args = new String[count]; | |
179 for( int j=0; j<count; j++ ) { | |
180 args[j] = m.group(j+1); | |
181 } | |
182 } | |
183 Object val = Luan.first( fn.call(luan,args) ); | |
184 if( val != null ) { | |
185 String replacement = luan.luanToString(val); | |
186 m.appendReplacement(sb,replacement); | |
187 } | |
188 i++; | |
189 } | |
190 m.appendTail(sb); | |
191 return new Object[]{ sb.toString(), i }; | |
192 } | |
193 throw new LuanException( "bad argument #3 to 'gsub' (string/function/table expected)" ); | |
194 } | |
195 | |
196 // note - String.format() is too stupid to convert between ints and floats. | 100 // note - String.format() is too stupid to convert between ints and floats. |
197 public static String format(String format,Object... args) { | 101 public static String format(String format,Object... args) { |
198 return String.format(format,args); | 102 return String.format(format,args); |
199 } | 103 } |
200 | 104 |
210 } else { | 114 } else { |
211 return Long.valueOf(s,base); | 115 return Long.valueOf(s,base); |
212 } | 116 } |
213 } catch(NumberFormatException e) {} | 117 } catch(NumberFormatException e) {} |
214 return null; | 118 return null; |
215 } | |
216 | |
217 public static boolean matches(String s,String pattern) throws LuanException { | |
218 Utils.checkNotNull(s); | |
219 return Pattern.compile(pattern).matcher(s).find(); | |
220 } | 119 } |
221 | 120 |
222 public static String[] split(String s,String s2,Integer limit) throws LuanException { | 121 public static String[] split(String s,String s2,Integer limit) throws LuanException { |
223 Utils.checkNotNull(s); | 122 Utils.checkNotNull(s); |
224 Utils.checkNotNull(s2,2); | 123 Utils.checkNotNull(s2,2); |