JAL-1807 Bob's JalviewJS prototype first commit
[jalviewjs.git] / unused / com / stevesoft / pat / BackMatch.java
1 //
2 // This software is now distributed according to
3 // the Lesser Gnu Public License.  Please see
4 // http://www.gnu.org/copyleft/lesser.txt for
5 // the details.
6 //    -- Happy Computing!
7 //
8 package com.stevesoft.pat;
9
10 import java.util.*;
11
12 /**
13  * Provides the ability to match a backreference from within a Pattern.
14  */
15 class BackMatch extends Pattern
16 {
17   int id;
18
19   BackMatch(int id)
20   {
21     this.id = id;
22   }
23
24   public String toString()
25   {
26     return "\\" + (id) + nextString();
27   }
28
29   public int matchInternal(int pos, Pthings p)
30   {
31     int i1 = p.marks[id];
32     int i2 = p.marks[id + p.nMarks];
33     int imax = i2 - i1;
34     if (i1 < 0 || imax < 0 || pos + imax > p.src.length())
35     {
36       return -1;
37     }
38     int ns = p.src.length() - pos;
39     if (imax < ns)
40     {
41       ns = imax;
42     }
43     for (int i = 0; i < ns; i++)
44     {
45       if (p.src.charAt(i + i1) != p.src.charAt(pos + i))
46       {
47         return -1;
48       }
49     }
50     return nextMatch(pos + imax, p);
51   }
52
53   Pattern clone1(Hashtable h)
54   {
55     return new BackMatch(id);
56   }
57 }