dlr 01/08/24 14:12:28
Modified: util/src/java/org/apache/commons/util StringUtils.java
Log:
Merged implode(Object[], String) into join(String[], String), and
deprecated implode(). Resulting merged function now does better
pre-allocation of buffer size and handles the case where the list is
empty. Changed first parameter from String[] to Object[].
Revision Changes Path
1.4 +13 -15
jakarta-commons-sandbox/util/src/java/org/apache/commons/util/StringUtils.java
Index: StringUtils.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/util/src/java/org/apache/commons/util/StringUtils.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -u -r1.3 -r1.4
--- StringUtils.java 2001/08/24 19:06:54 1.3
+++ StringUtils.java 2001/08/24 21:12:28 1.4
@@ -76,7 +76,7 @@
* @author <a href="mailto:[EMAIL PROTECTED]">Jon S. Stevens</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Daniel Rall</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Greg Coladonato</a>
- * @version $Id: StringUtils.java,v 1.3 2001/08/24 19:06:54 bayard Exp $
+ * @version $Id: StringUtils.java,v 1.4 2001/08/24 21:12:28 dlr Exp $
*/
public class StringUtils
{
@@ -262,17 +262,22 @@
}
/**
- * Joins the elements of the provided array into a single string
- * containing a list of CSV elements.
+ * Joins the elements of the provided array into a single CSV
+ * string containing the provided list of elements. No delimiter
+ * is added before or after the list.
*
* @param list The list of values to join together.
* @param separator The separator character.
* @return The CSV text.
*/
- public static String join(String[] list, String separator)
+ public static String join(Object[] list, String separator)
{
- StringBuffer csv = new StringBuffer();
- for (int i = 0; i < list.length; i++)
+ int listSize = list.length;
+ int bufSize = (listSize == 0 ? 0 :(list[0].toString().length() +
+ separator.length()) * listSize);
+ StringBuffer csv = new StringBuffer(bufSize);
+
+ for (int i = 0; i < listSize; i++)
{
if (i > 0)
{
@@ -406,17 +411,10 @@
* @param sep String delimiter
*
* @return String delimited list of the passed in object's toStrings.
+ * @deprecated Use join(Object[], String) instead.
*/
static public String implode(Object[] objs, String sep) {
- int sz = objs.length;
- StringBuffer buffer = new
StringBuffer((objs[0].toString().length()+sep.length())*sz);
- for(int i = 0; i<sz; i++) {
- buffer.append(objs[i]);
- if(i != sz - 1) {
- buffer.append(sep);
- }
- }
- return buffer.toString();
+ return join(objs, sep);
}