// -- Happy Computing!\r
//\r
package com.stevesoft.pat;\r
-/** \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
+/**\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
- public RegRes() {}\r
- public RegRes(RegRes r) {\r
- copyOutOf(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
- 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
+ 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
- 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
+ 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
- return true;\r
+ //marks = (int[])r.marks.clone();\r
+ }\r
+ catch (Throwable t)\r
+ {}\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
+ 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
- /** 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
+ if (marks == null && r.marks != null)\r
+ {\r
+ return false;\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
+ if (marks != null && r.marks == null)\r
+ {\r
+ return false;\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
+ 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
- /** 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
+ 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
- /** 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
+ //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
- /** 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
+ //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
- /** 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
+ 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