Changeset 15862 in josm


Ignore:
Timestamp:
2020-02-16T15:45:59+01:00 (4 years ago)
Author:
simon04
Message:

Simplify Utils.strip

Location:
trunk
Files:
2 edited

Legend:

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

    r15760 r15862  
    9090    private static final Pattern REMOVE_DIACRITICS = Pattern.compile("\\p{InCombiningDiacriticalMarks}+");
    9191
    92     private static final char[] DEFAULT_STRIP = {'\u200B', '\uFEFF'};
     92    private static final String DEFAULT_STRIP = "\uFEFF\u200B";
    9393
    9494    private static final String[] SIZE_UNITS = {"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"};
     
    781781        if (str != null) {
    782782            for (int i = 0; i < str.length(); i++) {
    783                 if (!isStrippedChar(str.charAt(i), DEFAULT_STRIP)) {
     783                if (!isStrippedChar(str.charAt(i), null)) {
    784784                    return false;
    785785                }
     
    820820            return str;
    821821        }
    822         return strip(str, stripChars(skipChars));
    823     }
    824 
    825     private static String strip(final String str, final char... skipChars) {
    826822
    827823        int start = 0;
     
    835831        }
    836832        boolean trailingSkipChar = true;
    837         while (trailingSkipChar && end > start + 1) {
     833        while (trailingSkipChar && end > start) {
    838834            trailingSkipChar = isStrippedChar(str.charAt(end - 1), skipChars);
    839835            if (trailingSkipChar) {
     
    845841    }
    846842
    847     private static boolean isStrippedChar(char c, final char... skipChars) {
    848         return Character.isWhitespace(c) || Character.isSpaceChar(c) || stripChar(skipChars, c);
    849     }
    850 
    851     private static char[] stripChars(final String skipChars) {
    852         if (skipChars == null || skipChars.isEmpty()) {
    853             return DEFAULT_STRIP;
    854         }
    855 
    856         char[] chars = new char[DEFAULT_STRIP.length + skipChars.length()];
    857         System.arraycopy(DEFAULT_STRIP, 0, chars, 0, DEFAULT_STRIP.length);
    858         skipChars.getChars(0, skipChars.length(), chars, DEFAULT_STRIP.length);
    859 
    860         return chars;
    861     }
    862 
    863     private static boolean stripChar(final char[] strip, char c) {
    864         for (char s : strip) {
    865             if (c == s) {
    866                 return true;
    867             }
    868         }
    869         return false;
     843    private static boolean isStrippedChar(char c, final String skipChars) {
     844        return Character.isWhitespace(c) || Character.isSpaceChar(c)
     845                || DEFAULT_STRIP.indexOf(c) >= 0
     846                || (skipChars != null && skipChars.indexOf(c) >= 0);
    870847    }
    871848
  • trunk/test/unit/org/openstreetmap/josm/tools/UtilsTest.java

    r15760 r15862  
    7171        assertEquals("", Utils.strip("   "));
    7272        assertEquals("", Utils.strip(someWhite));
     73        assertEquals("", Utils.strip("\u200B"));
     74        assertEquals("", Utils.strip("\uFEFF"));
    7375        assertEquals("a", Utils.strip("a"));
    7476        assertEquals("ab", Utils.strip("ab"));
     
    8587
    8688        // extended skip
     89        assertNull(Utils.strip(null, "abc"));
     90        assertEquals("", Utils.strip("", "b"));
     91        assertEquals("", Utils.strip("abbbb", "ab"));
    8792        assertEquals("a", Utils.strip("a", "b"));
     93        assertEquals("a", Utils.strip("abbbb", "b"));
    8894        assertEquals("b", Utils.strip("acbcac", "ac"));
    8995    }
Note: See TracChangeset for help on using the changeset viewer.