annotate Implab/JSON/StringTranslator.cs @ 161:2a8466f0cb8a v2

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