Skip to content

Commit

Permalink
update to Java 21
Browse files Browse the repository at this point in the history
  • Loading branch information
fglock committed Oct 19, 2024
1 parent 30861ca commit 559f0a1
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ file.
- The project uses the `maven-shade-plugin` for Maven to create a shaded JAR.
- The project uses the `com.github.johnrengelman.shadow` plugin for Gradle to create a shaded JAR.
- Both Maven and Gradle configurations are set to use Java 11.
- Both Maven and Gradle configurations are set to use Java 21.
## Running the jar file
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ version = '1.0-SNAPSHOT'

java {
toolchain {
languageVersion = JavaLanguageVersion.of(11)
languageVersion = JavaLanguageVersion.of(21)
}
}

Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
<url>http://maven.apache.org</url>

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
</properties>

<dependencies>
Expand Down
36 changes: 7 additions & 29 deletions src/main/java/org/perlonjava/runtime/HashSpecialVariable.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package org.perlonjava.runtime;

import java.util.AbstractMap;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import static org.perlonjava.runtime.RuntimeScalarCache.scalarUndef;

Expand All @@ -28,36 +26,16 @@ public class HashSpecialVariable extends AbstractMap<String, RuntimeScalar> {
*/
public HashSpecialVariable(Matcher matcher) {
this.matcher = matcher;
this.namedGroups = extractNamedGroups(matcher.pattern());
}

/**
* Extracts named groups and their indices from the given pattern.
*
* @param pattern the regex pattern
* @return a map of named group names to their indices
*/
private Map<String, Integer> extractNamedGroups(Pattern pattern) {
Map<String, Integer> namedGroups = new HashMap<>();
String regex = pattern.toString();
Matcher groupMatcher = Pattern.compile("\\(\\?<([a-zA-Z][a-zA-Z0-9]*)>").matcher(regex);
int index = 1; // Group indices start at 1

while (groupMatcher.find()) {
String groupName = groupMatcher.group(1);
namedGroups.put(groupName, index++);
}

return namedGroups;
this.namedGroups = matcher.pattern().namedGroups(); // Use Java 20's built-in namedGroups support
}

@Override
public Set<Entry<String, RuntimeScalar>> entrySet() {
Set<Entry<String, RuntimeScalar>> entries = new HashSet<>();
for (String name : namedGroups.keySet()) {
int groupIndex = namedGroups.get(name);
if (groupIndex != -1 && matcher.group(name) != null) {
entries.add(new SimpleEntry<>(name, new RuntimeScalar(matcher.group(name))));
String matchedValue = matcher.group(name); // Use Matcher.group(String name)
if (matchedValue != null) {
entries.add(new SimpleEntry<>(name, new RuntimeScalar(matchedValue)));
}
}
return entries;
Expand All @@ -67,9 +45,9 @@ public Set<Entry<String, RuntimeScalar>> entrySet() {
public RuntimeScalar get(Object key) {
if (key instanceof String) {
String name = (String) key;
int groupIndex = namedGroups.getOrDefault(name, -1);
if (groupIndex != -1 && matcher.group(groupIndex) != null) {
return new RuntimeScalar(matcher.group(groupIndex));
String matchedValue = matcher.group(name); // Use Matcher.group(String name)
if (matchedValue != null) {
return new RuntimeScalar(matchedValue);
}
}
return scalarUndef;
Expand Down

0 comments on commit 559f0a1

Please sign in to comment.