Skip to content

Commit 751c5e8

Browse files
committed
ported core and data updates from Java mode
1 parent 394fa89 commit 751c5e8

File tree

3 files changed

+111
-2
lines changed

3 files changed

+111
-2
lines changed

core/src/processing/core/PApplet.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4525,7 +4525,20 @@ static public BufferedReader createReader(File file) {
45254525
static public BufferedReader createReader(InputStream input) {
45264526
InputStreamReader isr =
45274527
new InputStreamReader(input, StandardCharsets.UTF_8);
4528-
return new BufferedReader(isr);
4528+
4529+
BufferedReader reader = new BufferedReader(isr);
4530+
// consume the Unicode BOM (byte order marker) if present
4531+
try {
4532+
reader.mark(1);
4533+
int c = reader.read();
4534+
// if not the BOM, back up to the beginning again
4535+
if (c != '\uFEFF') {
4536+
reader.reset();
4537+
}
4538+
} catch (IOException e) {
4539+
e.printStackTrace();
4540+
}
4541+
return reader;
45294542
}
45304543

45314544

core/src/processing/data/StringDict.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,26 @@ public StringDict(String[][] pairs) {
130130
}
131131

132132

133+
/**
134+
* Create a dictionary that maps between column titles and cell entries
135+
* in a TableRow. If two columns have the same name, the later column's
136+
* values will override the earlier values.
137+
*/
138+
public StringDict(TableRow row) {
139+
this(row.getColumnCount());
140+
141+
String[] titles = row.getColumnTitles();
142+
if (titles == null) {
143+
titles = new StringList(IntList.fromRange(row.getColumnCount())).array();
144+
}
145+
for (int col = 0; col < row.getColumnCount(); col++) {
146+
set(titles[col], row.getString(col));
147+
}
148+
// remove unused and overwritten entries
149+
crop();
150+
}
151+
152+
133153
/**
134154
* @webref stringdict:method
135155
* @brief Returns the number of key/value pairs

core/src/processing/data/Table.java

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,15 @@ protected void parse(InputStream input, String options) throws IOException {
379379
} else {
380380
InputStreamReader isr = new InputStreamReader(input, encoding);
381381
BufferedReader reader = new BufferedReader(isr);
382+
383+
// strip out the Unicode BOM, if present
384+
reader.mark(1);
385+
int c = reader.read();
386+
// if not the BOM, back up to the beginning again
387+
if (c != '\uFEFF') {
388+
reader.reset();
389+
}
390+
382391
/*
383392
if (awfulCSV) {
384393
parseAwfulCSV(reader, header);
@@ -672,6 +681,12 @@ protected boolean ingest() {
672681
addPiece(start, i, hasEscapedQuotes);
673682
start = i+2;
674683
return true;
684+
685+
} else {
686+
// This is a lone-wolf quote, occasionally seen in exports.
687+
// It's a single quote in the middle of some other text,
688+
// and not escaped properly. Pray for the best!
689+
i++;
675690
}
676691

677692
} else { // not a quoted line
@@ -1791,7 +1806,7 @@ public void addColumn(String title) {
17911806

17921807

17931808
/**
1794-
* @param type the type to be used for the new column: INT, LONG, FLOAT, DOUBLE, STRING, or CATEGORY
1809+
* @param type the type to be used for the new column: INT, LONG, FLOAT, DOUBLE, or STRING
17951810
*/
17961811
public void addColumn(String title, int type) {
17971812
insertColumn(columns.length, title, type);
@@ -4034,17 +4049,78 @@ public void removeTokens(String tokens, String columnName) {
40344049

40354050
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
40364051

4052+
40374053
/**
40384054
* @webref table:method
40394055
* @brief Trims whitespace from values
40404056
* @see Table#removeTokens(String)
40414057
*/
40424058
public void trim() {
4059+
columnTitles = PApplet.trim(columnTitles);
40434060
for (int col = 0; col < getColumnCount(); col++) {
40444061
trim(col);
40454062
}
4063+
// remove empty columns
4064+
int lastColumn = getColumnCount() - 1;
4065+
//while (isEmptyColumn(lastColumn) && lastColumn >= 0) {
4066+
while (isEmptyArray(getStringColumn(lastColumn)) && lastColumn >= 0) {
4067+
lastColumn--;
4068+
}
4069+
setColumnCount(lastColumn + 1);
4070+
4071+
// trim() works from both sides
4072+
while (getColumnCount() > 0 && isEmptyArray(getStringColumn(0))) {
4073+
removeColumn(0);
4074+
}
4075+
4076+
// remove empty rows (starting from the end)
4077+
int lastRow = lastRowIndex();
4078+
//while (isEmptyRow(lastRow) && lastRow >= 0) {
4079+
while (isEmptyArray(getStringRow(lastRow)) && lastRow >= 0) {
4080+
lastRow--;
4081+
}
4082+
setRowCount(lastRow + 1);
4083+
4084+
while (getRowCount() > 0 && isEmptyArray(getStringRow(0))) {
4085+
removeRow(0);
4086+
}
40464087
}
40474088

4089+
4090+
protected boolean isEmptyArray(String[] contents) {
4091+
for (String entry : contents) {
4092+
if (entry != null && entry.length() > 0) {
4093+
return false;
4094+
}
4095+
}
4096+
return true;
4097+
}
4098+
4099+
4100+
/*
4101+
protected boolean isEmptyColumn(int column) {
4102+
String[] contents = getStringColumn(column);
4103+
for (String entry : contents) {
4104+
if (entry != null && entry.length() > 0) {
4105+
return false;
4106+
}
4107+
}
4108+
return true;
4109+
}
4110+
4111+
4112+
protected boolean isEmptyRow(int row) {
4113+
String[] contents = getStringRow(row);
4114+
for (String entry : contents) {
4115+
if (entry != null && entry.length() > 0) {
4116+
return false;
4117+
}
4118+
}
4119+
return true;
4120+
}
4121+
*/
4122+
4123+
40484124
/**
40494125
* @param column ID number of the column to trim
40504126
*/

0 commit comments

Comments
 (0)