From 4284bb2a6cc526d18fdccb6c8c6fb60dd475e6eb Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Thu, 14 May 2020 14:31:48 +0300 Subject: [PATCH] C: fix naming when bytecode contains method names with special characters --- .../generate/LowLevelNameProvider.java | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/core/src/main/java/org/teavm/backend/lowlevel/generate/LowLevelNameProvider.java b/core/src/main/java/org/teavm/backend/lowlevel/generate/LowLevelNameProvider.java index 2690cb652..4b0603a29 100644 --- a/core/src/main/java/org/teavm/backend/lowlevel/generate/LowLevelNameProvider.java +++ b/core/src/main/java/org/teavm/backend/lowlevel/generate/LowLevelNameProvider.java @@ -160,22 +160,23 @@ public abstract class LowLevelNameProvider implements NameProvider { private String sanitize(String name) { StringBuilder sb = new StringBuilder(); - for (int i = 0; i < name.length(); ++i) { - char c = name.charAt(i); - switch (c) { - case '>': - case '<': - case '$': - sb.append('_'); - break; - default: - sb.append(c); - break; - } + char c = name.charAt(0); + sb.append(isIdentifierStart(c) ? c : '_'); + for (int i = 1; i < name.length(); ++i) { + c = name.charAt(i); + sb.append(isIdentifierPart(c) ? c : '_'); } return sb.toString(); } + private static boolean isIdentifierStart(char c) { + return c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z' || c == '_'; + } + + private static boolean isIdentifierPart(char c) { + return isIdentifierStart(c) || c >= '0' && c <= '9'; + } + private String pickUnoccupied(String name) { return pickUnoccupied(name, occupiedTopLevelNames); }