BadScript 2
Loading...
Searching...
No Matches
BadStringExtension.cs
Go to the documentation of this file.
9
11
16{
25 private static BadObject StringSplit(string str, BadObject splitChar, BadObject skipEmpty)
26 {
27 if (splitChar is not IBadString splitStr)
28 {
29 throw new BadRuntimeException("splitChar must be a string");
30 }
31
32 bool skip = false;
33
34 if (skipEmpty is not IBadBoolean skipB)
35 {
36 if (skipEmpty != BadObject.Null)
37 {
38 throw new BadRuntimeException("skipEmpty must be a boolean");
39 }
40 }
41 else
42 {
43 skip = skipB.Value;
44 }
45
46 return new BadArray(str.Split(new[] { splitStr.Value },
47 skip ? StringSplitOptions.RemoveEmptyEntries : StringSplitOptions.None
48 )
49 .Select(x => (BadObject)x)
50 .ToList()
51 );
52 }
53
54
56 protected override void AddExtensions(BadInteropExtensionProvider provider)
57 {
58 provider.RegisterObject<string>("ToLower",
59 o => new BadDynamicInteropFunction("ToLower",
60 _ => o.ToLower(),
62 )
63 );
64
65 provider.RegisterObject<string>("ToUpper",
66 o => new BadDynamicInteropFunction("ToUpper",
67 _ => o.ToUpper(),
69 )
70 );
71
72 provider.RegisterObject<string>("IsLetters", s => s.All(char.IsLetter));
73 provider.RegisterObject<string>("IsDigits", s => s.All(char.IsDigit));
74 provider.RegisterObject<string>("IsWhiteSpace", s => s.All(char.IsWhiteSpace));
75
78 .ARRAY_ACCESS_OPERATOR_NAME,
79 (_, i) => s[(int)i]
80 .ToString(),
82 "index"
83 )
84 );
85 provider.RegisterObject<string>("Length", a => BadObject.Wrap((decimal)a.Length));
86
87 provider.RegisterObject<string>("Format",
88 s => new BadInteropFunction("Format",
89 args => string.Format(s,
90 args.Cast<object?>()
91 .ToArray()
92 ),
93 false,
95 new BadFunctionParameter("args", false, false, true)
96 )
97 );
98
99 provider.RegisterObject<string>("Split",
100 s => new BadInteropFunction("Split",
101 args => StringSplit(s,
102 args[0],
103 args.Length == 2 ? args[1] : BadObject.Null
104 ),
105 false,
107 "splitStr",
108 new BadFunctionParameter("skipEmpty",
109 true,
110 false,
111 false
112 )
113 )
114 );
115
116 //Substring
117 provider.RegisterObject<string>("Substring",
119 (_, start, end) => s.Substring((int)start, (int)end),
121 "start",
122 "end"
123 )
124 );
125
126 //IndexOf
127 provider.RegisterObject<string>("IndexOf",
128 s => new BadDynamicInteropFunction<string>("IndexOf",
129 (_, str) => (decimal)s.IndexOf(str, StringComparison.Ordinal),
131 new BadFunctionParameter("str", false, true, false, null, BadNativeClassBuilder.GetNative("string"))
132 )
133 );
134
135 provider.RegisterObject<string>("Contains",
136 s => new BadDynamicInteropFunction<string>("Contains",
137 (_, str) => s.Contains(str),
139 new BadFunctionParameter("str", false, true, false, null, BadNativeClassBuilder.GetNative("string"))
140 )
141 );
142
143 //LastIndexOf
144 provider.RegisterObject<string>("LastIndexOf",
145 s => new BadDynamicInteropFunction<string>("LastIndexOf",
146 (_, str) => (decimal)s.LastIndexOf(str, StringComparison.Ordinal),
148 new BadFunctionParameter("str", false, true, false, null, BadNativeClassBuilder.GetNative("string"))
149 )
150 );
151
152 //Replace
153 provider.RegisterObject<string>("Replace",
155 (_, oldStr, newStr) => s.Replace(oldStr, newStr),
157 new BadFunctionParameter("oldStr", false, true, false, null, BadNativeClassBuilder.GetNative("string")),
158 new BadFunctionParameter("newStr", false, true, false, null, BadNativeClassBuilder.GetNative("string"))
159 )
160 );
161
162 //Trim
163 provider.RegisterObject<string>("Trim",
164 s => new BadDynamicInteropFunction("Trim",
165 _ => s.Trim(),
167 )
168 );
169
170 //TrimStart
171 provider.RegisterObject<string>("TrimStart",
172 s => new BadDynamicInteropFunction("TrimStart",
173 _ => s.TrimStart(),
175 )
176 );
177
178 //TrimEnd
179 provider.RegisterObject<string>("TrimEnd",
180 s => new BadDynamicInteropFunction("TrimEnd",
181 _ => s.TrimEnd(),
183 )
184 );
185
186 //PadLeft
187 provider.RegisterObject<string>("PadLeft",
188 s => new BadDynamicInteropFunction<decimal>("PadLeft",
189 (_, padding) => s.PadLeft((int)padding),
191 "padding"
192 )
193 );
194
195 //PadRight
196 provider.RegisterObject<string>("PadRight",
197 s => new BadDynamicInteropFunction<decimal>("PadRight",
198 (_, padding) => s.PadRight((int)padding),
200 "padding"
201 )
202 );
203
204 //Remove
205 provider.RegisterObject<string>("Remove",
207 (_, start, count) => s.Remove((int)start, (int)count),
209 "start",
210 "count"
211 )
212 );
213
214 //Insert
215 provider.RegisterObject<string>("Insert",
217 (_, index, str) => s.Insert((int)index, str),
219 "index",
220 "str"
221 )
222 );
223
224 //EndsWith
225 provider.RegisterObject<string>("EndsWith",
226 s => new BadDynamicInteropFunction<string>("EndsWith",
227 (_, str) => s.EndsWith(str, StringComparison.Ordinal),
229 new BadFunctionParameter("str", false, true, false, null, BadNativeClassBuilder.GetNative("string"))
230 )
231 );
232
233 //StartsWith
234 provider.RegisterObject<string>("StartsWith",
235 s => new BadDynamicInteropFunction<string>("StartsWith",
236 (_, str) => s.StartsWith(str, StringComparison.Ordinal),
238 new BadFunctionParameter("str", false, true, false, null, BadNativeClassBuilder.GetNative("string"))
239 )
240 );
241 }
242}
Contains Static Data for the BadScript Language.
static BadObject StringSplit(string str, BadObject splitChar, BadObject skipEmpty)
Split a string into an array.
override void AddExtensions(BadInteropExtensionProvider provider)
Public Extension API for the BS2 Runtime.
void RegisterObject(Type t, string propName, BadObject obj)
Registers the specified extension for the specified type.
Interop Function taking an array of arguments.
Implements a Dynamic List/Array for the BadScript Language.
Definition BadArray.cs:17
The Base Class for all BadScript Objects.
Definition BadObject.cs:14
static readonly BadObject Null
The Null Value for the BadScript Language.
Definition BadObject.cs:28
Helper Class that Builds a Native Class from a Prototype.
static BadClassPrototype GetNative(string name)
Returns a Native Class Prototype for the given Native Type.
Implements the Interface for Native Boolean.
Definition IBadBoolean.cs:7
Implements the Interface for Native Strings.
Definition IBadString.cs:7
Contains Shared Data Structures and Functionality.
Contains Common Interop Extensions for the BadScript2 Runtime.
Contains the Error Objects for the BadScript2 Language.
Contains the Interop Function Classes for the BadScript2 Language.
Contains the Interop Abstractions and Implementations for the BadScript2 Language.
Contains Runtime Function Objects.
Contains the Native Runtime Objects.
Definition BadBoolean.cs:6
Contains the Runtime Objects.
Definition BadArray.cs:10