needed for applet search
[jalview.git] / src / com / stevesoft / pat / RegRes.java
diff --git a/src/com/stevesoft/pat/RegRes.java b/src/com/stevesoft/pat/RegRes.java
new file mode 100755 (executable)
index 0000000..f181564
--- /dev/null
@@ -0,0 +1,166 @@
+//\r
+// This software is now distributed according to\r
+// the Lesser Gnu Public License.  Please see\r
+// http://www.gnu.org/copyleft/lesser.txt for\r
+// the details.\r
+//    -- Happy Computing!\r
+//\r
+package com.stevesoft.pat;\r
+import java.util.*;\r
+\r
+/** \r
+        Shareware: package pat\r
+   <a href="copyright.html">Copyright 2001, Steven R. Brandt</a>\r
+*/ /**\r
+This class is used to store a result from Regex */\r
+public class RegRes implements Cloneable {\r
+    protected int[] marks = null;\r
+    protected boolean didMatch_ = false;\r
+    protected StringLike src=null;\r
+\r
+    /** Obtain the text String that was matched against. */\r
+    public String getString() { return src.toString(); }\r
+    /** Obtain the source StringLike object. */\r
+    public StringLike getStringLike() { return src; }\r
+    protected int charsMatched_=0,matchFrom_=0,numSubs_=0;\r
+    public String toString() {\r
+        StringBuffer sb = new StringBuffer();\r
+        sb.append("match="+matchedFrom()+":"+charsMatched());\r
+        if(!didMatch()) return sb.toString();\r
+        for(int i=0;i<numSubs();i++) {\r
+            int n = i + 1;\r
+            sb.append(" sub("+n+")="+matchedFrom(n)+\r
+                ":"+charsMatched(n));\r
+        }\r
+        return sb.toString();\r
+    }\r
+    public RegRes() {}\r
+    public RegRes(RegRes r) {\r
+      copyOutOf(r);\r
+    }\r
+    public void copyOutOf(RegRes r) {\r
+        if(r.marks == null)\r
+            marks = null;\r
+        else try {\r
+                //marks = (Hashtable)r.marks.clone();\r
+                marks = new int[r.marks.length];\r
+                for(int i=0;i<marks.length;i++)\r
+                    marks[i]=r.marks[i];\r
+                //marks = (int[])r.marks.clone();\r
+            } catch (Throwable t) {}\r
+        didMatch_ = r.didMatch_;\r
+        src = r.src;\r
+        charsMatched_ = r.charsMatched_;\r
+        matchFrom_ = r.matchFrom_;\r
+        numSubs_ = r.numSubs_;\r
+    }\r
+    public Object clone() { return new RegRes(this); }\r
+    public boolean equals(RegRes r) {\r
+        if(charsMatched_!=r.charsMatched_\r
+                || matchFrom_   !=r.matchFrom_\r
+                || didMatch_    !=r.didMatch_\r
+                || numSubs_     !=r.numSubs_\r
+                || !src.unwrap().equals(r.src.unwrap()))\r
+            return false;\r
+        if(marks==null && r.marks!=null)\r
+            return false;\r
+        if(marks!=null && r.marks==null)\r
+            return false;\r
+        for(int i=1;i<=numSubs_;i++) {\r
+            if(matchedFrom(i) != r.matchedFrom(i))\r
+                return false;\r
+            else if(charsMatched(i) != r.charsMatched(i))\r
+                return false;\r
+        }\r
+        return true;\r
+    }\r
+    /** Obtains the match if successful, null otherwise.*/\r
+    public String stringMatched() {\r
+        int mf=matchedFrom(), cm = charsMatched();\r
+        return !didMatch_ || mf<0 || cm<0 ? null :\r
+        src.substring(mf,mf+cm);\r
+    }\r
+    /** Obtains the position backreference number i begins to match, or\r
+         -1 if backreference i was not matched. */\r
+    public int matchedFrom(int i) {\r
+        if(marks==null||i>numSubs_) return -1;\r
+        //Integer in=(Integer)marks.get("left"+i);\r
+        //return in == null ? -1 : in.intValue();\r
+        return marks[i];\r
+    }\r
+    /** Obtains the number of characters matched by backreference i, or\r
+         -1 if backreference i was not matched. */\r
+    public int charsMatched(int i) {\r
+        if(marks==null||i>numSubs_||!didMatch_) return -1;\r
+        //Integer in = (Integer)marks.get("right"+i);\r
+        //int i2 = in==null ? -1 : in.intValue();\r
+        int mf = matchedFrom(i);\r
+        return mf < 0 ? -1 : marks[i+numSubs_]-matchedFrom(i);\r
+    }\r
+    /** This is either equal to matchedFrom(i)+charsMatched(i) if the match\r
+        was successful, or -1 if it was not. */\r
+    public int matchedTo(int i) {\r
+        if(marks==null||i>numSubs_||!didMatch_) return -1;\r
+        return marks[i+numSubs_];\r
+    }\r
+    /** Obtains a substring matching the nth set\r
+                of parenthesis from the pattern. See\r
+                numSubs(void), or null if the nth backrefence did\r
+                not match. */\r
+    public String stringMatched(int i) {\r
+        int mf = matchedFrom(i), cm = charsMatched(i);\r
+        return !didMatch_ || mf<0 || cm<0 ? null :\r
+        src.substring(mf,mf+cm);\r
+    }\r
+    /** This returns the part of the string that preceeds the match,\r
+         or null if the match failed.*/\r
+    public String left() {\r
+        int mf = matchedFrom();\r
+        return !didMatch_ || (mf<0) ? null : src.substring(0,mf);\r
+    }\r
+    /** This returns the part of the string that follows the ith\r
+                backreference, or null if the backreference did not match. */\r
+    public String left(int i) {\r
+        int mf = matchedFrom(i);\r
+        return !didMatch_ || (mf<0) ? null : src.substring(0,mf);\r
+    }\r
+    /** This returns the part of the string that follows the match,\r
+         or null if the backreference did not match.*/\r
+    public String right() {\r
+        int mf = matchedFrom(), cm = charsMatched();\r
+        return !didMatch_ || mf<0 || cm<0 ? null : src.substring(mf+\r
+            cm,src.length());\r
+    }\r
+    /** This returns the string to the right of the ith backreference,\r
+         or null if the backreference did not match. */\r
+    public String right(int i) {\r
+        int mf = matchedFrom(i), cm = charsMatched(i);\r
+        return !didMatch_ || mf<0 || cm<0 ? null :\r
+        src.substring(mf+cm,src.length());\r
+    }\r
+    /** After a successful match, this returns the location of\r
+                the first matching character, or -1 if the match failed.*/\r
+    public int matchedFrom() { return !didMatch_ ? -1 : matchFrom_; }\r
+    /** After a successful match, this returns the number of\r
+                characters in the match, or -1 if the match failed. */\r
+    public int charsMatched() { return !didMatch_||matchFrom_<0 ? -1 : charsMatched_; }\r
+    /** This is matchedFrom()+charsMatched() after a successful match,\r
+        or -1 otherwise. */\r
+    public int matchedTo() { return !didMatch_ ? -1 : matchFrom_+charsMatched_;}\r
+    /** This returns the number of\r
+                backreferences (parenthesis) in the pattern,\r
+                i.e. the pattern "(ab)" has\r
+                one, the pattern "(a)(b)" has two, etc. */\r
+    public int numSubs() { return numSubs_; }\r
+    /** Contains true if the last match was successful. */\r
+    public boolean didMatch() { return didMatch_; }\r
+\r
+    /** An older name for matchedFrom. */\r
+    public int matchFrom() { return matchedFrom(); }\r
+    /** An older name for stringMatched(). */\r
+    public String substring() { return stringMatched(); }\r
+    /** An older name for matchedFrom. */\r
+    public int matchFrom(int i) { return matchedFrom(i); }\r
+    /** An older name for stringMatched. */\r
+    public String substring(int i) { return stringMatched(i); }\r
+}\r