Changeset 17613 in josm


Ignore:
Timestamp:
2021-03-21T09:23:34+01:00 (4 years ago)
Author:
simon04
Message:

fix #14124 - Add MapCSS function title

Returns a title-cased version of the string where words start with an uppercase character and the remaining characters are lowercase.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/Functions.java

    r16643 r17613  
    973973
    974974    /**
     975     * Returns a title-cased version of the string where words start with an uppercase character and the remaining characters are lowercase
     976     *
     977     * Also known as "capitalize".
     978     * @param str The source string
     979     * @return The resulting string
     980     * @since 17613
     981     * @see Character#toTitleCase(char)
     982     */
     983    public static String title(String str) {
     984        // adapted from org.apache.commons.lang3.text.WordUtils.capitalize
     985        if (str == null) {
     986            return null;
     987        }
     988        final char[] buffer = str.toCharArray();
     989        boolean capitalizeNext = true;
     990        for (int i = 0; i < buffer.length; i++) {
     991            final char ch = buffer[i];
     992            if (Character.isWhitespace(ch)) {
     993                capitalizeNext = true;
     994            } else if (capitalizeNext) {
     995                buffer[i] = Character.toTitleCase(ch);
     996                capitalizeNext = false;
     997            } else {
     998                buffer[i] = Character.toLowerCase(ch);
     999            }
     1000        }
     1001        return new String(buffer);
     1002    }
     1003
     1004    /**
    9751005     * Trim whitespaces from the string {@code s}.
    9761006     * @param s The source string
  • trunk/test/unit/org/openstreetmap/josm/gui/mappaint/mapcss/FunctionsTest.java

    r17275 r17613  
    33
    44import static org.junit.jupiter.api.Assertions.assertEquals;
     5import static org.junit.jupiter.api.Assertions.assertNull;
    56import static org.junit.jupiter.api.Assertions.assertTrue;
    67import static org.openstreetmap.josm.data.osm.OsmPrimitiveType.NODE;
     
    5455            return new Environment(osm);
    5556        }
     57    }
     58
     59    /**
     60     * Unit test of {@link Functions#title}.
     61     */
     62    @Test
     63    void testTitle() {
     64        assertNull(Functions.title(null));
     65        assertEquals("", Functions.title(""));
     66        assertEquals("I Am Fine", Functions.title("i am FINE"));
    5667    }
    5768
Note: See TracChangeset for help on using the changeset viewer.