IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
TypeNameUtil.cs
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2015 Jeroen Frijters
3
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
7
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
11
12 1. The origin of this software must not be misrepresented; you must not
13 claim that you wrote the original software. If you use this software
14 in a product, an acknowledgment in the product documentation would be
15 appreciated but is not required.
16 2. Altered source versions must be plainly marked as such, and must not be
17 misrepresented as being the original software.
18 3. This notice may not be removed or altered from any source distribution.
19
20 Jeroen Frijters
21 jeroen@frijters.net
22
23*/
24
25using System.Text;
26using System;
27
28namespace IKVM.Runtime
29{
30
31 static class TypeNameUtil
32 {
33
34 // note that MangleNestedTypeName() assumes that there are less than 16 special characters
35 private const string specialCharactersString = "\\+,[]*&\u0000";
36 internal const string ProxiesContainer = "__<Proxies>";
37
38 internal static string ReplaceIllegalCharacters(string name)
39 {
40 name = UnicodeUtil.EscapeInvalidSurrogates(name);
41 // only the NUL character is illegal in CLR type names, so we replace it with a space
42 return name.Replace('\u0000', ' ');
43 }
44
45 internal static string Unescape(string name)
46 {
47 int pos = name.IndexOf('\\');
48 if (pos == -1)
49 return name;
50
51 var sb = new ValueStringBuilder(name.Length);
52 sb.Append(name.AsSpan(0, pos));
53
54 for (int i = pos; i < name.Length; i++)
55 {
56 var c = name[i];
57 if (c == '\\')
58 c = name[++i];
59
60 sb.Append(c);
61 }
62
63 return sb.ToString();
64 }
65
66 internal static string MangleNestedTypeName(string name)
67 {
68 var sb = new ValueStringBuilder();
69
70 foreach (char c in name)
71 {
72 var index = specialCharactersString.IndexOf(c);
73 if (c == '.')
74 {
75 sb.Append("_");
76 }
77 else if (c == '_')
78 {
79 sb.Append("^-");
80 }
81 else if (index == -1)
82 {
83 sb.Append(c);
84 if (c == '^')
85 sb.Append(c);
86 }
87 else
88 {
89 sb.Append('^');
90 sb.Append(string.Format("{0:X1}", index));
91 }
92 }
93
94 return sb.ToString();
95 }
96
97 internal static string UnmangleNestedTypeName(string name)
98 {
99 var sb = new ValueStringBuilder(name.Length);
100 for (int i = 0; i < name.Length; i++)
101 {
102 var c = name[i];
103 var index = specialCharactersString.IndexOf(c);
104 if (c == '_')
105 {
106 sb.Append('.');
107 }
108 else if (c == '^')
109 {
110 c = name[++i];
111 if (c == '-')
112 sb.Append('_');
113 else if (c == '^')
114 sb.Append('^');
115 else
116 sb.Append(specialCharactersString[c - '0']);
117 }
118 else
119 {
120 sb.Append(c);
121 }
122 }
123
124 return sb.ToString();
125 }
126
127 internal static string GetProxyNestedName(RuntimeJavaType[] interfaces)
128 {
129 var sb = new ValueStringBuilder();
130
131 foreach (var tw in interfaces)
132 {
133 sb.Append(tw.Name.Length.ToString());
134 sb.Append('|');
135 sb.Append(tw.Name);
136 }
137
138 return TypeNameUtil.MangleNestedTypeName(sb.ToString());
139 }
140
141 internal static string GetProxyName(RuntimeJavaType[] interfaces)
142 {
143 return ProxiesContainer + "+" + GetProxyNestedName(interfaces);
144 }
145
146 }
147
148}