mirror of
https://github.com/VibedByKaKi/alexandroid.git
synced 2026-12-05 20:09:49 +01:00
112 lines
4.5 KiB
Java
112 lines
4.5 KiB
Java
package org.commonmark.internal.util;
|
|
|
|
import java.nio.charset.Charset;
|
|
import java.util.Locale;
|
|
import java.util.regex.Matcher;
|
|
import java.util.regex.Pattern;
|
|
|
|
/* compiled from: r8-map-id-b9432e0f623b4a4bff4ba50a8a24c4aafad2c1e11092747b9fb5e6a22af5a940 */
|
|
/* loaded from: classes11.dex */
|
|
public class Escaping {
|
|
public static final String ENTITY = "&(?:#x[a-f0-9]{1,6}|#[0-9]{1,7}|[a-z][a-z0-9]{1,31});";
|
|
public static final String ESCAPABLE = "[!\"#$%&'()*+,./:;<=>?@\\[\\\\\\]^_`{|}~-]";
|
|
private static final Pattern BACKSLASH_OR_AMP = Pattern.compile("[\\\\&]");
|
|
private static final Pattern ENTITY_OR_ESCAPED_CHAR = Pattern.compile("\\\\[!\"#$%&'()*+,./:;<=>?@\\[\\\\\\]^_`{|}~-]|&(?:#x[a-f0-9]{1,6}|#[0-9]{1,7}|[a-z][a-z0-9]{1,31});", 2);
|
|
private static final Pattern ESCAPE_IN_URI = Pattern.compile("(%[a-fA-F0-9]{0,2}|[^:/?#@!$&'()*+,;=a-zA-Z0-9\\-._~])");
|
|
private static final char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
|
|
private static final Pattern WHITESPACE = Pattern.compile("[ \t\r\n]+");
|
|
private static final Replacer UNESCAPE_REPLACER = new Replacer() { // from class: org.commonmark.internal.util.Escaping.1
|
|
@Override // org.commonmark.internal.util.Escaping.Replacer
|
|
public void replace(String str, StringBuilder sb) {
|
|
if (str.charAt(0) == '\\') {
|
|
sb.append((CharSequence) str, 1, str.length());
|
|
} else {
|
|
sb.append(Html5Entities.entityToString(str));
|
|
}
|
|
}
|
|
};
|
|
private static final Replacer URI_REPLACER = new Replacer() { // from class: org.commonmark.internal.util.Escaping.2
|
|
@Override // org.commonmark.internal.util.Escaping.Replacer
|
|
public void replace(String str, StringBuilder sb) {
|
|
if (str.startsWith("%")) {
|
|
if (str.length() == 3) {
|
|
sb.append(str);
|
|
return;
|
|
} else {
|
|
sb.append("%25");
|
|
sb.append((CharSequence) str, 1, str.length());
|
|
return;
|
|
}
|
|
}
|
|
for (byte b : str.getBytes(Charset.forName("UTF-8"))) {
|
|
sb.append('%');
|
|
sb.append(Escaping.HEX_DIGITS[(b >> 4) & 15]);
|
|
sb.append(Escaping.HEX_DIGITS[b & 15]);
|
|
}
|
|
}
|
|
};
|
|
|
|
/* compiled from: r8-map-id-b9432e0f623b4a4bff4ba50a8a24c4aafad2c1e11092747b9fb5e6a22af5a940 */
|
|
public interface Replacer {
|
|
void replace(String str, StringBuilder sb);
|
|
}
|
|
|
|
public static String escapeHtml(String str) {
|
|
String str2;
|
|
StringBuilder sb = null;
|
|
for (int i = 0; i < str.length(); i++) {
|
|
char charAt = str.charAt(i);
|
|
if (charAt == '\"') {
|
|
str2 = """;
|
|
} else if (charAt == '&') {
|
|
str2 = "&";
|
|
} else if (charAt == '<') {
|
|
str2 = "<";
|
|
} else if (charAt != '>') {
|
|
if (sb != null) {
|
|
sb.append(charAt);
|
|
}
|
|
} else {
|
|
str2 = ">";
|
|
}
|
|
if (sb == null) {
|
|
sb = new StringBuilder();
|
|
sb.append((CharSequence) str, 0, i);
|
|
}
|
|
sb.append(str2);
|
|
}
|
|
return sb != null ? sb.toString() : str;
|
|
}
|
|
|
|
public static String normalizeLabelContent(String str) {
|
|
String trim = str.trim();
|
|
Locale locale = Locale.ROOT;
|
|
return WHITESPACE.matcher(trim.toLowerCase(locale).toUpperCase(locale)).replaceAll(" ");
|
|
}
|
|
|
|
public static String percentEncodeUrl(String str) {
|
|
return replaceAll(ESCAPE_IN_URI, str, URI_REPLACER);
|
|
}
|
|
|
|
private static String replaceAll(Pattern pattern, String str, Replacer replacer) {
|
|
Matcher matcher = pattern.matcher(str);
|
|
if (!matcher.find()) {
|
|
return str;
|
|
}
|
|
StringBuilder sb = new StringBuilder(str.length() + 16);
|
|
int i = 0;
|
|
do {
|
|
sb.append((CharSequence) str, i, matcher.start());
|
|
replacer.replace(matcher.group(), sb);
|
|
i = matcher.end();
|
|
} while (matcher.find());
|
|
if (i != str.length()) {
|
|
sb.append((CharSequence) str, i, str.length());
|
|
}
|
|
return sb.toString();
|
|
}
|
|
|
|
public static String unescapeString(String str) {
|
|
return BACKSLASH_OR_AMP.matcher(str).find() ? replaceAll(ENTITY_OR_ESCAPED_CHAR, str, UNESCAPE_REPLACER) : str;
|
|
}
|
|
}
|