Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
StringValue.cs
1using System;
2using System.Reflection;
7
9{
13 public sealed class StringValue : SemiGroupElement
14 {
15 private static readonly StringValues associatedSemiGroup = new StringValues();
16 private static readonly CaseInsensitiveStringValues associatedSemiGroupCis = new CaseInsensitiveStringValues();
17
18 private string value;
19 private string valueLower = null;
20 private bool caseInsensitive;
21
26 public StringValue(string Value)
27 {
28 this.value = Value;
29 this.caseInsensitive = false;
30 }
31
37 public StringValue(string Value, bool CaseInsensitive)
38 {
39 this.value = Value;
40 this.caseInsensitive = CaseInsensitive;
41 }
42
46 public string Value
47 {
48 get => this.value;
49 set => this.value = value;
50 }
51
55 public bool CaseInsensitive
56 {
57 get => this.caseInsensitive;
58 set => this.caseInsensitive = value;
59 }
60
62 public override string ToString()
63 {
64 return Expression.EncodeString(this.value);
65 }
66
71 {
72 get
73 {
74 if (this.caseInsensitive)
75 return associatedSemiGroupCis;
76 else
77 return associatedSemiGroup;
78 }
79 }
80
84 public override object AssociatedObjectValue => this.value;
85
92 {
93 if (Element.IsScalar)
94 return new StringValue(ScriptNode.ToString(Element) + this.value, this.caseInsensitive);
95 else
96 {
99
100 foreach (IElement E in Element.ChildElements)
101 {
102 SE = E as ISemiGroupElement;
103 if (SE is null)
104 Elements.Add(new StringValue(ScriptNode.ToString(E) + this.value, this.caseInsensitive));
105 else
106 Elements.Add(this.AddLeft(SE));
107 }
108
109 return (ISemiGroupElement)Element.Encapsulate(Elements, null);
110 }
111 }
112
119 {
120 if (Element.IsScalar)
121 return new StringValue(this.value + ScriptNode.ToString(Element), this.caseInsensitive);
122 else
123 {
126
127 foreach (IElement E in Element.ChildElements)
128 {
129 SE = E as ISemiGroupElement;
130 if (SE is null)
131 Elements.Add(new StringValue(this.value + ScriptNode.ToString(E), this.caseInsensitive));
132 else
133 Elements.Add(this.AddRight(SE));
134 }
135
136 return (ISemiGroupElement)Element.Encapsulate(Elements, null);
137 }
138 }
139
141 public override bool Equals(object obj)
142 {
143 if (obj is StringValue S)
144 return string.Compare(this.value, S.value, this.caseInsensitive || S.caseInsensitive) == 0;
145
146 if (!(obj is IElement E))
147 return false;
148
149 if (!(E.AssociatedObjectValue is string s))
150 return false;
151
152 return string.Compare(this.value, s, this.caseInsensitive) == 0;
153 }
154
156 public override int GetHashCode()
157 {
158 if (this.caseInsensitive)
159 {
160 if (this.valueLower is null)
161 this.valueLower = this.value.ToLower();
162
163 return this.valueLower.GetHashCode();
164 }
165 else
166 return this.value.GetHashCode();
167 }
168
175 public override bool TryConvertTo(Type DesiredType, out object Value)
176 {
177 if (DesiredType == typeof(string))
178 {
179 Value = this.value;
180 return true;
181 }
182 else if (DesiredType == typeof(char))
183 {
184 if (this.value.Length == 1)
185 {
186 Value = this.value[0];
187 return true;
188 }
189 else
190 {
191 Value = null;
192 return false;
193 }
194 }
195 else if (DesiredType.IsAssignableFrom(typeof(string).GetTypeInfo()))
196 {
197 Value = this.value;
198 return true;
199 }
200 else if (DesiredType.IsAssignableFrom(typeof(StringValue).GetTypeInfo()))
201 {
202 Value = this;
203 return true;
204 }
205 else
206 return Expression.TryConvert(this.value, DesiredType, true, out Value);
207 }
208
212 public static readonly StringValue Empty = new StringValue(string.Empty);
213 }
214}
A chunked list is a linked list of chunks of objects of type T .
Definition: ChunkedList.cs:54
Base class for all types of elements.
Definition: Element.cs:14
virtual IElement Encapsulate(ChunkedList< IElement > Elements, ScriptNode Node)
Encapsulates a set of elements into a similar structure as that provided by the current element.
Definition: Element.cs:61
virtual bool IsScalar
If the element represents a scalar value.
Definition: Element.cs:48
virtual ICollection< IElement > ChildElements
An enumeration of child elements. If the element is a scalar, this property will return null.
Definition: Element.cs:53
Base class for all types of semigroup elements.
Class managing a script expression.
Definition: Expression.cs:41
static bool TryConvert(object Value, Type DesiredType, bool AcceptInformationLoss, out object Result)
Tries to convert an object Value to an object of type DesiredType .
Definition: Expression.cs:5530
static string EncodeString(string s)
Converts a string value to a parsable expression string.
Definition: Expression.cs:4986
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
override string ToString()
Definition: ScriptNode.cs:359
Semi-group of case-insensitive string values.
override ISemiGroupElement AddLeft(ISemiGroupElement Element)
Tries to add an element to the current element, from the left.
Definition: StringValue.cs:91
override object AssociatedObjectValue
Associated object value.
Definition: StringValue.cs:84
static readonly StringValue Empty
The empty string.
Definition: StringValue.cs:212
override bool TryConvertTo(Type DesiredType, out object Value)
Converts the value to a .NET type.
Definition: StringValue.cs:175
StringValue(string Value)
String value.
Definition: StringValue.cs:26
string Value
String value.
Definition: StringValue.cs:47
override bool Equals(object obj)
Compares the element to another. If elements are equal.
Definition: StringValue.cs:141
bool CaseInsensitive
If the string value is case insensitive or not.
Definition: StringValue.cs:56
override ISemiGroup AssociatedSemiGroup
Associated Semi-Group.
Definition: StringValue.cs:71
override int GetHashCode()
Calculates a hash code of the element. Hash code.
Definition: StringValue.cs:156
override ISemiGroupElement AddRight(ISemiGroupElement Element)
Tries to add an element to the current element, from the right.
Definition: StringValue.cs:118
StringValue(string Value, bool CaseInsensitive)
String value.
Definition: StringValue.cs:37
Semi-group of string values.
Definition: StringValues.cs:10
Basic interface for all types of elements.
Definition: IElement.cs:21
Basic interface for all types of semigroup elements.
Basic interface for all types of semigroups.
Definition: ISemiGroup.cs:10