diff options
-rw-r--r-- | src/java/com/jogamp/common/util/StringUtil.java | 10 | ||||
-rw-r--r-- | src/junit/com/jogamp/common/util/TestStringUtil.java | 78 |
2 files changed, 45 insertions, 43 deletions
diff --git a/src/java/com/jogamp/common/util/StringUtil.java b/src/java/com/jogamp/common/util/StringUtil.java index 4084a03..b9c86b9 100644 --- a/src/java/com/jogamp/common/util/StringUtil.java +++ b/src/java/com/jogamp/common/util/StringUtil.java @@ -177,9 +177,11 @@ public class StringUtil { } /** - * Remove all given separator unicode characters from given text, - * i.e. all leading, all ending as well as duplicate consecutive separator within. - * The latter reduces the separator to just a single one. + * Remove all leading, trailing and duplicate-within {@code separators} unicode character from the {@code text}. + * <p> + * Duplicate {@code separators} unicode character within the {@code text} are reduced to one occurrence + * and might be replaced with {@code replacement} if not {@code null}. + * </p> * @param text the source text * @param separators separator unicode characters, pass {@code null} for {@link Character#isWhitespace(int) whitespace}. * Consider using {@link #WHITESPACE} to cover all unicode space character. @@ -187,7 +189,7 @@ public class StringUtil { * If {@code null}, the first found separator is used. * @return stripped text */ - public static String strip(final String text, final String separators, final String replacement) { + public static String trim(final String text, final String separators, final String replacement) { if (text == null ) { return ""; } diff --git a/src/junit/com/jogamp/common/util/TestStringUtil.java b/src/junit/com/jogamp/common/util/TestStringUtil.java index 89f126a..31e8c7f 100644 --- a/src/junit/com/jogamp/common/util/TestStringUtil.java +++ b/src/junit/com/jogamp/common/util/TestStringUtil.java @@ -69,54 +69,54 @@ public class TestStringUtil { } @Test - public void test03Strip() { + public void test03Trim() { { - Assert.assertEquals("", StringUtil.strip(null, null, null)); - Assert.assertEquals("", StringUtil.strip("", null, null)); - Assert.assertEquals("Hello", StringUtil.strip("Hello", null, null)); - Assert.assertEquals("Hello JogAmp", StringUtil.strip("Hello JogAmp", null, null)); - Assert.assertEquals("Hello JogAmp", StringUtil.strip(" Hello JogAmp ", null, null)); - Assert.assertEquals("Hello JogAmp", StringUtil.strip("Hello JogAmp", null, null)); - Assert.assertEquals("Hello JogAmp", StringUtil.strip(" Hello JogAmp ", null, null)); + Assert.assertEquals("", StringUtil.trim(null, null, null)); + Assert.assertEquals("", StringUtil.trim("", null, null)); + Assert.assertEquals("Hello", StringUtil.trim("Hello", null, null)); + Assert.assertEquals("Hello JogAmp", StringUtil.trim("Hello JogAmp", null, null)); + Assert.assertEquals("Hello JogAmp", StringUtil.trim(" Hello JogAmp ", null, null)); + Assert.assertEquals("Hello JogAmp", StringUtil.trim("Hello JogAmp", null, null)); + Assert.assertEquals("Hello JogAmp", StringUtil.trim(" Hello JogAmp ", null, null)); final String seperators = " \t\n\r"; - Assert.assertEquals("", StringUtil.strip(null, seperators, null)); - Assert.assertEquals("", StringUtil.strip("", seperators, null)); - Assert.assertEquals("Hello", StringUtil.strip("Hello", seperators, null)); - Assert.assertEquals("Hello JogAmp", StringUtil.strip("Hello JogAmp", seperators, null)); - Assert.assertEquals("Hello JogAmp", StringUtil.strip(" Hello JogAmp ", seperators, null)); - Assert.assertEquals("Hello JogAmp", StringUtil.strip("Hello JogAmp", seperators, null)); - Assert.assertEquals("Hello JogAmp", StringUtil.strip(" Hello JogAmp ", seperators, null)); - - Assert.assertEquals("Hello JogAmp", StringUtil.strip("\tHello JogAmp\n", seperators, null)); - Assert.assertEquals("Hello\tJogAmp", StringUtil.strip("Hello\t\tJogAmp", seperators, null)); - Assert.assertEquals("Hello JogAmp", StringUtil.strip("Hello \tJogAmp", seperators, null)); - Assert.assertEquals("Hello\nJogAmp", StringUtil.strip("\t Hello\n\tJogAmp \n", seperators, null)); + Assert.assertEquals("", StringUtil.trim(null, seperators, null)); + Assert.assertEquals("", StringUtil.trim("", seperators, null)); + Assert.assertEquals("Hello", StringUtil.trim("Hello", seperators, null)); + Assert.assertEquals("Hello JogAmp", StringUtil.trim("Hello JogAmp", seperators, null)); + Assert.assertEquals("Hello JogAmp", StringUtil.trim(" Hello JogAmp ", seperators, null)); + Assert.assertEquals("Hello JogAmp", StringUtil.trim("Hello JogAmp", seperators, null)); + Assert.assertEquals("Hello JogAmp", StringUtil.trim(" Hello JogAmp ", seperators, null)); + + Assert.assertEquals("Hello JogAmp", StringUtil.trim("\tHello JogAmp\n", seperators, null)); + Assert.assertEquals("Hello\tJogAmp", StringUtil.trim("Hello\t\tJogAmp", seperators, null)); + Assert.assertEquals("Hello JogAmp", StringUtil.trim("Hello \tJogAmp", seperators, null)); + Assert.assertEquals("Hello\nJogAmp", StringUtil.trim("\t Hello\n\tJogAmp \n", seperators, null)); } { final String replacement = "Z"; - Assert.assertEquals("", StringUtil.strip(null, null, replacement)); - Assert.assertEquals("", StringUtil.strip("", null, replacement)); - Assert.assertEquals("Hello", StringUtil.strip("Hello", null, replacement)); - Assert.assertEquals("HelloZJogAmp", StringUtil.strip("Hello JogAmp", null, replacement)); - Assert.assertEquals("HelloZJogAmp", StringUtil.strip(" Hello JogAmp ", null, replacement)); - Assert.assertEquals("HelloZJogAmp", StringUtil.strip("Hello JogAmp", null, replacement)); - Assert.assertEquals("HelloZJogAmp", StringUtil.strip(" Hello JogAmp ", null, replacement)); + Assert.assertEquals("", StringUtil.trim(null, null, replacement)); + Assert.assertEquals("", StringUtil.trim("", null, replacement)); + Assert.assertEquals("Hello", StringUtil.trim("Hello", null, replacement)); + Assert.assertEquals("HelloZJogAmp", StringUtil.trim("Hello JogAmp", null, replacement)); + Assert.assertEquals("HelloZJogAmp", StringUtil.trim(" Hello JogAmp ", null, replacement)); + Assert.assertEquals("HelloZJogAmp", StringUtil.trim("Hello JogAmp", null, replacement)); + Assert.assertEquals("HelloZJogAmp", StringUtil.trim(" Hello JogAmp ", null, replacement)); final String IDIOSPC = String.valueOf(Character.toChars(0x3000)); final String seperators = StringUtil.WHITESPACE; - Assert.assertEquals("", StringUtil.strip(null, seperators, replacement)); - Assert.assertEquals("", StringUtil.strip("", seperators, replacement)); - Assert.assertEquals("Hello", StringUtil.strip("Hello", seperators, replacement)); - Assert.assertEquals("HelloZJogAmp", StringUtil.strip("Hello JogAmp", seperators, replacement)); - Assert.assertEquals("HelloZJogAmp", StringUtil.strip(" Hello"+IDIOSPC+"JogAmp ", seperators, replacement)); - Assert.assertEquals("HelloZJogAmp", StringUtil.strip("Hello JogAmp", seperators, replacement)); - Assert.assertEquals("HelloZJogAmp", StringUtil.strip(" Hello JogAmp ", seperators, replacement)); - - Assert.assertEquals("HelloZJogAmp", StringUtil.strip("\tHello JogAmp\n", seperators, replacement)); - Assert.assertEquals("HelloZJogAmp", StringUtil.strip("Hello\t\tJogAmp", seperators, replacement)); - Assert.assertEquals("HelloZJogAmp", StringUtil.strip("Hello \tJogAmp", seperators, replacement)); - Assert.assertEquals("HelloZJogAmp", StringUtil.strip("\t Hello\n\tJogAmp \n", seperators, replacement)); + Assert.assertEquals("", StringUtil.trim(null, seperators, replacement)); + Assert.assertEquals("", StringUtil.trim("", seperators, replacement)); + Assert.assertEquals("Hello", StringUtil.trim("Hello", seperators, replacement)); + Assert.assertEquals("HelloZJogAmp", StringUtil.trim("Hello JogAmp", seperators, replacement)); + Assert.assertEquals("HelloZJogAmp", StringUtil.trim(" Hello"+IDIOSPC+"JogAmp ", seperators, replacement)); + Assert.assertEquals("HelloZJogAmp", StringUtil.trim("Hello JogAmp", seperators, replacement)); + Assert.assertEquals("HelloZJogAmp", StringUtil.trim(" Hello JogAmp ", seperators, replacement)); + + Assert.assertEquals("HelloZJogAmp", StringUtil.trim("\tHello JogAmp\n", seperators, replacement)); + Assert.assertEquals("HelloZJogAmp", StringUtil.trim("Hello\t\tJogAmp", seperators, replacement)); + Assert.assertEquals("HelloZJogAmp", StringUtil.trim("Hello \tJogAmp", seperators, replacement)); + Assert.assertEquals("HelloZJogAmp", StringUtil.trim("\t Hello\n\tJogAmp \n", seperators, replacement)); } } |