annotate Implab/Formats/JSON/StringTranslator.cs @ 166:b84cdbe82e7f ref20160224

sync
author cin
date Mon, 29 Feb 2016 18:41:01 +0300
parents 419aa51b04fd
children 0c3c69fe225b
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;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
2 using Implab.Parsing;
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
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
10 namespace Implab.JSON {
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>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
14 public class StringTranslator : Scanner {
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
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
37 public StringTranslator()
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
38 : base(JSONGrammar.Instance.JsonStringDFA.States, JSONGrammar.Instance.JsonStringDFA.Alphabet.GetTranslationMap()) {
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 public string Translate(string data) {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
42 Safe.ArgumentNotNull(data, "data");
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
43 return Translate(data.ToCharArray());
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
44 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
45
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
46 public string Translate(char[] data) {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
47 Safe.ArgumentNotNull(data, "data");
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
48 return Translate(data, data.Length);
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
49 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
50
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
51 public string Translate(char[] data, int length) {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
52 Safe.ArgumentNotNull(data, "data");
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
53 Safe.ArgumentInRange(length, 0, data.Length, "length");
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
54
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
55 var translated = new char[length];
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
56
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
57 Feed(data,length);
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
58
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
59 int pos = 0;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
60
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
61 while (ReadTokenInternal()) {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
62 switch ((JSONGrammar.TokenType)TokenTags[0]) {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
63 case JSONGrammar.TokenType.UnescapedChar:
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
64 Array.Copy(m_buffer,m_tokenOffset,translated,pos,m_tokenLen);
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
65 pos += m_tokenLen;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
66 break;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
67 case JSONGrammar.TokenType.EscapedChar:
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
68 translated[pos] = _escMap[m_buffer[m_tokenOffset + 1]];
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
69 pos++;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
70 break;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
71 case JSONGrammar.TokenType.EscapedUnicode:
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
72 translated[pos] = TranslateHexUnicode(m_buffer,m_tokenOffset + 2);
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
73 pos++;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
74 break;
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
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
78 return new String(translated, 0, pos);
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
79 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
80
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
81 internal static char TranslateEscapedChar(char symbol) {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
82 return _escMap[symbol];
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
83 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
84
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
85 internal static char TranslateHexUnicode(char[] symbols, int offset) {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
86 Debug.Assert(symbols != null);
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
87 Debug.Assert(symbols.Length - offset >= 4);
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
88
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
89 int value = (_hexMap[symbols[offset]] << 12)
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
90 | (_hexMap[symbols[offset + 1]] << 8)
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
91 | (_hexMap[symbols[offset + 2]] << 4)
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
92 | (_hexMap[symbols[offset + 3]]);
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
93 return (char)value;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
94 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
95 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
96 }