StringDecomposer.java

net.minecraft.util.StringDecomposer

信息

  • 全限定名:net.minecraft.util.StringDecomposer
  • 类型:public class
  • 包:net.minecraft.util
  • 源码路径:src/main/java/net/minecraft/util/StringDecomposer.java
  • 起始行号:L8
  • 职责:

    TODO

字段/常量

  • REPLACEMENT_CHAR

    • 类型: char
    • 修饰符: private static final
    • 源码定位: L9
    • 说明:

      TODO

  • STOP_ITERATION

    • 类型: Optional<Object>
    • 修饰符: private static final
    • 源码定位: L10
    • 说明:

      TODO

内部类/嵌套类型

构造器

方法

下面的方法块按源码顺序生成。

private static boolean feedChar(Style style, FormattedCharSink output, int pos, char ch) @ L12

  • 方法名:feedChar
  • 源码定位:L12
  • 返回类型:boolean
  • 修饰符:private static

参数:

  • style: Style
  • output: FormattedCharSink
  • pos: int
  • ch: char

说明:

TODO

public static boolean iterate(String string, Style style, FormattedCharSink output) @ L16

  • 方法名:iterate
  • 源码定位:L16
  • 返回类型:boolean
  • 修饰符:public static

参数:

  • string: String
  • style: Style
  • output: FormattedCharSink

说明:

TODO

public static boolean iterateBackwards(String string, Style style, FormattedCharSink output) @ L47

  • 方法名:iterateBackwards
  • 源码定位:L47
  • 返回类型:boolean
  • 修饰符:public static

参数:

  • string: String
  • style: Style
  • output: FormattedCharSink

说明:

TODO

public static boolean iterateFormatted(String string, Style style, FormattedCharSink output) @ L76

  • 方法名:iterateFormatted
  • 源码定位:L76
  • 返回类型:boolean
  • 修饰符:public static

参数:

  • string: String
  • style: Style
  • output: FormattedCharSink

说明:

TODO

public static boolean iterateFormatted(String string, int offset, Style style, FormattedCharSink output) @ L80

  • 方法名:iterateFormatted
  • 源码定位:L80
  • 返回类型:boolean
  • 修饰符:public static

参数:

  • string: String
  • offset: int
  • style: Style
  • output: FormattedCharSink

说明:

TODO

public static boolean iterateFormatted(String string, int offset, Style currentStyle, Style resetStyle, FormattedCharSink output) @ L84

  • 方法名:iterateFormatted
  • 源码定位:L84
  • 返回类型:boolean
  • 修饰符:public static

参数:

  • string: String
  • offset: int
  • currentStyle: Style
  • resetStyle: Style
  • output: FormattedCharSink

说明:

TODO

public static boolean iterateFormatted(FormattedText component, Style rootStyle, FormattedCharSink output) @ L128

  • 方法名:iterateFormatted
  • 源码定位:L128
  • 返回类型:boolean
  • 修饰符:public static

参数:

  • component: FormattedText
  • rootStyle: Style
  • output: FormattedCharSink

说明:

TODO

public static String filterBrokenSurrogates(String input) @ L132

  • 方法名:filterBrokenSurrogates
  • 源码定位:L132
  • 返回类型:String
  • 修饰符:public static

参数:

  • input: String

说明:

TODO

public static String getPlainText(FormattedText input) @ L141

  • 方法名:getPlainText
  • 源码定位:L141
  • 返回类型:String
  • 修饰符:public static

参数:

  • input: FormattedText

说明:

TODO

代码

public class StringDecomposer {
    private static final char REPLACEMENT_CHAR = '\ufffd';
    private static final Optional<Object> STOP_ITERATION = Optional.of(Unit.INSTANCE);
 
    private static boolean feedChar(Style style, FormattedCharSink output, int pos, char ch) {
        return Character.isSurrogate(ch) ? output.accept(pos, style, 65533) : output.accept(pos, style, ch);
    }
 
