tangxu
2024-12-29 72e75456f8b30ec5b6f355539d9c883b0f810d21
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
using System;
using System.Collections.Generic;
using System.Globalization;
using DPumpHydr.WinFrmUI.WenSkin.Json.Utilities;
 
namespace DPumpHydr.WinFrmUI.WenSkin.Json.Linq.JsonPath
{
    internal class ArraySliceFilter : PathFilter
    {
        public int? Start { get; set; }
 
        public int? End { get; set; }
 
        public int? Step { get; set; }
 
        public override IEnumerable<JToken> ExecuteFilter(IEnumerable<JToken> current, bool errorWhenNoMatch)
        {
            if (Step == 0)
            {
                throw new JsonException("Step cannot be zero.");
            }
            foreach (JToken t in current)
            {
                JArray a = t as JArray;
                if (a != null)
                {
                    int stepCount = Step ?? 1;
                    int num = Start ?? ((stepCount <= 0) ? (a.Count - 1) : 0);
                    int stopIndex3 = End ?? ((stepCount > 0) ? a.Count : (-1));
                    if (Start < 0)
                    {
                        num = a.Count + num;
                    }
                    if (End < 0)
                    {
                        stopIndex3 = a.Count + stopIndex3;
                    }
                    num = Math.Max(num, (stepCount <= 0) ? int.MinValue : 0);
                    num = Math.Min(num, (stepCount > 0) ? a.Count : (a.Count - 1));
                    stopIndex3 = Math.Max(stopIndex3, -1);
                    stopIndex3 = Math.Min(stopIndex3, a.Count);
                    bool positiveStep = stepCount > 0;
                    if (IsValid(num, stopIndex3, positiveStep))
                    {
                        for (int i = num; IsValid(i, stopIndex3, positiveStep); i += stepCount)
                        {
                            yield return a[i];
                        }
                    }
                    else if (errorWhenNoMatch)
                    {
                        throw new JsonException("Array slice of {0} to {1} returned no results.".FormatWith(CultureInfo.InvariantCulture, Start.HasValue ? Start.GetValueOrDefault().ToString(CultureInfo.InvariantCulture) : "*", End.HasValue ? End.GetValueOrDefault().ToString(CultureInfo.InvariantCulture) : "*"));
                    }
                }
                else if (errorWhenNoMatch)
                {
                    throw new JsonException("Array slice is not valid on {0}.".FormatWith(CultureInfo.InvariantCulture, t.GetType().Name));
                }
            }
        }
 
        private bool IsValid(int index, int stopIndex, bool positiveStep)
        {
            if (positiveStep)
            {
                return index < stopIndex;
            }
            return index > stopIndex;
        }
    }
}