Skip to content

Commit

Permalink
Apply different mangle rules for filenames and methods
Browse files Browse the repository at this point in the history
  • Loading branch information
pomadchin committed Jul 27, 2021
1 parent 21e8f94 commit 1e0b62e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private ClassName(String moduleName, String className) {
this.moduleName = moduleName;
this.className = className;
this.simpleName = className.substring(className.lastIndexOf('.') + 1);
this.mangledName = mangleName(className);
this.mangledName = mangleFileName(className);
}

@Override
Expand Down
40 changes: 29 additions & 11 deletions plugin/src/main/java/com/github/sbt/jni/javah/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,41 @@ public void close() throws IOException {
}
});

public static String mangleChar(char ch) {
if (ch == '.') {
return "_";
} else if (ch == '_') {
return "_1";
} else if (ch == ';') {
return "_2";
} else if (ch == '[') {
return "_3";
} else if ((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && (ch <= 'Z'))) {
return String.valueOf(ch);
} else {
return String.format("_0%04x", (int) ch);
}
}

public static String mangleName(String name) {
StringBuilder builder = new StringBuilder(name.length() * 2);
int len = name.length();
for (int i = 0; i < len; i++) {
char ch = name.charAt(i);
if (ch == '.') {
builder.append('_');
} else if (ch == '_') {
builder.append("_1");
} else if (ch == ';') {
builder.append("_2");
} else if (ch == '[') {
builder.append("_3");
} else if ((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && (ch <= 'Z'))) {
builder.append(ch);
builder.append(mangleChar(ch));
}
return builder.toString();
}

public static String mangleFileName(String name) {
StringBuilder builder = new StringBuilder(name.length() * 2);
int len = name.length();
for (int i = 0; i < len; i++) {
char ch = name.charAt(i);
if (ch == '$') {
builder.append("__");
} else {
builder.append(String.format("_0%04x", (int) ch));
builder.append(mangleChar(ch));
}
}
return builder.toString();
Expand Down

0 comments on commit 1e0b62e

Please sign in to comment.