You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by br...@apache.org on 2014/05/06 19:28:51 UTC

svn commit: r1592810 - in /commons/proper/lang/trunk/src: changes/changes.xml main/java/org/apache/commons/lang3/StringUtils.java test/java/org/apache/commons/lang3/StringUtilsTest.java

Author: britter
Date: Tue May  6 17:28:50 2014
New Revision: 1592810

URL: http://svn.apache.org/r1592810
Log:
LANG-1006: Add wrap (with String or char) to StringUtils. This closes PR #21 from github. Thanks to Thiago Andrade.

Modified:
    commons/proper/lang/trunk/src/changes/changes.xml
    commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringUtils.java
    commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/StringUtilsTest.java

Modified: commons/proper/lang/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/changes/changes.xml?rev=1592810&r1=1592809&r2=1592810&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/changes/changes.xml [utf-8] (original)
+++ commons/proper/lang/trunk/src/changes/changes.xml [utf-8] Tue May  6 17:28:50 2014
@@ -22,6 +22,7 @@
   <body>
 
   <release version="3.4" date="tba" description="tba">
+    <action issue="LANG-1006" type="update" dev="britter" due-to="Thiago Andrade">Add wrap (with String or char) to StringUtils</action>
     <action issue="LANG-1005" type="update" dev="britter" due-to="Michael Osipov">Extend DurationFormatUtils#formatDurationISO default pattern to match #formatDurationHMS</action>
     <action issue="LANG-1007" type="update" dev="britter" due-to="Thiago Andrade">Fixing NumberUtils JAVADoc comments for max methods</action>
   	<action issue="LANG-731" type="update" dev="djones">Better Javadoc for BitField class</action>

Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringUtils.java?rev=1592810&r1=1592809&r2=1592810&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringUtils.java (original)
+++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringUtils.java Tue May  6 17:28:50 2014
@@ -588,7 +588,7 @@ public class StringUtils {
         str = stripStart(str, stripChars);
         return stripEnd(str, stripChars);
     }
-
+    
     /**
      * <p>Strips any of a set of characters from the start of a String.</p>
      *
@@ -7636,4 +7636,73 @@ public class StringUtils {
         return new String(bytes, charset != null ? charset : Charset.defaultCharset());
     }
 
+    /**
+     * <p>
+     * Wraps a String with a char.
+     * <p>
+     * 
+     * <p>
+     * A {@code null} input String returns {@code null}.
+     * </p>
+     * 
+     * <pre>
+     * StringUtils.wrap(null, *)   = null
+     * StringUtils.wrap("", *)     = ""
+     * StringUtils.wrap("ab", '\0')  = "ab"
+     * StringUtils.wrap("ab", 'x')  = "xabx"
+     * StringUtils.wrap("ab", '\'')   = "'ab'"
+     * StringUtils.wrap("\"ab\"", '\"') = "\"ab\""
+     * </pre>
+     * 
+     * @param str
+     *            the String to be wrapper, may be null
+     * @param wrapWith
+     *            the char that will wrap str
+     * @return wrapped String, {@code null} if null String input
+     * @since 3.4
+     */
+    public static String wrap(String str, char wrapWith) {
+
+        if (isEmpty(str) || wrapWith == '\0')
+            return str;
+
+        return wrapWith + str + wrapWith;
+    }
+
+    /**
+     * <p>
+     * Wrapps a String with another String.
+     * <p>
+     * 
+     * <p>
+     * A {@code null} input String returns {@code null}.
+     * </p>
+     * 
+     * <pre>
+     * StringUtils.wrap(null, *)   = null
+     * StringUtils.wrap("", *)     = ""
+     * StringUtils.wrap("ab", null)   = "ab"
+     * StringUtils.wrap("ab", "x")  = "xabx"
+     * StringUtils.wrap("ab", "\"")   = "\"ab\""
+     * StringUtils.wrap("\"ab\"", "\"") = "\"\"ab\"\""
+     * StringUtils.wrap("ab", "'")   = "'ab'"
+     * StringUtils.wrap("'abcd'", "'") = "''abcd''"
+     * StringUtils.wrap("\"abcd\"", "'") = "'\"abcd\"'"
+     * StringUtils.wrap("'abcd'", "\"") = "\"'abcd'\""
+     * </pre>
+     * 
+     * @param str
+     *            the String to be wrapper, may be null
+     * @param wrapWith
+     *            the String that will wrap str
+     * @return wrapped String, {@code null} if null String input
+     * @since 3.4
+     */
+    public static String wrap(String str, String wrapWith) {
+
+        if (isEmpty(str) || isEmpty(wrapWith))
+            return str;
+
+        return wrapWith.concat(str).concat(wrapWith);
+    }
 }

Modified: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/StringUtilsTest.java?rev=1592810&r1=1592809&r2=1592810&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/StringUtilsTest.java (original)
+++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/StringUtilsTest.java Tue May  6 17:28:50 2014
@@ -2454,4 +2454,41 @@ public class StringUtilsTest {
         expectedBytes = expectedString.getBytes(encoding);
         assertEquals(expectedString, StringUtils.toEncodedString(expectedBytes, Charset.forName(encoding)));
     }
+    
+    // -----------------------------------------------------------------------
+
+    @Test
+    public void testWrap_StringChar() {
+        assertNull(StringUtils.wrap(null, null));
+        assertNull(StringUtils.wrap(null, '\0'));
+        assertNull(StringUtils.wrap(null, '1'));
+
+        assertEquals(null, StringUtils.wrap(null, null));
+        assertEquals("", StringUtils.wrap("", '\0'));
+        assertEquals("xabx", StringUtils.wrap("ab", 'x'));
+        assertEquals("\"ab\"", StringUtils.wrap("ab", '\"'));
+        assertEquals("\"\"ab\"\"", StringUtils.wrap("\"ab\"", '\"'));
+        assertEquals("'ab'", StringUtils.wrap("ab", '\''));
+        assertEquals("''abcd''", StringUtils.wrap("'abcd'", '\''));
+        assertEquals("'\"abcd\"'", StringUtils.wrap("\"abcd\"", '\''));
+        assertEquals("\"'abcd'\"", StringUtils.wrap("'abcd'", '\"'));
+    }
+
+    @Test
+    public void testWrap_StringString() {
+        assertNull(StringUtils.wrap(null, null));
+        assertNull(StringUtils.wrap(null, ""));
+        assertNull(StringUtils.wrap(null, "1"));
+
+        assertEquals(null, StringUtils.wrap(null, null));
+        assertEquals("", StringUtils.wrap("", ""));
+        assertEquals("ab", StringUtils.wrap("ab", null));
+        assertEquals("xabx", StringUtils.wrap("ab", "x"));
+        assertEquals("\"ab\"", StringUtils.wrap("ab", "\""));
+        assertEquals("\"\"ab\"\"", StringUtils.wrap("\"ab\"", "\""));
+        assertEquals("'ab'", StringUtils.wrap("ab", "'"));
+        assertEquals("''abcd''", StringUtils.wrap("'abcd'", "'"));
+        assertEquals("'\"abcd\"'", StringUtils.wrap("\"abcd\"", "'"));
+        assertEquals("\"'abcd'\"", StringUtils.wrap("'abcd'", "\""));
+    }
 }