Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not remove wrapping quotes #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 2 additions & 21 deletions src/main/java/pipetableformatter/ColumnSplitter.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public class ColumnSplitter implements Iterable<String>, Iterator<String> {
private int startIndex = 0;
private int length = 0;
private boolean insideQuoted = false;
private boolean quoted = false;
private Character delimiter;
private int prevStartIndex;
private int columnIndex;
Expand Down Expand Up @@ -62,7 +61,7 @@ public String next() {
prevStartIndex = startIndex;
startIndex = endIndex + 1;
columnIndex++;
return openQuotes(value);
return value;
}

private boolean hasMoreChars(int endIndex) {
Expand All @@ -75,28 +74,10 @@ private boolean notDelimiter(int index) {

private void detectQuoted(int index) {
if (line.charAt(index) == '"') {
quoted = true;
insideQuoted = !insideQuoted;
}
}

private String openQuotes(String value) {
if (quoted) {
quoted = false;
return replaceTwoQuotesWithOne(removeOpenAndCloseQuotes(value));
} else {
return value;
}
}

private String replaceTwoQuotesWithOne(String value) {
return value.replaceAll("\"\"", "\"");
}

private String removeOpenAndCloseQuotes(String value) {
return value.replaceAll("(^ *\")|(\" *$)", "");
}

public int currentColumnStartIndex() {
return prevStartIndex + leadingSpaces;
}
Expand Down Expand Up @@ -128,7 +109,7 @@ public int getLeadingSpaces() {
return leadingSpaces;
}

public String getIndentetion() {
public String getIndentation() {
if(leadingSpaces > 0) {
return String.format("%" + leadingSpaces + "s", " ");
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/pipetableformatter/PipeTableParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private PipeTable.Row splitForColumns(String line, boolean rowWithCaret, String

PipeTable.Row row = new PipeTable.Row(cells, endOfLine);
row.setCommented(tableRow.isCommented());
row.setIndentation(tableRow.getIndentetion());
row.setIndentation(tableRow.getIndentation());
row.setLeadingPipe(tableRow.hasLeadingPipe());
row.setTrailingPipe(tableRow.hasTrailingPipe());

Expand Down
6 changes: 3 additions & 3 deletions src/test/java/pipetableformatter/PipeTableParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,21 @@ public void splitsLineForColumnsByComma() {
@Test
public void ignoresCommasInsideDoubleQuotes() {
PipeTable pipeTable = new PipeTableParser(" \"col1val1,col1val2\",column2").parse();
assertThat(pipeTable.getValue(0, 0), is("col1val1,col1val2"));
assertThat(pipeTable.getValue(0, 0), is("\"col1val1,col1val2\""));
assertThat(pipeTable.getValue(0, 1), is("column2"));
}

@Test
public void ignoresPipeInsideDoubleQuotes() {
PipeTable pipeTable = new PipeTableParser(" |\"col1val1|col1val2\"|column2|").parse();
assertThat(pipeTable.getValue(0, 0), is("col1val1|col1val2"));
assertThat(pipeTable.getValue(0, 0), is("\"col1val1|col1val2\""));
assertThat(pipeTable.getValue(0, 1), is("column2"));
}

@Test
public void treatsTwoDoubleQuotesInsideQuotesAsOne() {
PipeTable pipeTable = new PipeTableParser(" |\"val1\"\"val2\"|column2|").parse();
assertThat(pipeTable.getValue(0, 0), is("val1\"val2"));
assertThat(pipeTable.getValue(0, 0), is("\"val1\"\"val2\""));
}

@Test
Expand Down