desruisseaux commented on code in PR #610:
URL: https://github.com/apache/maven-jar-plugin/pull/610#discussion_r4115126239


##########
src/main/java/org/apache/maven/plugins/jar/ToolExecutor.java:
##########
@@ -69,6 +70,104 @@ final class ToolExecutor {
      */
     private static final String CREATED_BY = "Created-By";
 
+    /**
+     * Matches any character that is not an ASCII letter or digit.
+     * Used by {@link #cleanModuleName(String)} to mirror the JDK's {@code 
ModulePath.cleanModuleName()} algorithm.
+     */
+    private static final Pattern NON_ALPHANUM = 
Pattern.compile("[^A-Za-z0-9]");
+
+    /**
+     * Matches two or more consecutive dots.
+     * Used by {@link #cleanModuleName(String)} to collapse repeated 
separators.
+     */
+    private static final Pattern REPEATING_DOTS = Pattern.compile("\\.{2,}");
+
+    /**
+     * Sanitizes a candidate automatic module name by applying the same 
algorithm that the JDK uses
+     * to derive an automatic module name from a JAR file name
+     * (see {@code jdk.internal.module.ModulePath.cleanModuleName()}):
+     * non-alphanumeric characters (including hyphens) are replaced with 
{@code '.'}, repeated dots
+     * are collapsed to a single dot, and leading/trailing dots are stripped.
+     *
+     * @param  name the raw, potentially invalid module name
+     * @return the sanitized name, or an empty string if nothing remains after 
sanitization
+     */
+    private static String cleanModuleName(String name) {
+        name = NON_ALPHANUM.matcher(name).replaceAll(".");
+        name = REPEATING_DOTS.matcher(name).replaceAll(".");
+        int len = name.length();
+        if (len > 0 && name.charAt(0) == '.') {
+            name = name.replaceFirst("^\\.+", "");
+        }
+        len = name.length();
+        if (len > 0 && name.charAt(len - 1) == '.') {
+            name = name.replaceFirst("\\.+$", "");
+        }

Review Comment:
   I propose to replace the full method by the following code. It accepts any 
valid Java identifier, including with supplementary Unicode characters (the 
encoding of `MANIFEST.MF` files is UTF-8).
   
   ```java
   private static String cleanModuleName(final String name) {
       final int length = name.length();
       final var b = new StringBuilder(length);
       boolean wantStart = true;
       int n;
       for (int i = 0; i < length; i += n) {
           final int c = name.codePointAt(i);
           n = Character.charCount(c);
           if (wantStart) {
               if (!Character.isJavaIdentifierStart(c)) {
                   continue;
               }
               wantStart = false;
           } else {
               if (!Character.isJavaIdentifierPart(c)) {
                   wantStart = true;
                   b.append('.');
                   continue;
               }
           }
           b.appendCodePoint(c);
       }
       int end = b.length() - 1;
       if (end >= 0 && b.charAt(end) == '.') {
           b.setLength(end);
       }
       return b.toString();
   }
   ```
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to