-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStringEx.cs
More file actions
278 lines (253 loc) · 9.32 KB
/
Copy pathStringEx.cs
File metadata and controls
278 lines (253 loc) · 9.32 KB
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
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Linq;
using Newtonsoft.Json.Linq;
namespace Inversion.Extensions {
/// <summary>
/// An extension class providing extensions for string.
/// </summary>
public static class StringEx {
private static readonly Regex _xmlName = new Regex("[_:A-Za-z][-._:A-Za-z0-9]*");
/// <summary>
/// Determines if the string is not null
/// and has a length greater than zero.
/// </summary>
/// <param name="self">The subject of extension.</param>
/// <returns>
/// Returns <b>true</b> if the string has a values;
/// otherwise returns <b>false</b>.
/// </returns>
public static bool HasValue(this string self) {
return !String.IsNullOrEmpty(self);
}
/// <summary>
/// Checks if a string has a value and if not
/// throws an <see cref="ArgumentNullException"/>.
/// </summary>
/// <param name="self">The subject of extension.</param>
/// <param name="message">
/// The message to use as part of the exception.
/// </param>
/// <seealso cref="HasValue"/>
public static void AssertHasValue(this string self, string message) {
if (!self.HasValue()) throw new ArgumentNullException(message);
}
/// <summary>
/// Places the
/// </summary>
/// <param name="self"></param>
/// <param name="number"></param>
/// <param name="character"></param>
/// <returns></returns>
public static string Prepend(this string self, int number, char character) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < number; i++) {
sb.Append(character);
}
sb.Append(self);
return sb.ToString();
}
/// <summary>
/// Determines if a string is a valid XML tag name.
/// </summary>
/// <param name="self">The subject of extension.</param>
/// <returns>
/// Returns <b>true</b> if the string is a valid XML name;
/// otherwise, returns <b>false</b>.
/// </returns>
public static bool IsXmlName(this string self) {
return _xmlName.IsMatch(self);
}
/// <summary>
/// Filters out characters from string by testing them with a predicate.
/// </summary>
/// <param name="self">The string to act upon.</param>
/// <param name="test">The predicate to test each character with.</param>
/// <returns>Returns a new string containing only those charcters for which the test returned true.</returns>
public static string Filter(this string self, Predicate<char> test) {
StringBuilder sb = new StringBuilder();
foreach (char c in self) {
if (test(c)) {
sb.Append(c);
}
}
return sb.ToString();
}
/// <summary>
/// Produces a new string by removing all non-numeric characters from the sting provided.
/// </summary>
/// <param name="self">The string to act upon.</param>
/// <returns>The new filtered string.</returns>
public static string RemoveNonNumeric(this string self) {
return self.Filter(c => !char.IsNumber(c));
}
/// <summary>
/// Produces a new string by removing all alphabetic characters from the sting provided.
/// </summary>
/// <param name="self">The string to act upon.</param>
/// <returns>The new filtered string.</returns>
public static string RemoveNonAlpha(this string self) {
return self.Filter(c => !char.IsLetter(c));
}
/// <summary>
/// Produces a new string by removing all non-alpha-numeric characters from the sting provided.
/// </summary>
/// <param name="self">The string to act upon.</param>
/// <returns>The new filtered string.</returns>
public static string RemoveNonAlphaNumeric(this string self) {
return self.Filter(c => !char.IsLetterOrDigit(c));
}
/// <summary>
/// Produces a new string by removing all whitespace characters from the sting provided.
/// </summary>
/// <param name="self">The string to act upon.</param>
/// <returns>The new filtered string.</returns>
public static string RemoveWhitespace(this string self) {
return self.Filter(c => !char.IsWhiteSpace(c));
}
/// <summary>
/// This method ensures that the returned string has only valid XML unicode
/// charcters as specified in the XML 1.0 standard. For reference please see
/// http://www.w3.org/TR/2000/REC-xml-20001006#NT-Char for the
/// standard reference.
/// </summary>
/// <param name="self">The string being acted upon.</param>
/// <returns>A copy of the input string with non-valid charcters removed.</returns>
public static string RemoveInvalidXmlCharacters(this string self) {
return self.Filter(c => !((c == 0x9)
|| (c == 0xA)
|| (c == 0xD)
|| ((c >= 0x20) && (c <= 0xD7FF))
|| ((c >= 0xE000) && (c <= 0xFFFD)))
/*|| ((c >= 0x10000) && (c <= 0x10FFFF))*/
);
}
/// <summary>
/// Removes characters from the left side of a string.
/// </summary>
/// <param name="self">The string to be acted upon.</param>
/// <param name="amount">The number of charcters to remove.</param>
/// <returns>Returns a new string with the characters removed.</returns>
public static string TrimLeftBy(this string self, int amount) {
return self.Substring(amount);
}
/// <summary>
/// Removes characters from the right side of a string.
/// </summary>
/// <param name="self">The string to be acted upon.</param>
/// <param name="amount">The number of charcters to remove.</param>
/// <returns>Returns a new string with the characters removed.</returns>
public static string TrimRightBy(this string self, int amount) {
return self.Substring(0, self.Length - amount);
}
/// <summary>
/// Removes characters from the left and right sides of a string.
/// </summary>
/// <param name="self">The string to be acted upon.</param>
/// <param name="amount">The number of charcters to remove.</param>
/// <returns>Returns a new string with the characters removed.</returns>
public static string TrimEndsBy(this string self, int amount) {
return self.Substring(amount, self.Length - amount);
}
/// <summary>
/// Generates a simple hash for a string.
/// </summary>
/// <remarks>
/// This hash is not asserted to be fit for any particular purpose
/// other than simple features where you just need a hash of a string.
/// </remarks>
/// <param name="self">The string to be acted upon.</param>
/// <returns>
/// Returns a simple hash of a string.
/// </returns>
public static string Hash(this string self) {
MD5 md5Hasher = MD5.Create();
byte[] data = md5Hasher.ComputeHash(System.Text.Encoding.Default.GetBytes(self));
string hash = BitConverter.ToString(data);
return hash;
}
/// <summary>
/// Regards all occurrences of substrings starting and finishing with `|`
/// pipe charcters as potential keys, and if those keys occur within the
/// provided dictionary, replaces those keys in the provided text
/// with the corresposponding value in the dictionary.
/// </summary>
/// <remarks>
/// This is performed as a single scan of characters and should be used
/// in preference in those situations where you find yourself
/// doing multiple replacements on a large string, as this will do them in one go.
/// </remarks>
/// <param name="text">The text to act upon.</param>
/// <param name="kv">The dictionary of key-value pairs for substitution.</param>
/// <returns>Returns a new string with any matching keys replaced.</returns>
public static string ReplaceKeys(this string text, IDictionary<string, string> kv) {
bool readingKey = false;
StringBuilder currentKey = new StringBuilder();
StringBuilder result = new StringBuilder();
foreach (char c in text) {
if (readingKey) {
if (c == '|') { // finish reading key
if (kv.ContainsKey(currentKey.ToString())) {
result.Append(kv[currentKey.ToString()]);
}
readingKey = false;
} else { // reading the key
currentKey.Append(c);
}
} else {
if (c == '|') { // start reading key
readingKey = true;
currentKey.Clear();
} else { // just reading regular text
result.Append(c);
}
}
}
return result.ToString();
}
/// <summary>
/// Loads the string into an xml document.
/// </summary>
/// <param name="self">The string being acted upon.</param>
/// <returns>
/// Returns an xml document with the string loaded.
/// </returns>
public static XmlDocument AsXmlDocument(this string self) {
XmlDocument xml = new XmlDocument();
xml.LoadXml(self);
return xml;
}
/// <summary>
/// Loads the string into an XDocument.
/// </summary>
/// <param name="self">The string being acted upon.</param>
/// <returns>
/// Returns an XDocument with the string loaded.
/// </returns>
public static XDocument AsXDocument(this string self) {
XDocument xml = XDocument.Parse(self);
return xml;
}
/// <summary>
/// Loads the string into an XElement.
/// </summary>
/// <param name="self">The string being acted upon.</param>
/// <returns>Returns an XElement with the string loaded.</returns>
public static XElement AsXElement(this string self) {
XElement xml = XElement.Parse(self);
return xml;
}
/// <summary>
/// Loads the string into a JObject.
/// </summary>
/// <param name="self">The string being acted upon.</param>
/// <returns>Returns a JObject with the string loaded.</returns>
public static JObject AsJObject(this string self) {
return JObject.Parse(self);
}
}
}