Skip to content

Commit 98952af

Browse files
author
Oren (electricessence)
committed
Reformat and contract improvments.
1 parent 6a8044a commit 98952af

1 file changed

Lines changed: 51 additions & 31 deletions

File tree

Text.cs

Lines changed: 51 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
using System;
77
using System.Collections.Generic;
8+
using System.Diagnostics.Contracts;
89
using System.IO;
910
using System.Linq;
1011
using System.Text;
@@ -13,7 +14,7 @@
1314

1415
namespace Open.Text
1516
{
16-
public static class Extensions
17+
public static class Extensions
1718
{
1819
private const uint BYTE_RED = 1024;
1920
private static readonly string[] _byte_labels = new[] { "KB", "MB", "GB", "TB" };
@@ -31,6 +32,7 @@ public static string BeforeFirst(this string source, string search, StringCompar
3132
{
3233
if (source == null)
3334
throw new NullReferenceException();
35+
Contract.EndContractBlock();
3436

3537
if (String.IsNullOrEmpty(search))
3638
return null;
@@ -50,6 +52,7 @@ public static string AfterFirst(this string source, string search, StringCompari
5052
{
5153
if (source == null)
5254
throw new NullReferenceException();
55+
Contract.EndContractBlock();
5356

5457
if (String.IsNullOrEmpty(search))
5558
return null;
@@ -69,6 +72,7 @@ public static string BeforeLast(this string source, string search, StringCompari
6972
{
7073
if (source == null)
7174
throw new NullReferenceException();
75+
Contract.EndContractBlock();
7276

7377
if (String.IsNullOrEmpty(search))
7478
return null;
@@ -88,6 +92,7 @@ public static string AfterLast(this string source, string search, StringComparis
8892
{
8993
if (source == null)
9094
throw new NullReferenceException();
95+
Contract.EndContractBlock();
9196

9297
if (String.IsNullOrEmpty(search))
9398
return null;
@@ -105,6 +110,7 @@ public static string ToTitleCase(this string source)
105110
{
106111
if (source == null)
107112
throw new NullReferenceException();
113+
Contract.EndContractBlock();
108114

109115
return ToTitleCaseRegex.Replace(
110116
source.ToLowerInvariant(),
@@ -125,6 +131,7 @@ public static string AssertIsNotNullOrWhiteSpace(this string source)
125131
{
126132
if (String.IsNullOrWhiteSpace(source))
127133
throw new ArgumentException();
134+
Contract.EndContractBlock();
128135

129136
return source;
130137
}
@@ -188,10 +195,11 @@ public static bool IsAlphaNumeric(this string source, bool trim = false)
188195
#region Regex helper methods.
189196
public static string GetValue(this GroupCollection groups, string groupName, bool throwIfInvalid = false)
190197
{
191-
if (groups == null)
198+
if (groups == null)
192199
throw new NullReferenceException();
193-
if (groupName == null)
194-
throw new ArgumentNullException("groupName");
200+
if (groupName == null)
201+
throw new ArgumentNullException(nameof(groupName));
202+
Contract.EndContractBlock();
195203

196204
var group = groups[groupName];
197205
if (group == null)
@@ -212,8 +220,9 @@ public static string GetValue(this GroupCollection groups, string groupName, boo
212220
/// </summary>
213221
public static StringBuilder AppendAll<T>(this StringBuilder target, IEnumerable<T> values, string separator = null)
214222
{
215-
if (target == null)
223+
if (target == null)
216224
throw new NullReferenceException();
225+
Contract.EndContractBlock();
217226

218227
if (values != null)
219228
{
@@ -236,8 +245,9 @@ public static StringBuilder AppendAll<T>(this StringBuilder target, IEnumerable<
236245
/// </summary>
237246
public static StringBuilder AppendAll<T>(this StringBuilder target, IEnumerable<T> values, char separator)
238247
{
239-
if (target == null)
248+
if (target == null)
240249
throw new NullReferenceException();
250+
Contract.EndContractBlock();
241251

242252
if (values != null)
243253
foreach (var value in values)
@@ -251,10 +261,11 @@ public static StringBuilder AppendAll<T>(this StringBuilder target, IEnumerable<
251261
/// </summary>
252262
public static StringBuilder AppendWithSeparator<T>(this StringBuilder target, string separator, params T[] values)
253263
{
254-
if (target == null)
264+
if (target == null)
255265
throw new NullReferenceException();
256-
if (values==null || values.Length==0)
266+
if (values == null || values.Length == 0)
257267
throw new ArgumentException("Parameters missing.");
268+
Contract.EndContractBlock();
258269

259270
if (!String.IsNullOrEmpty(separator) && target.Length != 0)
260271
target.Append(separator);
@@ -268,10 +279,11 @@ public static StringBuilder AppendWithSeparator<T>(this StringBuilder target, st
268279
/// </summary>
269280
public static StringBuilder AppendWithSeparator<T>(this StringBuilder target, char separator, params T[] values)
270281
{
271-
if (target == null)
282+
if (target == null)
272283
throw new NullReferenceException();
273-
if (values==null || values.Length==0)
284+
if (values == null || values.Length == 0)
274285
throw new ArgumentException("Parameters missing.");
286+
Contract.EndContractBlock();
275287

276288
if (target.Length != 0)
277289
target.Append(separator);
@@ -285,22 +297,23 @@ public static StringBuilder AppendWithSeparator<T>(this StringBuilder target, ch
285297
/// </summary>
286298
public static void AppendWithSeparator<T>(this StringBuilder target, IDictionary<string, T> source, string key, string itemSeparator, string keyValueSeparator)
287299
{
288-
if (target == null)
300+
if (target == null)
289301
throw new NullReferenceException();
290-
if (source==null)
291-
throw new ArgumentNullException("source");
292-
if (key==null)
293-
throw new ArgumentNullException("key");
294-
if (itemSeparator==null)
295-
throw new ArgumentNullException("itemSeparator");
296-
if (keyValueSeparator==null)
297-
throw new ArgumentNullException("keyValueSeparator");
298-
299-
if(source.TryGetValue(key, out T result))
302+
if (source == null)
303+
throw new ArgumentNullException(nameof(source));
304+
if (key == null)
305+
throw new ArgumentNullException(nameof(key));
306+
if (itemSeparator == null)
307+
throw new ArgumentNullException(nameof(itemSeparator));
308+
if (keyValueSeparator == null)
309+
throw new ArgumentNullException(nameof(keyValueSeparator));
310+
Contract.EndContractBlock();
311+
312+
if (source.TryGetValue(key, out T result))
300313
target
301-
.AppendWithSeparator(itemSeparator, key)
302-
.Append(keyValueSeparator)
303-
.Append(result);
314+
.AppendWithSeparator(itemSeparator, key)
315+
.Append(keyValueSeparator)
316+
.Append(result);
304317
}
305318
#endregion
306319

@@ -311,8 +324,9 @@ public static void AppendWithSeparator<T>(this StringBuilder target, IDictionary
311324
/// </summary>
312325
public static string ToString(this double? value, string format)
313326
{
314-
if (format==null)
315-
throw new ArgumentNullException("format");
327+
if (format == null)
328+
throw new ArgumentNullException(nameof(format));
329+
Contract.EndContractBlock();
316330

317331
return value.HasValue ? value.Value.ToString(format) : double.NaN.ToString(format);
318332
}
@@ -323,8 +337,9 @@ public static string ToString(this double? value, string format)
323337
/// </summary>
324338
public static string ToString(this float? value, string format)
325339
{
326-
if (format==null)
327-
throw new ArgumentNullException("format");
340+
if (format == null)
341+
throw new ArgumentNullException(nameof(format));
342+
Contract.EndContractBlock();
328343

329344
return value.HasValue ? value.Value.ToString(format) : float.NaN.ToString(format);
330345
}
@@ -334,8 +349,9 @@ public static string ToString(this float? value, string format)
334349
/// </summary>
335350
public static string ToString(this int? value, string format)
336351
{
337-
if (format==null)
338-
throw new ArgumentNullException("format");
352+
if (format == null)
353+
throw new ArgumentNullException(nameof(format));
354+
Contract.EndContractBlock();
339355

340356
return value.HasValue ? value.Value.ToString(format) : 0.ToString(format);
341357
}
@@ -426,7 +442,8 @@ public static string ReplaceWhiteSpace(this string source, string replace = " ")
426442
if (source == null)
427443
throw new NullReferenceException();
428444
if (replace == null)
429-
throw new ArgumentNullException("replace");
445+
throw new ArgumentNullException(nameof(replace));
446+
Contract.EndContractBlock();
430447

431448
return WHITESPACE.Replace(source, replace);
432449
}
@@ -435,6 +452,7 @@ public static string TrimStart(this string source, string pattern)
435452
{
436453
if (source == null)
437454
throw new NullReferenceException();
455+
Contract.EndContractBlock();
438456

439457
if (pattern == null)
440458
return source.TrimStart();
@@ -451,6 +469,7 @@ public static string TrimEnd(this string source, string pattern)
451469
{
452470
if (source == null)
453471
throw new NullReferenceException();
472+
Contract.EndContractBlock();
454473

455474
if (pattern == null)
456475
return source.TrimEnd();
@@ -473,6 +492,7 @@ public static void WriteLineNoTabs(this TextWriter writer, string s = null)
473492
{
474493
if (writer == null)
475494
throw new NullReferenceException();
495+
Contract.EndContractBlock();
476496

477497
if (s != null)
478498
writer.Write(s);

0 commit comments

Comments
 (0)