Made joinEscaped() handle null array elements by converting them to the

empty string.


git-svn-id: https://samskivert.googlecode.com/svn/trunk@550 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
mdb
2002-01-30 18:21:15 +00:00
parent a7eef89a75
commit 2fb0906dda
2 changed files with 11 additions and 4 deletions
@@ -1,5 +1,5 @@
// //
// $Id: StringUtil.java,v 1.19 2002/01/30 18:11:27 mdb Exp $ // $Id: StringUtil.java,v 1.20 2002/01/30 18:21:14 mdb Exp $
// //
// samskivert library - useful routines for java programs // samskivert library - useful routines for java programs
// Copyright (C) 2001 Michael Bayne // Copyright (C) 2001 Michael Bayne
@@ -355,7 +355,8 @@ public class StringUtil
* Joins an array of strings into a single string, separated by * Joins an array of strings into a single string, separated by
* commas, and escaping commas that occur in the individual string * commas, and escaping commas that occur in the individual string
* values such that a subsequent call to {@link #parseStringArray} * values such that a subsequent call to {@link #parseStringArray}
* would recreated the string array properly. * would recreated the string array properly. Any elements in the
* values array that are null will be treated as an empty string.
*/ */
public static String joinEscaped (String[] values) public static String joinEscaped (String[] values)
{ {
@@ -365,7 +366,8 @@ public class StringUtil
if (i > 0) { if (i > 0) {
buf.append(", "); buf.append(", ");
} }
buf.append(replace(values[i], ",", ",,")); String value = (values[i] == null) ? "" : values[i];
buf.append(replace(value, ",", ",,"));
} }
return buf.toString(); return buf.toString();
} }
@@ -1,5 +1,5 @@
// //
// $Id: StringUtilTest.java,v 1.1 2002/01/30 18:11:27 mdb Exp $ // $Id: StringUtilTest.java,v 1.2 2002/01/30 18:21:15 mdb Exp $
// //
// samskivert library - useful routines for java programs // samskivert library - useful routines for java programs
// Copyright (C) 2001 Michael Bayne // Copyright (C) 2001 Michael Bayne
@@ -43,6 +43,11 @@ public class StringUtilTest extends TestCase
// now join them back together // now join them back together
String joined = StringUtil.joinEscaped(tokens); String joined = StringUtil.joinEscaped(tokens);
assert("joined.equals(source)", joined.equals(source)); assert("joined.equals(source)", joined.equals(source));
// make sure null to empty string works
tokens = new String[] { "this", null, "is", null, "a", null, "test" };
joined = StringUtil.joinEscaped(tokens);
assert("null elements work", joined.equals("this, , is, , a, , test"));
} }
public static Test suite () public static Test suite ()