Skip to content

Commit

Permalink
Improve 2DA table parser
Browse files Browse the repository at this point in the history
Fixes issues with malformed tables (e.g. IWD2: RT_FURY.2DA or
RT_NORM.2DA) when table alignment is performed.
  • Loading branch information
Argent77 committed May 15, 2024
1 parent 057e6e0 commit 891d640
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/org/infinity/resource/text/PlainTextResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,24 +182,32 @@ public static String alignTableColumns(String text, int spaces, boolean alignPer
int maxCols = 0;
int maxTokenLength = 0;
final List<List<String>> matrix = new ArrayList<>(lines.length);
for (int i = 0; i < lines.length; i++) {
final String[] tokens = lines[i].split("\\s+");
int i = 0;
for (final String item : lines) {
final String line = item.trim();
if (line.isEmpty()) {
continue;
}

final String[] tokens = line.split("\\s+");
if (tokens.length > 0) {
matrix.add(new ArrayList<>(tokens.length));
if (matrix.size() == 3) {
matrix.get(matrix.size() - 1).add("");
i++;
final ArrayList<String> row = new ArrayList<>(tokens.length);
if (i == 3) {
row.add("");
}
for (String token : tokens) {
for (final String token : tokens) {
if (!token.isEmpty()) {
matrix.get(i).add(token);
row.add(token);
}
}
if (matrix.size() > 2) {
maxCols = Math.max(maxCols, matrix.get(matrix.size() - 1).size());
if (i > 2) {
maxCols = Math.max(maxCols, row.size());
for (String token : tokens) {
maxTokenLength = Math.max(maxTokenLength, token.length());
}
}
matrix.add(row);
}
}

Expand Down

0 comments on commit 891d640

Please sign in to comment.