Ignore:
Timestamp:
2015-05-31T17:06:27+02:00 (7 years ago)
Author:
Don-vip
Message:

fix #11498 - Warn about obvious misspelled tag values (modified patch by mdk) + javadoc

File:
1 edited

Legend:

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

    r8429 r8435  
    4040import java.util.Collection;
    4141import java.util.Collections;
     42import java.util.HashSet;
    4243import java.util.Iterator;
    4344import java.util.List;
    4445import java.util.Locale;
     46import java.util.Set;
    4547import java.util.concurrent.ExecutorService;
    4648import java.util.concurrent.Executors;
     
    899901     * @since 5772
    900902     */
    901     public static String strip(String str) {
     903    public static String strip(final String str) {
     904        return strip(str, null);
     905    }
     906
     907    /**
     908     * An alternative to {@link String#trim()} to effectively remove all leading and trailing white characters, including Unicode ones.
     909     * @param str The string to strip
     910     * @param skipChars additional characters to skip
     911     * @return <code>str</code>, without leading and trailing characters, according to
     912     *         {@link Character#isWhitespace(char)}, {@link Character#isSpaceChar(char)} and skipChars.
     913     * @since 8435
     914     */
     915    public static String strip(final String str, final String skipChars) {
    902916        if (str == null || str.isEmpty()) {
    903917            return str;
    904918        }
    905         int start = 0, end = str.length();
    906         boolean leadingWhite = true;
    907         while (leadingWhite && start < end) {
     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        }
     930        int start = 0;
     931        int end = str.length();
     932        boolean leadingSkipChar = true;
     933        while (leadingSkipChar && start < end) {
    908934            char c = str.charAt(start);
    909             // '\u200B' (ZERO WIDTH SPACE character) needs to be handled manually because of change in Unicode 6.0 (Java 7, see #8918)
    910             // same for '\uFEFF' (ZERO WIDTH NO-BREAK SPACE)
    911             leadingWhite = (Character.isWhitespace(c) || Character.isSpaceChar(c) || c == '\u200B' || c == '\uFEFF');
    912             if (leadingWhite) {
     935            leadingSkipChar = Character.isWhitespace(c) || Character.isSpaceChar(c) || skipSet.contains(c);
     936            if (leadingSkipChar) {
    913937                start++;
    914938            }
    915939        }
    916         boolean trailingWhite = true;
    917         while (trailingWhite && end > start+1) {
     940        boolean trailingSkipChar = true;
     941        while (trailingSkipChar && end > start+1) {
    918942            char c = str.charAt(end-1);
    919             trailingWhite = (Character.isWhitespace(c) || Character.isSpaceChar(c) || c == '\u200B' || c == '\uFEFF');
    920             if (trailingWhite) {
     943            trailingSkipChar = Character.isWhitespace(c) || Character.isSpaceChar(c) || skipSet.contains(c);
     944            if (trailingSkipChar) {
    921945                end--;
    922946            }
Note: See TracChangeset for help on using the changeset viewer.