X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fcom%2Fstevesoft%2Fpat%2FRegex.java;h=455f04028b6d304abe8fde277962f7d684f5e80b;hb=4b2392caa53f1de3400e7916d1d9c7b815d446f4;hp=861c33b856229b685ab3bde6472945201380437e;hpb=aced09c4feeaf3406269442c14e54abeeb4cad81;p=jalview.git diff --git a/src/com/stevesoft/pat/Regex.java b/src/com/stevesoft/pat/Regex.java index 861c33b..455f040 100755 --- a/src/com/stevesoft/pat/Regex.java +++ b/src/com/stevesoft/pat/Regex.java @@ -9,14 +9,18 @@ package com.stevesoft.pat; import jalview.util.MessageManager; -import java.io.*; -import java.util.*; +import java.io.File; +import java.io.FilenameFilter; +import java.util.BitSet; +import java.util.Hashtable; + +import com.stevesoft.pat.wrap.StringWrap; -import com.stevesoft.pat.wrap.*; /** Matches a Unicode punctuation character. */ class UnicodePunct extends UniValidator { + @Override public int validate(StringLike s, int from, int to) { return from < s.length() && Prop.isPunct(s.charAt(from)) ? to : -1; @@ -26,6 +30,7 @@ class UnicodePunct extends UniValidator /** Matches a Unicode white space character. */ class UnicodeWhite extends UniValidator { + @Override public int validate(StringLike s, int from, int to) { return from < s.length() && Prop.isWhite(s.charAt(from)) ? to : -1; @@ -37,6 +42,7 @@ class UnicodeWhite extends UniValidator */ class NUnicodePunct extends UniValidator { + @Override public int validate(StringLike s, int from, int to) { return from < s.length() && !Prop.isPunct(s.charAt(from)) ? to : -1; @@ -48,6 +54,7 @@ class NUnicodePunct extends UniValidator */ class NUnicodeWhite extends UniValidator { + @Override public int validate(StringLike s, int from, int to) { return from < s.length() && !Prop.isWhite(s.charAt(from)) ? to : -1; @@ -57,6 +64,7 @@ class NUnicodeWhite extends UniValidator /** Matches a Unicode word character: an alphanumeric or underscore. */ class UnicodeW extends UniValidator { + @Override public int validate(StringLike s, int from, int to) { if (from >= s.length()) @@ -72,6 +80,7 @@ class UnicodeW extends UniValidator /** Matches a character that is not a Unicode alphanumeric or underscore. */ class NUnicodeW extends UniValidator { + @Override public int validate(StringLike s, int from, int to) { if (from >= s.length()) @@ -87,6 +96,7 @@ class NUnicodeW extends UniValidator /** Matches a Unicode decimal digit. */ class UnicodeDigit extends UniValidator { + @Override public int validate(StringLike s, int from, int to) { return from < s.length() && Prop.isDecimalDigit(s.charAt(from)) ? to @@ -97,6 +107,7 @@ class UnicodeDigit extends UniValidator /** Matches a character that is not a Unicode digit. */ class NUnicodeDigit extends UniValidator { + @Override public int validate(StringLike s, int from, int to) { return from < s.length() && !Prop.isDecimalDigit(s.charAt(from)) ? to @@ -107,6 +118,7 @@ class NUnicodeDigit extends UniValidator /** Matches a Unicode math character. */ class UnicodeMath extends UniValidator { + @Override public int validate(StringLike s, int from, int to) { return from < s.length() && Prop.isMath(s.charAt(from)) ? to : -1; @@ -116,6 +128,7 @@ class UnicodeMath extends UniValidator /** Matches a non-math Unicode character. */ class NUnicodeMath extends UniValidator { + @Override public int validate(StringLike s, int from, int to) { return from < s.length() && !Prop.isMath(s.charAt(from)) ? to : -1; @@ -125,6 +138,7 @@ class NUnicodeMath extends UniValidator /** Matches a Unicode currency symbol. */ class UnicodeCurrency extends UniValidator { + @Override public int validate(StringLike s, int from, int to) { return from < s.length() && Prop.isCurrency(s.charAt(from)) ? to : -1; @@ -134,6 +148,7 @@ class UnicodeCurrency extends UniValidator /** Matches a non-currency symbol Unicode character. */ class NUnicodeCurrency extends UniValidator { + @Override public int validate(StringLike s, int from, int to) { return from < s.length() && !Prop.isCurrency(s.charAt(from)) ? to : -1; @@ -143,6 +158,7 @@ class NUnicodeCurrency extends UniValidator /** Matches a Unicode alphabetic character. */ class UnicodeAlpha extends UniValidator { + @Override public int validate(StringLike s, int from, int to) { return from < s.length() && Prop.isAlphabetic(s.charAt(from)) ? to : -1; @@ -152,6 +168,7 @@ class UnicodeAlpha extends UniValidator /** Matches a non-alphabetic Unicode character. */ class NUnicodeAlpha extends UniValidator { + @Override public int validate(StringLike s, int from, int to) { return from < s.length() && !Prop.isAlphabetic(s.charAt(from)) ? to @@ -162,6 +179,7 @@ class NUnicodeAlpha extends UniValidator /** Matches an upper case Unicode character. */ class UnicodeUpper extends UniValidator { + @Override public int validate(StringLike s, int from, int to) { return from < s.length() && isUpper(s.charAt(from)) ? to : -1; @@ -176,6 +194,7 @@ class UnicodeUpper extends UniValidator /** Matches an upper case Unicode character. */ class UnicodeLower extends UniValidator { + @Override public int validate(StringLike s, int from, int to) { return from < s.length() && isLower(s.charAt(from)) ? to : -1; @@ -252,7 +271,9 @@ class UnicodeLower extends UniValidator * *
  * \d+
- * 
, but note that the sequence + * + * + * , but note that the sequence * *
  * (?e=#)
@@ -262,8 +283,8 @@ class UnicodeLower extends UniValidator
  * small differences as well. I will either make my package conform or note them
  * as I become aware of them.
  * 

- * This package supports additional patterns not in perl5:

+ * This package supports additional patterns not in perl5:
+ *
* * * @@ -277,8 +298,8 @@ class UnicodeLower extends UniValidator * "look behind." It fails if it attempts to move to a position before the * beginning of the string. "x(?<1)" is equivalent to "(?=x)". The number, 1 * in this example, is the number of characters to move backwards. - *
(?@())Group
- * + * + * * * @author Steven R. Brandt * @version package com.stevesoft.pat, release 1.5.3 @@ -286,6 +307,7 @@ class UnicodeLower extends UniValidator */ public class Regex extends RegRes implements FilenameFilter { + /** * BackRefOffset gives the identity number of the first pattern. Version 1.0 * used zero, version 1.1 uses 1 to be more compatible with perl. @@ -595,7 +617,7 @@ public class Regex extends RegRes implements FilenameFilter /** Essentially clones the Regex object */ public Regex(Regex r) { - super((RegRes) r); + super(r); dontMatchInQuotes = r.dontMatchInQuotes; esc = r.esc; ignoreCase = r.ignoreCase; @@ -627,9 +649,8 @@ public class Regex extends RegRes implements FilenameFilter * search or matchAt methods. * * @exception com.stevesoft.pat.RegSyntax - * is thrown if a syntax error is encountered in the - * pattern. For example, "x{3,1}" or "*a" are not valid - * patterns. + * is thrown if a syntax error is encountered in the pattern. For + * example, "x{3,1}" or "*a" are not valid patterns. * @see com.stevesoft.pat.Regex#search * @see com.stevesoft.pat.Regex#matchAt */ @@ -671,6 +692,7 @@ public class Regex extends RegRes implements FilenameFilter * patterns are equal as well as the most recent match. If a Regex is compare * with a RegRes, only the result of the most recent match is compared. */ + @Override public boolean equals(Object o) { if (o instanceof Regex) @@ -691,6 +713,7 @@ public class Regex extends RegRes implements FilenameFilter } /** A clone by any other name would smell as sweet. */ + @Override public Object clone() { return new Regex(this); @@ -777,7 +800,9 @@ public class Regex extends RegRes implements FilenameFilter { if (s == null) { - throw new NullPointerException(MessageManager.getString("exception.null_string_given_to_regex_search")); + throw new NullPointerException( + MessageManager + .getString("exception.null_string_given_to_regex_search")); } return _search(s, 0, s.length()); } @@ -786,7 +811,9 @@ public class Regex extends RegRes implements FilenameFilter { if (sl == null) { - throw new NullPointerException(MessageManager.getString("exception.null_string_like_given_to_regex_search")); + throw new NullPointerException( + MessageManager + .getString("exception.null_string_like_given_to_regex_search")); } return _search(sl, 0, sl.length()); } @@ -795,7 +822,9 @@ public class Regex extends RegRes implements FilenameFilter { if (s == null) { - throw new NullPointerException(MessageManager.getString("exception.null_string_given_to_regex_reverse_search")); + throw new NullPointerException( + MessageManager + .getString("exception.null_string_given_to_regex_reverse_search")); } return _reverseSearch(s, 0, s.length()); } @@ -804,7 +833,9 @@ public class Regex extends RegRes implements FilenameFilter { if (sl == null) { - throw new NullPointerException(MessageManager.getString("exception.null_string_like_given_to_regex_reverse_search")); + throw new NullPointerException( + MessageManager + .getString("exception.null_string_like_given_to_regex_reverse_search")); } return _reverseSearch(sl, 0, sl.length()); } @@ -821,7 +852,9 @@ public class Regex extends RegRes implements FilenameFilter { if (s == null) { - throw new NullPointerException(MessageManager.getString("exception.null_string_like_given_to_regex_search_from")); + throw new NullPointerException( + MessageManager + .getString("exception.null_string_like_given_to_regex_search_from")); } return _search(s, start, s.length()); } @@ -830,7 +863,9 @@ public class Regex extends RegRes implements FilenameFilter { if (s == null) { - throw new NullPointerException(MessageManager.getString("exception.null_string_like_given_to_regex_search_from")); + throw new NullPointerException( + MessageManager + .getString("exception.null_string_like_given_to_regex_search_from")); } return _search(s, start, s.length()); } @@ -842,7 +877,9 @@ public class Regex extends RegRes implements FilenameFilter { if (s == null) { - throw new NullPointerException(MessageManager.getString("exception.null_string_like_given_to_regex_search_region")); + throw new NullPointerException( + MessageManager + .getString("exception.null_string_like_given_to_regex_search_region")); } return _search(s, start, end); } @@ -963,13 +1000,13 @@ public class Regex extends RegRes implements FilenameFilter * if(skipper == null) { for(long i=start;i<=up;i++) { charsMatched_ = * thePattern.matchAt(s,i,pt); if(charsMatched_ >= 0) { matchFrom_ = * thePattern.mfrom; marks = pt.marks; gFlagto = matchFrom_+charsMatched_; - * return didMatch_=true; } } } else { pt.no_check = true; for(long i=start;i<=up;i++) { - * i = skipper.find(src,i,up); if(i<0) { charsMatched_ = matchFrom_ = -1; - * return didMatch_ = false; } charsMatched_ = thePattern.matchAt(s,i,pt); - * if(charsMatched_ >= 0) { matchFrom_ = thePattern.mfrom; marks = pt.marks; - * gFlagto = matchFrom_+charsMatched_; gFlags = s; return didMatch_=true; } - * else { i = s.adjustIndex(i); up = s.adjustEnd(i); } } } return - * didMatch_=false; } + * return didMatch_=true; } } } else { pt.no_check = true; for(long + * i=start;i<=up;i++) { i = skipper.find(src,i,up); if(i<0) { charsMatched_ = + * matchFrom_ = -1; return didMatch_ = false; } charsMatched_ = + * thePattern.matchAt(s,i,pt); if(charsMatched_ >= 0) { matchFrom_ = + * thePattern.mfrom; marks = pt.marks; gFlagto = matchFrom_+charsMatched_; + * gFlags = s; return didMatch_=true; } else { i = s.adjustIndex(i); up = + * s.adjustEnd(i); } } } return didMatch_=false; } */ boolean _reverseSearch(String s, int start, int end) @@ -1060,7 +1097,7 @@ public class Regex extends RegRes implements FilenameFilter { try { - return (Regex) getClass().newInstance(); + return getClass().newInstance(); } catch (InstantiationException ie) { return null; @@ -1097,8 +1134,8 @@ public class Regex extends RegRes implements FilenameFilter * the Pattern. * * @exception com.stevesoft.pat.RegSyntax - * is thrown when a nonsensensical pattern is supplied. For - * example, a pattern beginning with *. + * is thrown when a nonsensensical pattern is supplied. For + * example, a pattern beginning with *. */ protected void compile1(StrPos sp, Rthings mk) throws RegSyntax { @@ -1624,7 +1661,7 @@ public class Regex extends RegRes implements FilenameFilter { if (p instanceof Any && p.next == null) { - return (Pattern) new DotMulti(lo, hi); + return new DotMulti(lo, hi); } return RegOpt.safe4fm(p) ? (Pattern) new FastMulti(lo, hi, p) : (Pattern) new Multi(lo, hi, p); @@ -1784,6 +1821,7 @@ public class Regex extends RegRes implements FilenameFilter * representations. Also be prepared to see some strange output if your * characters are not printable. */ + @Override public String toString() { if (false && thePattern == null) @@ -1884,6 +1922,7 @@ public class Regex extends RegRes implements FilenameFilter * * @see com.stevesoft.pat.FileRegex */ + @Override public boolean accept(File dir, String s) { return search(s); @@ -1903,9 +1942,9 @@ public class Regex extends RegRes implements FilenameFilter * optimized() method. *

* This method will attempt to rewrite your pattern in a way that makes it - * faster (not all patterns execute at the same speed). In general, "(?: ... )" - * will be faster than "( ... )" so if you don't need the backreference, you - * should group using the former pattern. + * faster (not all patterns execute at the same speed). In general, + * "(?: ... )" will be faster than "( ... )" so if you don't need the + * backreference, you should group using the former pattern. *

* It will also introduce new pattern elements that you can't get to * otherwise, for example if you have a large table of strings, i.e. the