changeset 1777:e59349d53fec

optimize String.replace and regex.gsub
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 10 Aug 2023 16:24:20 -0600
parents da85925fea9c
children 1725fdb6c4f5
files src/luan/modules/RegexLuan.java src/luan/modules/StringLuan.java
diffstat 2 files changed, 29 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
diff -r da85925fea9c -r e59349d53fec src/luan/modules/RegexLuan.java
--- a/src/luan/modules/RegexLuan.java	Fri Jul 28 11:53:03 2023 +0300
+++ b/src/luan/modules/RegexLuan.java	Thu Aug 10 16:24:20 2023 -0600
@@ -71,14 +71,16 @@
 		Utils.checkNotNull(s);
 		int max = n==null ? Integer.MAX_VALUE : n;
 		final Matcher m = pattern.matcher(s);
+		if( max <= 0 || !m.find() )
+			return new Object[]{ s, 0 };
 		if( repl instanceof String ) {
 			String replacement = (String)repl;
 			int i = 0;
 			StringBuffer sb = new StringBuffer();
-			while( i<max && m.find() ) {
+			do {
 				m.appendReplacement(sb,replacement);
 				i++;
-			}
+			} while( i<max && m.find() );
 			m.appendTail(sb);
 			return new Object[]{ sb.toString(), i };
 		}
@@ -86,7 +88,7 @@
 			LuanTable t = (LuanTable)repl;
 			int i = 0;
 			StringBuffer sb = new StringBuffer();
-			while( i<max && m.find() ) {
+			do {
 				String match = m.groupCount()==0 ? m.group() : m.group(1);
 				Object val = t.get(luan,match);
 				if( val != null ) {
@@ -94,7 +96,7 @@
 					m.appendReplacement(sb,replacement);
 				}
 				i++;
-			}
+			} while( i<max && m.find() );
 			m.appendTail(sb);
 			return new Object[]{ sb.toString(), i };
 		}
@@ -102,7 +104,7 @@
 			LuanFunction fn = (LuanFunction)repl;
 			int i = 0;
 			StringBuffer sb = new StringBuffer();
-			while( i<max && m.find() ) {
+			do {
 				Object[] args;
 				final int count = m.groupCount();
 				if( count == 0 ) {
@@ -119,7 +121,7 @@
 					m.appendReplacement(sb,replacement);
 				}
 				i++;
-			}
+			} while( i<max && m.find() );
 			m.appendTail(sb);
 			return new Object[]{ sb.toString(), i };
 		}
diff -r da85925fea9c -r e59349d53fec src/luan/modules/StringLuan.java
--- a/src/luan/modules/StringLuan.java	Fri Jul 28 11:53:03 2023 +0300
+++ b/src/luan/modules/StringLuan.java	Thu Aug 10 16:24:20 2023 -0600
@@ -148,12 +148,32 @@
 		Utils.checkNotNull(s2,2);
 		return s.endsWith(s2);
 	}
-
+/*
 	public static String replace(String s,String target,String replacement) throws LuanException {
 		Utils.checkNotNull(s);
 		Utils.checkNotNull(target,2);
 		Utils.checkNotNull(replacement,3);
 		return s.replace(target,replacement);
 	}
+*/
+	public static String replace(String s,String target,String replacement) throws LuanException {
+		Utils.checkNotNull(s);
+		Utils.checkNotNull(target,2);
+		Utils.checkNotNull(replacement,3);
+		int i = s.indexOf(target);
+		if( i == -1 )
+			return s;
+		int starting = 0;
+		int n = target.length();
+		StringBuilder sb = new StringBuilder();
+		do {
+			sb.append(s,starting,i);
+			sb.append(replacement);
+			starting = i + n;
+			i = s.indexOf(target,starting);
+		} while( i != -1 );
+		sb.append(s,starting,s.length());
+		return sb.toString();
+	}
 
 }