changeset 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 2f4c99c02436
children 7d2ab44f7a59
files src/luan/modules/StringLuan.java website/src/manual.html.luan
diffstat 2 files changed, 12 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
diff -r 2f4c99c02436 -r 5c69d2e8bd75 src/luan/modules/StringLuan.java
--- a/src/luan/modules/StringLuan.java	Mon Jul 25 17:31:50 2022 -0600
+++ b/src/luan/modules/StringLuan.java	Tue Jul 26 12:44:52 2022 -0600
@@ -4,6 +4,7 @@
 import java.util.Arrays;
 import java.util.regex.Pattern;
 import java.util.regex.Matcher;
+import goodjava.util.GoodUtils;
 import luan.Luan;
 import luan.LuanTable;
 import luan.LuanFunction;
@@ -92,23 +93,10 @@
 		return s.substring(start,end);
 	}
 
-	public static Object[] find(String s,String pattern,Integer init,Boolean plain) {
+	public static Object[] find(String s,String s2,Integer init) {
 		int start = start(s,init,0);
-		if( Boolean.TRUE.equals(plain) ) {
-			int i = s.indexOf(pattern,start);
-			return i == -1 ? null : new Integer[]{i+1,i+pattern.length()};
-		}
-		Matcher m = Pattern.compile(pattern).matcher(s);
-		if( !m.find(start) )
-			return null;
-		int n = m.groupCount();
-		Object[] rtn = new Object[2+n];
-		rtn[0] = m.start() + 1;
-		rtn[1] = m.end();
-		for( int i=0; i<n; i++ ) {
-			rtn[2+i] = m.group(i+1);
-		}
-		return rtn;
+		int i = s.indexOf(s2,start);
+		return i == -1 ? null : new Integer[]{i+1,i+s2.length()};
 	}
 
 	public static String[] match(String s,String pattern,Integer init) {
@@ -231,11 +219,11 @@
 		return Pattern.compile(pattern).matcher(s).find();
 	}
 
-	public static String[] split(String s,String pattern,Integer limit) throws LuanException {
+	public static String[] split(String s,String s2,Integer limit) throws LuanException {
 		Utils.checkNotNull(s);
-		Utils.checkNotNull(pattern,2);
+		Utils.checkNotNull(s2,2);
 		int n = limit==null ? -1 : limit;
-		return s.split(pattern,n);
+		return GoodUtils.split(s,s2,n);
 	}
 
 	public static String digest_message(String algorithm,String input) throws LuanException, NoSuchAlgorithmException {
diff -r 2f4c99c02436 -r 5c69d2e8bd75 website/src/manual.html.luan
--- a/website/src/manual.html.luan	Mon Jul 25 17:31:50 2022 -0600
+++ b/website/src/manual.html.luan	Tue Jul 26 12:44:52 2022 -0600
@@ -2446,23 +2446,18 @@
 						end
 					}
 					["String.find"] = {
-						title = "<code>String.find (s, pattern [, init [, plain]])</code>"
+						title = "<code>String.find (s, s2 [, init])</code>"
 						content = function()
 %>
 <p>
-Looks for the first match of
-<code>pattern</code> (see <a href="http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html">Pattern</a>) in the string <code>s</code>.
+Looks for the first substring
+<code>s2</code> in the string <code>s</code>.
 If it finds a match, then <code>find</code> returns the indices of&nbsp;<code>s</code>
 where this occurrence starts and ends;
 otherwise, it returns <b>nil</b>.
 A third, optional numerical argument <code>init</code> specifies
 where to start the search;
 its default value is&nbsp;1 and can be negative.
-A value of <b>true</b> as a fourth, optional argument <code>plain</code>
-turns off the pattern matching facilities,
-so the function does a plain "find substring" operation,
-with no characters in <code>pattern</code> being considered magic.
-Note that if <code>plain</code> is given, then <code>init</code> must be given as well.
 </p>
 
 <p>
@@ -2709,11 +2704,11 @@
 						end
 					}
 					["String.split"] = {
-						title = "<code>String.split (s, pattern [, limit])</code>"
+						title = "<code>String.split (s, s2 [, limit])</code>"
 						content = function()
 %>
 <p>
-Splits <code>s</code> using regex <code>pattern</code> and returns the results.  If <code>limit</code> is positive, then only returns at most that many results.  If <code>limit</code> is zero, then remove trailing empty results.
+Splits <code>s</code> using substring <code>s2</code> and returns the results.  If <code>limit</code> is positive, then only returns at most that many results.  If <code>limit</code> is zero, then remove trailing empty results.
 </p>
 <%
 						end