Ticket #2612: SearchCompiler-regex-cleanup.patch

File SearchCompiler-regex-cleanup.patch, 4.4 KB (added by avarab@…, 17 years ago)

Patch to clean up regex code in the search engine and change the regex compilation flags

  • src/org/openstreetmap/josm/actions/search/SearchCompiler.java

     
    105105
    106106                Pattern searchKey   = null;
    107107                Pattern searchValue = null;
     108                int searchFlags = regexFlags();
    108109
    109                 if (caseSensitive) {
    110                     try {
    111                         searchKey = Pattern.compile(key);
    112                     } catch (PatternSyntaxException e) {
    113                         throw new ParseError(tr(rxErrorMsg, e.getPattern(), e.getIndex(), e.getMessage()));
    114                     }
    115                     try {
    116                         searchValue = Pattern.compile(value);
    117                     } catch (PatternSyntaxException e) {
    118                         throw new ParseError(tr(rxErrorMsg, e.getPattern(), e.getIndex(), e.getMessage()));
    119                     }
    120                 } else {
    121                     try {
    122                         searchKey = Pattern.compile(key, Pattern.CASE_INSENSITIVE);
    123                     } catch (PatternSyntaxException e) {
    124                         throw new ParseError(tr(rxErrorMsg, e.getPattern(), e.getIndex(), e.getMessage()));
    125                     }
    126                     try {
    127                         searchValue = Pattern.compile(value, Pattern.CASE_INSENSITIVE);
    128                     } catch (PatternSyntaxException e) {
    129                         throw new ParseError(tr(rxErrorMsg, e.getPattern(), e.getIndex(), e.getMessage()));
    130                     }
     110                try {
     111                    searchKey = Pattern.compile(key, searchFlags);
     112                } catch (PatternSyntaxException e) {
     113                    throw new ParseError(tr(rxErrorMsg, e.getPattern(), e.getIndex(), e.getMessage()));
    131114                }
    132115
     116                try {
     117                    searchValue = Pattern.compile(value, searchFlags);
     118                } catch (PatternSyntaxException e) {
     119                    throw new ParseError(tr(rxErrorMsg, e.getPattern(), e.getIndex(), e.getMessage()));
     120                }
     121
    133122                for (Entry<String, String> e : osm.keys.entrySet()) {
    134123                    String k = e.getKey();
    135124                    String v = e.getValue();
     
    182171
    183172            if (regexSearch) {
    184173                search = s;
    185                 if (caseSensitive) {
    186                     try {
    187                         searchRegex = Pattern.compile(search);
    188                     } catch (PatternSyntaxException e) {
    189                         throw new ParseError(tr(rxErrorMsg, e.getPattern(), e.getIndex(), e.getMessage()));
    190                     }
    191                 } else {
    192                     try {
    193                         searchRegex = Pattern.compile(search, Pattern.CASE_INSENSITIVE);
    194                     } catch (PatternSyntaxException e) {
    195                         throw new ParseError(tr(rxErrorMsg, e.getPattern(), e.getIndex(), e.getMessage()));
    196                     }
     174                int searchFlags = regexFlags();
     175
     176                try {
     177                    searchRegex = Pattern.compile(search, searchFlags);
     178                } catch (PatternSyntaxException e) {
     179                    throw new ParseError(tr(rxErrorMsg, e.getPattern(), e.getIndex(), e.getMessage()));
    197180                }
    198181            } else {
    199182                search = caseSensitive ? s : s.toLowerCase();
     
    490473            return new KeyValue(key, value);
    491474        }
    492475    }
     476
     477    private int regexFlags() {
     478        int searchFlags = 0;
     479
     480        // Enables canonical Unicode equivalence so that e.g. the two
     481        // forms of "égal", "\u00e9gal" and "e\u0301gal" will match.
     482        //
     483        // I don't know how common this is in the OSM data but it
     484        // makes sense to match "é" to "é" no matter how the character
     485        // happened to be constructed.
     486        searchFlags |= Pattern.CANON_EQ;
     487
     488        // Make "." match any character including newline (/s in Perl)
     489        searchFlags |= Pattern.DOTALL;
     490
     491        // CASE_INSENSITIVE by itself only matches US-ASCII case
     492        // insensitively, but the OSM data is in Unicode. With
     493        // UNICODE_CASE casefolding is made Unicode-aware.
     494        if (!caseSensitive)
     495            searchFlags |= (Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
     496       
     497        return searchFlags;
     498    }
    493499}