-
-
Notifications
You must be signed in to change notification settings - Fork 362
/
Copy pathStringFunctions.cs
297 lines (255 loc) · 8.48 KB
/
StringFunctions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// Copyright (c) Alexandre Mutel. All rights reserved.
// Licensed under the BSD-Clause 2 license.
// See license.txt file in the project root for full license information.
using System;
using System.Collections;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Scriban.Runtime;
using Scriban.Syntax;
namespace Scriban.Functions
{
/// <summary>
/// String functions available through the object 'string' in scriban.
/// </summary>
public class StringFunctions : ScriptObject
{
public StringFunctions()
{
// We need to handle "slice"/"truncate" differently as we have an optional parameters
this.SetValue("slice", new DelegateCustomFunction(Slice), true);
}
public static string Upcase(string text)
{
return text?.ToUpperInvariant();
}
public static string Downcase(string text)
{
return text?.ToLowerInvariant();
}
public static string Capitalize(string text)
{
if (string.IsNullOrEmpty(text) || char.IsUpper(text[0]))
{
return text ?? string.Empty;
}
var builder = new StringBuilder(text);
builder[0] = char.ToUpper(builder[0]);
return builder.ToString();
}
public static string Capitalizewords(string text)
{
if (string.IsNullOrEmpty(text))
{
return string.Empty;
}
var builder = new StringBuilder(text.Length);
var previousSpace = true;
for (int i = 0; i < text.Length; i++)
{
var c = text[i];
if (char.IsWhiteSpace(c))
{
previousSpace = true;
}
else if (previousSpace && char.IsLetter(c))
{
// TODO: Handle culture
c = char.ToUpper(c);
previousSpace = false;
}
builder.Append(c);
}
return builder.ToString();
}
public static string Pluralize(string single, string multiple, int number)
{
return number == 1 ? single : multiple;
}
public static string Remove(string remove, string text)
{
if (string.IsNullOrEmpty(remove) || string.IsNullOrEmpty(text))
{
return text;
}
return text.Replace(remove, string.Empty);
}
public static string RemoveFirst(string remove, string text)
{
return ReplaceFirst(remove, string.Empty, text);
}
public static string Strip(string text)
{
return text?.Trim();
}
public static string LStrip(string text)
{
return text?.TrimStart();
}
public static string RStrip(string text)
{
return text?.TrimEnd();
}
public static IEnumerable Split(string pattern, string text)
{
if (string.IsNullOrEmpty(text))
{
return Enumerable.Empty<string>();
}
pattern = pattern ?? string.Empty;
return text.Split(new[] {pattern}, StringSplitOptions.RemoveEmptyEntries);
}
public static string StripNewlines(string text)
{
if (string.IsNullOrEmpty(text))
{
return string.Empty;
}
return Regex.Replace(text, @"\r?\n", string.Empty);
}
public static string Truncate(int length, string text)
{
if (string.IsNullOrEmpty(text))
{
return string.Empty;
}
int lMinusTruncate = length - "...".Length;
return text.Length > length ? text.Substring(0, lMinusTruncate < 0 ? 0 : lMinusTruncate) + "..." : text;
}
public static string Truncatewords(int count, string text)
{
if (string.IsNullOrEmpty(text))
{
return string.Empty;
}
var builder = new StringBuilder();
bool isFirstWord = true;
foreach (var word in Regex.Split(text, @"\s+"))
{
if (count <= 0)
{
break;
}
if (!isFirstWord)
{
builder.Append(' ');
}
builder.Append(word);
isFirstWord = false;
count--;
}
builder.Append("...");
return builder.ToString();
}
public static string Replace(string match, string replace, string text)
{
if (string.IsNullOrEmpty(text))
{
return string.Empty;
}
match = match ?? string.Empty;
replace = replace ?? string.Empty;
return text.Replace(match, replace);
}
public static string ReplaceFirst(string match, string replace, string text)
{
if (string.IsNullOrEmpty(text))
{
return string.Empty;
}
if (string.IsNullOrEmpty(match))
{
return text;
}
replace = replace ?? string.Empty;
var indexOfMatch = text.IndexOf(match);
if (indexOfMatch < 0)
{
return text;
}
var builder = new StringBuilder();
builder.Append(text.Substring(0, indexOfMatch));
builder.Append(replace);
builder.Append(text.Substring(indexOfMatch + match.Length));
return builder.ToString();
}
public static bool StartsWith(string start, string text)
{
if (string.IsNullOrEmpty(start))
{
throw new ArgumentException("Invalid null start string to match against text", nameof(start));
}
if (string.IsNullOrEmpty(text))
{
throw new ArgumentException("Invalid null text", nameof(text));
}
return text.StartsWith(start);
}
[ScriptMemberIgnore]
public static string Slice(string text, int start, int length = -1)
{
if (text == null || start > text.Length)
{
return string.Empty;
}
if (length < 0)
{
length = text.Length - start;
}
if (start < 0)
{
start = Math.Max(start + text.Length, 0);
}
var end = start + length;
if (end <= start)
{
return string.Empty;
}
if (end > text.Length)
{
length = text.Length - start;
}
return text.Substring(start, length);
}
public static string Handleize(string text)
{
var builder = new StringBuilder();
char lastChar = (char)0;
for (int i = 0; i < text.Length; i++)
{
var c = text[i];
if (char.IsLetterOrDigit(c))
{
lastChar = c;
builder.Append(c);
}
else if (lastChar != '-')
{
builder.Append('-');
lastChar = '-';
}
}
if (builder.Length > 0 && builder[builder.Length - 1] == '-')
{
builder.Length--;
}
return builder.ToString();
}
private static object Slice(TemplateContext context, ScriptNode callerContext, ScriptArray parameters)
{
if (parameters.Count < 2 || parameters.Count > 3)
{
throw new ScriptRuntimeException(callerContext.Span, $"Unexpected number of arguments [{parameters.Count}] for slice. Expecting at least 2 parameters <start> <length>? <text>");
}
var text = context.ToString(callerContext.Span, parameters[parameters.Count - 1]);
var start = context.ToInt(callerContext.Span, parameters[0]);
var length = -1;
if (parameters.Count == 3)
{
length = context.ToInt(callerContext.Span, parameters[1]);
}
return Slice(text, start, length);
}
}
}