Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Mid.cs
1using System;
2using System.Collections.Generic;
8
10{
15 {
26 : base(new ScriptNode[] { X, Pos, Len }, new ArgumentType[] { ArgumentType.Normal, ArgumentType.Scalar, ArgumentType.Scalar },
28 {
29 }
30
34 public override string FunctionName => nameof(Mid);
35
39 public override string[] DefaultArgumentNames
40 {
41 get { return new string[] { "x", "Pos", "Len" }; }
42 }
43
51 {
52 double Pos = Expression.ToDouble(Arguments[1].AssociatedObjectValue);
53 if (Pos < 0 || Pos > int.MaxValue || Pos != Math.Truncate(Pos))
54 throw new ScriptRuntimeException("Expected nonnegative integer in second argument.", this);
55
56 int i = (int)Pos;
57
58 double Len = Expression.ToDouble(Arguments[2].AssociatedObjectValue);
59 if (Len < 0 || Len > int.MaxValue || Len != Math.Truncate(Len))
60 throw new ScriptRuntimeException("Expected nonnegative integer in third argument.", this);
61
62 int c = (int)Len;
63
64 if (Arguments[0].AssociatedObjectValue is string s)
65 {
66 int l = s.Length;
67
68 if (i > l)
69 return StringValue.Empty;
70 else if (i + c >= l)
71 return new StringValue(s.Substring(i));
72 else
73 return new StringValue(s.Substring(i, c));
74 }
75 else
76 {
77 ICollection<IElement> ChildElements;
78
79 if (Arguments[0] is IVector Vector)
80 {
81 int l = Vector.Dimension;
82
83 if (i > l)
84 i = l;
85
86 if (i + c > l)
87 c = l - i;
88
89 if (i == 0 && c == l)
90 return Vector;
91
92 ChildElements = Vector.VectorElements;
93 }
94 else if (Arguments[0] is ISet Set)
95 {
96 int? Size = Set.Size;
97
98 if (Size.HasValue)
99 {
100 int l = Size.Value;
101
102 if (i > l)
103 i = l;
104
105 if (i + c > l)
106 c = l - i;
107
108 if (i == 0 && c == l)
109 return Set;
110 }
111
112 ChildElements = Set.ChildElements;
113 }
114 else
115 throw new ScriptRuntimeException("First argument expected to be a string, vector or a set.", this);
116
117 IElement[] Result = new IElement[c];
118
119 if (ChildElements is IElement[] V)
120 Array.Copy(V, i, Result, 0, c);
121 else if (c > 0)
122 {
123 int j = 0;
124
125 foreach (IElement Element in ChildElements)
126 {
127 if (i-- <= 0)
128 {
129 Result[j++] = Element;
130 if (j == c)
131 break;
132 }
133 }
134 }
135
136 return Arguments[0].Encapsulate(Result, this);
137 }
138 }
139
140 }
141}
Base class for all types of elements.
Definition: Element.cs:13
Base class for all types of sets.
Definition: Set.cs:14
virtual ? int Size
Size of set, if finite and known, otherwise null is returned.
Definition: Set.cs:92
override ICollection< IElement > ChildElements
An enumeration of child elements. If the element is a scalar, this property will return null.
Definition: Set.cs:73
Class managing a script expression.
Definition: Expression.cs:39
static double ToDouble(object Object)
Converts an object to a double value.
Definition: Expression.cs:4824
override string[] DefaultArgumentNames
Default Argument names
Definition: Mid.cs:40
Mid(ScriptNode X, ScriptNode Pos, ScriptNode Len, int Start, int Length, Expression Expression)
Mid(x,Pos,Len)
Definition: Mid.cs:25
override string FunctionName
Name of the function
Definition: Mid.cs:34
override IElement Evaluate(IElement[] Arguments, Variables Variables)
Evaluates the function on a vector argument.
Definition: Mid.cs:50
Base class for multivariate funcions.
ScriptNode[] Arguments
Function arguments.
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
int Length
Length of expression covered by node.
Definition: ScriptNode.cs:101
Expression Expression
Expression of which the node is a part.
Definition: ScriptNode.cs:177
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
static readonly StringValue Empty
The empty string.
Definition: StringValue.cs:211
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:20
Basic interface for vectors.
Definition: IVector.cs:9
Basic interface for all types of sets.
Definition: ISet.cs:10
ArgumentType
Type of parameter used in a function definition or a lambda definition.
Definition: IFunction.cs:9