summaryrefslogtreecommitdiff
path: root/sources/scalac/util/NameTransformer.java
blob: b04b6653794406e7551aa71e6ca278b656ebecdd (plain) (blame)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*     ____ ____  ____ ____  ______                                     *\
**    / __// __ \/ __// __ \/ ____/    SOcos COmpiles Scala             **
**  __\_ \/ /_/ / /__/ /_/ /\_ \       (c) 2002, LAMP/EPFL              **
** /_____/\____/\___/\____/____/                                        **
**                                                                      **
** $Id$
\*                                                                      */

package scalac.util;

/** A name transformer for replacing operator symbols in names by predefined
 *  names of the form $opname.
 *
 *  @author     Martin Odersky, Christine Roeckl
 *  @version    1.1
 */
public class NameTransformer {

    public static String[] operatorName = new String[128];

    static {
       operatorName['~'] = "$tilde";
       operatorName['='] = "$eq";
       operatorName['<'] = "$less";
       operatorName['>'] = "$greater";
       operatorName['!'] = "$bang";
       operatorName['#'] = "$hash";
       operatorName['%'] = "$percent";
       operatorName['^'] = "$up";
       operatorName['&'] = "$amp";
       operatorName['|'] = "$bar";
       operatorName['*'] = "$times";
       operatorName['/'] = "$div";
       operatorName['+'] = "$plus";
       operatorName['-'] = "$minus";
       operatorName[':'] = "$colon";
       operatorName['\\']= "$bslash";
    }

    /** Replace operator symbols by corresponding "$op_name" in names.
     */
    public static Name encode(Name name) {
        String string = name.toString();
        StringBuffer buffer = null;
        for (int i = 0; i < string.length(); i++) {
            char c = string.charAt(i);
            if (c < 128) {
                String operator = operatorName[c];
                if (operator != null) {
                    if (buffer == null) {
                        int capacity = string.length() - 1 + operator.length();
                        buffer = new StringBuffer(capacity);
                        buffer.append(string.substring(0, i));
                    }
                    buffer.append(operator);
                    continue;
                }
            }
            if (buffer != null) buffer.append(c);
        }
        return buffer == null ? name : Name.fromString(buffer.toString());
    }

    /** Replace "$op_name" by corresponding operator symbols in names.
     */
    public static String decode(Name name) {
        return decode(name.toString());
    }
    public static String decode(String string) {
        StringBuffer buffer = null;
        for (int i = 0; i < string.length(); i++) {
            char c = string.charAt(i);
            if (c == '$') {
                int index = -1;
                int length = -1; // an operator may be a prefix of another one
                for (int j = 0; j < operatorName.length; j++) {
                    String operator = operatorName[j];
                    if (operator == null) continue;
                    if (operator.length() <= length) continue;
                    if (!string.startsWith(operator, i)) continue;
                    index = j;
                    length = operator.length();
                }
                if (length >= 0) {
                    if (buffer == null) {
                        int capacity = string.length() - length + 1;
                        buffer = new StringBuffer(capacity);
                        buffer.append(string.substring(0, i));
                    }
                    buffer.append((char)index);
                    i += length - 1;
                    continue;
                }
            }
            if (buffer != null) buffer.append(c);
        }
        return buffer == null ? string : buffer.toString();
    }

}