aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/hysky/skyblocker/utils/TextUtils.java
blob: 4c64195268d193331d22f2e62d9ef0e7de0bf033 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package de.hysky.skyblocker.utils;

import net.minecraft.text.Text;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Utility class for text operations.
 */
public final class TextUtils {
	private TextUtils() {}

	/**
	 * Finds the first occurrence of the pattern in the list of texts, using {@link Matcher#matches()} ()}.
	 *
	 * @param list the list of texts to search in
	 * @param pattern the pattern to search for
	 * @return the index of the first occurrence if found, -1 otherwise
	 */
	public static int indexOfInList(List<Text> list, Pattern pattern) {
		return indexOfInList(list, pattern, 0);
	}

	/**
	 * Finds the first occurrence of the pattern in the list of texts starting from the specified index, using {@link Matcher#matches()}}.
	 *
	 * @param list the list of texts to search in
	 * @param pattern the pattern to search for
	 * @param startIndex the index to start searching from (inclusive)
	 * @return the index of the first occurrence if found, -1 otherwise
	 */
	public static int indexOfInList(List<Text> list, Pattern pattern, int startIndex) {
		if (startIndex >= list.size()) return -1; // Start index is out of bounds, or the list is empty

		Matcher matcher = pattern.matcher(""); // Empty matcher
		for (int i = startIndex, listSize = list.size(); i < listSize; i++) {
			Text text = list.get(i);
			if (matcher.reset(text.getString()).matches()) return i;
		}

		return -1;
	}

	/**
	 * Finds the first occurrence of the pattern in the list of texts using {@link Matcher#find()}.
	 *
	 * @param list the list of texts to search in
	 * @param pattern the pattern to search for
	 * @return the matcher if found, {@code null} otherwise
	 */
	@Nullable
	public static Matcher findInList(List<Text> list, Pattern pattern) {
		return findInList(list, pattern, 0);
	}

	/**
	 * Finds the first occurrence of the pattern in the list of texts starting from the specified index using {@link Matcher#find()}.
	 *
	 * @param list the list of texts to search in
	 * @param pattern the pattern to search for
	 * @param startIndex the index to start searching from (inclusive)
	 * @return the matcher if found, {@code null} otherwise
	 */
	@Nullable
	public static Matcher findInList(List<Text> list, Pattern pattern, int startIndex) {
		if (startIndex >= list.size()) return null; // Start index is out of bounds, or the list is empty

		Matcher matcher = pattern.matcher(""); // Empty matcher
		for (int i = startIndex, listSize = list.size(); i < listSize; i++) {
			Text text = list.get(i);
			if (matcher.reset(text.getString()).find()) return matcher;
		}

		return null;
	}

	/**
	 * Finds the first occurrence of the pattern in the list of texts using {@link Matcher#matches()}.
	 *
	 * @param list the list of texts to search in
	 * @param pattern the pattern to search for
	 * @return the matcher if found, {@code null} otherwise
	 */
	@Nullable
	public static Matcher matchInList(List<Text> list, Pattern pattern) {
		return matchInList(list, pattern, 0);
	}

	/**
	 * Finds the first occurrence of the pattern in the list of texts starting from the specified index using {@link Matcher#matches()}.
	 *
	 * @param list the list of texts to search in
	 * @param pattern the pattern to search for
	 * @param startIndex the index to start searching from (inclusive)
	 * @return the matcher if found, {@code null} otherwise
	 */
	@Nullable
	public static Matcher matchInList(List<Text> list, Pattern pattern, int startIndex) {
		if (startIndex >= list.size()) return null; // Start index is out of bounds, or the list is empty

		Matcher matcher = pattern.matcher(""); // Empty matcher
		for (int i = startIndex, listSize = list.size(); i < listSize; i++) {
			Text text = list.get(i);
			if (matcher.reset(text.getString()).matches()) return matcher;
		}

		return null;
	}
}