-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBackRef.java
60 lines (55 loc) · 1.27 KB
/
BackRef.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package edu.lsu.cct.piraha;
public class BackRef extends Pattern {
final int num;
final boolean ignCase;
public BackRef(int num,boolean ignCase) {
this.num = num-1;
this.ignCase = ignCase;
}
@Override
public boolean match(Matcher m) {
//System.out.println("num="+num+" mc="+m.matchCount());
if(num >= m.groupCount()) {
return false;
}
Group backRef = m.group(num);
int begin = backRef.getBegin();
int end = backRef.getEnd();
int n = end-begin;
int pos = m.getTextPos();
if((pos + n) > m.text.length())
return false;
if (ignCase) {
for (int i = 0; i < n; i++) {
int nn = pos + i;
char c1 = Character.toLowerCase(m.text.charAt(i+begin));
char c2 = Character.toLowerCase(m.text.charAt(nn));
//System.out.println("ign: "+c1+" <=> "+c2);
if (c1 != c2) {
return false;
}
}
} else {
for (int i = 0; i < n; i++) {
int nn = pos + i;
char c1 = m.text.charAt(i+begin);
char c2 = m.text.charAt(nn);
//System.out.println("!ign: "+c1+" <=> "+c2);
if (c1 != c2) {
return false;
}
}
}
m.setTextPos(pos+n);
return true;
}
@Override
public String decompile() {
return "\\"+(1+num);
}
@Override
public boolean eq(Object obj) {
BackRef ref = (BackRef)obj;
return ref.num == num;
}
}