@@ -189,6 +189,14 @@ public static String rowToString(JSONArray ja, char delimiter) {
189189 return sb .toString ();
190190 }
191191
192+ /**
193+ * Append a single row value, quoting it when required by the delimiter or
194+ * content.
195+ *
196+ * @param sb the destination buffer
197+ * @param object the value to append
198+ * @param delimiter the delimiter used between row values
199+ */
192200 private static void appendRowValue (StringBuilder sb , Object object , char delimiter ) {
193201 if (object == null ) {
194202 return ;
@@ -201,17 +209,38 @@ private static void appendRowValue(StringBuilder sb, Object object, char delimit
201209 }
202210 }
203211
204- private static boolean shouldQuoteValue (String string , char delimiter ) {
205- return !string .isEmpty () && (string .indexOf (delimiter ) >= 0 ||
206- string .indexOf ('\n' ) >= 0 || string .indexOf ('\r' ) >= 0 ||
207- string .indexOf (0 ) >= 0 || string .charAt (0 ) == '"' );
212+ /**
213+ * Determine whether a row value should be quoted.
214+ *
215+ * @param value the row value to evaluate
216+ * @param delimiter the delimiter used between row values
217+ * @return {@code true} if the value should be quoted
218+ */
219+ private static boolean shouldQuoteValue (String value , char delimiter ) {
220+ if (value .isEmpty ()) {
221+ return false ;
222+ }
223+ boolean containsDelimiter = value .indexOf (delimiter ) >= 0 ;
224+ boolean containsNewline = value .indexOf ('\n' ) >= 0 ;
225+ boolean containsCarriageReturn = value .indexOf ('\r' ) >= 0 ;
226+ boolean containsNullCharacter = value .indexOf (0 ) >= 0 ;
227+ boolean startsWithQuote = value .charAt (0 ) == '"' ;
228+ return containsDelimiter || containsNewline || containsCarriageReturn ||
229+ containsNullCharacter || startsWithQuote ;
208230 }
209231
210- private static void appendQuotedValue (StringBuilder sb , String string ) {
232+ /**
233+ * Append a row value surrounded by quotes, omitting characters that should
234+ * not appear inside the quoted value.
235+ *
236+ * @param sb the destination buffer
237+ * @param value the value to append
238+ */
239+ private static void appendQuotedValue (StringBuilder sb , String value ) {
211240 sb .append ('"' );
212- int length = string .length ();
241+ int length = value .length ();
213242 for (int j = 0 ; j < length ; j += 1 ) {
214- char c = string .charAt (j );
243+ char c = value .charAt (j );
215244 if (c >= ' ' && c != '"' ) {
216245 sb .append (c );
217246 }
0 commit comments