annotate Implab/Formats/JSON/StringTranslator.cs @ 178:d5c5db0335ee ref20160224

working on JSON parser
author cin
date Wed, 23 Mar 2016 19:51:45 +0300
parents 0c3c69fe225b
children c32688129f14
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
176
0c3c69fe225b rewritten the text scanner
cin
parents: 163
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>
176
0c3c69fe225b rewritten the text scanner
cin
parents: 163
diff changeset
14 public class StringTranslator : TextScanner<JSONGrammar.TokenType> {
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() {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
19 var chars = new char[] { 'b', 'f', 't', 'r', 'n', '\\', '/' };
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
20 var vals = new char[] { '\b', '\f', '\t', '\r', '\n', '\\', '/' };
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
176
0c3c69fe225b rewritten the text scanner
cin
parents: 163
diff changeset
37 public StringTranslator() {
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
38 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
39
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
40 public string Translate(string data) {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
41 Safe.ArgumentNotNull(data, "data");
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
42 return Translate(data.ToCharArray());
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
43 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
44
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
45 public string Translate(char[] data) {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
46 Safe.ArgumentNotNull(data, "data");
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
47 return Translate(data, data.Length);
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
48 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
49
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
50 public string Translate(char[] data, int length) {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
51 Safe.ArgumentNotNull(data, "data");
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
52 Safe.ArgumentInRange(length, 0, data.Length, "length");
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
53
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
54 var translated = new char[length];
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
55
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
56 Feed(data,length);
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
57
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
58 int pos = 0;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
59
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
60 while (ReadTokenInternal()) {
176
0c3c69fe225b rewritten the text scanner
cin
parents: 163
diff changeset
61 switch ((JSONGrammar.TokenType)Tags[0]) {
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
62 case JSONGrammar.TokenType.UnescapedChar:
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
63 Array.Copy(m_buffer,m_tokenOffset,translated,pos,m_tokenLen);
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
64 pos += m_tokenLen;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
65 break;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
66 case JSONGrammar.TokenType.EscapedChar:
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
67 translated[pos] = _escMap[m_buffer[m_tokenOffset + 1]];
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
68 pos++;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
69 break;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
70 case JSONGrammar.TokenType.EscapedUnicode:
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
71 translated[pos] = TranslateHexUnicode(m_buffer,m_tokenOffset + 2);
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
72 pos++;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
73 break;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
74 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
75 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
76
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
77 return new String(translated, 0, pos);
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
78 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
79
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
80 internal static char TranslateEscapedChar(char symbol) {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
81 return _escMap[symbol];
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
82 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
83
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
84 internal static char TranslateHexUnicode(char[] symbols, int offset) {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
85 Debug.Assert(symbols != null);
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
86 Debug.Assert(symbols.Length - offset >= 4);
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
87
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
88 int value = (_hexMap[symbols[offset]] << 12)
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
89 | (_hexMap[symbols[offset + 1]] << 8)
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
90 | (_hexMap[symbols[offset + 2]] << 4)
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
91 | (_hexMap[symbols[offset + 3]]);
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
92 return (char)value;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
93 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
94 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
95 }