Added String.IndexOf(string)

Added String.Insert(int, string)
Added String.Replace(string, string)
This commit is contained in:
Scalpel_cp 2008-09-07 15:00:51 +00:00
parent 768d83e799
commit 697bbbdf06
3 changed files with 31 additions and 52 deletions

View file

@ -141,6 +141,12 @@ namespace Cosmos.Kernel.Plugs {
return false;
}
public static int IndexOf(string aThis,
string aValue)
{
return aThis.IndexOf(aValue, 0, aThis.Length, StringComparison.CurrentCulture);
}
public static int IndexOf(string aThis,
char aSeparator,
int aStartIndex,
@ -170,6 +176,11 @@ namespace Cosmos.Kernel.Plugs {
return xResult;
}
public static string Insert(string aThis, int aStartPos, string aValue)
{
return aThis.Substring(0, aStartPos) + aValue + aThis.Substring(aStartPos);
}
public static int LastIndexOf(string aThis, char aChar, int aStartIndex, int aCount)
{
return LastIndexOfAny(aThis, new char[] {aChar}, aStartIndex, aCount);
@ -212,44 +223,15 @@ namespace Cosmos.Kernel.Plugs {
return aThis.Substring(0, aStart) + aThis.Substring(aStart+aCount, aThis.Length-(aStart+aCount));
}
//private static string[] Split(string aThis,
// char[] aSeparators,
// StringSplitOptions aSplitOptions)
//{
// List<string> xResult = new List<string>();
// int xCurPos = 0;
// if (CharArrayContainsChar(aSeparators,
// aThis[0]))
// {
// xCurPos = 1;
// if (aSplitOptions == StringSplitOptions.None)
// {
// xResult.Add("");
// }
// }
// while (xCurPos < aThis.Length)
// {
// int xNextPos = String.IndexOfAny(aThis,
// aSeparators,
// xCurPos, aThis.Length - xCurPos);
// if (xNextPos == -1)
// {
// xResult.Add(aThis.Substring(xCurPos));
// break;
// }
// if (xNextPos == xCurPos)
// {
// if (aSplitOptions == StringSplitOptions.None)
// {
// xResult.Add("");
// }
// xCurPos = xNextPos + 1;
// }
// xResult.Add(aThis.Substring(xCurPos,
// xNextPos - xCurPos));
// xCurPos = xNextPos + 1;
// }
// return xResult.ToArray();
//}
public static string Replace(string aThis, string oldValue, string newValue)
{
while (aThis.IndexOf(oldValue) != -1)
{
int xIndex = aThis.IndexOf(oldValue);
aThis = aThis.Remove(xIndex, oldValue.Length);
aThis = aThis.Insert(xIndex, newValue);
}
return aThis;
}
}
}

View file

@ -71,13 +71,15 @@ namespace FrodeTest.Test
Check.Text = "String.IndexOf";
Check.Validate("test".IndexOf('t', 0, 2) == 0);
Check.Validate("test".IndexOf('B', 1, 1) == -1);
Check.Validate("test".IndexOf("st") == 2);
Check.Validate("test".IndexOf("es", 1, 3, StringComparison.CurrentCulture) == 1);
Check.Text = "String.IndexOfAny";
Check.Validate("test".IndexOfAny(new char[] { 'a', 'b' }, 0, 4) == -1);
Check.Validate("test".IndexOfAny(new char[] { 'e', 's' }, 0, 4) == 1);
//Check.Text = "String.Insert";
//Check.Validate("Hello".Insert(5, " World").Equals("Hello World"));
Check.Text = "String.Insert";
Check.Validate("Hello".Insert(5, " World").Equals("Hello World"));
//Check.Text = "String.IsNormalized";
//Check.Validate("test".IsNormalized());
@ -85,6 +87,9 @@ namespace FrodeTest.Test
Check.Text = "String.LastIndexOf";
Check.Validate("Readme.txt".LastIndexOf('.') == 6);
Console.WriteLine("Press any key to continue");
Console.ReadLine();
Check.Text = "String.LastIndexOfAny";
Check.Validate("test".LastIndexOfAny(new char[] { 'a', 'b' }, 0, 4) == -1);
Check.Validate("test".LastIndexOfAny(new char[] { 'a', 't' }, 3, 4) == 3);
@ -105,10 +110,11 @@ namespace FrodeTest.Test
Check.Validate("test".Remove(2).Equals("te"));
Check.Validate("test".Remove(1, 2).Equals("tt"));
Check.Text = "String.Replace(char, char)";
Check.Validate("test".Replace('t', 'p').Equals("pesp"));
Check.Text = "String.Replace(string, string)";
//Check.Validate("test".Replace("es", "amti").Equals("tamtit")); //Uses .Insert - fix that first.
Check.Validate("test".Replace("es", "amti").Equals("tamtit")); //Uses .Insert - fix that first.
Check.Text = "String.Split";
Check.Validate("Hello World".Split(new string[] { "l" }, 30, StringSplitOptions.RemoveEmptyEntries).Length == 3);

View file

@ -96,16 +96,7 @@ namespace Indy.IL2CPU.CustomImplementation.System {
return new string(cs);
}
public static string Replace(string aThis, string oldValue, string newValue)
{
while (aThis.IndexOf(oldValue) != -1)
{
int xIndex = aThis.IndexOf(oldValue);
aThis.Remove(xIndex, oldValue.Length);
aThis.Insert(xIndex, newValue);
}
return aThis;
}
// HACK: We need to redo this once char support is complete (only returns 0, -1).
public static int CompareTo(string aThis, string other) {