-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathVariableFormatter.java
More file actions
53 lines (44 loc) · 1.86 KB
/
VariableFormatter.java
File metadata and controls
53 lines (44 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package liquidjava.utils;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public final class VariableFormatter {
private static final Pattern INSTACE_VAR_PATTERN = Pattern.compile("^#(.+)_([0-9]+)$");
private static final Pattern INSTANCE_VAR_TEXT_PATTERN = Pattern.compile("#[^\\s,;:()\\[\\]{}]+_[0-9]+");
private static final char[] SUPERSCRIPT_CHARS = { '⁰', '¹', '²', '³', '⁴', '⁵', '⁶', '⁷', '⁸', '⁹' };
public static String formatVariable(String name) {
if (name == null)
return null;
Matcher matcher = INSTACE_VAR_PATTERN.matcher(name);
if (!matcher.matches())
return name;
String baseName = matcher.group(1);
String counter = matcher.group(2);
String prefix = isSpecialIdentifier(baseName) ? "#" : "";
return prefix + baseName + toSuperscript(counter);
}
public static String formatText(String text) {
if (text == null)
return null;
Matcher textMatcher = INSTANCE_VAR_TEXT_PATTERN.matcher(text);
StringBuilder sb = new StringBuilder();
while (textMatcher.find()) {
String token = textMatcher.group();
textMatcher.appendReplacement(sb, Matcher.quoteReplacement(formatVariable(token)));
}
textMatcher.appendTail(sb);
return sb.toString();
}
private static String toSuperscript(String number) {
StringBuilder sb = new StringBuilder(number.length());
for (char c : number.toCharArray()) {
int index = c - '0';
if (index < 0 || index >= SUPERSCRIPT_CHARS.length)
return number;
sb.append(SUPERSCRIPT_CHARS[index]);
}
return sb.toString();
}
private static boolean isSpecialIdentifier(String id) {
return id.equals("fresh") || id.equals("ret");
}
}