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

Force UTF-8 for saving SGF to fix the second issue in #820 #823

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
12 changes: 9 additions & 3 deletions src/main/java/featurecat/lizzie/rules/SGFParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class SGFParser {
private static final String[] listProps =
new String[] {"LB", "CR", "SQ", "MA", "TR", "AB", "AW", "AE"};
private static final String[] markupProps = new String[] {"LB", "CR", "SQ", "MA", "TR"};
private static final String writerEncoding = "UTF-8";

public static boolean load(String filename) throws IOException {
// Clear the board
Expand Down Expand Up @@ -567,7 +568,7 @@ public static String saveToString() throws IOException {
}

public static void save(Board board, String filename) throws IOException {
try (Writer writer = new OutputStreamWriter(new FileOutputStream(filename))) {
try (Writer writer = new OutputStreamWriter(new FileOutputStream(filename), writerEncoding)) {
saveToStream(board, writer);
}
}
Expand All @@ -588,7 +589,8 @@ private static void saveToStream(Board board, Writer writer) throws IOException
if (handicap != 0) generalProps.append(String.format("HA[%s]", handicap));
generalProps.append(
String.format(
"KM[%s]PW[%s]PB[%s]DT[%s]AP[Lizzie: %s]SZ[%s]",
"CA[%s]KM[%s]PW[%s]PB[%s]DT[%s]AP[Lizzie: %s]SZ[%s]",
writerEncoding,
komi,
playerW,
playerB,
Expand Down Expand Up @@ -935,7 +937,11 @@ public static void addProperties(Map<String, String> props, String propsStr) {
*/
public static String propertiesString(Map<String, String> props) {
StringBuilder sb = new StringBuilder();
props.forEach((key, value) -> sb.append(nodeString(key, value)));
// Place "CA" before non-ASCII characters for safety.
String charsetKey = "CA";
String charsetValue = props.get(charsetKey);
if (charsetValue != null) sb.append(nodeString(charsetKey, charsetValue));
props.forEach((key, value) -> sb.append(key == charsetKey ? "" : nodeString(key, value)));
return sb.toString();
}

Expand Down