comparison src/luan/modules/RegexLuan.java @ 1777:e59349d53fec

optimize String.replace and regex.gsub
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 10 Aug 2023 16:24:20 -0600
parents 2f3a8f16f583
children
comparison
equal deleted inserted replaced
1776:da85925fea9c 1777:e59349d53fec
69 69
70 public Object[] gsub(Luan luan,String s,Object repl,Integer n) throws LuanException { 70 public Object[] gsub(Luan luan,String s,Object repl,Integer n) throws LuanException {
71 Utils.checkNotNull(s); 71 Utils.checkNotNull(s);
72 int max = n==null ? Integer.MAX_VALUE : n; 72 int max = n==null ? Integer.MAX_VALUE : n;
73 final Matcher m = pattern.matcher(s); 73 final Matcher m = pattern.matcher(s);
74 if( max <= 0 || !m.find() )
75 return new Object[]{ s, 0 };
74 if( repl instanceof String ) { 76 if( repl instanceof String ) {
75 String replacement = (String)repl; 77 String replacement = (String)repl;
76 int i = 0; 78 int i = 0;
77 StringBuffer sb = new StringBuffer(); 79 StringBuffer sb = new StringBuffer();
78 while( i<max && m.find() ) { 80 do {
79 m.appendReplacement(sb,replacement); 81 m.appendReplacement(sb,replacement);
80 i++; 82 i++;
81 } 83 } while( i<max && m.find() );
82 m.appendTail(sb); 84 m.appendTail(sb);
83 return new Object[]{ sb.toString(), i }; 85 return new Object[]{ sb.toString(), i };
84 } 86 }
85 if( repl instanceof LuanTable ) { 87 if( repl instanceof LuanTable ) {
86 LuanTable t = (LuanTable)repl; 88 LuanTable t = (LuanTable)repl;
87 int i = 0; 89 int i = 0;
88 StringBuffer sb = new StringBuffer(); 90 StringBuffer sb = new StringBuffer();
89 while( i<max && m.find() ) { 91 do {
90 String match = m.groupCount()==0 ? m.group() : m.group(1); 92 String match = m.groupCount()==0 ? m.group() : m.group(1);
91 Object val = t.get(luan,match); 93 Object val = t.get(luan,match);
92 if( val != null ) { 94 if( val != null ) {
93 String replacement = luan.luanToString(val); 95 String replacement = luan.luanToString(val);
94 m.appendReplacement(sb,replacement); 96 m.appendReplacement(sb,replacement);
95 } 97 }
96 i++; 98 i++;
97 } 99 } while( i<max && m.find() );
98 m.appendTail(sb); 100 m.appendTail(sb);
99 return new Object[]{ sb.toString(), i }; 101 return new Object[]{ sb.toString(), i };
100 } 102 }
101 if( repl instanceof LuanFunction ) { 103 if( repl instanceof LuanFunction ) {
102 LuanFunction fn = (LuanFunction)repl; 104 LuanFunction fn = (LuanFunction)repl;
103 int i = 0; 105 int i = 0;
104 StringBuffer sb = new StringBuffer(); 106 StringBuffer sb = new StringBuffer();
105 while( i<max && m.find() ) { 107 do {
106 Object[] args; 108 Object[] args;
107 final int count = m.groupCount(); 109 final int count = m.groupCount();
108 if( count == 0 ) { 110 if( count == 0 ) {
109 args = new String[]{m.group()}; 111 args = new String[]{m.group()};
110 } else { 112 } else {
117 if( val != null ) { 119 if( val != null ) {
118 String replacement = luan.luanToString(val); 120 String replacement = luan.luanToString(val);
119 m.appendReplacement(sb,replacement); 121 m.appendReplacement(sb,replacement);
120 } 122 }
121 i++; 123 i++;
122 } 124 } while( i<max && m.find() );
123 m.appendTail(sb); 125 m.appendTail(sb);
124 return new Object[]{ sb.toString(), i }; 126 return new Object[]{ sb.toString(), i };
125 } 127 }
126 throw new LuanException( "bad argument #3 to 'gsub' (string/function/table expected)" ); 128 throw new LuanException( "bad argument #3 to 'gsub' (string/function/table expected)" );
127 } 129 }