163
|
1 using Implab;
|
176
|
2 using Implab.Formats;
|
163
|
3 using System;
|
|
4 using System.Collections.Generic;
|
|
5 using System.Diagnostics;
|
|
6 using System.Linq;
|
|
7 using System.Text;
|
|
8 using System.Threading.Tasks;
|
|
9
|
176
|
10 namespace Implab.Formats.JSON {
|
163
|
11 /// <summary>
|
|
12 /// Класс для преобразования экранированной строки JSON
|
|
13 /// </summary>
|
176
|
14 public class StringTranslator : TextScanner<JSONGrammar.TokenType> {
|
163
|
15 static readonly char[] _escMap;
|
|
16 static readonly int[] _hexMap;
|
|
17
|
|
18 static StringTranslator() {
|
|
19 var chars = new char[] { 'b', 'f', 't', 'r', 'n', '\\', '/' };
|
|
20 var vals = new char[] { '\b', '\f', '\t', '\r', '\n', '\\', '/' };
|
|
21
|
|
22 _escMap = new char[chars.Max() + 1];
|
|
23
|
|
24 for (int i = 0; i < chars.Length; i++)
|
|
25 _escMap[chars[i]] = vals[i];
|
|
26
|
|
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' };
|
|
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 };
|
|
29
|
|
30 _hexMap = new int[hexs.Max() + 1];
|
|
31
|
|
32 for (int i = 0; i < hexs.Length; i++)
|
|
33 _hexMap[hexs[i]] = ints[i];
|
|
34
|
|
35 }
|
|
36
|
176
|
37 public StringTranslator() {
|
163
|
38 }
|
|
39
|
|
40 public string Translate(string data) {
|
|
41 Safe.ArgumentNotNull(data, "data");
|
|
42 return Translate(data.ToCharArray());
|
|
43 }
|
|
44
|
|
45 public string Translate(char[] data) {
|
|
46 Safe.ArgumentNotNull(data, "data");
|
|
47 return Translate(data, data.Length);
|
|
48 }
|
|
49
|
|
50 public string Translate(char[] data, int length) {
|
|
51 Safe.ArgumentNotNull(data, "data");
|
|
52 Safe.ArgumentInRange(length, 0, data.Length, "length");
|
|
53
|
|
54 var translated = new char[length];
|
|
55
|
|
56 Feed(data,length);
|
|
57
|
|
58 int pos = 0;
|
|
59
|
|
60 while (ReadTokenInternal()) {
|
176
|
61 switch ((JSONGrammar.TokenType)Tags[0]) {
|
163
|
62 case JSONGrammar.TokenType.UnescapedChar:
|
|
63 Array.Copy(m_buffer,m_tokenOffset,translated,pos,m_tokenLen);
|
|
64 pos += m_tokenLen;
|
|
65 break;
|
|
66 case JSONGrammar.TokenType.EscapedChar:
|
|
67 translated[pos] = _escMap[m_buffer[m_tokenOffset + 1]];
|
|
68 pos++;
|
|
69 break;
|
|
70 case JSONGrammar.TokenType.EscapedUnicode:
|
|
71 translated[pos] = TranslateHexUnicode(m_buffer,m_tokenOffset + 2);
|
|
72 pos++;
|
|
73 break;
|
|
74 }
|
|
75 }
|
|
76
|
|
77 return new String(translated, 0, pos);
|
|
78 }
|
|
79
|
|
80 internal static char TranslateEscapedChar(char symbol) {
|
|
81 return _escMap[symbol];
|
|
82 }
|
|
83
|
|
84 internal static char TranslateHexUnicode(char[] symbols, int offset) {
|
|
85 Debug.Assert(symbols != null);
|
|
86 Debug.Assert(symbols.Length - offset >= 4);
|
|
87
|
|
88 int value = (_hexMap[symbols[offset]] << 12)
|
|
89 | (_hexMap[symbols[offset + 1]] << 8)
|
|
90 | (_hexMap[symbols[offset + 2]] << 4)
|
|
91 | (_hexMap[symbols[offset + 3]]);
|
|
92 return (char)value;
|
|
93 }
|
|
94 }
|
|
95 }
|