forked from TestableIO/System.IO.Abstractions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringExtensions.cs
59 lines (51 loc) · 1.67 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
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Text;
namespace System.IO.Abstractions.TestingHelpers
{
internal static class StringExtensions
{
[Pure]
internal static string[] SplitLines(this string input)
{
var list = new List<string>();
using (var reader = new StringReader(input))
{
string str;
while ((str = reader.ReadLine()) != null)
{
list.Add(str);
}
}
return list.ToArray();
}
[Pure]
public static string Replace(this string source, string oldValue, string newValue, StringComparison comparisonType)
{
// from http://stackoverflow.com/a/22565605 with some adaptions
if (string.IsNullOrEmpty(oldValue))
{
throw new ArgumentNullException("oldValue");
}
if (source.Length == 0)
{
return source;
}
if (newValue == null)
{
newValue = string.Empty;
}
var result = new StringBuilder();
int startingPos = 0;
int nextMatch;
while ((nextMatch = source.IndexOf(oldValue, startingPos, comparisonType)) > -1)
{
result.Append(source, startingPos, nextMatch - startingPos);
result.Append(newValue);
startingPos = nextMatch + oldValue.Length;
}
result.Append(source, startingPos, source.Length - startingPos);
return result.ToString();
}
}
}