annotate Implab/Formats/Json/JsonStringScanner.cs @ 243:b1e0ffdf3451 v3

working on promises
author cin
date Wed, 24 Jan 2018 19:24:10 +0300
parents 3e26338eb977
children 7c7e9ad6fe4a
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));
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
46 Safe.ArgumentGreaterThan(offset, 0, nameof(offset));
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
47 Safe.ArgumentGreaterThan(length, 0, nameof(length));
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 (offset + length > data.Length)
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
50 throw new ArgumentOutOfRangeException("Specified offset and length are out of the string bounds");
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
51
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
52 if (length <= _defaultBuffer) {
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
53 var buffer = new char[length];
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
54 data.CopyTo(offset, buffer, 0, length);
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
55
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
56 return new JsonStringScanner(null, buffer, 0, length, length);
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
57 } else {
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
58 var buffer = new char[_defaultBuffer];
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
59 data.CopyTo(offset, buffer, 0, _defaultBuffer);
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
60 return new JsonStringScanner(data, buffer, 0, _defaultBuffer, offset + _defaultBuffer);
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
61 }
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
62 }
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
63
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
64 public static JsonStringScanner Create(char[] data, int offset, int length) {
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
65 Safe.ArgumentNotNull(data, nameof(data));
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
66 Safe.ArgumentGreaterThan(offset, 0, nameof(offset));
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
67 Safe.ArgumentGreaterThan(length, 0, nameof(length));
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
68
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
69 if (offset + length > data.Length)
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
70 throw new ArgumentOutOfRangeException("Specified offset and length are out of the array bounds");
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
71
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
72 return new JsonStringScanner(null, data, offset, offset + length, offset + length);
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
73
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
74 }
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
75 }
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
76 }