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

Add JsonWriter #17

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions src/main/java/net/fabricmc/mappingio/MappingWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.nio.file.Path;

import net.fabricmc.mappingio.format.EnigmaWriter;
import net.fabricmc.mappingio.format.JsonWriter;
import net.fabricmc.mappingio.format.MappingFormat;
import net.fabricmc.mappingio.format.Tiny1Writer;
import net.fabricmc.mappingio.format.Tiny2Writer;
Expand All @@ -45,6 +46,7 @@ static MappingWriter create(Writer writer, MappingFormat format) throws IOExcept
switch (format) {
case TINY: return new Tiny1Writer(writer);
case TINY_2: return new Tiny2Writer(writer, false);
case JSON: return new JsonWriter(writer);
default: throw new UnsupportedOperationException("format "+format+" is not implemented");
}
}
Expand Down
81 changes: 81 additions & 0 deletions src/main/java/net/fabricmc/mappingio/format/JsonUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright (c) 2021 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.fabricmc.mappingio.format;

import java.io.IOException;
import java.io.Writer;

final class JsonUtil {
public static boolean needEscape(String s) {
for (int pos = 0, len = s.length(); pos < len; pos++) {
char c = s.charAt(pos);
if (toEscape.indexOf(c) >= 0) return true;
}

return false;
}

public static void writeEscaped(String s, Writer out) throws IOException {
final int len = s.length();
int start = 0;

for (int pos = 0; pos < len; pos++) {
char c = s.charAt(pos);
int idx = toEscape.indexOf(c);

if (idx >= 0) {
out.write(s, start, pos - start);
out.write('\\');
out.write(escaped.charAt(idx));
start = pos + 1;
}
}

out.write(s, start, len - start);
}

public static String unescape(String str) {
int pos = str.indexOf('\\');
if (pos < 0) return str;

StringBuilder ret = new StringBuilder(str.length() - 1);
int start = 0;

do {
ret.append(str, start, pos);
pos++;
int type;

if (pos >= str.length()) {
throw new RuntimeException("incomplete escape sequence at the end");
} else if ((type = escaped.indexOf(str.charAt(pos))) < 0) {
throw new RuntimeException("invalid escape character: \\"+str.charAt(pos));
} else {
ret.append(toEscape.charAt(type));
}

start = pos + 1;
} while ((pos = str.indexOf('\\', start)) >= 0);

ret.append(str, start, str.length());

return ret.toString();
}

private static final String toEscape = "\"\\\b\f\n\r\t";
private static final String escaped = "\"\\bfnrt";
}
Loading