Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
IriHelper.cs
Go to the documentation of this file.
2using System.Text;
3
4namespace System;
5
6internal static class IriHelper
7{
8 internal static bool CheckIriUnicodeRange(char unicode, bool isQuery)
9 {
10 if (!IsInInclusiveRange(unicode, 160u, 55295u) && !IsInInclusiveRange(unicode, 63744u, 64975u) && !IsInInclusiveRange(unicode, 65008u, 65519u))
11 {
12 if (isQuery)
13 {
14 return IsInInclusiveRange(unicode, 57344u, 63743u);
15 }
16 return false;
17 }
18 return true;
19 }
20
21 internal static bool CheckIriUnicodeRange(char highSurr, char lowSurr, out bool isSurrogatePair, bool isQuery)
22 {
23 if (Rune.TryCreate(highSurr, lowSurr, out var result))
24 {
25 isSurrogatePair = true;
26 if ((result.Value & 0xFFFF) < 65534 && (uint)(result.Value - 917504) >= 4096u)
27 {
28 if (!isQuery)
29 {
30 return result.Value < 983040;
31 }
32 return true;
33 }
34 return false;
35 }
36 isSurrogatePair = false;
37 return false;
38 }
39
40 internal static bool CheckIriUnicodeRange(uint value, bool isQuery)
41 {
42 if (value <= 65535)
43 {
44 if (!IsInInclusiveRange(value, 160u, 55295u) && !IsInInclusiveRange(value, 63744u, 64975u) && !IsInInclusiveRange(value, 65008u, 65519u))
45 {
46 if (isQuery)
47 {
48 return IsInInclusiveRange(value, 57344u, 63743u);
49 }
50 return false;
51 }
52 return true;
53 }
54 if ((value & 0xFFFF) < 65534 && !IsInInclusiveRange(value, 917504u, 921599u))
55 {
56 if (!isQuery)
57 {
58 return value < 983040;
59 }
60 return true;
61 }
62 return false;
63 }
64
65 [MethodImpl(MethodImplOptions.AggressiveInlining)]
66 private static bool IsInInclusiveRange(uint value, uint min, uint max)
67 {
68 return value - min <= max - min;
69 }
70
71 internal static bool CheckIsReserved(char ch, UriComponents component)
72 {
73 if ((UriComponents.AbsoluteUri & component) == 0)
74 {
75 if (component == (UriComponents)0)
76 {
77 return UriHelper.IsGenDelim(ch);
78 }
79 return false;
80 }
81 return ";/?:@&=+$,#[]!'()*".Contains(ch);
82 }
83
84 internal unsafe static string EscapeUnescapeIri(char* pInput, int start, int end, UriComponents component)
85 {
86 int num = end - start;
87 System.Text.ValueStringBuilder valueStringBuilder;
88 if (num <= 512)
89 {
90 Span<char> initialBuffer = stackalloc char[512];
91 valueStringBuilder = new System.Text.ValueStringBuilder(initialBuffer);
92 }
93 else
94 {
95 valueStringBuilder = new System.Text.ValueStringBuilder(num);
96 }
97 System.Text.ValueStringBuilder to = valueStringBuilder;
98 Span<byte> destination = stackalloc byte[4];
99 for (int i = start; i < end; i++)
100 {
101 char c = pInput[i];
102 if (c == '%')
103 {
104 if (end - i > 2)
105 {
106 c = UriHelper.DecodeHexChars(pInput[i + 1], pInput[i + 2]);
107 if (c == '\uffff' || c == '%' || CheckIsReserved(c, component) || UriHelper.IsNotSafeForUnescape(c))
108 {
109 to.Append(pInput[i++]);
110 to.Append(pInput[i++]);
111 to.Append(pInput[i]);
112 }
113 else if (c <= '\u007f')
114 {
115 to.Append(c);
116 i += 2;
117 }
118 else
119 {
120 int num2 = PercentEncodingHelper.UnescapePercentEncodedUTF8Sequence(pInput + i, end - i, ref to, component == UriComponents.Query, iriParsing: true);
121 i += num2 - 1;
122 }
123 }
124 else
125 {
126 to.Append(pInput[i]);
127 }
128 }
129 else if (c > '\u007f')
130 {
131 bool isSurrogatePair = false;
132 char c2 = '\0';
133 bool flag;
134 if (char.IsHighSurrogate(c) && i + 1 < end)
135 {
136 c2 = pInput[i + 1];
137 flag = CheckIriUnicodeRange(c, c2, out isSurrogatePair, component == UriComponents.Query);
138 }
139 else
140 {
141 flag = CheckIriUnicodeRange(c, component == UriComponents.Query);
142 }
143 if (flag)
144 {
145 to.Append(c);
146 if (isSurrogatePair)
147 {
148 to.Append(c2);
149 }
150 }
151 else
152 {
153 Rune result;
154 if (isSurrogatePair)
155 {
156 result = new Rune(c, c2);
157 }
158 else if (!Rune.TryCreate(c, out result))
159 {
160 result = Rune.ReplacementChar;
161 }
163 Span<byte> span2 = span;
164 for (int j = 0; j < span2.Length; j++)
165 {
166 byte b = span2[j];
167 UriHelper.EscapeAsciiChar(b, ref to);
168 }
169 }
170 if (isSurrogatePair)
171 {
172 i++;
173 }
174 }
175 else
176 {
177 to.Append(pInput[i]);
178 }
179 }
180 return to.ToString();
181 }
182}
static bool IsInInclusiveRange(uint value, uint min, uint max)
Definition IriHelper.cs:66
static bool CheckIriUnicodeRange(char highSurr, char lowSurr, out bool isSurrogatePair, bool isQuery)
Definition IriHelper.cs:21
static unsafe string EscapeUnescapeIri(char *pInput, int start, int end, UriComponents component)
Definition IriHelper.cs:84
static bool CheckIsReserved(char ch, UriComponents component)
Definition IriHelper.cs:71
static bool CheckIriUnicodeRange(uint value, bool isQuery)
Definition IriHelper.cs:40
static bool CheckIriUnicodeRange(char unicode, bool isQuery)
Definition IriHelper.cs:8
static unsafe int UnescapePercentEncodedUTF8Sequence(char *input, int length, ref System.Text.ValueStringBuilder dest, bool isQuery, bool iriParsing)
static bool IsNotSafeForUnescape(char ch)
Definition UriHelper.cs:426
static void EscapeAsciiChar(byte b, ref System.Text.ValueStringBuilder to)
Definition UriHelper.cs:409
static bool IsGenDelim(char ch)
Definition UriHelper.cs:435
static char DecodeHexChars(int first, int second)
Definition UriHelper.cs:415
int Length
Definition Span.cs:70
static Rune ReplacementChar
Definition Rune.cs:53
static bool TryCreate(char ch, out Rune result)
Definition Rune.cs:511
int EncodeToUtf8(Span< byte > destination)
Definition Rune.cs:383