mirror of
https://github.com/HMCL-dev/HMCL.git
synced 2026-10-17 05:31:22 +08:00
fix(csv): 修复 CSV 转义引号和换行的格式错误 (#6931)
This commit is contained in:
@@ -83,22 +83,14 @@ public final class CSVTable {
|
||||
out.append(txt);
|
||||
else {
|
||||
out.append('"');
|
||||
// Escape quotes by doubling them as required by RFC 4180;
|
||||
// line breaks are kept literally inside quoted fields.
|
||||
for (int i = 0; i < txt.length(); i++) {
|
||||
char c = txt.charAt(i);
|
||||
switch (c) {
|
||||
case '"':
|
||||
out.append("\\\"");
|
||||
break;
|
||||
case '\r':
|
||||
out.append("\\r");
|
||||
break;
|
||||
case '\n':
|
||||
out.append("\\n");
|
||||
break;
|
||||
default:
|
||||
out.append(c);
|
||||
break;
|
||||
}
|
||||
if (c == '"')
|
||||
out.append("\"\"");
|
||||
else
|
||||
out.append(c);
|
||||
}
|
||||
out.append('"');
|
||||
}
|
||||
|
||||
@@ -62,7 +62,7 @@ public final class CSVTableTest {
|
||||
table.set(1, 2, "c");
|
||||
table.set(3, 2, "d\"e\n");
|
||||
|
||||
assertEquals("a,b,,c\n,,,\n\"a,b\",c,,\"d\\\"e\\n\"\n", table.toString());
|
||||
assertEquals("a,b,,c\n,,,\n\"a,b\",c,,\"d\"\"e\n\"\n", table.toString());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user