aboutsummaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorscria1000 <91804886+scria1000@users.noreply.github.com>2021-12-04 07:41:23 +0000
committerscria1000 <91804886+scria1000@users.noreply.github.com>2021-12-04 07:41:23 +0000
commit1f8d9e6a7d2a903b644f7ec0067f9ae9874b1219 (patch)
tree5397b22359fc3c0d65197e0dd2a3b0a3d1b3cf57 /app
parentc9e7b113798049df72414ed6ad01f9fe1d2081a7 (diff)
downloadinfinity-for-reddit-1f8d9e6a7d2a903b644f7ec0067f9ae9874b1219.tar
infinity-for-reddit-1f8d9e6a7d2a903b644f7ec0067f9ae9874b1219.tar.gz
infinity-for-reddit-1f8d9e6a7d2a903b644f7ec0067f9ae9874b1219.tar.bz2
infinity-for-reddit-1f8d9e6a7d2a903b644f7ec0067f9ae9874b1219.tar.lz
infinity-for-reddit-1f8d9e6a7d2a903b644f7ec0067f9ae9874b1219.tar.xz
infinity-for-reddit-1f8d9e6a7d2a903b644f7ec0067f9ae9874b1219.tar.zst
infinity-for-reddit-1f8d9e6a7d2a903b644f7ec0067f9ae9874b1219.zip
Statically compile frequently used Regex pattern
Diffstat (limited to '')
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/utils/Utils.java45
1 files changed, 30 insertions, 15 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/Utils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/Utils.java
index 79a294be..41bb5e98 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/Utils.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/Utils.java
@@ -49,7 +49,7 @@ import ml.docilealligator.infinityforreddit.SortType;
import ml.docilealligator.infinityforreddit.UploadedImage;
import retrofit2.Retrofit;
-public class Utils {
+public final class Utils {
public static final int NETWORK_TYPE_OTHER = -1;
public static final int NETWORK_TYPE_WIFI = 0;
public static final int NETWORK_TYPE_CELLULAR = 1;
@@ -60,21 +60,36 @@ public class Utils {
private static final long MONTH_MILLIS = 30 * DAY_MILLIS;
private static final long YEAR_MILLIS = 12 * MONTH_MILLIS;
+ public static final Pattern[] REGEX_PATTERN = {
+ Pattern.compile("((?<=[\\s])|^)/[rRuU]/[\\w-]+/{0,1}"),
+ Pattern.compile("((?<=[\\s])|^)[rRuU]/[\\w-]+/{0,1}"),
+ Pattern.compile("\\^{2,}"),
+ Pattern.compile("(^|^ *|\\n *)#(?!($|\\s|#))"),
+ Pattern.compile("(^|^ *|\\n *)##(?!($|\\s|#))"),
+ Pattern.compile("(^|^ *|\\n *)###(?!($|\\s|#))"),
+ Pattern.compile("(^|^ *|\\n *)####(?!($|\\s|#))"),
+ Pattern.compile("(^|^ *|\\n *)#####(?!($|\\s|#))"),
+ Pattern.compile("(^|^ *|\\n *)######(?!($|\\s|#))"),
+ Pattern.compile("!\\[gif]\\(giphy\\|\\w+\\)"),
+ Pattern.compile("!\\[gif]\\(giphy\\|\\w+\\|downsized\\)"),
+ Pattern.compile("!\\[gif]\\(emote\\|\\w+\\|\\w+\\)"),
+ Pattern.compile("!\\[img]\\(emote\\|\\w+\\|\\w+\\)")
+ };
+
public static String modifyMarkdown(String markdown) {
- StringBuilder regexed = new StringBuilder(markdown
- .replaceAll("((?<=[\\s])|^)/[rRuU]/[\\w-]+/{0,1}", "[$0](https://www.reddit.com$0)")
- .replaceAll("((?<=[\\s])|^)[rRuU]/[\\w-]+/{0,1}", "[$0](https://www.reddit.com/$0)")
- .replaceAll("\\^{2,}", "^")
- .replaceAll("(^|^ *|\\n *)#(?!($|\\s|#))", "$0 ")
- .replaceAll("(^|^ *|\\n *)##(?!($|\\s|#))", "$0 ")
- .replaceAll("(^|^ *|\\n *)###(?!($|\\s|#))", "$0 ")
- .replaceAll("(^|^ *|\\n *)####(?!($|\\s|#))", "$0 ")
- .replaceAll("(^|^ *|\\n *)#####(?!($|\\s|#))", "$0 ")
- .replaceAll("(^|^ *|\\n *)######(?!($|\\s|#))", "$0 "));
+ String regexed = REGEX_PATTERN[0].matcher(markdown).replaceAll("[$0](https://www.reddit.com$0)");
+ regexed = REGEX_PATTERN[1].matcher(regexed).replaceAll("[$0](https://www.reddit.com/$0)");
+ regexed = REGEX_PATTERN[2].matcher(regexed).replaceAll("^");
+ regexed = REGEX_PATTERN[3].matcher(regexed).replaceAll("$0");
+ regexed = REGEX_PATTERN[4].matcher(regexed).replaceAll("$0");
+ regexed = REGEX_PATTERN[5].matcher(regexed).replaceAll("$0");
+ regexed = REGEX_PATTERN[6].matcher(regexed).replaceAll("$0");
+ regexed = REGEX_PATTERN[7].matcher(regexed).replaceAll("$0");
+ regexed = REGEX_PATTERN[8].matcher(regexed).replaceAll("$0");
//return fixSuperScript(regexed);
// We don't want to fix super scripts here because we need the original markdown later for editing posts
- return regexed.toString();
+ return regexed;
}
public static String fixSuperScript(String regexedMarkdown) {
@@ -121,21 +136,21 @@ public class Utils {
public static String parseInlineGifInComments(String markdown) {
StringBuilder markdownStringBuilder = new StringBuilder(markdown);
- Pattern inlineGifPattern = Pattern.compile("!\\[gif]\\(giphy\\|\\w+\\)");
+ Pattern inlineGifPattern = REGEX_PATTERN[9];
Matcher matcher = inlineGifPattern.matcher(markdownStringBuilder);
while (matcher.find()) {
markdownStringBuilder.replace(matcher.start(), matcher.end(), "[gif](https://i.giphy.com/media/" + markdownStringBuilder.substring(matcher.start() + "![gif](giphy|".length(), matcher.end() - 1) + "/giphy.mp4)");
matcher = inlineGifPattern.matcher(markdownStringBuilder);
}
- Pattern inlineGifPattern2 = Pattern.compile("!\\[gif]\\(giphy\\|\\w+\\|downsized\\)");
+ Pattern inlineGifPattern2 = REGEX_PATTERN[10];
Matcher matcher2 = inlineGifPattern2.matcher(markdownStringBuilder);
while (matcher2.find()) {
markdownStringBuilder.replace(matcher2.start(), matcher2.end(), "[gif](https://i.giphy.com/media/" + markdownStringBuilder.substring(matcher2.start() + "![gif](giphy|".length(), matcher2.end() - "|downsized\\)".length() + 1) + "/giphy.mp4)");
matcher2 = inlineGifPattern2.matcher(markdownStringBuilder);
}
- Pattern inlineGifPattern3 = Pattern.compile("!\\[gif]\\(emote\\|\\w+\\|\\w+\\)");
+ Pattern inlineGifPattern3 = REGEX_PATTERN[11];
Matcher matcher3 = inlineGifPattern3.matcher(markdownStringBuilder);
while (matcher3.find()) {
markdownStringBuilder.replace(matcher3.start(), matcher3.end(),