Changeset 5772 in josm


Ignore:
Timestamp:
2013-03-13T00:29:53+01:00 (7 years ago)
Author:
Don-vip
Message:

see #8505 - strip of trailing and leading whitespace characters of input URL (proper solution than String.trim(), see Utils.strip()) + unit test

Location:
trunk
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/OpenLocationAction.java

    r5691 r5772  
    3535import org.openstreetmap.josm.gui.widgets.HistoryComboBox;
    3636import org.openstreetmap.josm.tools.Shortcut;
     37import org.openstreetmap.josm.tools.Utils;
    3738
    3839/**
     
    119120        if (dialog.getValue() != 1) return;
    120121        remindUploadAddressHistory(uploadAddresses);
    121         openUrl(layer.isSelected(), uploadAddresses.getText());
     122        openUrl(layer.isSelected(), Utils.strip(uploadAddresses.getText()));
    122123    }
    123124   
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r5589 r5772  
    573573        return connection;
    574574    }
     575   
     576    /**
     577     * An alternative to {@link String#trim()} to effectively remove all leading and trailing white characters, including Unicode ones.
     578     * @see <a href="http://closingbraces.net/2008/11/11/javastringtrim/">Java’s String.trim has a strange idea of whitespace</a>
     579     * @see <a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4080617">JDK bug 4080617</a> 
     580     * @param str The string to strip
     581     * @return <code>str</code>, without leading and trailing characters, according to
     582     *         {@link Character#isWhitespace(char)} and {@link Character#isSpaceChar(char)}.
     583     * @since 5772
     584     */
     585    public static String strip(String str) {
     586        if (str == null || str.isEmpty()) {
     587            return str;
     588        }
     589        int start = 0, end = str.length();
     590        boolean leadingWhite = true;
     591        while (leadingWhite && start < end) {
     592            char c = str.charAt(start);
     593            if (leadingWhite = (Character.isWhitespace(c) || Character.isSpaceChar(c))) {
     594                start++;
     595            }
     596        }
     597        boolean trailingWhite = true;
     598        while (trailingWhite && end > start+1) {
     599            char c = str.charAt(end-1);
     600            if (trailingWhite = (Character.isWhitespace(c) || Character.isSpaceChar(c))) {
     601                end--;
     602            }
     603        }
     604        return str.substring(start, end);
     605    }
    575606}
Note: See TracChangeset for help on using the changeset viewer.