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 )
132 );
133
134 provider.RegisterObject<string>("Contains",
135 s => new BadDynamicInteropFunction<string>("Contains",
136 (_, str) => s.Contains(str),
138 )
139 );
140
141 //LastIndexOf
142 provider.RegisterObject<string>("LastIndexOf",
143 s => new BadDynamicInteropFunction<string>("LastIndexOf",
144 (_, str) => (decimal)s.LastIndexOf(str, StringComparison.Ordinal),
146 )
147 );
148
149 //Replace
150 provider.RegisterObject<string>("Replace",
152 (_, oldStr, newStr) => s.Replace(oldStr, newStr),
154 )
155 );
156
157 //Trim
158 provider.RegisterObject<string>("Trim",
159 s => new BadDynamicInteropFunction("Trim",
160 _ => s.Trim(),
162 )
163 );
164
165 //TrimStart
166 provider.RegisterObject<string>("TrimStart",
167 s => new BadDynamicInteropFunction("TrimStart",
168 _ => s.TrimStart(),
170 )
171 );
172
173 //TrimEnd
174 provider.RegisterObject<string>("TrimEnd",
175 s => new BadDynamicInteropFunction("TrimEnd",
176 _ => s.TrimEnd(),
178 )
179 );
180
181 //PadLeft
182 provider.RegisterObject<string>("PadLeft",
183 s => new BadDynamicInteropFunction<decimal>("PadLeft",
184 (_, padding) => s.PadLeft((int)padding),
186 "padding"
187 )
188 );
189
190 //PadRight
191 provider.RegisterObject<string>("PadRight",
192 s => new BadDynamicInteropFunction<decimal>("PadRight",
193 (_, padding) => s.PadRight((int)padding),
195 "padding"
196 )
197 );
198
199 //Remove
200 provider.RegisterObject<string>("Remove",
202 (_, start, count) => s.Remove((int)start, (int)count),
204 "start",
205 "count"
206 )
207 );
208
209 //Insert
210 provider.RegisterObject<string>("Insert",
212 (_, index, str) => s.Insert((int)index, str),
214 "index",
215 "str"
216 )
217 );
218
219 //EndsWith
220 provider.RegisterObject<string>("EndsWith",
221 s => new BadDynamicInteropFunction<string>("EndsWith",
222 (_, str) => s.EndsWith(str, StringComparison.Ordinal),
224 )
225 );
226
227 //StartsWith
228 provider.RegisterObject<string>("StartsWith",
229 s => new BadDynamicInteropFunction<string>("StartsWith",
230 (_, str) => s.StartsWith(str, StringComparison.Ordinal),
232 )
233 );
234 }
235}
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