X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fcom%2Fstevesoft%2Fpat%2FRegex.java;h=6d07427645a62a04efebc4b350d137be946f50da;hb=fca7d6a803e4edc9aeee0ee39ca980d11ca13346;hp=04bb0daea5b381b984e9bb2aeda42d0070a8d526;hpb=506d60f0e188723ddc91c26824b41ac7034df3fe;p=jalview.git diff --git a/src/com/stevesoft/pat/Regex.java b/src/com/stevesoft/pat/Regex.java index 04bb0da..6d07427 100755 --- a/src/com/stevesoft/pat/Regex.java +++ b/src/com/stevesoft/pat/Regex.java @@ -7,14 +7,19 @@ // package com.stevesoft.pat; -import java.io.*; -import java.util.*; +import jalview.util.MessageManager; -import com.stevesoft.pat.wrap.*; +import java.io.File; +import java.io.FilenameFilter; +import java.util.BitSet; +import java.util.Hashtable; + +import com.stevesoft.pat.wrap.StringWrap; /** 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; @@ -24,6 +29,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; @@ -35,6 +41,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; @@ -46,6 +53,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; @@ -55,6 +63,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()) @@ -70,6 +79,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()) @@ -85,6 +95,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 @@ -95,6 +106,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 @@ -105,6 +117,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; @@ -114,6 +127,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; @@ -123,6 +137,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; @@ -132,6 +147,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; @@ -141,6 +157,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; @@ -150,6 +167,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 @@ -160,6 +178,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; @@ -174,6 +193,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; @@ -250,7 +270,9 @@ class UnicodeLower extends UniValidator * *
  * \d+
- * 
, but note that the sequence + * + * + * , but note that the sequence * *
  * (?e=#)
@@ -260,8 +282,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:
+ *
* * * @@ -275,8 +297,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 @@ -593,7 +615,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; @@ -625,9 +647,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 */ @@ -669,6 +690,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) @@ -689,6 +711,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); @@ -775,7 +798,9 @@ public class Regex extends RegRes implements FilenameFilter { if (s == null) { - throw new NullPointerException("Null String Given to Regex.search"); + throw new NullPointerException( + MessageManager + .getString("exception.null_string_given_to_regex_search")); } return _search(s, 0, s.length()); } @@ -785,7 +810,8 @@ public class Regex extends RegRes implements FilenameFilter if (sl == null) { throw new NullPointerException( - "Null StringLike Given to Regex.search"); + MessageManager + .getString("exception.null_string_like_given_to_regex_search")); } return _search(sl, 0, sl.length()); } @@ -795,7 +821,8 @@ public class Regex extends RegRes implements FilenameFilter if (s == null) { throw new NullPointerException( - "Null String Given to Regex.reverseSearch"); + MessageManager + .getString("exception.null_string_given_to_regex_reverse_search")); } return _reverseSearch(s, 0, s.length()); } @@ -805,7 +832,8 @@ public class Regex extends RegRes implements FilenameFilter if (sl == null) { throw new NullPointerException( - "Null StringLike Given to Regex.reverseSearch"); + MessageManager + .getString("exception.null_string_like_given_to_regex_reverse_search")); } return _reverseSearch(sl, 0, sl.length()); } @@ -823,7 +851,8 @@ public class Regex extends RegRes implements FilenameFilter if (s == null) { throw new NullPointerException( - "Null String Given to Regex.searchFrom"); + MessageManager + .getString("exception.null_string_like_given_to_regex_search_from")); } return _search(s, start, s.length()); } @@ -833,7 +862,8 @@ public class Regex extends RegRes implements FilenameFilter if (s == null) { throw new NullPointerException( - "Null String Given to Regex.searchFrom"); + MessageManager + .getString("exception.null_string_like_given_to_regex_search_from")); } return _search(s, start, s.length()); } @@ -846,7 +876,8 @@ public class Regex extends RegRes implements FilenameFilter if (s == null) { throw new NullPointerException( - "Null String Given to Regex.searchRegion"); + MessageManager + .getString("exception.null_string_like_given_to_regex_search_region")); } return _search(s, start, end); } @@ -967,13 +998,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) @@ -1064,13 +1095,16 @@ public class Regex extends RegRes implements FilenameFilter { try { - return (Regex) getClass().newInstance(); + return getClass().getDeclaredConstructor().newInstance(); } catch (InstantiationException ie) { return null; } catch (IllegalAccessException iae) { return null; + } catch (ReflectiveOperationException roe) + { + return null; } } @@ -1101,8 +1135,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 { @@ -1628,7 +1662,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); @@ -1788,6 +1822,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) @@ -1888,6 +1923,7 @@ public class Regex extends RegRes implements FilenameFilter * * @see com.stevesoft.pat.FileRegex */ + @Override public boolean accept(File dir, String s) { return search(s); @@ -1907,9 +1943,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