commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bay...@apache.org
Subject svn commit: r597281 - /commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringEscapeUtils.java
Date Thu, 22 Nov 2007 01:26:40 GMT
Author: bayard
Date: Wed Nov 21 17:26:39 2007
New Revision: 597281

URL: http://svn.apache.org/viewvc?rev=597281&view=rev
Log:
Replacing the escaped quote with CSV_QUOTE, and changing the non-Writer version to reuse the
Writer version - See: LANG-374

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=597281&r1=597280&r2=597281&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 Wed
Nov 21 17:26:39 2007
@@ -40,7 +40,9 @@
  */
 public class StringEscapeUtils {
 
-    private static final char[] CSV_SEARCH_CHARS = new char[] {',', '"', CharUtils.CR, CharUtils.LF};
+    private static final char CSV_DELIMITER = ',';
+    private static final char CSV_QUOTE = '"';
+    private static final char[] CSV_SEARCH_CHARS = new char[] {CSV_DELIMITER, CSV_QUOTE,
CharUtils.CR, CharUtils.LF};
 
     /**
      * <p><code>StringEscapeUtils</code> instances should NOT be constructed
in
@@ -718,17 +720,15 @@
         if (StringUtils.containsNone(str, CSV_SEARCH_CHARS)) {
             return str;
         }
-        StringBuffer buffer = new StringBuffer(str.length() + 10);
-        buffer.append('"');
-        for (int i = 0; i < str.length(); i++) {
-            char c = str.charAt(i);
-            if (c == '"') {
-                buffer.append('"'); // escape double quote
-            }
-            buffer.append(c);
+        try {
+            StringWriter writer = new StringWriter();
+            escapeCsv(writer, str);
+            return writer.toString();
+        } catch (IOException ioe) {
+            // this should never ever happen while writing to a StringWriter
+            ioe.printStackTrace();
+            return null;
         }
-        buffer.append('"');
-        return buffer.toString();
     }
 
     /**
@@ -761,15 +761,15 @@
             }
             return;
         }
-        out.write('"');
+        out.write(CSV_QUOTE);
         for (int i = 0; i < str.length(); i++) {
             char c = str.charAt(i);
-            if (c == '"') {
-                out.write('"'); // escape double quote
+            if (c == CSV_QUOTE) {
+                out.write(CSV_QUOTE); // escape double quote
             }
             out.write(c);
         }
-        out.write('"');
+        out.write(CSV_QUOTE);
     }
 
 }



Mime
View raw message