tangxu
2024-11-04 ebd031e3bed6c1cfddce8fc9b98f7f9e95fb9e32
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using DPumpHydr.WinFrmUI.WenSkin.Json.Utilities;
 
namespace DPumpHydr.WinFrmUI.WenSkin.Json
{
    internal struct JsonPosition
    {
        private static readonly char[] SpecialCharacters = new char[6] { '.', ' ', '[', ']', '(', ')' };
 
        internal JsonContainerType Type;
 
        internal int Position;
 
        internal string PropertyName;
 
        internal bool HasIndex;
 
        public JsonPosition(JsonContainerType type)
        {
            Type = type;
            HasIndex = TypeHasIndex(type);
            Position = -1;
            PropertyName = null;
        }
 
        internal int CalculateLength()
        {
            switch (Type)
            {
            case JsonContainerType.Object:
                return PropertyName.Length + 5;
            case JsonContainerType.Array:
            case JsonContainerType.Constructor:
                return MathUtils.IntLength((ulong)Position) + 2;
            default:
                throw new ArgumentOutOfRangeException("Type");
            }
        }
 
        internal void WriteTo(StringBuilder sb)
        {
            switch (Type)
            {
            case JsonContainerType.Object:
            {
                string propertyName = PropertyName;
                if (propertyName.IndexOfAny(SpecialCharacters) != -1)
                {
                    sb.Append("['");
                    sb.Append(propertyName);
                    sb.Append("']");
                    break;
                }
                if (sb.Length > 0)
                {
                    sb.Append('.');
                }
                sb.Append(propertyName);
                break;
            }
            case JsonContainerType.Array:
            case JsonContainerType.Constructor:
                sb.Append('[');
                sb.Append(Position);
                sb.Append(']');
                break;
            }
        }
 
        internal static bool TypeHasIndex(JsonContainerType type)
        {
            if (type != JsonContainerType.Array)
            {
                return type == JsonContainerType.Constructor;
            }
            return true;
        }
 
        internal static string BuildPath(List<JsonPosition> positions, JsonPosition? currentPosition)
        {
            int num = 0;
            if (positions != null)
            {
                for (int i = 0; i < positions.Count; i++)
                {
                    num += positions[i].CalculateLength();
                }
            }
            if (currentPosition.HasValue)
            {
                num += currentPosition.GetValueOrDefault().CalculateLength();
            }
            StringBuilder stringBuilder = new StringBuilder(num);
            if (positions != null)
            {
                foreach (JsonPosition position in positions)
                {
                    position.WriteTo(stringBuilder);
                }
            }
            currentPosition?.WriteTo(stringBuilder);
            return stringBuilder.ToString();
        }
 
        internal static string FormatMessage(IJsonLineInfo lineInfo, string path, string message)
        {
            if (!message.EndsWith(Environment.NewLine, StringComparison.Ordinal))
            {
                message = message.Trim();
                if (!message.EndsWith('.'))
                {
                    message += ".";
                }
                message += " ";
            }
            message += "Path '{0}'".FormatWith(CultureInfo.InvariantCulture, path);
            if (lineInfo != null && lineInfo.HasLineInfo())
            {
                message += ", line {0}, position {1}".FormatWith(CultureInfo.InvariantCulture, lineInfo.LineNumber, lineInfo.LinePosition);
            }
            message += ".";
            return message;
        }
    }
}