- Timestamp:
- 2024-05-30T23:44:58+02:00 (6 months ago)
- Location:
- trunk
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/ExpressionFactory.java
r18972 r19089 255 255 FACTORY_MAP.put("URL_encode", Factory.of(String.class, Functions::URL_encode)); 256 256 FACTORY_MAP.put("XML_encode", Factory.of(String.class, Functions::XML_encode)); 257 FACTORY_MAP.put("siunit_length", Factory.of(String.class, Functions::siunit_length)); 257 258 FACTORY_MAP.put("abs", Factory.of(Math::acos)); 258 259 FACTORY_MAP.put("acos", Factory.of(Math::acos)); -
trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/Functions.java
r18876 r19089 1186 1186 1187 1187 /** 1188 * Convert a length unit to meters 1189 * <p> 1190 * Tries to convert a length unit to meter value or returns a - when impossible 1191 * @param s arbitrary string representing a length 1192 * @return the length converted to meters 1193 * @since 19089 1194 */ 1195 public static String siunit_length(String s) { 1196 if (s == null) 1197 return null; 1198 try { 1199 return Utils.unitToMeter(s).toString(); 1200 } catch (IllegalArgumentException e) { 1201 Logging.debug(e); 1202 } 1203 return "-"; 1204 } 1205 1206 /** 1188 1207 * Calculates the CRC32 checksum from a string (based on RFC 1952). 1189 1208 * @param s the string -
trunk/src/org/openstreetmap/josm/tools/Utils.java
r19080 r19089 91 91 private static final Pattern REMOVE_DIACRITICS = Pattern.compile("\\p{InCombiningDiacriticalMarks}+"); 92 92 93 private static final Pattern PATTERN_LENGTH = Pattern.compile("^(\\d+(?:\\.\\d+)?)(cm|mm|m|ft|in|'|\")?$"); 94 private static final Pattern PATTERN_LENGTH2 = Pattern.compile("^(\\d+(?:\\.\\d+)?)(ft|')(\\d+(?:\\.\\d+)?)(in|\")?$"); 95 93 96 private static final String DEFAULT_STRIP = "\uFEFF\u200B"; 94 97 … … 1526 1529 * @return byte array of data in input stream (empty if stream is null) 1527 1530 * @throws IOException if any I/O error occurs 1528 * @deprecated since xxx-- use {@link InputStream#readAllBytes()} instead1531 * @deprecated since 19089 -- use {@link InputStream#readAllBytes()} instead 1529 1532 */ 1530 1533 @Deprecated … … 2059 2062 return string == null ? null : string.intern(); 2060 2063 } 2064 2065 /** 2066 * Convert a length unit to meters 2067 * @param s arbitrary string representing a length 2068 * @return the length converted to meters 2069 * @since 19089 2070 */ 2071 public static Double unitToMeter(String s) throws IllegalArgumentException { 2072 s = s.replaceAll(" ", "").replaceAll(",", "."); 2073 Matcher m = PATTERN_LENGTH.matcher(s); 2074 if (m.matches()) { 2075 Double v = Double.valueOf(m.group(1)); 2076 if ("cm".equals(m.group(2))) 2077 v *= 0.01; 2078 else if ("mm".equals(m.group(2))) 2079 v *= 0.001; 2080 else if ("ft".equals(m.group(2)) || "'".equals(m.group(2))) 2081 v *= 0.3048; 2082 else if ("in".equals(m.group(2)) || "\"".equals(m.group(2))) 2083 v *= 0.0254; 2084 return v; 2085 } else { 2086 m = PATTERN_LENGTH2.matcher(s); 2087 if (m.matches()) { 2088 return Double.valueOf(m.group(1))*0.3048+Double.valueOf(m.group(3))*0.0254; 2089 } 2090 } 2091 throw new IllegalArgumentException("Invalid length value: " + s); 2092 } 2061 2093 } -
trunk/test/unit/org/openstreetmap/josm/tools/UtilsTest.java
r19056 r19089 552 552 assertEquals(-1.0, Utils.getStandardDeviation(new double[]{0})); 553 553 } 554 555 /** 556 * Test of {@link Utils#unitToMeter(String)} 557 */ 558 @Test 559 void testUnitToMeter() { 560 assertEquals(1.2, Utils.unitToMeter("1.2")); 561 assertEquals(1.3, Utils.unitToMeter(" 1,3 m ")); 562 assertEquals(1.4, Utils.unitToMeter("1.4m")); 563 assertEquals(1.5, Utils.unitToMeter("150cm")); 564 assertEquals(1.6, Utils.unitToMeter("1600.0mm")); 565 assertEquals(3.048, Utils.unitToMeter("10ft")); 566 assertEquals(6.096, Utils.unitToMeter("20'")); 567 assertEquals(2.54, Utils.unitToMeter("100in")); 568 assertEquals(5.08, Utils.unitToMeter("200\"")); 569 assertEquals(3.0734, Utils.unitToMeter("10ft1in")); 570 assertEquals(6.1468, Utils.unitToMeter("20'2\"")); 571 assertThrows(IllegalArgumentException.class, () -> Utils.unitToMeter("Hallo")); 572 } 554 573 }
Note:
See TracChangeset
for help on using the changeset viewer.