| 1 | package net.minecraft.util;
|
| 2 |
|
| 3 | import java.util.Optional;
|
| 4 | import net.minecraft.ChatFormatting;
|
| 5 | import net.minecraft.network.chat.FormattedText;
|
| 6 | import net.minecraft.network.chat.Style;
|
| 7 |
|
| 8 | public class StringDecomposer {
|
| 9 | private static final char REPLACEMENT_CHAR = '�';
|
| 10 | private static final Optional<Object> STOP_ITERATION = Optional.of(Unit.INSTANCE);
|
| 11 |
|
| 12 | private static boolean feedChar(final Style style, final FormattedCharSink output, final int pos, final char ch) {
|
| 13 | return Character.isSurrogate(ch) ? output.accept(pos, style, 65533) : output.accept(pos, style, ch);
|
| 14 | }
|
| 15 |
|
| 16 | public static boolean iterate(final String string, final Style style, final FormattedCharSink output) {
|
| 17 | int size = string.length();
|
| 18 |
|
| 19 | for (int i = 0; i < size; i++) {
|
| 20 | char ch = string.charAt(i);
|
| 21 | if (Character.isHighSurrogate(ch)) {
|
| 22 | if (i + 1 >= size) {
|
| 23 | if (!output.accept(i, style, 65533)) {
|
| 24 | return false;
|
| 25 | }
|
| 26 | break;
|
| 27 | }
|
| 28 |
|
| 29 | char low = string.charAt(i + 1);
|
| 30 | if (Character.isLowSurrogate(low)) {
|
| 31 | if (!output.accept(i, style, Character.toCodePoint(ch, low))) {
|
| 32 | return false;
|
| 33 | }
|
| 34 |
|
| 35 | i++;
|
| 36 | } else if (!output.accept(i, style, 65533)) {
|
| 37 | return false;
|
| 38 | }
|
| 39 | } else if (!feedChar(style, output, i, ch)) {
|
| 40 | return false;
|
| 41 | }
|
| 42 | }
|
| 43 |
|
| 44 | return true;
|
| 45 | }
|
| 46 |
|
| 47 | public static boolean iterateBackwards(final String string, final Style style, final FormattedCharSink output) {
|
| 48 | int size = string.length();
|
| 49 |
|
| 50 | for (int i = size - 1; i >= 0; i--) {
|
| 51 | char ch = string.charAt(i);
|
| 52 | if (Character.isLowSurrogate(ch)) {
|
| 53 | if (i - 1 < 0) {
|
| 54 | if (!output.accept(0, style, 65533)) {
|
| 55 | return false;
|
| 56 | }
|
| 57 | break;
|
| 58 | }
|
| 59 |
|
| 60 | char high = string.charAt(i - 1);
|
| 61 | if (Character.isHighSurrogate(high)) {
|
| 62 | if (!output.accept(--i, style, Character.toCodePoint(high, ch))) {
|
| 63 | return false;
|
| 64 | }
|
| 65 | } else if (!output.accept(i, style, 65533)) {
|
| 66 | return false;
|
| 67 | }
|
| 68 | } else if (!feedChar(style, output, i, ch)) {
|
| 69 | return false;
|
| 70 | }
|
| 71 | }
|
| 72 |
|
| 73 | return true;
|
| 74 | }
|
| 75 |
|
| 76 | public static boolean iterateFormatted(final String string, final Style style, final FormattedCharSink output) {
|
| 77 | return iterateFormatted(string, 0, style, output);
|
| 78 | }
|
| 79 |
|
| 80 | public static boolean iterateFormatted(final String string, final int offset, final Style style, final FormattedCharSink output) {
|
| 81 | return iterateFormatted(string, offset, style, style, output);
|
| 82 | }
|
| 83 |
|
| 84 | public static boolean iterateFormatted(final String string, final int offset, final Style currentStyle, final Style resetStyle, final FormattedCharSink output) {
|
| 85 | int size = string.length();
|
| 86 | Style style = currentStyle;
|
| 87 |
|
| 88 | for (int i = offset; i < size; i++) {
|
| 89 | char ch = string.charAt(i);
|
| 90 | if (ch == 167) {
|
| 91 | if (i + 1 >= size) {
|
| 92 | break;
|
| 93 | }
|
| 94 |
|
| 95 | char code = string.charAt(i + 1);
|
| 96 | ChatFormatting formatting = ChatFormatting.getByCode(code);
|
| 97 | if (formatting != null) {
|
| 98 | style = formatting == ChatFormatting.RESET ? resetStyle : style.applyLegacyFormat(formatting);
|
| 99 | }
|
| 100 |
|
| 101 | i++;
|
| 102 | } else if (Character.isHighSurrogate(ch)) {
|
| 103 | if (i + 1 >= size) {
|
| 104 | if (!output.accept(i, style, 65533)) {
|
| 105 | return false;
|
| 106 | }
|
| 107 | break;
|
| 108 | }
|
| 109 |
|
| 110 | char low = string.charAt(i + 1);
|
| 111 | if (Character.isLowSurrogate(low)) {
|
| 112 | if (!output.accept(i, style, Character.toCodePoint(ch, low))) {
|
| 113 | return false;
|
| 114 | }
|
| 115 |
|
| 116 | i++;
|
| 117 | } else if (!output.accept(i, style, 65533)) {
|
| 118 | return false;
|
| 119 | }
|
| 120 | } else if (!feedChar(style, output, i, ch)) {
|
| 121 | return false;
|
| 122 | }
|
| 123 | }
|
| 124 |
|
| 125 | return true;
|
| 126 | }
|
| 127 |
|
| 128 | public static boolean iterateFormatted(final FormattedText component, final Style rootStyle, final FormattedCharSink output) {
|
| 129 | return component.visit((style, contents) -> iterateFormatted(contents, 0, style, output) ? Optional.empty() : STOP_ITERATION, rootStyle).isEmpty();
|
| 130 | }
|
| 131 |
|
| 132 | public static String filterBrokenSurrogates(final String input) {
|
| 133 | StringBuilder builder = new StringBuilder();
|
| 134 | iterate(input, Style.EMPTY, (position, style, codepoint) -> {
|
| 135 | builder.appendCodePoint(codepoint);
|
| 136 | return true;
|
| 137 | });
|
| 138 | return builder.toString();
|
| 139 | }
|
| 140 |
|
| 141 | public static String getPlainText(final FormattedText input) {
|
| 142 | StringBuilder builder = new StringBuilder();
|
| 143 | iterateFormatted(input, Style.EMPTY, (position, style, codepoint) -> {
|
| 144 | builder.appendCodePoint(codepoint);
|
| 145 | return true;
|
| 146 | });
|
| 147 | return builder.toString();
|
| 148 | }
|
| 149 | }
|
| 150 |
|