annotate Implab/Formats/Json/StringTranslator.cs @ 259:7d52dc684bbd v3

PollingComponent: implemented correct stopping
author cin
date Fri, 13 Apr 2018 03:57:39 +0300
parents 3e26338eb977
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
1 using Implab;
176
0c3c69fe225b rewritten the text scanner
cin
parents: 163
diff changeset
2 using Implab.Formats;
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
3 using System;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
4 using System.Collections.Generic;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
5 using System.Diagnostics;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
6 using System.Linq;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
7 using System.Text;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
8 using System.Threading.Tasks;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
9
228
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents: 226
diff changeset
10 namespace Implab.Formats.Json {
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
11 /// <summary>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
12 /// Класс для преобразования экранированной строки JSON
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
13 /// </summary>
180
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 176
diff changeset
14 static class StringTranslator {
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
15 static readonly char[] _escMap;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
16 static readonly int[] _hexMap;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
17
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
18 static StringTranslator() {
226
9428ea36838e fixed JSON parser error when parsing escaped double quote symbol
cin
parents: 180
diff changeset
19 var chars = new char[] { 'b', 'f', 't', 'r', 'n', '\\', '/', '"' };
9428ea36838e fixed JSON parser error when parsing escaped double quote symbol
cin
parents: 180
diff changeset
20 var vals = new char[] { '\b', '\f', '\t', '\r', '\n', '\\', '/', '"' };
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
21
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
22 _escMap = new char[chars.Max() + 1];
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
23
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
24 for (int i = 0; i < chars.Length; i++)
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
25 _escMap[chars[i]] = vals[i];
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
26
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
27 var hexs = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'A', 'B', 'C', 'D', 'E', 'F' };
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
28 var ints = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 10, 11, 12, 13, 14, 15 };
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
29
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
30 _hexMap = new int[hexs.Max() + 1];
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
31
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
32 for (int i = 0; i < hexs.Length; i++)
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
33 _hexMap[hexs[i]] = ints[i];
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
34
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
35 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
36
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
37 internal static char TranslateEscapedChar(char symbol) {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
38 return _escMap[symbol];
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
39 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
40
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
41 internal static char TranslateHexUnicode(char[] symbols, int offset) {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
42 Debug.Assert(symbols != null);
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
43 Debug.Assert(symbols.Length - offset >= 4);
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
44
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
45 int value = (_hexMap[symbols[offset]] << 12)
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
46 | (_hexMap[symbols[offset + 1]] << 8)
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
47 | (_hexMap[symbols[offset + 2]] << 4)
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
48 | (_hexMap[symbols[offset + 3]]);
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
49 return (char)value;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
50 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
51 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
52 }