-//
-// 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()
- {
- javajs.util.SB sb = new javajs.util.SB();
- 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);
- }
-}
+//\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
+ */\r
+public class RegRes implements Cloneable\r
+{\r
+ protected int[] marks = null;\r
+\r
+ protected boolean didMatch_ = false;\r
+\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
+\r
+ public String toString()\r
+ {\r
+ javajs.util.SB sb = new javajs.util.SB();\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\r
+ .append(" sub(" + n + ")=" + matchedFrom(n) + ":"\r
+ + charsMatched(n));\r
+ }\r
+ return sb.toString();\r
+ }\r
+\r
+ public RegRes()\r
+ {\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
+ } catch (Throwable t)\r
+ {\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_ || matchFrom_ != r.matchFrom_\r
+ || didMatch_ != r.didMatch_ || 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 : src.substring(mf, mf\r
+ + cm);\r
+ }\r
+\r
+ /**\r
+ * Obtains the position backreference number i begins to match, or -1 if\r
+ * backreference i was not matched.\r
+ */\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
+ /**\r
+ * Obtains the number of characters matched by backreference i, or -1 if\r
+ * backreference i was not matched.\r
+ */\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
+ /**\r
+ * This is either equal to matchedFrom(i)+charsMatched(i) if the match was\r
+ * successful, or -1 if it was not.\r
+ */\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
+ /**\r
+ * Obtains a substring matching the nth set of parenthesis from the pattern.\r
+ * See numSubs(void), or null if the nth backrefence did not match.\r
+ */\r
+ public String stringMatched(int i)\r
+ {\r
+ int mf = matchedFrom(i), cm = charsMatched(i);\r
+ return !didMatch_ || mf < 0 || cm < 0 ? null : src.substring(mf, mf\r
+ + cm);\r
+ }\r
+\r
+ /**\r
+ * This returns the part of the string that preceeds the match, or null if the\r
+ * match failed.\r
+ */\r
+ public String left()\r
+ {\r
+ int mf = matchedFrom();\r
+ return !didMatch_ || (mf < 0) ? null : src.substring(0, mf);\r
+ }\r
+\r
+ /**\r
+ * This returns the part of the string that follows the ith backreference, or\r
+ * null if the backreference did not match.\r
+ */\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
+ /**\r
+ * This returns the part of the string that follows the match, or null if the\r
+ * backreference did not match.\r
+ */\r
+ public String right()\r
+ {\r
+ int mf = matchedFrom(), cm = charsMatched();\r
+ return !didMatch_ || mf < 0 || cm < 0 ? null : src.substring(mf + cm,\r
+ src.length());\r
+ }\r
+\r
+ /**\r
+ * This returns the string to the right of the ith backreference, or null if\r
+ * the backreference did not match.\r
+ */\r
+ public String right(int i)\r
+ {\r
+ int mf = matchedFrom(i), cm = charsMatched(i);\r
+ return !didMatch_ || mf < 0 || cm < 0 ? null : src.substring(mf + cm,\r
+ src.length());\r
+ }\r
+\r
+ /**\r
+ * After a successful match, this returns the location of the first matching\r
+ * character, or -1 if the match failed.\r
+ */\r
+ public int matchedFrom()\r
+ {\r
+ return !didMatch_ ? -1 : matchFrom_;\r
+ }\r
+\r
+ /**\r
+ * After a successful match, this returns the number of characters in the\r
+ * match, or -1 if the match failed.\r
+ */\r
+ public int charsMatched()\r
+ {\r
+ return !didMatch_ || matchFrom_ < 0 ? -1 : charsMatched_;\r
+ }\r
+\r
+ /**\r
+ * This is matchedFrom()+charsMatched() after a successful match, or -1\r
+ * otherwise.\r
+ */\r
+ public int matchedTo()\r
+ {\r
+ return !didMatch_ ? -1 : matchFrom_ + charsMatched_;\r
+ }\r
+\r
+ /**\r
+ * This returns the number of backreferences (parenthesis) in the pattern,\r
+ * i.e. the pattern "(ab)" has one, the pattern "(a)(b)" has two, etc.\r
+ */\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