228
|
1 using System;
|
|
2 using System.Collections.Generic;
|
|
3 using System.Linq;
|
|
4 using System.Text;
|
|
5 using System.Threading.Tasks;
|
|
6
|
|
7 namespace Implab.Formats.Json {
|
|
8 public class JsonStringScanner : JsonScanner {
|
|
9 const int _defaultBuffer = 64;
|
|
10
|
|
11 readonly string m_data;
|
|
12 int m_offset;
|
|
13
|
|
14 JsonStringScanner(string data, char[] buffer, int pos, int length, int offset) : base(buffer, pos, length) {
|
|
15 m_data = data;
|
|
16 m_offset = offset;
|
|
17 }
|
|
18
|
|
19 protected override int Read(char[] buffer, int offset, int size) {
|
|
20 if (m_data == null)
|
|
21 return 0;
|
|
22 if (m_offset >= m_data.Length)
|
|
23 return 0;
|
|
24
|
|
25 var count = Math.Min(size, m_data.Length - m_offset);
|
|
26
|
|
27 m_data.CopyTo(m_offset, buffer, offset, count);
|
|
28 m_offset += count;
|
|
29
|
|
30 return count;
|
|
31 }
|
|
32
|
|
33 public static JsonStringScanner Create(string data) {
|
|
34 Safe.ArgumentNotNull(data, nameof(data));
|
|
35
|
|
36 if (data.Length <= _defaultBuffer)
|
|
37 return new JsonStringScanner(null, data.ToCharArray(), 0, data.Length, data.Length);
|
|
38
|
|
39 var buffer = new char[_defaultBuffer];
|
|
40 data.CopyTo(0, buffer, 0, _defaultBuffer);
|
|
41 return new JsonStringScanner(data, buffer, 0, _defaultBuffer, _defaultBuffer);
|
|
42 }
|
|
43
|
|
44 public static JsonStringScanner Create(string data, int offset, int length) {
|
|
45 Safe.ArgumentNotNull(data, nameof(data));
|
251
|
46 Safe.ArgumentInRange(offset >= 0 && offset < data.Length , nameof(offset));
|
|
47 Safe.ArgumentInRange(length >= 0 && offset + length <= data.Length, nameof(length));
|
228
|
48
|
|
49 if (length <= _defaultBuffer) {
|
|
50 var buffer = new char[length];
|
|
51 data.CopyTo(offset, buffer, 0, length);
|
|
52
|
|
53 return new JsonStringScanner(null, buffer, 0, length, length);
|
|
54 } else {
|
|
55 var buffer = new char[_defaultBuffer];
|
|
56 data.CopyTo(offset, buffer, 0, _defaultBuffer);
|
|
57 return new JsonStringScanner(data, buffer, 0, _defaultBuffer, offset + _defaultBuffer);
|
|
58 }
|
|
59 }
|
|
60
|
|
61 public static JsonStringScanner Create(char[] data, int offset, int length) {
|
|
62 Safe.ArgumentNotNull(data, nameof(data));
|
251
|
63 Safe.ArgumentInRange(offset >= 0 && offset < data.Length , nameof(offset));
|
|
64 Safe.ArgumentInRange(length >= 0 && offset + length <= data.Length, nameof(length));
|
228
|
65
|
|
66 return new JsonStringScanner(null, data, offset, offset + length, offset + length);
|
|
67
|
|
68 }
|
|
69 }
|
|
70 }
|