From 2c6068b36ac352a9364e68e7ca91ea67bb5c4ce0 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Sat, 22 Jun 2019 23:56:32 +0300 Subject: [PATCH] JS: reduce the probability of colliding local variable name with a keyword --- .../javascript/rendering/RenderingUtil.java | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/core/src/main/java/org/teavm/backend/javascript/rendering/RenderingUtil.java b/core/src/main/java/org/teavm/backend/javascript/rendering/RenderingUtil.java index 5e6e7c68a..667ba3a97 100644 --- a/core/src/main/java/org/teavm/backend/javascript/rendering/RenderingUtil.java +++ b/core/src/main/java/org/teavm/backend/javascript/rendering/RenderingUtil.java @@ -142,14 +142,17 @@ public final class RenderingUtil { } public static String indexToId(int index, String startChars) { - StringBuilder sb = new StringBuilder(); - sb.append(startChars.charAt(index % startChars.length())); - index /= startChars.length(); - while (index > 0) { - sb.append(VARIABLE_PART_CHARS.charAt(index % VARIABLE_PART_CHARS.length())); - index /= VARIABLE_PART_CHARS.length(); + if (index >= startChars.length()) { + index -= startChars.length() - VARIABLE_PART_CHARS.length(); + StringBuilder sb = new StringBuilder(); + while (index >= startChars.length()) { + sb.append(VARIABLE_PART_CHARS.charAt(index % VARIABLE_PART_CHARS.length())); + index /= VARIABLE_PART_CHARS.length(); + } + return sb.append(startChars.charAt(index % startChars.length())).reverse().toString(); + } else { + return String.valueOf(startChars.charAt(index)); } - return sb.toString(); } public static String indexToId(int index) {