Mercurial Hosting > luan
annotate src/luan/modules/RegexLuan.java @ 1802:ca98dee04e08 default tip
add Parsers.json_null
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 21 Apr 2024 21:25:15 -0600 |
parents | e59349d53fec |
children |
rev | line source |
---|---|
1716 | 1 package luan.modules; |
2 | |
3 import java.util.regex.Pattern; | |
4 import java.util.regex.Matcher; | |
5 import luan.Luan; | |
6 import luan.LuanMutable; | |
7 import luan.LuanTable; | |
8 import luan.LuanFunction; | |
9 import luan.LuanException; | |
10 | |
11 | |
12 public final class RegexLuan implements LuanMutable { | |
13 public Pattern pattern; | |
14 private boolean immutable = false; | |
15 | |
16 public RegexLuan(String s) { | |
17 this.pattern = Pattern.compile(s); | |
18 } | |
19 | |
20 @Override public boolean isImmutable() { | |
21 return immutable; | |
22 } | |
23 | |
24 @Override public void makeImmutable() { | |
25 if(immutable) | |
26 return; | |
27 immutable = true; | |
28 } | |
29 | |
30 public void set(String s) throws LuanException { | |
31 if( immutable ) | |
32 throw new LuanException("regex is immutable"); | |
33 this.pattern = Pattern.compile(s); | |
34 } | |
35 | |
36 public Object[] find(String s,Integer init) throws LuanException { | |
37 int start = StringLuan.start(s,init,0); | |
38 Matcher m = pattern.matcher(s); | |
39 if( !m.find(start) ) | |
40 return null; | |
41 int n = m.groupCount(); | |
42 Object[] rtn = new Object[2+n]; | |
43 rtn[0] = m.start() + 1; | |
44 rtn[1] = m.end(); | |
45 for( int i=0; i<n; i++ ) { | |
46 rtn[2+i] = m.group(i+1); | |
47 } | |
48 return rtn; | |
49 } | |
50 | |
51 public LuanFunction gmatch(String s) throws LuanException { | |
52 Utils.checkNotNull(s); | |
53 final Matcher m = pattern.matcher(s); | |
54 return new LuanFunction() { | |
55 @Override public Object call(Luan luan,Object[] args) { | |
56 if( !m.find() ) | |
57 return null; | |
58 final int n = m.groupCount(); | |
59 if( n == 0 ) | |
60 return m.group(); | |
61 String[] rtn = new String[n]; | |
62 for( int i=0; i<n; i++ ) { | |
63 rtn[i] = m.group(i+1); | |
64 } | |
65 return rtn; | |
66 } | |
67 }; | |
68 } | |
69 | |
70 public Object[] gsub(Luan luan,String s,Object repl,Integer n) throws LuanException { | |
71 Utils.checkNotNull(s); | |
72 int max = n==null ? Integer.MAX_VALUE : n; | |
73 final Matcher m = pattern.matcher(s); | |
1777
e59349d53fec
optimize String.replace and regex.gsub
Franklin Schmidt <fschmidt@gmail.com>
parents:
1719
diff
changeset
|
74 if( max <= 0 || !m.find() ) |
e59349d53fec
optimize String.replace and regex.gsub
Franklin Schmidt <fschmidt@gmail.com>
parents:
1719
diff
changeset
|
75 return new Object[]{ s, 0 }; |
1716 | 76 if( repl instanceof String ) { |
77 String replacement = (String)repl; | |
78 int i = 0; | |
79 StringBuffer sb = new StringBuffer(); | |
1777
e59349d53fec
optimize String.replace and regex.gsub
Franklin Schmidt <fschmidt@gmail.com>
parents:
1719
diff
changeset
|
80 do { |
1716 | 81 m.appendReplacement(sb,replacement); |
82 i++; | |
1777
e59349d53fec
optimize String.replace and regex.gsub
Franklin Schmidt <fschmidt@gmail.com>
parents:
1719
diff
changeset
|
83 } while( i<max && m.find() ); |
1716 | 84 m.appendTail(sb); |
85 return new Object[]{ sb.toString(), i }; | |
86 } | |
87 if( repl instanceof LuanTable ) { | |
88 LuanTable t = (LuanTable)repl; | |
89 int i = 0; | |
90 StringBuffer sb = new StringBuffer(); | |
1777
e59349d53fec
optimize String.replace and regex.gsub
Franklin Schmidt <fschmidt@gmail.com>
parents:
1719
diff
changeset
|
91 do { |
1716 | 92 String match = m.groupCount()==0 ? m.group() : m.group(1); |
93 Object val = t.get(luan,match); | |
94 if( val != null ) { | |
95 String replacement = luan.luanToString(val); | |
96 m.appendReplacement(sb,replacement); | |
97 } | |
98 i++; | |
1777
e59349d53fec
optimize String.replace and regex.gsub
Franklin Schmidt <fschmidt@gmail.com>
parents:
1719
diff
changeset
|
99 } while( i<max && m.find() ); |
1716 | 100 m.appendTail(sb); |
101 return new Object[]{ sb.toString(), i }; | |
102 } | |
103 if( repl instanceof LuanFunction ) { | |
104 LuanFunction fn = (LuanFunction)repl; | |
105 int i = 0; | |
106 StringBuffer sb = new StringBuffer(); | |
1777
e59349d53fec
optimize String.replace and regex.gsub
Franklin Schmidt <fschmidt@gmail.com>
parents:
1719
diff
changeset
|
107 do { |
1716 | 108 Object[] args; |
109 final int count = m.groupCount(); | |
110 if( count == 0 ) { | |
111 args = new String[]{m.group()}; | |
112 } else { | |
113 args = new String[count]; | |
114 for( int j=0; j<count; j++ ) { | |
115 args[j] = m.group(j+1); | |
116 } | |
117 } | |
118 Object val = Luan.first( fn.call(luan,args) ); | |
119 if( val != null ) { | |
120 String replacement = luan.luanToString(val); | |
121 m.appendReplacement(sb,replacement); | |
122 } | |
123 i++; | |
1777
e59349d53fec
optimize String.replace and regex.gsub
Franklin Schmidt <fschmidt@gmail.com>
parents:
1719
diff
changeset
|
124 } while( i<max && m.find() ); |
1716 | 125 m.appendTail(sb); |
126 return new Object[]{ sb.toString(), i }; | |
127 } | |
128 throw new LuanException( "bad argument #3 to 'gsub' (string/function/table expected)" ); | |
129 } | |
130 | |
131 public String[] match(String s,Integer init) throws LuanException { | |
132 int start = StringLuan.start(s,init,0); | |
133 Matcher m = pattern.matcher(s); | |
134 if( !m.find(start) ) | |
135 return null; | |
136 int n = m.groupCount(); | |
137 if( n == 0 ) | |
138 return new String[]{m.group()}; | |
139 String[] rtn = new String[n]; | |
140 for( int i=0; i<n; i++ ) { | |
141 rtn[i] = m.group(i+1); | |
142 } | |
143 return rtn; | |
144 } | |
145 | |
146 public boolean matches(String s) { | |
147 return pattern.matcher(s).find(); | |
148 } | |
1719 | 149 |
150 public String[] split(String s,Integer limit) throws LuanException { | |
151 Utils.checkNotNull(s); | |
152 int n = limit==null ? -1 : limit; | |
153 return pattern.split(s,n); | |
154 } | |
1716 | 155 } |