IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
UnicodeUtil.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*/
24using System;
25
26namespace IKVM.Runtime
27{
28
29 static class UnicodeUtil
30 {
31
32 // We use part of the Supplementary Private Use Area-B to encode
33 // invalid surrogates. If we encounter either of these two
34 // markers, we always encode the surrogate (single or pair)
35 private const char HighSurrogatePrefix = '\uDBFF';
36 private const char LowSurrogatePrefix = '\uDBFE';
37
38 // Identifiers in ECMA CLI metadata and strings in custom attribute blobs are encoded
39 // using UTF-8 and don't allow partial surrogates, so we have to "complete" them to
40 // produce valid Unicode and reverse the process when we read back the names.
41 internal static string EscapeInvalidSurrogates(string str)
42 {
43 if (str != null)
44 {
45 for (int i = 0; i < str.Length; i++)
46 {
47 char c = str[i];
48 if (Char.IsLowSurrogate(c))
49 {
50 str = str.Substring(0, i) + LowSurrogatePrefix + c + str.Substring(i + 1);
51 i++;
52 }
53 else if (Char.IsHighSurrogate(c))
54 {
55 i++;
56 // always escape the markers
57 if (c == HighSurrogatePrefix || c == LowSurrogatePrefix || i == str.Length || !Char.IsLowSurrogate(str[i]))
58 {
59 str = str.Substring(0, i - 1) + HighSurrogatePrefix + (char)(c + 0x400) + str.Substring(i);
60 }
61 }
62 }
63 }
64 return str;
65 }
66
67 internal static string UnescapeInvalidSurrogates(string str)
68 {
69 if (str != null)
70 {
71 for (int i = 0; i < str.Length; i++)
72 {
73 switch (str[i])
74 {
75 case HighSurrogatePrefix:
76 str = str.Substring(0, i) + (char)(str[i + 1] - 0x400) + str.Substring(i + 2);
77 break;
78 case LowSurrogatePrefix:
79 str = str.Substring(0, i) + str[i + 1] + str.Substring(i + 2);
80 break;
81 }
82 }
83 }
84 return str;
85 }
86
87 internal static string[] EscapeInvalidSurrogates(string[] str)
88 {
89 if (str != null)
90 {
91 for (int i = 0; i < str.Length; i++)
92 {
93 str[i] = EscapeInvalidSurrogates(str[i]);
94 }
95 }
96 return str;
97 }
98
99 internal static string[] UnescapeInvalidSurrogates(string[] str)
100 {
101 if (str != null)
102 {
103 for (int i = 0; i < str.Length; i++)
104 {
105 str[i] = UnescapeInvalidSurrogates(str[i]);
106 }
107 }
108 return str;
109 }
110 }
111
112}