forked from QuantConnect/Lean
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringExtensions.cs
314 lines (272 loc) · 13.3 KB
/
StringExtensions.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Globalization;
namespace QuantConnect
{
/// <summary>
/// Provides extension methods for properly parsing and serializing values while properly using
/// an IFormatProvider/CultureInfo when applicable
/// </summary>
public static class StringExtensions
{
private static readonly CultureInfo CultureInfo = CultureInfo.InvariantCulture;
private static readonly IFormatProvider FormatProvider = CultureInfo;
private static readonly StringComparison StringComparison = StringComparison.InvariantCulture;
/// <summary>
/// Converts the provided <paramref name="value"/> as <typeparamref name="T"/>
/// using <see cref="CultureInfo"/>
/// </summary>
public static T ConvertInvariant<T>(this object value)
{
return (T) value.ConvertInvariant(typeof(T));
}
/// <summary>
/// Converts the provided <paramref name="value"/> as <paramref name="conversionType"/>
/// using <see cref="CultureInfo"/>
/// </summary>
/// <remarks>
/// This implementation uses the Convert.ToXXX methods. This causes null values to be converted to the default value
/// for the provided <paramref name="conversionType"/>. This is in contrast to directly calling <see cref="IConvertible.ToType"/>
/// which results in an <see cref="InvalidCastException"/> or a <see cref="FormatException"/>. Since existing code is
/// dependent on this null -> default value conversion behavior, it has been preserved in this method.
/// </remarks>
public static object ConvertInvariant(this object value, Type conversionType)
{
switch (Type.GetTypeCode(conversionType))
{
// these cases are purposefully ordered to ensure the compiler can generate a jump table vs a binary tree
case TypeCode.Empty:
throw new ArgumentException("StringExtensions.ConvertInvariant does not support converting to TypeCode.Empty");
case TypeCode.Object:
var convertible = value as IConvertible;
if (convertible != null)
{
return convertible.ToType(conversionType, FormatProvider);
}
return Convert.ChangeType(value, conversionType, FormatProvider);
case TypeCode.DBNull:
throw new ArgumentException("StringExtensions.ConvertInvariant does not support converting to TypeCode.DBNull");
case TypeCode.Boolean:
return Convert.ToBoolean(value, FormatProvider);
case TypeCode.Char:
return Convert.ToChar(value, FormatProvider);
case TypeCode.SByte:
return Convert.ToSByte(value, FormatProvider);
case TypeCode.Byte:
return Convert.ToByte(value, FormatProvider);
case TypeCode.Int16:
return Convert.ToInt16(value, FormatProvider);
case TypeCode.UInt16:
return Convert.ToUInt16(value, FormatProvider);
case TypeCode.Int32:
return Convert.ToInt32(value, FormatProvider);
case TypeCode.UInt32:
return Convert.ToUInt32(value, FormatProvider);
case TypeCode.Int64:
return Convert.ToInt64(value, FormatProvider);
case TypeCode.UInt64:
return Convert.ToUInt64(value, FormatProvider);
case TypeCode.Single:
return Convert.ToSingle(value, FormatProvider);
case TypeCode.Double:
return Convert.ToDouble(value, FormatProvider);
case TypeCode.Decimal:
return Convert.ToDecimal(value, FormatProvider);
case TypeCode.DateTime:
return Convert.ToDateTime(value, FormatProvider);
case TypeCode.String:
return Convert.ToString(value, FormatProvider);
default:
return Convert.ChangeType(value, conversionType, FormatProvider);
}
}
/// <summary>
/// Non-extension method alias for <see cref="FormattableString.Invariant"/>
/// This supports the <code>using static QuantConnect.StringExtensions</code> syntax
/// and is aimed at ensuring all formatting is piped through this class instead of
/// alternatively piping through directly to <see cref="FormattableString.Invariant"/>
/// </summary>
public static string Invariant(FormattableString formattable)
{
return FormattableString.Invariant(formattable);
}
/// <summary>
/// Converts the provided value to a string using <see cref="CultureInfo"/>
/// </summary>
public static string ToStringInvariant(this IConvertible convertible)
{
if (convertible == null)
{
return string.Empty;
}
return convertible.ToString(FormatProvider);
}
/// <summary>
/// Formats the provided value using the specified <paramref name="format"/> and
/// <see cref="CultureInfo"/>
/// </summary>
public static string ToStringInvariant(this IFormattable formattable, string format)
{
if (formattable == null)
{
return string.Empty;
}
// if we have a colon, this implies there's a width parameter in the format it seems this isn't handled
// as one would expect. For example, specifying something like $"{value,10:0.00}" would force the string
// to be at least 10 characters wide with extra padding in the front, but passing the string '10:0.00' or
// ',10:0.00' doesn't work. If we are able to detect a colon in the format and the values preceding the colon,
// are numeric, then we know it starts with a width parameter and we can pipe it into a custom-formed
// string.format call to get the correct output
if (format != null)
{
var indexOfColon = format.IndexOfInvariant(":");
if (indexOfColon != -1)
{
int padding;
var beforeColon = format.Substring(0, indexOfColon);
if (int.TryParse(beforeColon, out padding))
{
return string.Format(FormatProvider, $"{{0,{format}}}", formattable);
}
}
}
return formattable.ToString(format, FormatProvider);
}
/// <summary>
/// Provides a convenience methods for converting a <see cref="DateTime"/> to an invariant ISO-8601 string
/// </summary>
public static string ToIso8601Invariant(this DateTime dateTime)
{
return dateTime.ToStringInvariant("O");
}
/// <summary>
/// Checks if the string starts with the provided <paramref name="beginning"/> using <see cref="CultureInfo"/>
/// while optionally ignoring case.
/// </summary>
public static bool StartsWithInvariant(this string value, string beginning, bool ignoreCase = false)
{
return value.StartsWith(beginning, ignoreCase, CultureInfo);
}
/// <summary>
/// Checks if the string ends with the provided <paramref name="ending"/> using <see cref="CultureInfo"/>
/// while optionally ignoring case.
/// </summary>
public static bool EndsWithInvariant(this string value, string ending, bool ignoreCase = false)
{
return value.EndsWith(ending, ignoreCase, CultureInfo);
}
/// <summary>
/// Gets the index of the specified <paramref name="character"/> using <see cref="StringComparison"/>
/// </summary>
public static int IndexOfInvariant(this string value, char character)
{
return value.IndexOf(character);
}
/// <summary>
/// Gets the index of the specified <paramref name="substring"/> using <see cref="StringComparison"/>
/// or <see cref="System.StringComparison.InvariantCultureIgnoreCase"/> when <paramref name="ignoreCase"/> is true
/// </summary>
public static int IndexOfInvariant(this string value, string substring, bool ignoreCase = false)
{
return value.IndexOf(substring, ignoreCase
? StringComparison.InvariantCultureIgnoreCase
: StringComparison
);
}
/// <summary>
/// Gets the index of the specified <paramref name="substring"/> using <see cref="StringComparison"/>
/// or <see cref="System.StringComparison.InvariantCultureIgnoreCase"/> when <paramref name="ignoreCase"/> is true
/// </summary>
public static int LastIndexOfInvariant(this string value, string substring, bool ignoreCase = false)
{
return value.LastIndexOf(substring, ignoreCase
? StringComparison.InvariantCultureIgnoreCase
: StringComparison
);
}
/// <summary>
/// Provides a shorthand for avoiding the more verbose ternary equivalent.
/// Consider the following:
/// <code>
/// string.IsNullOrEmpty(str) ? (decimal?)null : Convert.ToDecimal(str, CultureInfo.InvariantCulture)
/// </code>
/// Can be expressed as:
/// <code>
/// str.IfNotNullOrEmpty<decimal?>(s => Convert.ToDecimal(str, CultureInfo.InvariantCulture))
/// </code>
/// When combined with additional methods from this class, reducing further to a declarative:
/// <code>
/// str.IfNotNullOrEmpty<decimal?>(s => s.ParseDecimalInvariant())
/// str.IfNotNullOrEmpty<decimal?>(s => s.ConvertInvariant<decimal>())
/// </code>
/// </summary>
/// <paramref name="value">The string value to check for null or empty</paramref>
/// <paramref name="defaultValue">The default value to use if null or empty</paramref>
/// <paramref name="func">Function run on non-null string w/ length > 0</paramref>
public static T IfNotNullOrEmpty<T>(this string value, T defaultValue, Func<string, T> func)
{
if (string.IsNullOrEmpty(value))
{
return defaultValue;
}
return func(value);
}
/// <summary>
/// Provides a shorthand for avoiding the more verbose ternary equivalent.
/// Consider the following:
/// <code>
/// string.IsNullOrEmpty(str) ? (decimal?)null : Convert.ToDecimal(str, CultureInfo.InvariantCulture)
/// </code>
/// Can be expressed as:
/// <code>
/// str.IfNotNullOrEmpty<decimal?>(s => Convert.ToDecimal(str, CultureInfo.InvariantCulture))
/// </code>
/// When combined with additional methods from this class, reducing further to a declarative:
/// <code>
/// str.IfNotNullOrEmpty<decimal?>(s => s.ParseDecimalInvariant())
/// str.IfNotNullOrEmpty<decimal?>(s => s.ConvertInvariant<decimal>())
/// </code>
/// </summary>
/// <paramref name="value">The string value to check for null or empty</paramref>
/// <paramref name="func">Function run on non-null string w/ length > 0</paramref>
public static T IfNotNullOrEmpty<T>(this string value, Func<string, T> func)
{
return value.IfNotNullOrEmpty(default(T), func);
}
/// <summary>
/// Retrieves a substring from this instance. The substring starts at a specified
/// character position and has a specified length.
/// </summary>
/// <paramref name="startIndex">The zero-based starting character position of a substring in this instance</paramref>
/// <paramref name="length">The number of characters in the substring</paramref>
public static string SafeSubstring(this string value, int startIndex, int length)
{
if (string.IsNullOrEmpty(value))
{
return value;
}
if (startIndex > value.Length - 1)
{
return string.Empty;
}
if (startIndex < - 1)
{
startIndex = 0;
}
return value.Substring(startIndex, Math.Min(length, value.Length - startIndex));
}
}
}