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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
// LibMatrix: File sourced from https://github.com/dotnet/runtime/pull/87147/files under the MIT license.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Text;
using System.Text.Encodings.Web;
namespace LibMatrix.Extensions;
internal sealed class UnicodeJsonEncoder : JavaScriptEncoder
{
internal static readonly UnicodeJsonEncoder Singleton = new UnicodeJsonEncoder();
private readonly bool _preferHexEscape;
private readonly bool _preferUppercase;
public UnicodeJsonEncoder()
: this(preferHexEscape: false, preferUppercase: false)
{
}
public UnicodeJsonEncoder(bool preferHexEscape, bool preferUppercase)
{
_preferHexEscape = preferHexEscape;
_preferUppercase = preferUppercase;
}
public override int MaxOutputCharactersPerInputCharacter => 6; // "\uXXXX" for a single char ("\uXXXX\uYYYY" [12 chars] for supplementary scalar value)
public override unsafe int FindFirstCharacterToEncode(char* text, int textLength)
{
for (int index = 0; index < textLength; ++index)
{
char value = text[index];
if (NeedsEncoding(value))
{
return index;
}
}
return -1;
}
public override unsafe bool TryEncodeUnicodeScalar(int unicodeScalar, char* buffer, int bufferLength, out int numberOfCharactersWritten)
{
bool encode = WillEncode(unicodeScalar);
if (!encode)
{
Span<char> span = new Span<char>(buffer, bufferLength);
int spanWritten;
bool succeeded = new Rune(unicodeScalar).TryEncodeToUtf16(span, out spanWritten);
numberOfCharactersWritten = spanWritten;
return succeeded;
}
if (!_preferHexEscape && unicodeScalar <= char.MaxValue && HasTwoCharacterEscape((char)unicodeScalar))
{
if (bufferLength < 2)
{
numberOfCharactersWritten = 0;
return false;
}
buffer[0] = '\\';
buffer[1] = GetTwoCharacterEscapeSuffix((char)unicodeScalar);
numberOfCharactersWritten = 2;
return true;
}
else
{
if (bufferLength < 6)
{
numberOfCharactersWritten = 0;
return false;
}
buffer[0] = '\\';
buffer[1] = 'u';
buffer[2] = '0';
buffer[3] = '0';
buffer[4] = ToHexDigit((unicodeScalar & 0xf0) >> 4, _preferUppercase);
buffer[5] = ToHexDigit(unicodeScalar & 0xf, _preferUppercase);
numberOfCharactersWritten = 6;
return true;
}
}
public override bool WillEncode(int unicodeScalar)
{
if (unicodeScalar > char.MaxValue)
{
return false;
}
return NeedsEncoding((char)unicodeScalar);
}
// https://datatracker.ietf.org/doc/html/rfc8259#section-7
private static bool NeedsEncoding(char value)
{
if (value == '"' || value == '\\')
{
return true;
}
return value <= '\u001f';
}
private static bool HasTwoCharacterEscape(char value)
{
// RFC 8259, Section 7, "char = " BNF
switch (value)
{
case '"':
case '\\':
case '/':
case '\b':
case '\f':
case '\n':
case '\r':
case '\t':
return true;
default:
return false;
}
}
private static char GetTwoCharacterEscapeSuffix(char value)
{
// RFC 8259, Section 7, "char = " BNF
switch (value)
{
case '"':
return '"';
case '\\':
return '\\';
case '/':
return '/';
case '\b':
return 'b';
case '\f':
return 'f';
case '\n':
return 'n';
case '\r':
return 'r';
case '\t':
return 't';
default:
throw new ArgumentOutOfRangeException(nameof(value));
}
}
private static char ToHexDigit(int value, bool uppercase)
{
if (value > 0xf)
{
throw new ArgumentOutOfRangeException(nameof(value));
}
if (value < 10)
{
return (char)(value + '0');
}
else
{
return (char)(value - 0xa + (uppercase ? 'A' : 'a'));
}
}
}
|