Merge branch 'master' of https://source.jalview.org/git/jalviewjs.git
[jalviewjs.git] / site / j2s / java / util / regex / Matcher.js
1 Clazz.declarePackage("java.util.regex");
2 Clazz.load(["java.util.regex.MatchResult"],"java.util.regex.Matcher",["java.lang.IllegalArgumentException","$.IndexOutOfBoundsException","$.NullPointerException","$.StringBuffer"],function(){
3 c$=Clazz.decorateAsClass(function(){
4 this.pat=null;
5 this.string=null;
6 this.leftBound=-1;
7 this.rightBound=-1;
8 this.appendPos=0;
9 this.replacement=null;
10 this.processedRepl=null;
11 this.replacementParts=null;
12 this.results=null;
13 Clazz.instantialize(this,arguments);
14 },java.util.regex,"Matcher",null,java.util.regex.MatchResult);
15 Clazz.defineMethod(c$,"appendReplacement",
16 function(sb,replacement){
17 this.processedRepl=this.processReplacement(replacement);
18 sb.append(this.string.subSequence(this.appendPos,this.start()));
19 sb.append(this.processedRepl);
20 this.appendPos=this.end();
21 return this;
22 },"StringBuffer,~S");
23 Clazz.defineMethod(c$,"processReplacement",
24 ($fz=function(replacement){
25 if(this.replacement!=null&&this.replacement.equals(replacement)){
26 if(this.replacementParts==null){
27 return this.processedRepl;
28 }else{
29 var sb=new StringBuffer();
30 for(var i=0;i<this.replacementParts.length;i++){
31 sb.append(this.replacementParts[i]);
32 }
33 return sb.toString();
34 }}else{
35 this.replacement=replacement;
36 var repl=replacement.toCharArray();
37 var res=new StringBuffer();
38 this.replacementParts=null;
39 var index=0;
40 var replacementPos=0;
41 var nextBackSlashed=false;
42 while(index<repl.length){
43 if((repl[index]).charCodeAt(0)==('\\').charCodeAt(0)&&!nextBackSlashed){
44 nextBackSlashed=true;
45 index++;
46 }if(nextBackSlashed){
47 res.append(repl[index]);
48 nextBackSlashed=false;
49 }else{
50 if((repl[index]).charCodeAt(0)==('$').charCodeAt(0)){
51 if(this.replacementParts==null){
52 this.replacementParts=new Array(0);
53 }try{
54 var gr=Integer.parseInt(String.instantialize(repl,++index,1));
55 if(replacementPos!=res.length()){
56 this.replacementParts[this.replacementParts.length]=res.subSequence(replacementPos,res.length());
57 replacementPos=res.length();
58 }this.replacementParts[this.replacementParts.length]=((Clazz.isClassDefined("java.util.regex.Matcher$1")?0:java.util.regex.Matcher.$Matcher$1$()),Clazz.innerTypeInstance(java.util.regex.Matcher$1,this,null));
59 var group=this.group(gr);
60 replacementPos+=group.length;
61 res.append(group);
62 }catch(e$$){
63 if(Clazz.instanceOf(e$$,IndexOutOfBoundsException)){
64 var iob=e$$;
65 {
66 throw iob;
67 }
68 }else if(Clazz.instanceOf(e$$,Exception)){
69 var e=e$$;
70 {
71 throw new IllegalArgumentException("Illegal regular expression format");
72 }
73 }else{
74 throw e$$;
75 }
76 }
77 }else{
78 res.append(repl[index]);
79 }}index++;
80 }
81 if(this.replacementParts!=null&&replacementPos!=res.length()){
82 this.replacementParts[this.replacementParts.length]=res.subSequence(replacementPos,res.length());
83 }return res.toString();
84 }},$fz.isPrivate=true,$fz),"~S");
85 Clazz.defineMethod(c$,"reset",
86 function(newSequence){
87 if(newSequence==null){
88 throw new NullPointerException("Empty new sequence!");
89 }this.string=newSequence;
90 return this.reset();
91 },"CharSequence");
92 Clazz.defineMethod(c$,"reset",
93 function(){
94 this.leftBound=0;
95 this.rightBound=this.string.length();
96 this.appendPos=0;
97 this.replacement=null;
98 {
99 var flags=""+(this.pat.regexp.ignoreCase?"i":"")
100 +(this.pat.regexp.global?"g":"")
101 +(this.pat.regexp.multiline?"m":"");
102 this.pat.regexp=new RegExp(this.pat.regexp.source,flags);
103 }return this;
104 });
105 Clazz.defineMethod(c$,"region",
106 function(leftBound,rightBound){
107 if(leftBound>rightBound||leftBound<0||rightBound<0||leftBound>this.string.length()||rightBound>this.string.length()){
108 throw new IndexOutOfBoundsException(leftBound+" is out of bound of "+rightBound);
109 }this.leftBound=leftBound;
110 this.rightBound=rightBound;
111 this.results=null;
112 this.appendPos=0;
113 this.replacement=null;
114 return this;
115 },"~N,~N");
116 Clazz.defineMethod(c$,"appendTail",
117 function(sb){
118 return sb.append(this.string.subSequence(this.appendPos,this.string.length()));
119 },"StringBuffer");
120 Clazz.defineMethod(c$,"replaceFirst",
121 function(replacement){
122 this.reset();
123 if(this.find()){
124 var sb=new StringBuffer();
125 this.appendReplacement(sb,replacement);
126 return this.appendTail(sb).toString();
127 }return this.string.toString();
128 },"~S");
129 Clazz.defineMethod(c$,"replaceAll",
130 function(replacement){
131 var sb=new StringBuffer();
132 this.reset();
133 while(this.find()){
134 this.appendReplacement(sb,replacement);
135 }
136 return this.appendTail(sb).toString();
137 },"~S");
138 Clazz.defineMethod(c$,"pattern",
139 function(){
140 return this.pat;
141 });
142 Clazz.defineMethod(c$,"group",
143 function(groupIndex){
144 if(this.results==null||groupIndex<0||groupIndex>this.results.length){
145 return null;
146 }return this.results[groupIndex];
147 },"~N");
148 Clazz.defineMethod(c$,"group",
149 function(){
150 return this.group(0);
151 });
152 Clazz.defineMethod(c$,"find",
153 function(startIndex){
154 var stringLength=this.string.length();
155 if(startIndex<0||startIndex>stringLength)throw new IndexOutOfBoundsException("Out of bound "+startIndex);
156 startIndex=this.findAt(startIndex);
157 return false;
158 },"~N");
159 Clazz.defineMethod(c$,"findAt",
160 ($fz=function(startIndex){
161 return-1;
162 },$fz.isPrivate=true,$fz),"~N");
163 Clazz.defineMethod(c$,"find",
164 function(){
165 {
166 this.results=this.pat.regexp.exec(this.string.subSequence(this.leftBound,this.rightBound));
167 }return(this.results!=null);
168 });
169 Clazz.defineMethod(c$,"start",
170 function(groupIndex){
171 var beginningIndex=0;
172 {
173 beginningIndex=this.pat.regexp.lastIndex;
174 }beginningIndex-=this.results[0].length;
175 return beginningIndex;
176 },"~N");
177 Clazz.defineMethod(c$,"end",
178 function(groupIndex){
179 {
180 return this.pat.regexp.lastIndex;
181 }return-1;
182 },"~N");
183 Clazz.defineMethod(c$,"matches",
184 function(){
185 return this.find();
186 });
187 c$.quoteReplacement=Clazz.defineMethod(c$,"quoteReplacement",
188 function(string){
189 if(string.indexOf('\\') < 0 && string.indexOf ('$')<0)return string;
190 var res=new StringBuffer(string.length*2);
191 var ch;
192 var len=string.length;
193 for(var i=0;i<len;i++){
194 switch(ch=string.charAt(i)){
195 case'$':
196 res.append('\\');
197 res.append('$');
198 break;
199 case'\\':
200 res.append('\\');
201 res.append('\\');
202 break;
203 default:
204 res.append(ch);
205 }
206 }
207 return res.toString();
208 },"~S");
209 Clazz.defineMethod(c$,"lookingAt",
210 function(){
211 return false;
212 });
213 Clazz.defineMethod(c$,"start",
214 function(){
215 return this.start(0);
216 });
217 Clazz.overrideMethod(c$,"groupCount",
218 function(){
219 return this.results==null?0:this.results.length;
220 });
221 Clazz.defineMethod(c$,"end",
222 function(){
223 return this.end(0);
224 });
225 Clazz.defineMethod(c$,"toMatchResult",
226 function(){
227 return this;
228 });
229 Clazz.defineMethod(c$,"useAnchoringBounds",
230 function(value){
231 return this;
232 },"~B");
233 Clazz.defineMethod(c$,"hasAnchoringBounds",
234 function(){
235 return false;
236 });
237 Clazz.defineMethod(c$,"useTransparentBounds",
238 function(value){
239 return this;
240 },"~B");
241 Clazz.defineMethod(c$,"hasTransparentBounds",
242 function(){
243 return false;
244 });
245 Clazz.defineMethod(c$,"regionStart",
246 function(){
247 return this.leftBound;
248 });
249 Clazz.defineMethod(c$,"regionEnd",
250 function(){
251 return this.rightBound;
252 });
253 Clazz.defineMethod(c$,"requireEnd",
254 function(){
255 return false;
256 });
257 Clazz.defineMethod(c$,"hitEnd",
258 function(){
259 return false;
260 });
261 Clazz.defineMethod(c$,"usePattern",
262 function(pat){
263 if(pat==null){
264 throw new IllegalArgumentException("Empty pattern!");
265 }this.pat=pat;
266 this.results=null;
267 return this;
268 },"java.util.regex.Pattern");
269 Clazz.makeConstructor(c$,
270 function(pat,cs){
271 this.pat=pat;
272 this.string=cs;
273 this.leftBound=0;
274 this.rightBound=this.string.toString().length;
275 },"java.util.regex.Pattern,CharSequence");
276 c$.$Matcher$1$=function(){
277 Clazz.pu$h(self.c$);
278 c$=Clazz.decorateAsClass(function(){
279 Clazz.prepareCallback(this,arguments);
280 this.grN=0;
281 Clazz.instantialize(this,arguments);
282 },java.util.regex,"Matcher$1");
283 Clazz.prepareFields(c$,function(){
284 this.grN=gr;
285 });
286 Clazz.overrideMethod(c$,"toString",
287 function(){
288 return this.b$["java.util.regex.Matcher"].group(this.grN);
289 });
290 c$=Clazz.p0p();
291 };
292 Clazz.defineStatics(c$,
293 "MODE_FIND",1,
294 "MODE_MATCH",2);
295 });