@Test public void escapeCsvWithSingleQuote() { CharSequence value = "\""; CharSequence expected = "\"\"\"\""; escapeCsv(value, expected); }
public static CharSequence escapeCsv(CharSequence value) { int length = checkNotNull(value, "value").length(); if (length == 0) { return value; } int last = length - 1; boolean quoted = isDoubleQuote(value.charAt(0)) && isDoubleQuote(value.charAt(last)) && length != 1; boolean foundSpecialCharacter = false; boolean escapedDoubleQuote = false; StringBuilder escaped = new StringBuilder(length + CSV_NUMBER_ESCAPE_CHARACTERS).append(DOUBLE_QUOTE); for (int i = 0; i < length; i++) { char current = value.charAt(i); switch (current) { case DOUBLE_QUOTE: if (i == 0 || i == last) { if (!quoted) { escaped.append(DOUBLE_QUOTE); } else { continue; } } else { boolean isNextCharDoubleQuote = isDoubleQuote(value.charAt(i + 1)); if (!isDoubleQuote(value.charAt(i - 1)) && (!isNextCharDoubleQuote || i + 1 == last)) { escaped.append(DOUBLE_QUOTE); escapedDoubleQuote = true; } break; } case LINE_FEED: case CARRIAGE_RETURN: case COMMA: foundSpecialCharacter = true; } escaped.append(current); } return escapedDoubleQuote || foundSpecialCharacter && !quoted ? escaped.append(DOUBLE_QUOTE) : value; }
StringUtil { public static CharSequence escapeCsv(CharSequence value) { int length = checkNotNull(value, "value").length(); if (length == 0) { return value; } int last = length - 1; boolean quoted = isDoubleQuote(value.charAt(0)) && isDoubleQuote(value.charAt(last)) && length != 1; boolean foundSpecialCharacter = false; boolean escapedDoubleQuote = false; StringBuilder escaped = new StringBuilder(length + CSV_NUMBER_ESCAPE_CHARACTERS).append(DOUBLE_QUOTE); for (int i = 0; i < length; i++) { char current = value.charAt(i); switch (current) { case DOUBLE_QUOTE: if (i == 0 || i == last) { if (!quoted) { escaped.append(DOUBLE_QUOTE); } else { continue; } } else { boolean isNextCharDoubleQuote = isDoubleQuote(value.charAt(i + 1)); if (!isDoubleQuote(value.charAt(i - 1)) && (!isNextCharDoubleQuote || i + 1 == last)) { escaped.append(DOUBLE_QUOTE); escapedDoubleQuote = true; } break; } case LINE_FEED: case CARRIAGE_RETURN: case COMMA: foundSpecialCharacter = true; } escaped.append(current); } return escapedDoubleQuote || foundSpecialCharacter && !quoted ? escaped.append(DOUBLE_QUOTE) : value; } }
StringUtil { public static CharSequence escapeCsv(CharSequence value) { int length = checkNotNull(value, "value").length(); if (length == 0) { return value; } int last = length - 1; boolean quoted = isDoubleQuote(value.charAt(0)) && isDoubleQuote(value.charAt(last)) && length != 1; boolean foundSpecialCharacter = false; boolean escapedDoubleQuote = false; StringBuilder escaped = new StringBuilder(length + CSV_NUMBER_ESCAPE_CHARACTERS).append(DOUBLE_QUOTE); for (int i = 0; i < length; i++) { char current = value.charAt(i); switch (current) { case DOUBLE_QUOTE: if (i == 0 || i == last) { if (!quoted) { escaped.append(DOUBLE_QUOTE); } else { continue; } } else { boolean isNextCharDoubleQuote = isDoubleQuote(value.charAt(i + 1)); if (!isDoubleQuote(value.charAt(i - 1)) && (!isNextCharDoubleQuote || i + 1 == last)) { escaped.append(DOUBLE_QUOTE); escapedDoubleQuote = true; } break; } case LINE_FEED: case CARRIAGE_RETURN: case COMMA: foundSpecialCharacter = true; } escaped.append(current); } return escapedDoubleQuote || foundSpecialCharacter && !quoted ? escaped.append(DOUBLE_QUOTE) : value; } private  StringUtil(); }
StringUtil { public static CharSequence escapeCsv(CharSequence value) { int length = checkNotNull(value, "value").length(); if (length == 0) { return value; } int last = length - 1; boolean quoted = isDoubleQuote(value.charAt(0)) && isDoubleQuote(value.charAt(last)) && length != 1; boolean foundSpecialCharacter = false; boolean escapedDoubleQuote = false; StringBuilder escaped = new StringBuilder(length + CSV_NUMBER_ESCAPE_CHARACTERS).append(DOUBLE_QUOTE); for (int i = 0; i < length; i++) { char current = value.charAt(i); switch (current) { case DOUBLE_QUOTE: if (i == 0 || i == last) { if (!quoted) { escaped.append(DOUBLE_QUOTE); } else { continue; } } else { boolean isNextCharDoubleQuote = isDoubleQuote(value.charAt(i + 1)); if (!isDoubleQuote(value.charAt(i - 1)) && (!isNextCharDoubleQuote || i + 1 == last)) { escaped.append(DOUBLE_QUOTE); escapedDoubleQuote = true; } break; } case LINE_FEED: case CARRIAGE_RETURN: case COMMA: foundSpecialCharacter = true; } escaped.append(current); } return escapedDoubleQuote || foundSpecialCharacter && !quoted ? escaped.append(DOUBLE_QUOTE) : value; } private  StringUtil(); static String[] split(String value, char delim); static String[] split(String value, char delim, int maxParts); static String substringAfter(String value, char delim); static boolean commonSuffixOfLength(String s, String p, int len); static String byteToHexStringPadded(int value); static T byteToHexStringPadded(T buf, int value); static String toHexStringPadded(byte[] src); static String toHexStringPadded(byte[] src, int offset, int length); static T toHexStringPadded(T dst, byte[] src); static T toHexStringPadded(T dst, byte[] src, int offset, int length); static String byteToHexString(int value); static T byteToHexString(T buf, int value); static String toHexString(byte[] src); static String toHexString(byte[] src, int offset, int length); static T toHexString(T dst, byte[] src); static T toHexString(T dst, byte[] src, int offset, int length); static String simpleClassName(Object o); static String simpleClassName(Class<?> clazz); static CharSequence escapeCsv(CharSequence value); static CharSequence unescapeCsv(CharSequence value); static List<CharSequence> unescapeCsvFields(CharSequence value); static int length(String s); static boolean isNullOrEmpty(String s); static boolean isSurrogate(char c); }
StringUtil { public static CharSequence escapeCsv(CharSequence value) { int length = checkNotNull(value, "value").length(); if (length == 0) { return value; } int last = length - 1; boolean quoted = isDoubleQuote(value.charAt(0)) && isDoubleQuote(value.charAt(last)) && length != 1; boolean foundSpecialCharacter = false; boolean escapedDoubleQuote = false; StringBuilder escaped = new StringBuilder(length + CSV_NUMBER_ESCAPE_CHARACTERS).append(DOUBLE_QUOTE); for (int i = 0; i < length; i++) { char current = value.charAt(i); switch (current) { case DOUBLE_QUOTE: if (i == 0 || i == last) { if (!quoted) { escaped.append(DOUBLE_QUOTE); } else { continue; } } else { boolean isNextCharDoubleQuote = isDoubleQuote(value.charAt(i + 1)); if (!isDoubleQuote(value.charAt(i - 1)) && (!isNextCharDoubleQuote || i + 1 == last)) { escaped.append(DOUBLE_QUOTE); escapedDoubleQuote = true; } break; } case LINE_FEED: case CARRIAGE_RETURN: case COMMA: foundSpecialCharacter = true; } escaped.append(current); } return escapedDoubleQuote || foundSpecialCharacter && !quoted ? escaped.append(DOUBLE_QUOTE) : value; } private  StringUtil(); static String[] split(String value, char delim); static String[] split(String value, char delim, int maxParts); static String substringAfter(String value, char delim); static boolean commonSuffixOfLength(String s, String p, int len); static String byteToHexStringPadded(int value); static T byteToHexStringPadded(T buf, int value); static String toHexStringPadded(byte[] src); static String toHexStringPadded(byte[] src, int offset, int length); static T toHexStringPadded(T dst, byte[] src); static T toHexStringPadded(T dst, byte[] src, int offset, int length); static String byteToHexString(int value); static T byteToHexString(T buf, int value); static String toHexString(byte[] src); static String toHexString(byte[] src, int offset, int length); static T toHexString(T dst, byte[] src); static T toHexString(T dst, byte[] src, int offset, int length); static String simpleClassName(Object o); static String simpleClassName(Class<?> clazz); static CharSequence escapeCsv(CharSequence value); static CharSequence unescapeCsv(CharSequence value); static List<CharSequence> unescapeCsvFields(CharSequence value); static int length(String s); static boolean isNullOrEmpty(String s); static boolean isSurrogate(char c); static final String EMPTY_STRING; static final String NEWLINE; static final char DOUBLE_QUOTE; static final char COMMA; static final char LINE_FEED; static final char CARRIAGE_RETURN; static final char TAB; }