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