Skip to content

Instantly share code, notes, and snippets.

@wushbin
Last active April 8, 2020 04:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wushbin/e2d7ca0fb906c481faa2f7df076a2782 to your computer and use it in GitHub Desktop.
Save wushbin/e2d7ca0fb906c481faa2f7df076a2782 to your computer and use it in GitHub Desktop.
public List<String> decodeCSV(List<String> lines) {
return decode(lines.get(0));
}
public List<String> decode(String line) {
List<String> result = new ArrayList<>();
StringBuilder sb = new StringBuilder();
boolean pendingWrap = false;
int len = line.length();
for (int i = 0; i < len; i++) {
char c = line.charAt(i);
if (c == ',') {
if (pendingWrap) {
sb.append(',');
} else {
result.add(sb.toString());
sb.setLength(0); // clear
}
} else if (c == '\"') {
if (pendingWrap && i + 1 < len && line.charAt(i + 1) == '\"') {
sb.append('\"');
i++;
} else {
pendingWrap = !pendingWrap;
}
} else {
sb.append(c);
}
}
if (sb.length() > 0) {
result.add(sb.toString());
}
return result;
}
public String encodeCSV(List<String> lines) {
StringBuilder sb = new StringBuilder();
for (String line : lines) {
sb.append(encode(line)).append(',');
}
sb.setLength(sb.length() - 1);
return sb.toString();
}
public String encode (String line){
boolean wrap = false;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < line.length(); i++) {
char c = line.charAt(i);
if (c == ',') {
wrap = true;
sb.append(c);
} else if (c == '\"') {
wrap = true;
sb.append('\"').append('\"');
} else {
sb.append(c);
}
}
return wrap ? "\"" + sb.toString() + "\"" : sb.toString();
}
import org.junit.Assert;
import org.junit.Test;
import java.io.*;
import java.util.*;
public class CSV {
class InputValues {
String cmd;
int n;
List<String> lines;
public InputValues(String cmd, int n, List<String> lines) {
this.cmd = cmd;
this.n = n;
this.lines = lines;
}
@Override
public String toString() {
String res = "cmd: " + cmd + "\nn: " + n;
for (String line : lines) {
res += "\n" + line;
}
return res;
}
}
@Test
public void testAll() {
File[] files = readFolder("src/input/CSV");
for (int i = 0; i < files.length; i+=2) {
CSV.InputValues testEncode = readFile(files[i]);
CSV.InputValues testDecode = readFile(files[i+1]);
String encodeResult = encodeCSV(testEncode.lines);
Assert.assertEquals(encodeResult, testDecode.lines.get(0));
List<String> decodeResult = decodeCSV(testDecode.lines);
Assert.assertEquals(decodeResult, testEncode.lines);
}
}
@Test
public void generateOutput() throws IOException {
File[] files = readFolder("src/input/CSV");
for (int i = 0; i < files.length; i++) {
File file = files[i];
CSV.InputValues test = readFile(file);
String outfileName = "src/output/CSV/" + file.getName().replace(".in", ".out");
File outfile = new File(outfileName);
outfile.getParentFile().mkdirs();
outfile.createNewFile();
PrintWriter printWriter = new PrintWriter(new FileWriter(outfileName));
if ("encode".equals(test.cmd)) {
String encodeResult = encodeCSV(test.lines);
printWriter.println(encodeResult);
} else if ("decode".equals(test.cmd)) {
List<String> decodeResult = decodeCSV(test.lines);
for (String line : decodeResult) {
printWriter.println(line);
}
}
printWriter.close();
}
}
public String encodeCSV(List<String> lines) {
StringBuilder sb = new StringBuilder();
for (String line : lines) {
sb.append(encode(line)).append(',');
}
sb.setLength(sb.length() - 1);
return sb.toString();
}
public String encode (String line){
boolean wrap = false;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < line.length(); i++) {
char c = line.charAt(i);
if (c == ',') {
wrap = true;
sb.append(c);
} else if (c == '\"') {
wrap = true;
sb.append('\"').append('\"');
} else {
sb.append(c);
}
}
return wrap ? "\"" + sb.toString() + "\"" : sb.toString();
}
public List<String> decodeCSV(List<String> lines) {
return decode(lines.get(0));
}
public List<String> decode(String line) {
List<String> result = new ArrayList<>();
StringBuilder sb = new StringBuilder();
boolean pendingWrap = false;
int len = line.length();
for (int i = 0; i < len; i++) {
char c = line.charAt(i);
if (c == ',') {
if (pendingWrap) {
sb.append(',');
} else {
result.add(sb.toString());
sb.setLength(0); // clear
}
} else if (c == '\"') {
if (pendingWrap && i + 1 < len && line.charAt(i + 1) == '\"') {
sb.append('\"');
i++;
} else {
pendingWrap = !pendingWrap;
}
} else {
sb.append(c);
}
}
if (sb.length() > 0) {
result.add(sb.toString());
}
return result;
}
private int getInt(String name) {
return Integer.parseInt(name.replaceAll("\\D", ""));
}
private File[] readFolder(String path) {
File folder = new File(path);
File[] files = folder.listFiles();
Arrays.sort(files, (a, b) -> getInt(a.getName()) - getInt(b.getName()));
return files;
}
private CSV.InputValues readFile(File file) {
try {
// assume the input file is always valid
Scanner sc = new Scanner(file);
String cmd = sc.nextLine().trim();
int n = -1;
if ("encode".equals(cmd)) {
n = Integer.parseInt(sc.nextLine().trim());
}
List<String> lines = new ArrayList<>();
while(sc.hasNextLine()){
lines.add(sc.nextLine());
}
return new CSV.InputValues(cmd, n, lines);
} catch (FileNotFoundException e) {
System.out.println(file.getName() + " not exist");
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment