Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Replace.cs
1using System.Text.RegularExpressions;
6
8{
13 {
14 private readonly bool useRegex;
15
26 : base(new ScriptNode[] { String, From, To }, argumentTypes3Scalar, Start, Length, Expression)
27 {
28 this.useRegex = false;
29 }
30
41 public Replace(ScriptNode String, ScriptNode From, ScriptNode To, bool UseRegex, int Start, int Length, Expression Expression)
42 : base(new ScriptNode[] { String, From, To }, argumentTypes3Scalar, Start, Length, Expression)
43 {
44 this.useRegex = UseRegex;
45 }
46
57 public Replace(ScriptNode String, ScriptNode From, ScriptNode To, ScriptNode Options, int Start, int Length, Expression Expression)
58 : base(new ScriptNode[] { String, From, To, Options }, argumentTypes4Scalar, Start, Length, Expression)
59 {
60 this.useRegex = true;
61 }
62
66 public override string FunctionName => nameof(Replace);
67
71 public override string[] DefaultArgumentNames => new string[] { "String", "From", "To" };
72
80 {
81 string s = Arguments[0].AssociatedObjectValue?.ToString() ?? string.Empty;
82 string From = Arguments[1].AssociatedObjectValue?.ToString() ?? string.Empty;
83 string To = Arguments[2].AssociatedObjectValue?.ToString() ?? string.Empty;
84
85 if (!this.useRegex)
86 return new StringValue(s.Replace(From, To));
87
88 RegexOptions Options;
89
90 if (Arguments.Length > 3)
91 Options = GetOptions(Arguments[3].AssociatedObjectValue?.ToString(), this);
92 else
93 Options = RegexOptions.Singleline;
94
95 Regex Expression = new Regex(From, Options);
96 Match M = Expression.Match(s);
97
98 while (M.Success)
99 {
100 s = s.Substring(0, M.Index) + To + s.Substring(M.Index + M.Length);
101 M = Expression.Match(s, M.Index);
102 }
103
104 return new StringValue(s);
105 }
106
115 public static RegexOptions GetOptions(string Options, ScriptNode Node)
116 {
117 RegexOptions Result = RegexOptions.Singleline;
118
119 if (!string.IsNullOrEmpty(Options))
120 {
121 foreach (char ch in Options)
122 {
123 switch (ch)
124 {
125 case 'i':
126 Result |= RegexOptions.IgnoreCase;
127 break;
128
129 case 'm':
130 Result &= ~RegexOptions.Singleline;
131 Result |= RegexOptions.Multiline;
132 break;
133
134 case 'x':
135 Result |= RegexOptions.IgnorePatternWhitespace;
136 break;
137
138 default:
139 throw new ScriptRuntimeException("Regular expression option not supported: " + ch, Node);
140 }
141 }
142 }
143
144 return Result;
145 }
146 }
147}
Class managing a script expression.
Definition: Expression.cs:39
Replace(String,From,To)
Definition: Replace.cs:13
Replace(ScriptNode String, ScriptNode From, ScriptNode To, int Start, int Length, Expression Expression)
Replaceenates the elements of a vector, optionally delimiting the elements with a Delimiter.
Definition: Replace.cs:25
static RegexOptions GetOptions(string Options, ScriptNode Node)
Converts a string-representation of regex options into an enumeration value.
Definition: Replace.cs:115
override IElement Evaluate(IElement[] Arguments, Variables Variables)
Evaluates the function.
Definition: Replace.cs:79
override string FunctionName
Name of the function
Definition: Replace.cs:66
Replace(ScriptNode String, ScriptNode From, ScriptNode To, ScriptNode Options, int Start, int Length, Expression Expression)
Replaceenates the elements of a vector, optionally delimiting the elements with a Delimiter.
Definition: Replace.cs:57
Replace(ScriptNode String, ScriptNode From, ScriptNode To, bool UseRegex, int Start, int Length, Expression Expression)
Replaceenates the elements of a vector, optionally delimiting the elements with a Delimiter.
Definition: Replace.cs:41
override string[] DefaultArgumentNames
Default Argument names
Definition: Replace.cs:71
Base class for multivariate funcions.
ScriptNode[] Arguments
Function arguments.
static readonly ArgumentType[] argumentTypes3Scalar
Three scalar parameters.
static readonly ArgumentType[] argumentTypes4Scalar
Four scalar parameters.
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
override string ToString()
Definition: ScriptNode.cs:359
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
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:20