Changeset 8567 in josm


Ignore:
Timestamp:
2015-07-04T18:53:26+02:00 (9 years ago)
Author:
Don-vip
Message:

fix #11651 - whitechars stripping optimization (modified patch by shinigami)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r8513 r8567  
    4040import java.util.Collection;
    4141import java.util.Collections;
    42 import java.util.HashSet;
    4342import java.util.Iterator;
    4443import java.util.List;
    4544import java.util.Locale;
    46 import java.util.Set;
    4745import java.util.concurrent.ExecutorService;
    4846import java.util.concurrent.Executors;
     
    8381
    8482    public static final String URL_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~:/?#[]@!$&'()*+,;=%";
     83
     84    private static char[] DEFAULT_STRIP = {'\u200B', '\uFEFF'};
    8585
    8686    /**
     
    902902     */
    903903    public static String strip(final String str) {
    904         return strip(str, null);
     904        if (str == null || str.isEmpty()) {
     905            return str;
     906        }
     907        return strip(str, DEFAULT_STRIP);
    905908    }
    906909
     
    917920            return str;
    918921        }
    919         // create set with chars to skip
    920         Set<Character> skipSet = new HashSet<>();
    921         // '\u200B' (ZERO WIDTH SPACE character) needs to be handled manually because of change in Unicode 6.0 (Java 7, see #8918)
    922         skipSet.add('\u200B');
    923         // same for '\uFEFF' (ZERO WIDTH NO-BREAK SPACE)
    924         skipSet.add('\uFEFF');
    925         if (skipChars != null) {
    926             for (char c : skipChars.toCharArray()) {
    927                 skipSet.add(c);
    928             }
    929         }
     922        return strip(str, stripChars(skipChars));
     923    }
     924
     925    private static String strip(final String str, final char[] skipChars) {
     926
    930927        int start = 0;
    931928        int end = str.length();
     
    933930        while (leadingSkipChar && start < end) {
    934931            char c = str.charAt(start);
    935             leadingSkipChar = Character.isWhitespace(c) || Character.isSpaceChar(c) || skipSet.contains(c);
     932            leadingSkipChar = Character.isWhitespace(c) || Character.isSpaceChar(c) || stripChar(skipChars, c);
    936933            if (leadingSkipChar) {
    937934                start++;
     
    939936        }
    940937        boolean trailingSkipChar = true;
    941         while (trailingSkipChar && end > start+1) {
    942             char c = str.charAt(end-1);
    943             trailingSkipChar = Character.isWhitespace(c) || Character.isSpaceChar(c) || skipSet.contains(c);
     938        while (trailingSkipChar && end > start + 1) {
     939            char c = str.charAt(end - 1);
     940            trailingSkipChar = Character.isWhitespace(c) || Character.isSpaceChar(c) || stripChar(skipChars, c);
    944941            if (trailingSkipChar) {
    945942                end--;
    946943            }
    947944        }
     945
    948946        return str.substring(start, end);
     947    }
     948
     949    private static char[] stripChars(final String skipChars) {
     950        if (skipChars == null || skipChars.isEmpty()) {
     951            return DEFAULT_STRIP;
     952        }
     953
     954        char[] chars = new char[DEFAULT_STRIP.length + skipChars.length()];
     955        System.arraycopy(DEFAULT_STRIP, 0, chars, 0, DEFAULT_STRIP.length);
     956        skipChars.getChars(0, skipChars.length(), chars, DEFAULT_STRIP.length);
     957
     958        return chars;
     959    }
     960
     961    private static boolean stripChar(final char[] strip, char c) {
     962        for (char s : strip) {
     963            if (c == s) {
     964                return true;
     965            }
     966        }
     967        return false;
    949968    }
    950969
     
    9901009        File josmTmpDir = new File(tmpDir, "JOSM");
    9911010        if (!josmTmpDir.exists() && !josmTmpDir.mkdirs()) {
    992             Main.warn("Unable to create temp directory "+josmTmpDir);
     1011            Main.warn("Unable to create temp directory " + josmTmpDir);
    9931012        }
    9941013        return josmTmpDir;
     
    10351054     * @return a human readable representation
    10361055     */
    1037     public static String getPositionListString(List<Integer> positionList)  {
     1056    public static String getPositionListString(List<Integer> positionList) {
    10381057        Collections.sort(positionList);
    10391058        final StringBuilder sb = new StringBuilder(32);
     
    10601079    }
    10611080
    1062 
    10631081    /**
    10641082     * Returns a list of capture groups if {@link Matcher#matches()}, or {@code null}.
     
    11591177
    11601178        for (int i = 0; i < query.length(); i++) {
    1161             String c = query.substring(i, i+1);
     1179            String c = query.substring(i, i + 1);
    11621180            if (URL_CHARS.contains(c)) {
    11631181                sb.append(c);
     
    12461264            String old = System.setProperty(key, value);
    12471265            if (!key.toLowerCase(Locale.ENGLISH).contains("password")) {
    1248                 Main.debug("System property '"+key+"' set to '"+value+"'. Old value was '"+old+"'");
     1266                Main.debug("System property '" + key + "' set to '" + value + "'. Old value was '" + old + "'");
    12491267            } else {
    1250                 Main.debug("System property '"+key+"' changed.");
     1268                Main.debug("System property '" + key + "' changed.");
    12511269            }
    12521270            return old;
     
    12831301        long start = System.currentTimeMillis();
    12841302        if (Main.isDebugEnabled()) {
    1285             Main.debug("Starting SAX parsing of "+is+" using "+dh);
     1303            Main.debug("Starting SAX parsing of " + is + " using " + dh);
    12861304        }
    12871305        newSafeSAXParser().parse(is, dh);
    12881306        if (Main.isDebugEnabled()) {
    1289             Main.debug("SAX parsing done in " + getDurationString(System.currentTimeMillis()-start));
     1307            Main.debug("SAX parsing done in " + getDurationString(System.currentTimeMillis() - start));
    12901308        }
    12911309    }
     
    12991317     * @since 8404
    13001318     */
    1301     public static boolean hasExtension(String filename, String ... extensions) {
     1319    public static boolean hasExtension(String filename, String... extensions) {
    13021320        String name = filename.toLowerCase(Locale.ENGLISH);
    13031321        for (String ext : extensions) {
    1304             if (name.endsWith("."+ext.toLowerCase(Locale.ENGLISH)))
     1322            if (name.endsWith("." + ext.toLowerCase(Locale.ENGLISH)))
    13051323                return true;
    13061324        }
     
    13161334     * @since 8404
    13171335     */
    1318     public static boolean hasExtension(File file, String ... extensions) {
     1336    public static boolean hasExtension(File file, String... extensions) {
    13191337        return hasExtension(file.getName(), extensions);
    13201338    }
Note: See TracChangeset for help on using the changeset viewer.