Mercurial > pub > ImplabNet
comparison Implab/Formats/JSON/StringTranslator.cs @ 176:0c3c69fe225b ref20160224
rewritten the text scanner
author | cin |
---|---|
date | Tue, 22 Mar 2016 18:58:40 +0300 |
parents | 419aa51b04fd |
children | c32688129f14 |
comparison
equal
deleted
inserted
replaced
175:96a89dcb4060 | 176:0c3c69fe225b |
---|---|
1 using Implab; | 1 using Implab; |
2 using Implab.Parsing; | 2 using Implab.Formats; |
3 using System; | 3 using System; |
4 using System.Collections.Generic; | 4 using System.Collections.Generic; |
5 using System.Diagnostics; | 5 using System.Diagnostics; |
6 using System.Linq; | 6 using System.Linq; |
7 using System.Text; | 7 using System.Text; |
8 using System.Threading.Tasks; | 8 using System.Threading.Tasks; |
9 | 9 |
10 namespace Implab.JSON { | 10 namespace Implab.Formats.JSON { |
11 /// <summary> | 11 /// <summary> |
12 /// Класс для преобразования экранированной строки JSON | 12 /// Класс для преобразования экранированной строки JSON |
13 /// </summary> | 13 /// </summary> |
14 public class StringTranslator : Scanner { | 14 public class StringTranslator : TextScanner<JSONGrammar.TokenType> { |
15 static readonly char[] _escMap; | 15 static readonly char[] _escMap; |
16 static readonly int[] _hexMap; | 16 static readonly int[] _hexMap; |
17 | 17 |
18 static StringTranslator() { | 18 static StringTranslator() { |
19 var chars = new char[] { 'b', 'f', 't', 'r', 'n', '\\', '/' }; | 19 var chars = new char[] { 'b', 'f', 't', 'r', 'n', '\\', '/' }; |
32 for (int i = 0; i < hexs.Length; i++) | 32 for (int i = 0; i < hexs.Length; i++) |
33 _hexMap[hexs[i]] = ints[i]; | 33 _hexMap[hexs[i]] = ints[i]; |
34 | 34 |
35 } | 35 } |
36 | 36 |
37 public StringTranslator() | 37 public StringTranslator() { |
38 : base(JSONGrammar.Instance.JsonStringDFA.States, JSONGrammar.Instance.JsonStringDFA.Alphabet.GetTranslationMap()) { | |
39 } | 38 } |
40 | 39 |
41 public string Translate(string data) { | 40 public string Translate(string data) { |
42 Safe.ArgumentNotNull(data, "data"); | 41 Safe.ArgumentNotNull(data, "data"); |
43 return Translate(data.ToCharArray()); | 42 return Translate(data.ToCharArray()); |
57 Feed(data,length); | 56 Feed(data,length); |
58 | 57 |
59 int pos = 0; | 58 int pos = 0; |
60 | 59 |
61 while (ReadTokenInternal()) { | 60 while (ReadTokenInternal()) { |
62 switch ((JSONGrammar.TokenType)TokenTags[0]) { | 61 switch ((JSONGrammar.TokenType)Tags[0]) { |
63 case JSONGrammar.TokenType.UnescapedChar: | 62 case JSONGrammar.TokenType.UnescapedChar: |
64 Array.Copy(m_buffer,m_tokenOffset,translated,pos,m_tokenLen); | 63 Array.Copy(m_buffer,m_tokenOffset,translated,pos,m_tokenLen); |
65 pos += m_tokenLen; | 64 pos += m_tokenLen; |
66 break; | 65 break; |
67 case JSONGrammar.TokenType.EscapedChar: | 66 case JSONGrammar.TokenType.EscapedChar: |