-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Now docs are correctly escaped and unescaped in tiny v2 format
- Loading branch information
1 parent
68cf047
commit b200fad
Showing
8 changed files
with
109 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
modules/mapping-api/src/main/java/cn/maxpixel/mcdecompiler/mapping/util/TinyUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package cn.maxpixel.mcdecompiler.mapping.util; | ||
|
||
public final class TinyUtil { | ||
private TinyUtil() { | ||
throw new AssertionError("No instances"); | ||
} | ||
|
||
public static String escape(String unescaped) { | ||
StringBuilder sb = new StringBuilder(unescaped.length() + 16); | ||
int mark = 0; | ||
for (int i = 0; i < unescaped.length(); i++) { | ||
char escaped = switch (unescaped.charAt(i)) { | ||
case '\\' -> '\\'; | ||
case '\n' -> 'n'; | ||
case '\r' -> 'r'; | ||
case '\t' -> 't'; | ||
case '\0' -> '0'; | ||
default -> '\0'; | ||
}; | ||
if (escaped != 0) { | ||
if (mark < i) sb.append(unescaped, mark, i); | ||
mark = i + 1; | ||
sb.append('\\').append(escaped); | ||
} | ||
} | ||
return sb.append(unescaped, mark, unescaped.length()).toString(); | ||
} | ||
|
||
public static String unescape(String escaped) { | ||
StringBuilder sb = new StringBuilder(escaped.length()); | ||
int mark = 0; | ||
for (int i = escaped.indexOf('\\'); i >= 0; i = escaped.indexOf('\\', mark)) { | ||
char unescaped = switch (escaped.charAt(++i)) { | ||
case '\\' -> '\\'; | ||
case 'n' -> '\n'; | ||
case 'r' -> '\r'; | ||
case 't' -> '\t'; | ||
case '0' -> '\0'; | ||
default -> throw new IllegalArgumentException("Unknown escape character: \\" + escaped.charAt(i)); | ||
}; | ||
if (mark < i - 1) sb.append(escaped, mark, i - 1); | ||
mark = i + 1; | ||
sb.append(unescaped); | ||
} | ||
return sb.append(escaped, mark, escaped.length()).toString(); | ||
} | ||
} |