Changeset 8567 in josm for trunk/src/org/openstreetmap/josm/tools
- Timestamp:
- 2015-07-04T18:53:26+02:00 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/tools/Utils.java
r8513 r8567 40 40 import java.util.Collection; 41 41 import java.util.Collections; 42 import java.util.HashSet;43 42 import java.util.Iterator; 44 43 import java.util.List; 45 44 import java.util.Locale; 46 import java.util.Set;47 45 import java.util.concurrent.ExecutorService; 48 46 import java.util.concurrent.Executors; … … 83 81 84 82 public static final String URL_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~:/?#[]@!$&'()*+,;=%"; 83 84 private static char[] DEFAULT_STRIP = {'\u200B', '\uFEFF'}; 85 85 86 86 /** … … 902 902 */ 903 903 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); 905 908 } 906 909 … … 917 920 return str; 918 921 } 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 930 927 int start = 0; 931 928 int end = str.length(); … … 933 930 while (leadingSkipChar && start < end) { 934 931 char c = str.charAt(start); 935 leadingSkipChar = Character.isWhitespace(c) || Character.isSpaceChar(c) || s kipSet.contains(c);932 leadingSkipChar = Character.isWhitespace(c) || Character.isSpaceChar(c) || stripChar(skipChars, c); 936 933 if (leadingSkipChar) { 937 934 start++; … … 939 936 } 940 937 boolean trailingSkipChar = true; 941 while (trailingSkipChar && end > start +1) {942 char c = str.charAt(end -1);943 trailingSkipChar = Character.isWhitespace(c) || Character.isSpaceChar(c) || s kipSet.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); 944 941 if (trailingSkipChar) { 945 942 end--; 946 943 } 947 944 } 945 948 946 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; 949 968 } 950 969 … … 990 1009 File josmTmpDir = new File(tmpDir, "JOSM"); 991 1010 if (!josmTmpDir.exists() && !josmTmpDir.mkdirs()) { 992 Main.warn("Unable to create temp directory " +josmTmpDir);1011 Main.warn("Unable to create temp directory " + josmTmpDir); 993 1012 } 994 1013 return josmTmpDir; … … 1035 1054 * @return a human readable representation 1036 1055 */ 1037 public static String getPositionListString(List<Integer> positionList) 1056 public static String getPositionListString(List<Integer> positionList) { 1038 1057 Collections.sort(positionList); 1039 1058 final StringBuilder sb = new StringBuilder(32); … … 1060 1079 } 1061 1080 1062 1063 1081 /** 1064 1082 * Returns a list of capture groups if {@link Matcher#matches()}, or {@code null}. … … 1159 1177 1160 1178 for (int i = 0; i < query.length(); i++) { 1161 String c = query.substring(i, i +1);1179 String c = query.substring(i, i + 1); 1162 1180 if (URL_CHARS.contains(c)) { 1163 1181 sb.append(c); … … 1246 1264 String old = System.setProperty(key, value); 1247 1265 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 + "'"); 1249 1267 } else { 1250 Main.debug("System property '" +key+"' changed.");1268 Main.debug("System property '" + key + "' changed."); 1251 1269 } 1252 1270 return old; … … 1283 1301 long start = System.currentTimeMillis(); 1284 1302 if (Main.isDebugEnabled()) { 1285 Main.debug("Starting SAX parsing of " +is+" using "+dh);1303 Main.debug("Starting SAX parsing of " + is + " using " + dh); 1286 1304 } 1287 1305 newSafeSAXParser().parse(is, dh); 1288 1306 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)); 1290 1308 } 1291 1309 } … … 1299 1317 * @since 8404 1300 1318 */ 1301 public static boolean hasExtension(String filename, String 1319 public static boolean hasExtension(String filename, String... extensions) { 1302 1320 String name = filename.toLowerCase(Locale.ENGLISH); 1303 1321 for (String ext : extensions) { 1304 if (name.endsWith("." +ext.toLowerCase(Locale.ENGLISH)))1322 if (name.endsWith("." + ext.toLowerCase(Locale.ENGLISH))) 1305 1323 return true; 1306 1324 } … … 1316 1334 * @since 8404 1317 1335 */ 1318 public static boolean hasExtension(File file, String 1336 public static boolean hasExtension(File file, String... extensions) { 1319 1337 return hasExtension(file.getName(), extensions); 1320 1338 }
Note:
See TracChangeset
for help on using the changeset viewer.