summaryrefslogtreecommitdiff
path: root/base/src/main/java/bjc/utils/ioutils/RegexStringEditor.java
blob: 71f67829d9f16c47fff6678f68692265e26e85ba (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package bjc.utils.ioutils;

import java.util.function.BiFunction;
import java.util.function.UnaryOperator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import bjc.utils.data.Toggle;
import bjc.utils.data.ValueToggle;
import bjc.utils.funcdata.FunctionalList;
import bjc.utils.funcdata.IList;
import bjc.utils.functypes.ID;

/**
 * Editor methods for strings based off the command language for the Sam editor.
 *
 * @author EVE
 *
 */
public class RegexStringEditor {
	private static final UnaryOperator<String> SID = ID.id();

	/**
	 * Replace every occurrence of the pattern with the result of applying
	 * the action to the string matched by the pattern.
	 *
	 * @param input
	 *                The input string to process.
	 *
	 * @param patt
	 *                The pattern to match the string against.
	 *
	 * @param action
	 *                The action to transform matches with.
	 *
	 * @return The string, with matches replaced with the action.
	 */
	public static String onOccurances(final String input, final Pattern patt, final UnaryOperator<String> action) {
		return reduceOccurances(input, patt, SID, action);
	}

	/**
	 * Replace every occurrence between the patterns with the result of
	 * applying the action to the strings between the patterns.
	 *
	 * @param input
	 *                The input string to process.
	 *
	 * @param patt
	 *                The pattern to match the string against.
	 *
	 * @param action
	 *                The action to transform matches with.
	 *
	 * @return The string, with strings between the matches replaced with
	 *         the action.
	 */
	public static String betweenOccurances(final String input, final Pattern patt,
			final UnaryOperator<String> action) {
		return reduceOccurances(input, patt, action, SID);
	}

	/**
	 * Execute actions between and on matches of a regular expression.
	 *
	 * @param input
	 *                The input string.
	 *
	 * @param rPatt
	 *                The pattern to match against the string.
	 *
	 * @param betweenAction
	 *                The function to execute between matches of the string.
	 *
	 * @param onAction
	 *                The function to execute on matches of the string.
	 *
	 * @return The string, with both actions applied.
	 */
	public static String reduceOccurances(final String input, final Pattern rPatt,
			final UnaryOperator<String> betweenAction, final UnaryOperator<String> onAction) {
		/*
		 * Get all of the occurances.
		 */
		final IList<String> occurances = listOccurances(input, rPatt);

		/*
		 * Execute the correct action on every occurance.
		 */
		final Toggle<UnaryOperator<String>> actions = new ValueToggle<>(onAction, betweenAction);
		final BiFunction<String, StringBuilder, StringBuilder> reducer = (strang, state) -> {
			return state.append(actions.get().apply(strang));
		};

		/*
		 * Convert the list back to a string.
		 */
		return occurances.reduceAux(new StringBuilder(), reducer, StringBuilder::toString);
	}

	/**
	 * Execute actions between and on matches of a regular expression.
	 *
	 * @param input
	 *                The input string.
	 *
	 * @param rPatt
	 *                The pattern to match against the string.
	 *
	 * @param betweenAction
	 *                The function to execute between matches of the string.
	 *
	 * @param onAction
	 *                The function to execute on matches of the string.
	 *
	 * @return The string, with both actions applied.
	 */
	public static IList<String> mapOccurances(final String input, final Pattern rPatt,
			final UnaryOperator<String> betweenAction, final UnaryOperator<String> onAction) {
		/*
		 * Get all of the occurances.
		 */
		final IList<String> occurances = listOccurances(input, rPatt);

		/*
		 * Execute the correct action on every occurance.
		 */
		final Toggle<UnaryOperator<String>> actions = new ValueToggle<>(onAction, betweenAction);
		return occurances.map(strang -> actions.get().apply(strang));
	}

	/**
	 * Separate a string into match/non-match segments.
	 *
	 * @param input
	 *                The string to separate.
	 *
	 * @param rPatt
	 *                The pattern to use for separation.
	 *
	 * @return The string, as a list of match/non-match segments,
	 *         starting/ending with a non-match segment.
	 */
	public static IList<String> listOccurances(final String input, final Pattern rPatt) {
		final IList<String> res = new FunctionalList<>();

		/*
		 * Create the matcher and work buffer.
		 */
		final Matcher matcher = rPatt.matcher(input);
		StringBuffer work = new StringBuffer();

		/*
		 * For every match.
		 */
		while (matcher.find()) {
			final String match = matcher.group();

			/*
			 * Append the text until the match to the buffer.
			 */
			matcher.appendReplacement(work, "");

			res.add(work.toString());
			res.add(match);

			/*
			 * Clear the buffer.
			 */
			work = new StringBuffer();
		}

		/*
		 * Add the text after the last match to the buffer.
		 */
		matcher.appendTail(work);
		res.add(work.toString());

		return res;
	}

	/**
	 * Apply an operation to a string if it matches a regular expression.
	 *
	 * @param input
	 *                The input string.
	 *
	 * @param patt
	 *                The pattern to match against it.
	 *
	 * @param action
	 *                The action to execute if it matches.
	 *
	 * @return The string, modified by the action if the pattern matched.
	 */
	public static String ifMatches(final String input, final Pattern patt, final UnaryOperator<String> action) {
		final Matcher matcher = patt.matcher(input);

		if (matcher.matches()) {
			return action.apply(input);
		} else {
			return input;
		}
	}

	/**
	 * Apply an operation to a string if it matches a regular expression.
	 *
	 * @param input
	 *                The input string.
	 *
	 * @param patt
	 *                The pattern to match against it.
	 *
	 * @param action
	 *                The action to execute if it doesn't match.
	 *
	 * @return The string, modified by the action if the pattern didn't
	 *         match.
	 */
	public static String ifNotMatches(final String input, final Pattern patt, final UnaryOperator<String> action) {
		final Matcher matcher = patt.matcher(input);

		if (matcher.matches()) {
			return input;
		} else {
			return action.apply(input);
		}
	}
}