Skip to content

Commit 59d289e

Browse files
committed
modified SB Ext & new icon
1 parent f56e86e commit 59d289e

2 files changed

Lines changed: 56 additions & 4 deletions

File tree

AlgorithmsLibrary/Extensions/StringBuilderExtensions.cs

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Text;
1+
using System;
2+
using System.Text;
23

34
namespace AlgorithmsLibrary.StringBuilderExtensions
45
{
@@ -14,12 +15,63 @@ public static bool Contains(this StringBuilder @this, string match)
1415
}
1516
public static string Substring(this StringBuilder @this, int startpos, int length)
1617
{
17-
return @this.ToString().Substring(startpos, length);
18+
StringBuilder stringBuilder = new StringBuilder();
19+
for (int i = startpos; i < length; i++)
20+
{
21+
stringBuilder.Append(@this[i]);
22+
}
23+
return stringBuilder.ToString();
24+
//@this.ToString().Substring(startpos, length);
1825
}
1926

20-
public static int IndexOf(this StringBuilder @this, string match)
27+
public static int IndexOf(this StringBuilder haystack, string needle)
2128
{
22-
return @this.ToString().IndexOf(match);
29+
if (haystack == null || needle == null)
30+
throw new ArgumentNullException();
31+
if (needle.Length == 0)
32+
return 0;//empty strings are everywhere!
33+
if (needle.Length == 1)//can't beat just spinning through for it
34+
{
35+
char c = needle[0];
36+
for (int idx = 0; idx != haystack.Length; ++idx)
37+
if (haystack[idx] == c)
38+
return idx;
39+
return -1;
40+
}
41+
int m = 0;
42+
int i = 0;
43+
int[] T = KMPTable(needle);
44+
while (m + i < haystack.Length)
45+
{
46+
if (needle[i] == haystack[m + i])
47+
{
48+
if (i == needle.Length - 1)
49+
return m == needle.Length ? -1 : m;//match -1 = failure to find conventional in .NET
50+
++i;
51+
}
52+
else
53+
{
54+
m = m + i - T[i];
55+
i = T[i] > -1 ? T[i] : 0;
56+
}
57+
}
58+
return -1;
59+
}
60+
private static int[] KMPTable(string sought)
61+
{
62+
int[] table = new int[sought.Length];
63+
int pos = 2;
64+
int cnd = 0;
65+
table[0] = -1;
66+
table[1] = 0;
67+
while (pos < table.Length)
68+
if (sought[pos - 1] == sought[cnd])
69+
table[pos++] = ++cnd;
70+
else if (cnd > 0)
71+
cnd = table[cnd];
72+
else
73+
table[pos++] = 0;
74+
return table;
2375
}
2476
}
2577
}

AlgorithmsWpf/fire.ico

468 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)