You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ni...@apache.org on 2007/11/17 10:27:37 UTC

svn commit: r595927 - /commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringEscapeUtils.java

Author: niallp
Date: Sat Nov 17 01:27:36 2007
New Revision: 595927

URL: http://svn.apache.org/viewvc?rev=595927&view=rev
Log:
LANG-374 - implement Sebb's improvement to use StringUtils.containsNone()

Modified:
    commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringEscapeUtils.java

Modified: commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringEscapeUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringEscapeUtils.java?rev=595927&r1=595926&r2=595927&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringEscapeUtils.java (original)
+++ commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringEscapeUtils.java Sat Nov 17 01:27:36 2007
@@ -40,6 +40,8 @@
  */
 public class StringEscapeUtils {
 
+    private static final char[] CSV_SEARCH_CHARS = new char[] {',', '"', CharUtils.CR, CharUtils.LF};
+
     /**
      * <p><code>StringEscapeUtils</code> instances should NOT be constructed in
      * standard programming.</p>
@@ -713,7 +715,7 @@
      * @since 2.4
      */
     public static String escapeCsv(String str) {
-        if (!containsCsvChars(str)) {
+        if (StringUtils.containsNone(str, CSV_SEARCH_CHARS)) {
             return str;
         }
         StringBuffer buffer = new StringBuffer(str.length() + 10);
@@ -753,7 +755,7 @@
      * @since 2.4
      */
     public static void escapeCsv(Writer out, String str) throws IOException {
-        if (!containsCsvChars(str)) {
+        if (StringUtils.containsNone(str, CSV_SEARCH_CHARS)) {
             if (str != null) {
                 out.write(str);
             }
@@ -768,21 +770,6 @@
             out.write(c);
         }
         out.write('"');
-    }
-
-    /**
-     * Determine if the String contains any characters that need escaping for CSV files.
-     *
-     * @param str  the string to escape, may be null
-     * @return <code>true</code> if the String contains characters that need escaping
-     * for CSV files, otherwise <code>false</code>
-     * @since 2.4
-     */
-    private static boolean containsCsvChars(String str) {
-        return (StringUtils.contains(str, '"') ||
-                StringUtils.contains(str, ',') ||
-                StringUtils.contains(str, CharUtils.CR) ||
-                StringUtils.contains(str, CharUtils.LF));
     }
 
 }