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(
47 str.Split(
48 new[]
49 {
50 splitStr.Value,
51 },
52 skip ? StringSplitOptions.RemoveEmptyEntries : StringSplitOptions.None
53 )
54 .Select(x => (BadObject)x)
55 .ToList()
56 );
57 }
58
59
61 protected override void AddExtensions(BadInteropExtensionProvider provider)
62 {
63 provider.RegisterObject<string>(
64 "ToLower",
66 "ToLower",
67 _ => o.ToLower(),
69 )
70 );
71 provider.RegisterObject<string>(
72 "ToUpper",
74 "ToUpper",
75 _ => o.ToUpper(),
77 )
78 );
79
80 provider.RegisterObject<string>("IsLetters", s => s.All(char.IsLetter));
81 provider.RegisterObject<string>("IsDigits", s => s.All(char.IsDigit));
82 provider.RegisterObject<string>("IsWhiteSpace", s => s.All(char.IsWhiteSpace));
83
84 provider.RegisterObject<string>(
88 (_, i) => s[(int)i].ToString(),
90 "index"
91 )
92 );
93 provider.RegisterObject<string>("Length", a => BadObject.Wrap((decimal)a.Length));
94 provider.RegisterObject<string>(
95 "Format",
96 s => new BadInteropFunction(
97 "Format",
98 args => string.Format(s, args.Cast<object?>().ToArray()),
99 false,
101 new BadFunctionParameter("args", false, false, true)
102 )
103 );
104
105 provider.RegisterObject<string>(
106 "Split",
107 s => new BadInteropFunction(
108 "Split",
109 args => StringSplit(s, args[0], args.Length == 2 ? args[1] : BadObject.Null),
110 false,
112 "splitStr",
113 new BadFunctionParameter("skipEmpty", true, false, false)
114 )
115 );
116
117 //Substring
118 provider.RegisterObject<string>(
119 "Substring",
121 "Substring",
122 (_, start, end) => s.Substring((int)start, (int)end),
124 "start",
125 "end"
126 )
127 );
128
129 //IndexOf
130 provider.RegisterObject<string>(
131 "IndexOf",
133 "IndexOf",
134 (_, str) => (decimal)s.IndexOf(str, StringComparison.Ordinal),
136 )
137 );
138
139 provider.RegisterObject<string>(
140 "Contains",
142 "Contains",
143 (_, str) => s.Contains(str),
145 )
146 );
147
148 //LastIndexOf
149 provider.RegisterObject<string>(
150 "LastIndexOf",
152 "LastIndexOf",
153 (_, str) => (decimal)s.LastIndexOf(str, StringComparison.Ordinal),
155 )
156 );
157
158 //Replace
159 provider.RegisterObject<string>(
160 "Replace",
162 "Replace",
163 (_, oldStr, newStr) => s.Replace(oldStr, newStr),
165 )
166 );
167
168 //Trim
169 provider.RegisterObject<string>(
170 "Trim",
172 "Trim",
173 _ => s.Trim(),
175 )
176 );
177
178 //TrimStart
179 provider.RegisterObject<string>(
180 "TrimStart",
182 "TrimStart",
183 _ => s.TrimStart(),
185 )
186 );
187
188 //TrimEnd
189 provider.RegisterObject<string>(
190 "TrimEnd",
192 "TrimEnd",
193 _ => s.TrimEnd(),
195 )
196 );
197
198 //PadLeft
199 provider.RegisterObject<string>(
200 "PadLeft",
202 "PadLeft",
203 (_, padding) => s.PadLeft((int)padding),
205 "padding"
206 )
207 );
208
209 //PadRight
210 provider.RegisterObject<string>(
211 "PadRight",
213 "PadRight",
214 (_, padding) => s.PadRight((int)padding),
216 "padding"
217 )
218 );
219
220 //Remove
221 provider.RegisterObject<string>(
222 "Remove",
224 "Remove",
225 (_, start, count) => s.Remove((int)start, (int)count),
227 "start",
228 "count"
229 )
230 );
231
232 //Insert
233 provider.RegisterObject<string>(
234 "Insert",
236 "Insert",
237 (_, index, str) => s.Insert((int)index, str),
239 "index",
240 "str"
241 )
242 );
243
244 //EndsWith
245 provider.RegisterObject<string>(
246 "EndsWith",
248 "EndsWith",
249 (_, str) => s.EndsWith(str, StringComparison.Ordinal),
251 )
252 );
253
254 //StartsWith
255 provider.RegisterObject<string>(
256 "StartsWith",
258 "StartsWith",
259 (_, str) => s.StartsWith(str, StringComparison.Ordinal),
261 )
262 );
263 }
264}
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