static void appendEscaping(final StringBuilder builder, final String text, final char[] escapeChars, final boolean allowEscapes) {
      int startIdx = 0;
      boolean unescapedFound = false;

      for (int i = 0; i < text.length(); i++) {
        final char test = text.charAt(i);
        boolean escaped = false;
        for (final char c : escapeChars) {
          if (test == c) {
            if (!allowEscapes) {
              throw new IllegalArgumentException("Invalid escapable character '" + test + "' found at index " + i + " in string '" + text + "'");
            }
            escaped = true;
            break;
          }
        }

        if (escaped) {
          if (unescapedFound) builder.append(text, startIdx, i);
          startIdx = i + 1;
          builder.append(TokenParser.ESCAPE).append(test);
        } else {
          unescapedFound = true;
        }
      }

      if (startIdx < text.length() && unescapedFound) {
        builder.append(text, startIdx, text.length());
      }
    }
mark tag boundaries within the stack, without needing to mess with typing too much.
text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/MiniMessageSerializer.java