comparison Implab/Formats/Json/JsonScanner.cs @ 236:302ca905c19e v2

JsonReader optimizations
author cin
date Tue, 21 Nov 2017 14:57:58 +0300
parents 3e26338eb977
children
comparison
equal deleted inserted replaced
235:b49969a7043c 236:302ca905c19e
8 namespace Implab.Formats.Json { 8 namespace Implab.Formats.Json {
9 /// <summary> 9 /// <summary>
10 /// Сканнер (лексер), разбивающий поток символов на токены JSON. 10 /// Сканнер (лексер), разбивающий поток символов на токены JSON.
11 /// </summary> 11 /// </summary>
12 public abstract class JsonScanner : Disposable { 12 public abstract class JsonScanner : Disposable {
13 readonly InputScanner<JsonGrammar.TokenType> m_jsonContext = JsonGrammar.CreateJsonExpressionScanner(); 13 readonly FastInputScanner<JsonGrammar.TokenType> m_jsonContext = JsonGrammar.CreateJsonExpressionScanner();
14 readonly InputScanner<JsonGrammar.TokenType> m_stringContext = JsonGrammar.CreateStringExpressionScanner(); 14 readonly FastInputScanner<JsonGrammar.TokenType> m_stringContext = JsonGrammar.CreateStringExpressionScanner();
15 15
16 readonly char[] m_unescapeBuf = new char[4]; 16 readonly char[] m_unescapeBuf = new char[4];
17 readonly char[] m_buffer; 17 readonly char[] m_buffer;
18 int m_length; 18 int m_length;
19 int m_pos; 19 int m_pos;
23 m_buffer = buffer; 23 m_buffer = buffer;
24 m_pos = pos; 24 m_pos = pos;
25 m_length = length; 25 m_length = length;
26 } 26 }
27 27
28 bool ReadChunk(InputScanner<JsonGrammar.TokenType> scanner, out JsonGrammar.TokenType tokenType) { 28 bool ReadChunk(FastInputScanner<JsonGrammar.TokenType> scanner, out JsonGrammar.TokenType tokenType) {
29 scanner.ResetState(); 29 scanner.ResetState();
30 30
31 while(scanner.Scan(m_buffer, m_pos, m_length)) { 31 while(scanner.Scan(m_buffer, m_pos, m_length)) {
32 // scanner requests new data 32 // scanner requests new data
33 33
69 69
70 m_pos = scannerPos; 70 m_pos = scannerPos;
71 return true; 71 return true;
72 } 72 }
73 73
74 bool ReadStringChunk(InputScanner<JsonGrammar.TokenType> scanner, out JsonGrammar.TokenType tokenType) { 74 bool ReadStringChunk(FastInputScanner<JsonGrammar.TokenType> scanner, out JsonGrammar.TokenType tokenType) {
75 scanner.ResetState(); 75 scanner.ResetState();
76 76
77 while (scanner.Scan(m_buffer, m_pos, m_length)) { 77 while (scanner.Scan(m_buffer, m_pos, m_length)) {
78 // scanner requests new data 78 // scanner requests new data
79 79
105 } 105 }
106 var scannerPos = scanner.Position; 106 var scannerPos = scanner.Position;
107 107
108 // scanner stops as scannerPos 108 // scanner stops as scannerPos
109 if (!scanner.IsFinal) 109 if (!scanner.IsFinal)
110 throw new ParserException($"Unexpected character '{m_buffer[scannerPos + 1]}'"); 110 throw new ParserException($"Unexpected character '{m_buffer[scannerPos]}'");
111 111
112 if (scannerPos != m_pos) { 112 if (scannerPos != m_pos) {
113 m_tokenBuilder.Append(m_buffer, m_pos, scannerPos - m_pos); 113 m_tokenBuilder.Append(m_buffer, m_pos, scannerPos - m_pos);
114 m_pos = scannerPos; 114 m_pos = scannerPos;
115 } 115 }