Mercurial > pub > ImplabNet
annotate Implab/Formats/JSON/StringTranslator.cs @ 233:d6fe09f5592c v2
Improved AsyncQueue
Removed ImplabFx
author | cin |
---|---|
date | Wed, 04 Oct 2017 15:44:47 +0300 |
parents | 6fa235c5a760 |
children |
rev | line source |
---|---|
163 | 1 using Implab; |
176 | 2 using Implab.Formats; |
163 | 3 using System; |
4 using System.Collections.Generic; | |
5 using System.Diagnostics; | |
6 using System.Linq; | |
7 using System.Text; | |
8 using System.Threading.Tasks; | |
9 | |
228 | 10 namespace Implab.Formats.Json { |
163 | 11 /// <summary> |
12 /// Класс для преобразования экранированной строки JSON | |
13 /// </summary> | |
180 | 14 static class StringTranslator { |
163 | 15 static readonly char[] _escMap; |
16 static readonly int[] _hexMap; | |
17 | |
18 static StringTranslator() { | |
226
9428ea36838e
fixed JSON parser error when parsing escaped double quote symbol
cin
parents:
180
diff
changeset
|
19 var chars = new char[] { 'b', 'f', 't', 'r', 'n', '\\', '/', '"' }; |
9428ea36838e
fixed JSON parser error when parsing escaped double quote symbol
cin
parents:
180
diff
changeset
|
20 var vals = new char[] { '\b', '\f', '\t', '\r', '\n', '\\', '/', '"' }; |
163 | 21 |
22 _escMap = new char[chars.Max() + 1]; | |
23 | |
24 for (int i = 0; i < chars.Length; i++) | |
25 _escMap[chars[i]] = vals[i]; | |
26 | |
27 var hexs = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'A', 'B', 'C', 'D', 'E', 'F' }; | |
28 var ints = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 10, 11, 12, 13, 14, 15 }; | |
29 | |
30 _hexMap = new int[hexs.Max() + 1]; | |
31 | |
32 for (int i = 0; i < hexs.Length; i++) | |
33 _hexMap[hexs[i]] = ints[i]; | |
34 | |
35 } | |
36 | |
37 internal static char TranslateEscapedChar(char symbol) { | |
38 return _escMap[symbol]; | |
39 } | |
40 | |
41 internal static char TranslateHexUnicode(char[] symbols, int offset) { | |
42 Debug.Assert(symbols != null); | |
43 Debug.Assert(symbols.Length - offset >= 4); | |
44 | |
45 int value = (_hexMap[symbols[offset]] << 12) | |
46 | (_hexMap[symbols[offset + 1]] << 8) | |
47 | (_hexMap[symbols[offset + 2]] << 4) | |
48 | (_hexMap[symbols[offset + 3]]); | |
49 return (char)value; | |
50 } | |
51 } | |
52 } |