Skip to content

Commit e84cba4

Browse files
committed
Remove old reflection in regex for named captures
1 parent b7a01a2 commit e84cba4

File tree

1 file changed

+10
-27
lines changed
  • src/main/java/com/laytonsmith/core/functions

1 file changed

+10
-27
lines changed

src/main/java/com/laytonsmith/core/functions/Regex.java

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.laytonsmith.core.functions;
22

3-
import com.laytonsmith.PureUtilities.Common.ReflectionUtils;
43
import com.laytonsmith.annotations.api;
54
import com.laytonsmith.annotations.core;
65
import com.laytonsmith.annotations.seealso;
@@ -44,8 +43,7 @@ public static String docs() {
4443
+ "[[Regex|regular expressions]]. Note that all the functions are just passthroughs"
4544
+ " to the Java regex mechanism. If you need to set a flag on the regex, where the api calls"
4645
+ " for a pattern, instead send array('pattern', 'flags') where flags is any of i, m, or s."
47-
+ " Alternatively, using the embedded flag system that Java provides is also valid. Named captures are"
48-
+ " also supported if you are using Java 7, otherwise they are not supported.";
46+
+ " Alternatively, using the embedded flag system that Java provides is also valid.";
4947
}
5048

5149
@api
@@ -104,15 +102,8 @@ public Mixed exec(Target t, Environment env, Mixed... args) throws ConfigRuntime
104102
ret.set(i, new CString(m.group(i), t), t);
105103
}
106104
}
107-
//Named groups are only supported in Java 7, but we can
108-
//dynamically enable this feature if they have it.
109-
Set<String> namedGroups = getNamedGroups(pattern.pattern());
110-
try {
111-
for(String key : namedGroups) {
112-
ret.set(key, (String) ReflectionUtils.invokeMethod(Matcher.class, m, "group", new Class[]{String.class}, new Object[]{key}), t);
113-
}
114-
} catch (ReflectionUtils.ReflectionException ex) {
115-
throw new CREFormatException("Named captures are only supported with Java 7.", t);
105+
for(String key : getNamedGroups(pattern.pattern())) {
106+
ret.set(key, m.group(key), t);
116107
}
117108
}
118109
return ret;
@@ -143,10 +134,9 @@ public Set<OptimizationOption> optimizationOptions() {
143134
public ExampleScript[] examples() throws ConfigCompileException {
144135
return new ExampleScript[]{
145136
new ExampleScript("Basic usage", "reg_match('(\\\\d)(\\\\d)(\\\\d)', 'abc123')"),
146-
//Java 7 can't be assumed to be working on the system running the doc gen, so we'll hardcode these.
147-
new ExampleScript("Named captures (Only works if your system is running Java 7)",
137+
new ExampleScript("Named captures ",
148138
"reg_match('abc(?<foo>\\\\d+)(xyz)', 'abc123xyz')", "{0: abc123xyz, 1: 123, 2: xyz, foo: 123}"),
149-
new ExampleScript("Named captures with backreferences (Only works if your system is running Java 7)",
139+
new ExampleScript("Named captures with backreferences",
150140
"reg_match('abc(?<foo>\\\\d+)def\\\\k<foo>', 'abc123def123')['foo']", "123")
151141
};
152142
}
@@ -206,14 +196,8 @@ public Mixed exec(Target t, Environment env, Mixed... args) throws ConfigRuntime
206196
for(int i = 1; i <= m.groupCount(); i++) {
207197
ret.set(i, new CString(m.group(i), t), t);
208198
}
209-
//Named groups are only supported in Java 7, but we can
210-
//dynamically enable this feature if they have it.
211-
try {
212-
for(String key : namedGroups) {
213-
ret.set(key, (String) ReflectionUtils.invokeMethod(Matcher.class, m, "group", new Class[]{String.class}, new Object[]{key}), t);
214-
}
215-
} catch (ReflectionUtils.ReflectionException e) {
216-
throw new CREFormatException("Named captures are only supported with Java 7.", t);
199+
for(String key : namedGroups) {
200+
ret.set(key, m.group(key), t);
217201
}
218202
fret.push(ret, t);
219203
}
@@ -245,10 +229,9 @@ public Set<OptimizationOption> optimizationOptions() {
245229
public ExampleScript[] examples() throws ConfigCompileException {
246230
return new ExampleScript[]{
247231
new ExampleScript("Basic usage", "reg_match_all('(\\\\d{3})', 'abc123456')"),
248-
//Same thing here, can't guarantee we're running Java 7 when these are generated.
249-
new ExampleScript("Named captures (Only works if your system is running Java 7)",
232+
new ExampleScript("Named captures",
250233
"reg_match_all('abc(?<foo>\\\\d+)(xyz)', 'abc123xyz')[0]['foo']", "123"),
251-
new ExampleScript("Named captures with backreferences (Only works if your system is running Java 7)",
234+
new ExampleScript("Named captures with backreferences",
252235
"reg_match_all('abc(?<foo>\\\\d+)def\\\\k<foo>', 'abc123def123')[0]['foo']", "123")
253236
};
254237
}
@@ -354,7 +337,7 @@ public ExampleScript[] examples() throws ConfigCompileException {
354337
return new ExampleScript[]{
355338
new ExampleScript("Basic usage", "reg_replace('\\\\d', 'Z', '123abc')"),
356339
new ExampleScript("Using backreferences", "reg_replace('abc(\\\\d+)', '$1', 'abc123')"),
357-
new ExampleScript("Using backreferences with named captures (Only works if your system is running Java 7)",
340+
new ExampleScript("Using backreferences with named captures",
358341
"reg_replace('abc(?<foo>\\\\d+)', '${foo}', 'abc123')", "123")
359342
};
360343
}

0 commit comments

Comments
 (0)