needed for applet search
[jalview.git] / src / com / stevesoft / pat / RegRes.java
1 //\r
2 // This software is now distributed according to\r
3 // the Lesser Gnu Public License.  Please see\r
4 // http://www.gnu.org/copyleft/lesser.txt for\r
5 // the details.\r
6 //    -- Happy Computing!\r
7 //\r
8 package com.stevesoft.pat;\r
9 import java.util.*;\r
10 \r
11 /** \r
12         Shareware: package pat\r
13    <a href="copyright.html">Copyright 2001, Steven R. Brandt</a>\r
14 */ /**\r
15 This class is used to store a result from Regex */\r
16 public class RegRes implements Cloneable {\r
17     protected int[] marks = null;\r
18     protected boolean didMatch_ = false;\r
19     protected StringLike src=null;\r
20 \r
21     /** Obtain the text String that was matched against. */\r
22     public String getString() { return src.toString(); }\r
23     /** Obtain the source StringLike object. */\r
24     public StringLike getStringLike() { return src; }\r
25     protected int charsMatched_=0,matchFrom_=0,numSubs_=0;\r
26     public String toString() {\r
27         StringBuffer sb = new StringBuffer();\r
28         sb.append("match="+matchedFrom()+":"+charsMatched());\r
29         if(!didMatch()) return sb.toString();\r
30         for(int i=0;i<numSubs();i++) {\r
31             int n = i + 1;\r
32             sb.append(" sub("+n+")="+matchedFrom(n)+\r
33                 ":"+charsMatched(n));\r
34         }\r
35         return sb.toString();\r
36     }\r
37     public RegRes() {}\r
38     public RegRes(RegRes r) {\r
39       copyOutOf(r);\r
40     }\r
41     public void copyOutOf(RegRes r) {\r
42         if(r.marks == null)\r
43             marks = null;\r
44         else try {\r
45                 //marks = (Hashtable)r.marks.clone();\r
46                 marks = new int[r.marks.length];\r
47                 for(int i=0;i<marks.length;i++)\r
48                     marks[i]=r.marks[i];\r
49                 //marks = (int[])r.marks.clone();\r
50             } catch (Throwable t) {}\r
51         didMatch_ = r.didMatch_;\r
52         src = r.src;\r
53         charsMatched_ = r.charsMatched_;\r
54         matchFrom_ = r.matchFrom_;\r
55         numSubs_ = r.numSubs_;\r
56     }\r
57     public Object clone() { return new RegRes(this); }\r
58     public boolean equals(RegRes r) {\r
59         if(charsMatched_!=r.charsMatched_\r
60                 || matchFrom_   !=r.matchFrom_\r
61                 || didMatch_    !=r.didMatch_\r
62                 || numSubs_     !=r.numSubs_\r
63                 || !src.unwrap().equals(r.src.unwrap()))\r
64             return false;\r
65         if(marks==null && r.marks!=null)\r
66             return false;\r
67         if(marks!=null && r.marks==null)\r
68             return false;\r
69         for(int i=1;i<=numSubs_;i++) {\r
70             if(matchedFrom(i) != r.matchedFrom(i))\r
71                 return false;\r
72             else if(charsMatched(i) != r.charsMatched(i))\r
73                 return false;\r
74         }\r
75         return true;\r
76     }\r
77     /** Obtains the match if successful, null otherwise.*/\r
78     public String stringMatched() {\r
79         int mf=matchedFrom(), cm = charsMatched();\r
80         return !didMatch_ || mf<0 || cm<0 ? null :\r
81         src.substring(mf,mf+cm);\r
82     }\r
83     /** Obtains the position backreference number i begins to match, or\r
84          -1 if backreference i was not matched. */\r
85     public int matchedFrom(int i) {\r
86         if(marks==null||i>numSubs_) return -1;\r
87         //Integer in=(Integer)marks.get("left"+i);\r
88         //return in == null ? -1 : in.intValue();\r
89         return marks[i];\r
90     }\r
91     /** Obtains the number of characters matched by backreference i, or\r
92          -1 if backreference i was not matched. */\r
93     public int charsMatched(int i) {\r
94         if(marks==null||i>numSubs_||!didMatch_) return -1;\r
95         //Integer in = (Integer)marks.get("right"+i);\r
96         //int i2 = in==null ? -1 : in.intValue();\r
97         int mf = matchedFrom(i);\r
98         return mf < 0 ? -1 : marks[i+numSubs_]-matchedFrom(i);\r
99     }\r
100     /** This is either equal to matchedFrom(i)+charsMatched(i) if the match\r
101         was successful, or -1 if it was not. */\r
102     public int matchedTo(int i) {\r
103         if(marks==null||i>numSubs_||!didMatch_) return -1;\r
104         return marks[i+numSubs_];\r
105     }\r
106     /** Obtains a substring matching the nth set\r
107                 of parenthesis from the pattern. See\r
108                 numSubs(void), or null if the nth backrefence did\r
109                 not match. */\r
110     public String stringMatched(int i) {\r
111         int mf = matchedFrom(i), cm = charsMatched(i);\r
112         return !didMatch_ || mf<0 || cm<0 ? null :\r
113         src.substring(mf,mf+cm);\r
114     }\r
115     /** This returns the part of the string that preceeds the match,\r
116          or null if the match failed.*/\r
117     public String left() {\r
118         int mf = matchedFrom();\r
119         return !didMatch_ || (mf<0) ? null : src.substring(0,mf);\r
120     }\r
121     /** This returns the part of the string that follows the ith\r
122                 backreference, or null if the backreference did not match. */\r
123     public String left(int i) {\r
124         int mf = matchedFrom(i);\r
125         return !didMatch_ || (mf<0) ? null : src.substring(0,mf);\r
126     }\r
127     /** This returns the part of the string that follows the match,\r
128          or null if the backreference did not match.*/\r
129     public String right() {\r
130         int mf = matchedFrom(), cm = charsMatched();\r
131         return !didMatch_ || mf<0 || cm<0 ? null : src.substring(mf+\r
132             cm,src.length());\r
133     }\r
134     /** This returns the string to the right of the ith backreference,\r
135          or null if the backreference did not match. */\r
136     public String right(int i) {\r
137         int mf = matchedFrom(i), cm = charsMatched(i);\r
138         return !didMatch_ || mf<0 || cm<0 ? null :\r
139         src.substring(mf+cm,src.length());\r
140     }\r
141     /** After a successful match, this returns the location of\r
142                 the first matching character, or -1 if the match failed.*/\r
143     public int matchedFrom() { return !didMatch_ ? -1 : matchFrom_; }\r
144     /** After a successful match, this returns the number of\r
145                 characters in the match, or -1 if the match failed. */\r
146     public int charsMatched() { return !didMatch_||matchFrom_<0 ? -1 : charsMatched_; }\r
147     /** This is matchedFrom()+charsMatched() after a successful match,\r
148         or -1 otherwise. */\r
149     public int matchedTo() { return !didMatch_ ? -1 : matchFrom_+charsMatched_;}\r
150     /** This returns the number of\r
151                 backreferences (parenthesis) in the pattern,\r
152                 i.e. the pattern "(ab)" has\r
153                 one, the pattern "(a)(b)" has two, etc. */\r
154     public int numSubs() { return numSubs_; }\r
155     /** Contains true if the last match was successful. */\r
156     public boolean didMatch() { return didMatch_; }\r
157 \r
158     /** An older name for matchedFrom. */\r
159     public int matchFrom() { return matchedFrom(); }\r
160     /** An older name for stringMatched(). */\r
161     public String substring() { return stringMatched(); }\r
162     /** An older name for matchedFrom. */\r
163     public int matchFrom(int i) { return matchedFrom(i); }\r
164     /** An older name for stringMatched. */\r
165     public String substring(int i) { return stringMatched(i); }\r
166 }\r