annotate Implab/Formats/Json/JsonStringScanner.cs @ 281:e0916ddc9950 v3 tip

code cleanup and refactoring
author cin
date Fri, 01 Jun 2018 21:35:24 +0300
parents 7c7e9ad6fe4a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
228
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
1 using System;
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
2 using System.Collections.Generic;
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
3 using System.Linq;
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
4 using System.Text;
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
5 using System.Threading.Tasks;
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
6
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
7 namespace Implab.Formats.Json {
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
8 public class JsonStringScanner : JsonScanner {
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
9 const int _defaultBuffer = 64;
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
10
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
11 readonly string m_data;
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
12 int m_offset;
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
13
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
14 JsonStringScanner(string data, char[] buffer, int pos, int length, int offset) : base(buffer, pos, length) {
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
15 m_data = data;
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
16 m_offset = offset;
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
17 }
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
18
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
19 protected override int Read(char[] buffer, int offset, int size) {
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
20 if (m_data == null)
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
21 return 0;
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
22 if (m_offset >= m_data.Length)
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
23 return 0;
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
24
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
25 var count = Math.Min(size, m_data.Length - m_offset);
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
26
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
27 m_data.CopyTo(m_offset, buffer, offset, count);
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
28 m_offset += count;
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
29
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
30 return count;
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
31 }
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
32
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
33 public static JsonStringScanner Create(string data) {
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
34 Safe.ArgumentNotNull(data, nameof(data));
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
35
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
36 if (data.Length <= _defaultBuffer)
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
37 return new JsonStringScanner(null, data.ToCharArray(), 0, data.Length, data.Length);
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
38
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
39 var buffer = new char[_defaultBuffer];
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
40 data.CopyTo(0, buffer, 0, _defaultBuffer);
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
41 return new JsonStringScanner(data, buffer, 0, _defaultBuffer, _defaultBuffer);
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
42 }
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
43
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
44 public static JsonStringScanner Create(string data, int offset, int length) {
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
45 Safe.ArgumentNotNull(data, nameof(data));
251
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 230
diff changeset
46 Safe.ArgumentInRange(offset >= 0 && offset < data.Length , nameof(offset));
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 230
diff changeset
47 Safe.ArgumentInRange(length >= 0 && offset + length <= data.Length, nameof(length));
228
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
48
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
49 if (length <= _defaultBuffer) {
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
50 var buffer = new char[length];
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
51 data.CopyTo(offset, buffer, 0, length);
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
52
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
53 return new JsonStringScanner(null, buffer, 0, length, length);
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
54 } else {
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
55 var buffer = new char[_defaultBuffer];
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
56 data.CopyTo(offset, buffer, 0, _defaultBuffer);
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
57 return new JsonStringScanner(data, buffer, 0, _defaultBuffer, offset + _defaultBuffer);
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
58 }
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
59 }
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
60
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
61 public static JsonStringScanner Create(char[] data, int offset, int length) {
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
62 Safe.ArgumentNotNull(data, nameof(data));
251
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 230
diff changeset
63 Safe.ArgumentInRange(offset >= 0 && offset < data.Length , nameof(offset));
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 230
diff changeset
64 Safe.ArgumentInRange(length >= 0 && offset + length <= data.Length, nameof(length));
228
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
65
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
66 return new JsonStringScanner(null, data, offset, offset + length, offset + length);
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
67
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
68 }
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
69 }
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
70 }