From c90176cf8a08c8f9ae0fd5e337c3ff97b3c47051 Mon Sep 17 00:00:00 2001 From: Ben Fry Date: Tue, 31 Jan 2017 04:55:03 -0500 Subject: [PATCH] fixing up StringDict(TableRow) and Table.trim() now removes extra rows and columns --- core/src/processing/data/StringDict.java | 7 +++- core/src/processing/data/Table.java | 50 ++++++++++++++++++++++++ core/todo.txt | 13 +++--- 3 files changed, 62 insertions(+), 8 deletions(-) diff --git a/core/src/processing/data/StringDict.java b/core/src/processing/data/StringDict.java index 1ea488359..a5d83fd09 100644 --- a/core/src/processing/data/StringDict.java +++ b/core/src/processing/data/StringDict.java @@ -110,9 +110,12 @@ public class StringDict { /** * Create a dictionary that maps between column titles and cell entries - * in a TableRow. + * in a TableRow. If two columns have the same name, the later column's + * values will override the earlier values. */ public StringDict(TableRow row) { + this(row.getColumnCount()); + String[] titles = row.getColumnTitles(); if (titles == null) { titles = new StringList(IntList.fromRange(row.getColumnCount())).array(); @@ -120,6 +123,8 @@ public class StringDict { for (int col = 0; col < row.getColumnCount(); col++) { set(titles[col], row.getString(col)); } + // remove unused and overwritten entries + crop(); } diff --git a/core/src/processing/data/Table.java b/core/src/processing/data/Table.java index 8f6ce05d0..61817d516 100644 --- a/core/src/processing/data/Table.java +++ b/core/src/processing/data/Table.java @@ -4059,8 +4059,58 @@ public class Table { for (int col = 0; col < getColumnCount(); col++) { trim(col); } + // remove empty columns + int lastColumn = getColumnCount() - 1; + //while (isEmptyColumn(lastColumn) && lastColumn >= 0) { + while (isEmptyArray(getStringColumn(lastColumn)) && lastColumn >= 0) { + lastColumn--; + } + setColumnCount(lastColumn + 1); + + // remove empty rows (starting from the end) + int lastRow = lastRowIndex(); + //while (isEmptyRow(lastRow) && lastRow >= 0) { + while (isEmptyArray(getStringRow(lastRow)) && lastRow >= 0) { + lastRow--; + } + setRowCount(lastRow + 1); } + + protected boolean isEmptyArray(String[] contents) { + for (String entry : contents) { + if (entry != null && entry.length() > 0) { + return false; + } + } + return true; + } + + + /* + protected boolean isEmptyColumn(int column) { + String[] contents = getStringColumn(column); + for (String entry : contents) { + if (entry != null && entry.length() > 0) { + return false; + } + } + return true; + } + + + protected boolean isEmptyRow(int row) { + String[] contents = getStringRow(row); + for (String entry : contents) { + if (entry != null && entry.length() > 0) { + return false; + } + } + return true; + } + */ + + /** * @param column ID number of the column to trim */ diff --git a/core/todo.txt b/core/todo.txt index e7552fe0e..fb384dbc2 100644 --- a/core/todo.txt +++ b/core/todo.txt @@ -3,14 +3,13 @@ X return null for PApplet.trim(null) X StringDict(TableRow) constructor X allow lone double quotes in the midst of csv strings X make trim() work on column titles as well -_ add trimRows() and trimColumns() -_ would you ever use one w/o the other? -_ should trim() just handle it? +o add trimRows() and trimColumns() +o would you ever use one w/o the other? +X just make this part of trim() X consume Unicode BOM (0xFEFF) in createReader() and Table parser - - -_ no prompt shows with selectInput() on 10.11 and 10.12 -_ https://github.com/processing/processing/issues/4758 +o no prompt shows with selectInput() on 10.11 and 10.12 +X https://github.com/processing/processing/issues/4758 +X can't fix, seems embedded in the Java implementation _ TRIANGLE_STRIP not working correctly with createShape() and default renderer _ https://github.com/processing/processing/issues/4678