    public static boolean iterate(String string, Style style, FormattedCharSink output) {
        int size = string.length();
 
        for (int i = 0; i < size; i++) {
            char ch = string.charAt(i);
            if (Character.isHighSurrogate(ch)) {
                if (i + 1 >= size) {
                    if (!output.accept(i, style, 65533)) {
                        return false;
                    }
                    break;
                }
 
                char low = string.charAt(i + 1);
                if (Character.isLowSurrogate(low)) {
                    if (!output.accept(i, style, Character.toCodePoint(ch, low))) {
                        return false;
                    }
 
                    i++;
                } else if (!output.accept(i, style, 65533)) {
                    return false;
                }
            } else if (!feedChar(style, output, i, ch)) {
                return false;
            }
        }
 
        return true;
    }
 
    public static boolean iterateBackwards(String string, Style style, FormattedCharSink output) {
        int size = string.length();
 
        for (int i = size - 1; i >= 0; i--) {
            char ch = string.charAt(i);
            if (Character.isLowSurrogate(ch)) {
                if (i - 1 < 0) {
                    if (!output.accept(0, style, 65533)) {
                        return false;
                    }
                    break;
                }
 
                char high = string.charAt(i - 1);
                if (Character.isHighSurrogate(high)) {
                    if (!output.accept(--i, style, Character.toCodePoint(high, ch))) {
                        return false;
                    }
                } else if (!output.accept(i, style, 65533)) {
                    return false;
                }
            } else if (!feedChar(style, output, i, ch)) {
                return false;
            }
        }
 
        return true;
    }
 
    public static boolean iterateFormatted(String string, Style style, FormattedCharSink output) {
        return iterateFormatted(string, 0, style, output);
    }
 
    public static boolean iterateFormatted(String string, int offset, Style style, FormattedCharSink output) {
        return iterateFormatted(string, offset, style, style, output);
    }
 
    public static boolean iterateFormatted(String string, int offset, Style currentStyle, Style resetStyle, FormattedCharSink output) {
        int size = string.length();
        Style style = currentStyle;
 
        for (int i = offset; i < size; i++) {
            char ch = string.charAt(i);
            if (ch == 167) {
                if (i + 1 >= size) {
                    break;
                }
 
                char code = string.charAt(i + 1);
                ChatFormatting formatting = ChatFormatting.getByCode(code);
                if (formatting != null) {
                    style = formatting == ChatFormatting.RESET ? resetStyle : style.applyLegacyFormat(formatting);
                }
 
                i++;
            } else if (Character.isHighSurrogate(ch)) {
                if (i + 1 >= size) {
                    if (!output.accept(i, style, 65533)) {
                        return false;
                    }
                    break;
                }
 
                char low = string.charAt(i + 1);
                if (Character.isLowSurrogate(low)) {
                    if (!output.accept(i, style, Character.toCodePoint(ch, low))) {
                        return false;
                    }
 
                    i++;
                } else if (!output.accept(i, style, 65533)) {
                    return false;
                }
            } else if (!feedChar(style, output, i, ch)) {
                return false;
            }
        }
 
        return true;
    }
 
    public static boolean iterateFormatted(FormattedText component, Style rootStyle, FormattedCharSink output) {
        return component.visit((style, contents) -> iterateFormatted(contents, 0, style, output) ? Optional.empty() : STOP_ITERATION, rootStyle).isEmpty();
    }
 
    public static String filterBrokenSurrogates(String input) {
        StringBuilder builder = new StringBuilder();
        iterate(input, Style.EMPTY, (position, style, codepoint) -> {
            builder.appendCodePoint(codepoint);
            return true;
        });
        return builder.toString();
    }
 
    public static String getPlainText(FormattedText input) {
        StringBuilder builder = new StringBuilder();
        iterateFormatted(input, Style.EMPTY, (position, style, codepoint) -> {
            builder.appendCodePoint(codepoint);
            return true;
        });
        return builder.toString();
    }
}

引用的其他类