From c26a2bcd95cb146d3712da342515de59782e5df5 Mon Sep 17 00:00:00 2001 From: Cyber Date: Wed, 16 Dec 2015 16:42:15 -0500 Subject: [PATCH 1/9] Fixed Broken String Functions fixed StartsWith, Contains, and EndsWith --- .../Cosmos.System.Plugs.csproj | 13 ++- .../Cosmos.System.Plugs/System/StringImpl.cs | 96 +++++++++++++++++++ 2 files changed, 105 insertions(+), 4 deletions(-) create mode 100644 source/Cosmos.System.Plugs/System/StringImpl.cs diff --git a/source/Cosmos.System.Plugs/Cosmos.System.Plugs.csproj b/source/Cosmos.System.Plugs/Cosmos.System.Plugs.csproj index 28b7edbad..4b1ce68e7 100644 --- a/source/Cosmos.System.Plugs/Cosmos.System.Plugs.csproj +++ b/source/Cosmos.System.Plugs/Cosmos.System.Plugs.csproj @@ -12,10 +12,14 @@ Cosmos.System.Plugs v4.5 512 - SAK - SAK - SAK - SAK + + + + + + + + true Cosmos.snk @@ -82,6 +86,7 @@ + diff --git a/source/Cosmos.System.Plugs/System/StringImpl.cs b/source/Cosmos.System.Plugs/System/StringImpl.cs new file mode 100644 index 000000000..cd4f94655 --- /dev/null +++ b/source/Cosmos.System.Plugs/System/StringImpl.cs @@ -0,0 +1,96 @@ +using Cosmos.IL2CPU.Plugs; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Plug = Cosmos.IL2CPU.Plugs.PlugAttribute; + +namespace Cosmos.System.Plugs.System +{ + [@Plug(Target = typeof(string))] + public static class StringImpl + { + public static bool StartsWith(this string str, string value) + { + Char[] di = str.ToCharArray(); + Char[] ci = value.ToCharArray(); + if (value.Length > str.Length) + { + return false; + } + for (int i = 0; i < ci.Length; i++) + { + if (di[i] != ci[i]) + { + return false; + + } + } + return true; + } + + public static bool Contains(this string str, string comp) + { + Char[] di = str.ToCharArray(); + Char[] ci = comp.ToCharArray(); + if (comp.Length == str.Length) + { + if (comp == str) + { + return true; + } + else + { + return false; + } + } + else if (!(comp.Length > str.Length) && (comp.Length != str.Length)) + { + for (int i = 0; i < str.Length; i++) + { + if (di[i] == ci[0]) + { + for (int j = 1; j < comp.Length; j++) + { + if (di[i + j] != ci[j]) + { + return false; + } + } + return true; + } + } + } + return false; + } + public static bool EndsWith(this string str, string comp) + { + Char[] di = str.ToCharArray(); + Char[] ci = comp.ToCharArray(); + if(str.Length == comp.Length) + { + if(str == comp) + { + return true; + } + return false; + } + else if(comp.Length > str.Length) + { + return false; + } + else + { + for (int i = str.Length - comp.Length; i < str.Length; i++) + { + if (di[str.Length - comp.Length + i] != ci[i]) + { + return false; + } + } + return true; + } + } + } +} From 1b4bdaec6dce248682c7e36e58280c075a02ce0c Mon Sep 17 00:00:00 2001 From: Cyber Date: Wed, 16 Dec 2015 16:44:37 -0500 Subject: [PATCH 2/9] Revert "Fixed Broken String Functions" This reverts commit c26a2bcd95cb146d3712da342515de59782e5df5. --- .../Cosmos.System.Plugs.csproj | 13 +-- .../Cosmos.System.Plugs/System/StringImpl.cs | 96 ------------------- 2 files changed, 4 insertions(+), 105 deletions(-) delete mode 100644 source/Cosmos.System.Plugs/System/StringImpl.cs diff --git a/source/Cosmos.System.Plugs/Cosmos.System.Plugs.csproj b/source/Cosmos.System.Plugs/Cosmos.System.Plugs.csproj index 4b1ce68e7..28b7edbad 100644 --- a/source/Cosmos.System.Plugs/Cosmos.System.Plugs.csproj +++ b/source/Cosmos.System.Plugs/Cosmos.System.Plugs.csproj @@ -12,14 +12,10 @@ Cosmos.System.Plugs v4.5 512 - - - - - - - - + SAK + SAK + SAK + SAK true Cosmos.snk @@ -86,7 +82,6 @@ - diff --git a/source/Cosmos.System.Plugs/System/StringImpl.cs b/source/Cosmos.System.Plugs/System/StringImpl.cs deleted file mode 100644 index cd4f94655..000000000 --- a/source/Cosmos.System.Plugs/System/StringImpl.cs +++ /dev/null @@ -1,96 +0,0 @@ -using Cosmos.IL2CPU.Plugs; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Plug = Cosmos.IL2CPU.Plugs.PlugAttribute; - -namespace Cosmos.System.Plugs.System -{ - [@Plug(Target = typeof(string))] - public static class StringImpl - { - public static bool StartsWith(this string str, string value) - { - Char[] di = str.ToCharArray(); - Char[] ci = value.ToCharArray(); - if (value.Length > str.Length) - { - return false; - } - for (int i = 0; i < ci.Length; i++) - { - if (di[i] != ci[i]) - { - return false; - - } - } - return true; - } - - public static bool Contains(this string str, string comp) - { - Char[] di = str.ToCharArray(); - Char[] ci = comp.ToCharArray(); - if (comp.Length == str.Length) - { - if (comp == str) - { - return true; - } - else - { - return false; - } - } - else if (!(comp.Length > str.Length) && (comp.Length != str.Length)) - { - for (int i = 0; i < str.Length; i++) - { - if (di[i] == ci[0]) - { - for (int j = 1; j < comp.Length; j++) - { - if (di[i + j] != ci[j]) - { - return false; - } - } - return true; - } - } - } - return false; - } - public static bool EndsWith(this string str, string comp) - { - Char[] di = str.ToCharArray(); - Char[] ci = comp.ToCharArray(); - if(str.Length == comp.Length) - { - if(str == comp) - { - return true; - } - return false; - } - else if(comp.Length > str.Length) - { - return false; - } - else - { - for (int i = str.Length - comp.Length; i < str.Length; i++) - { - if (di[str.Length - comp.Length + i] != ci[i]) - { - return false; - } - } - return true; - } - } - } -} From 0a5a46aac914e2f02521a46af63d060a05464fa7 Mon Sep 17 00:00:00 2001 From: Cyber Date: Wed, 16 Dec 2015 16:44:52 -0500 Subject: [PATCH 3/9] Revert "Revert "Fixed Broken String Functions"" This reverts commit 1b4bdaec6dce248682c7e36e58280c075a02ce0c. --- .../Cosmos.System.Plugs.csproj | 13 ++- .../Cosmos.System.Plugs/System/StringImpl.cs | 96 +++++++++++++++++++ 2 files changed, 105 insertions(+), 4 deletions(-) create mode 100644 source/Cosmos.System.Plugs/System/StringImpl.cs diff --git a/source/Cosmos.System.Plugs/Cosmos.System.Plugs.csproj b/source/Cosmos.System.Plugs/Cosmos.System.Plugs.csproj index 28b7edbad..4b1ce68e7 100644 --- a/source/Cosmos.System.Plugs/Cosmos.System.Plugs.csproj +++ b/source/Cosmos.System.Plugs/Cosmos.System.Plugs.csproj @@ -12,10 +12,14 @@ Cosmos.System.Plugs v4.5 512 - SAK - SAK - SAK - SAK + + + + + + + + true Cosmos.snk @@ -82,6 +86,7 @@ + diff --git a/source/Cosmos.System.Plugs/System/StringImpl.cs b/source/Cosmos.System.Plugs/System/StringImpl.cs new file mode 100644 index 000000000..cd4f94655 --- /dev/null +++ b/source/Cosmos.System.Plugs/System/StringImpl.cs @@ -0,0 +1,96 @@ +using Cosmos.IL2CPU.Plugs; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Plug = Cosmos.IL2CPU.Plugs.PlugAttribute; + +namespace Cosmos.System.Plugs.System +{ + [@Plug(Target = typeof(string))] + public static class StringImpl + { + public static bool StartsWith(this string str, string value) + { + Char[] di = str.ToCharArray(); + Char[] ci = value.ToCharArray(); + if (value.Length > str.Length) + { + return false; + } + for (int i = 0; i < ci.Length; i++) + { + if (di[i] != ci[i]) + { + return false; + + } + } + return true; + } + + public static bool Contains(this string str, string comp) + { + Char[] di = str.ToCharArray(); + Char[] ci = comp.ToCharArray(); + if (comp.Length == str.Length) + { + if (comp == str) + { + return true; + } + else + { + return false; + } + } + else if (!(comp.Length > str.Length) && (comp.Length != str.Length)) + { + for (int i = 0; i < str.Length; i++) + { + if (di[i] == ci[0]) + { + for (int j = 1; j < comp.Length; j++) + { + if (di[i + j] != ci[j]) + { + return false; + } + } + return true; + } + } + } + return false; + } + public static bool EndsWith(this string str, string comp) + { + Char[] di = str.ToCharArray(); + Char[] ci = comp.ToCharArray(); + if(str.Length == comp.Length) + { + if(str == comp) + { + return true; + } + return false; + } + else if(comp.Length > str.Length) + { + return false; + } + else + { + for (int i = str.Length - comp.Length; i < str.Length; i++) + { + if (di[str.Length - comp.Length + i] != ci[i]) + { + return false; + } + } + return true; + } + } + } +} From 224ef150009066308f904085ee92ae4b7f2b7e55 Mon Sep 17 00:00:00 2001 From: Cyber Date: Wed, 16 Dec 2015 16:45:11 -0500 Subject: [PATCH 4/9] Revert "Revert "Revert "Fixed Broken String Functions""" This reverts commit 0a5a46aac914e2f02521a46af63d060a05464fa7. --- .../Cosmos.System.Plugs.csproj | 13 +-- .../Cosmos.System.Plugs/System/StringImpl.cs | 96 ------------------- 2 files changed, 4 insertions(+), 105 deletions(-) delete mode 100644 source/Cosmos.System.Plugs/System/StringImpl.cs diff --git a/source/Cosmos.System.Plugs/Cosmos.System.Plugs.csproj b/source/Cosmos.System.Plugs/Cosmos.System.Plugs.csproj index 4b1ce68e7..28b7edbad 100644 --- a/source/Cosmos.System.Plugs/Cosmos.System.Plugs.csproj +++ b/source/Cosmos.System.Plugs/Cosmos.System.Plugs.csproj @@ -12,14 +12,10 @@ Cosmos.System.Plugs v4.5 512 - - - - - - - - + SAK + SAK + SAK + SAK true Cosmos.snk @@ -86,7 +82,6 @@ - diff --git a/source/Cosmos.System.Plugs/System/StringImpl.cs b/source/Cosmos.System.Plugs/System/StringImpl.cs deleted file mode 100644 index cd4f94655..000000000 --- a/source/Cosmos.System.Plugs/System/StringImpl.cs +++ /dev/null @@ -1,96 +0,0 @@ -using Cosmos.IL2CPU.Plugs; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Plug = Cosmos.IL2CPU.Plugs.PlugAttribute; - -namespace Cosmos.System.Plugs.System -{ - [@Plug(Target = typeof(string))] - public static class StringImpl - { - public static bool StartsWith(this string str, string value) - { - Char[] di = str.ToCharArray(); - Char[] ci = value.ToCharArray(); - if (value.Length > str.Length) - { - return false; - } - for (int i = 0; i < ci.Length; i++) - { - if (di[i] != ci[i]) - { - return false; - - } - } - return true; - } - - public static bool Contains(this string str, string comp) - { - Char[] di = str.ToCharArray(); - Char[] ci = comp.ToCharArray(); - if (comp.Length == str.Length) - { - if (comp == str) - { - return true; - } - else - { - return false; - } - } - else if (!(comp.Length > str.Length) && (comp.Length != str.Length)) - { - for (int i = 0; i < str.Length; i++) - { - if (di[i] == ci[0]) - { - for (int j = 1; j < comp.Length; j++) - { - if (di[i + j] != ci[j]) - { - return false; - } - } - return true; - } - } - } - return false; - } - public static bool EndsWith(this string str, string comp) - { - Char[] di = str.ToCharArray(); - Char[] ci = comp.ToCharArray(); - if(str.Length == comp.Length) - { - if(str == comp) - { - return true; - } - return false; - } - else if(comp.Length > str.Length) - { - return false; - } - else - { - for (int i = str.Length - comp.Length; i < str.Length; i++) - { - if (di[str.Length - comp.Length + i] != ci[i]) - { - return false; - } - } - return true; - } - } - } -} From 64440ba3c12b70c3b4e77c5cf75245ac8199f43e Mon Sep 17 00:00:00 2001 From: Cyber Date: Wed, 16 Dec 2015 16:47:07 -0500 Subject: [PATCH 5/9] Fixed Broken String Comparison Functions StartsWith() Contains() EndsWith() --- .../Cosmos.System.Plugs.csproj | 13 ++- .../Cosmos.System.Plugs/System/StringImpl.cs | 96 +++++++++++++++++++ 2 files changed, 105 insertions(+), 4 deletions(-) create mode 100644 source/Cosmos.System.Plugs/System/StringImpl.cs diff --git a/source/Cosmos.System.Plugs/Cosmos.System.Plugs.csproj b/source/Cosmos.System.Plugs/Cosmos.System.Plugs.csproj index 28b7edbad..4b1ce68e7 100644 --- a/source/Cosmos.System.Plugs/Cosmos.System.Plugs.csproj +++ b/source/Cosmos.System.Plugs/Cosmos.System.Plugs.csproj @@ -12,10 +12,14 @@ Cosmos.System.Plugs v4.5 512 - SAK - SAK - SAK - SAK + + + + + + + + true Cosmos.snk @@ -82,6 +86,7 @@ + diff --git a/source/Cosmos.System.Plugs/System/StringImpl.cs b/source/Cosmos.System.Plugs/System/StringImpl.cs new file mode 100644 index 000000000..cd4f94655 --- /dev/null +++ b/source/Cosmos.System.Plugs/System/StringImpl.cs @@ -0,0 +1,96 @@ +using Cosmos.IL2CPU.Plugs; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Plug = Cosmos.IL2CPU.Plugs.PlugAttribute; + +namespace Cosmos.System.Plugs.System +{ + [@Plug(Target = typeof(string))] + public static class StringImpl + { + public static bool StartsWith(this string str, string value) + { + Char[] di = str.ToCharArray(); + Char[] ci = value.ToCharArray(); + if (value.Length > str.Length) + { + return false; + } + for (int i = 0; i < ci.Length; i++) + { + if (di[i] != ci[i]) + { + return false; + + } + } + return true; + } + + public static bool Contains(this string str, string comp) + { + Char[] di = str.ToCharArray(); + Char[] ci = comp.ToCharArray(); + if (comp.Length == str.Length) + { + if (comp == str) + { + return true; + } + else + { + return false; + } + } + else if (!(comp.Length > str.Length) && (comp.Length != str.Length)) + { + for (int i = 0; i < str.Length; i++) + { + if (di[i] == ci[0]) + { + for (int j = 1; j < comp.Length; j++) + { + if (di[i + j] != ci[j]) + { + return false; + } + } + return true; + } + } + } + return false; + } + public static bool EndsWith(this string str, string comp) + { + Char[] di = str.ToCharArray(); + Char[] ci = comp.ToCharArray(); + if(str.Length == comp.Length) + { + if(str == comp) + { + return true; + } + return false; + } + else if(comp.Length > str.Length) + { + return false; + } + else + { + for (int i = str.Length - comp.Length; i < str.Length; i++) + { + if (di[str.Length - comp.Length + i] != ci[i]) + { + return false; + } + } + return true; + } + } + } +} From f8fabfd49e31b88837827d5d2b665bef4ebe89f5 Mon Sep 17 00:00:00 2001 From: Cyber Date: Wed, 16 Dec 2015 21:11:29 -0500 Subject: [PATCH 6/9] Revert "Fixed Broken String Comparison Functions" This reverts commit 64440ba3c12b70c3b4e77c5cf75245ac8199f43e. --- .../Cosmos.System.Plugs.csproj | 13 +-- .../Cosmos.System.Plugs/System/StringImpl.cs | 96 ------------------- 2 files changed, 4 insertions(+), 105 deletions(-) delete mode 100644 source/Cosmos.System.Plugs/System/StringImpl.cs diff --git a/source/Cosmos.System.Plugs/Cosmos.System.Plugs.csproj b/source/Cosmos.System.Plugs/Cosmos.System.Plugs.csproj index 4b1ce68e7..28b7edbad 100644 --- a/source/Cosmos.System.Plugs/Cosmos.System.Plugs.csproj +++ b/source/Cosmos.System.Plugs/Cosmos.System.Plugs.csproj @@ -12,14 +12,10 @@ Cosmos.System.Plugs v4.5 512 - - - - - - - - + SAK + SAK + SAK + SAK true Cosmos.snk @@ -86,7 +82,6 @@ - diff --git a/source/Cosmos.System.Plugs/System/StringImpl.cs b/source/Cosmos.System.Plugs/System/StringImpl.cs deleted file mode 100644 index cd4f94655..000000000 --- a/source/Cosmos.System.Plugs/System/StringImpl.cs +++ /dev/null @@ -1,96 +0,0 @@ -using Cosmos.IL2CPU.Plugs; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Plug = Cosmos.IL2CPU.Plugs.PlugAttribute; - -namespace Cosmos.System.Plugs.System -{ - [@Plug(Target = typeof(string))] - public static class StringImpl - { - public static bool StartsWith(this string str, string value) - { - Char[] di = str.ToCharArray(); - Char[] ci = value.ToCharArray(); - if (value.Length > str.Length) - { - return false; - } - for (int i = 0; i < ci.Length; i++) - { - if (di[i] != ci[i]) - { - return false; - - } - } - return true; - } - - public static bool Contains(this string str, string comp) - { - Char[] di = str.ToCharArray(); - Char[] ci = comp.ToCharArray(); - if (comp.Length == str.Length) - { - if (comp == str) - { - return true; - } - else - { - return false; - } - } - else if (!(comp.Length > str.Length) && (comp.Length != str.Length)) - { - for (int i = 0; i < str.Length; i++) - { - if (di[i] == ci[0]) - { - for (int j = 1; j < comp.Length; j++) - { - if (di[i + j] != ci[j]) - { - return false; - } - } - return true; - } - } - } - return false; - } - public static bool EndsWith(this string str, string comp) - { - Char[] di = str.ToCharArray(); - Char[] ci = comp.ToCharArray(); - if(str.Length == comp.Length) - { - if(str == comp) - { - return true; - } - return false; - } - else if(comp.Length > str.Length) - { - return false; - } - else - { - for (int i = str.Length - comp.Length; i < str.Length; i++) - { - if (di[str.Length - comp.Length + i] != ci[i]) - { - return false; - } - } - return true; - } - } - } -} From 2b46c857e7fbe119de669737099b86a1cf748707 Mon Sep 17 00:00:00 2001 From: Cyber Date: Wed, 16 Dec 2015 21:14:17 -0500 Subject: [PATCH 7/9] Fixed String Comparisons Fixed String Comparisons --- source/Cosmos.IL2CPU/Cosmos.IL2CPU.csproj | 12 +- .../CustomImplementation/System/StringImpl.cs | 147 ++++++++++++++---- 2 files changed, 128 insertions(+), 31 deletions(-) diff --git a/source/Cosmos.IL2CPU/Cosmos.IL2CPU.csproj b/source/Cosmos.IL2CPU/Cosmos.IL2CPU.csproj index e3d02687a..7b3f456ed 100644 --- a/source/Cosmos.IL2CPU/Cosmos.IL2CPU.csproj +++ b/source/Cosmos.IL2CPU/Cosmos.IL2CPU.csproj @@ -12,10 +12,14 @@ Cosmos.IL2CPU v4.5 512 - SAK - SAK - SAK - SAK + + + + + + + + true Cosmos.snk diff --git a/source/Cosmos.IL2CPU/CustomImplementation/System/StringImpl.cs b/source/Cosmos.IL2CPU/CustomImplementation/System/StringImpl.cs index 36aecce0a..e4dc2007f 100644 --- a/source/Cosmos.IL2CPU/CustomImplementation/System/StringImpl.cs +++ b/source/Cosmos.IL2CPU/CustomImplementation/System/StringImpl.cs @@ -177,8 +177,21 @@ public static bool StartsWith(string aThis, string aSubstring, StringComparison aComparison) { - Console.WriteLine("String.StartsWith not working!"); - throw new NotImplementedException(); + Char[] di = aThis.ToCharArray(); + Char[] ci = aSubstring.ToCharArray(); + if (aSubstring.Length > aThis.Length) + { + return false; + } + for (int i = 0; i < ci.Length; i++) + { + if (di[i] != ci[i]) + { + return false; + + } + } + return true; } //String concatenation plugs @@ -559,11 +572,36 @@ public static bool Contains(string aThis, string value) { - if (aThis.IndexOf(value) != -1) + Char[] di = aThis.ToCharArray(); + Char[] ci = value.ToCharArray(); + if (value.Length == aThis.Length) { - return true; + if (value == aThis) + { + return true; + } + else + { + return false; + } + } + else if (!(value.Length > aThis.Length) && (value.Length != aThis.Length)) + { + for (int i = 0; i < aThis.Length; i++) + { + if (di[i] == ci[0]) + { + for (int j = 1; j < value.Length; j++) + { + if (di[i + j] != ci[j]) + { + return false; + } + } + return true; + } + } } - return false; } @@ -574,30 +612,31 @@ public static bool EndsWith(string aThis, string aSubStr, StringComparison aComparison) { - if (aSubStr == null) + Char[] di = aThis.ToCharArray(); + Char[] ci = aSubStr.ToCharArray(); + if (aThis.Length == aSubStr.Length) { - throw new ArgumentNullException("aSubStr"); - } - - if (aThis == aSubStr) - { - return true; - } - - if (aSubStr.Length == 0) - { - return true; - } - - int xLastIdx = aThis.Length - aSubStr.Length; - for (int i = 0; i < aSubStr.Length; i++) - { - if (aThis[xLastIdx + i] != aSubStr[i]) + if (aThis == aSubStr) { - return false; + return true; } + return false; + } + else if (aThis.Length > aSubStr.Length) + { + return false; + } + else + { + for (int i = aThis.Length - aSubStr.Length; i < aThis.Length; i++) + { + if (di[aThis.Length - aSubStr.Length + i] != ci[i]) + { + return false; + } + } + return true; } - return true; } // System.Int32 System.String.IndexOf(System.String, System.Int32, System.Int32, System.StringComparison) @@ -689,11 +728,18 @@ public static bool StartsWith(string aThis, string aSubStr, bool aIgnoreCase, CultureInfo aCulture) { - for (int i = 0; i < aSubStr.Length; i++) + Char[] di = aThis.ToCharArray(); + Char[] ci = aSubStr.ToCharArray(); + if (aSubStr.Length > aThis.Length) { - if (aThis[i] != aSubStr[i]) + return false; + } + for (int i = 0; i < ci.Length; i++) + { + if (di[i] != ci[i]) { return false; + } } return true; @@ -760,6 +806,53 @@ return new string(new char[length]); } + public static string dTrimStart(string aThis, string aSubStr) + { + char[] ci = aThis.ToCharArray(); + char[] di = aSubStr.ToCharArray(); + + if(aThis.StartsWith(aSubStr)) + { + if(!(aThis == aSubStr)) + { + char[] oi = new char[ci.Length - di.Length]; + for(int i=0; i < ci.Length - di.Length; i++) + { + oi[i] = ci[i + di.Length]; + } + return oi.ToString(); + } + else + { + return ""; + } + } + else + { + throw new ArgumentNullException(); + } + + } + + + + + + + + + + + + + + + + + + + + /*public int IndexOf(char c) { // TODO: We can't get 'this' From e9b163a75b6ae293344cdd6ff244869ae140b930 Mon Sep 17 00:00:00 2001 From: Cyber Date: Thu, 17 Dec 2015 15:01:54 -0500 Subject: [PATCH 8/9] Revert "Fixed String Comparisons" This reverts commit 2b46c857e7fbe119de669737099b86a1cf748707. --- source/Cosmos.IL2CPU/Cosmos.IL2CPU.csproj | 12 +- .../CustomImplementation/System/StringImpl.cs | 145 ++++-------------- 2 files changed, 30 insertions(+), 127 deletions(-) diff --git a/source/Cosmos.IL2CPU/Cosmos.IL2CPU.csproj b/source/Cosmos.IL2CPU/Cosmos.IL2CPU.csproj index 7b3f456ed..e3d02687a 100644 --- a/source/Cosmos.IL2CPU/Cosmos.IL2CPU.csproj +++ b/source/Cosmos.IL2CPU/Cosmos.IL2CPU.csproj @@ -12,14 +12,10 @@ Cosmos.IL2CPU v4.5 512 - - - - - - - - + SAK + SAK + SAK + SAK true Cosmos.snk diff --git a/source/Cosmos.IL2CPU/CustomImplementation/System/StringImpl.cs b/source/Cosmos.IL2CPU/CustomImplementation/System/StringImpl.cs index e4dc2007f..36aecce0a 100644 --- a/source/Cosmos.IL2CPU/CustomImplementation/System/StringImpl.cs +++ b/source/Cosmos.IL2CPU/CustomImplementation/System/StringImpl.cs @@ -177,21 +177,8 @@ public static bool StartsWith(string aThis, string aSubstring, StringComparison aComparison) { - Char[] di = aThis.ToCharArray(); - Char[] ci = aSubstring.ToCharArray(); - if (aSubstring.Length > aThis.Length) - { - return false; - } - for (int i = 0; i < ci.Length; i++) - { - if (di[i] != ci[i]) - { - return false; - - } - } - return true; + Console.WriteLine("String.StartsWith not working!"); + throw new NotImplementedException(); } //String concatenation plugs @@ -572,36 +559,11 @@ public static bool Contains(string aThis, string value) { - Char[] di = aThis.ToCharArray(); - Char[] ci = value.ToCharArray(); - if (value.Length == aThis.Length) + if (aThis.IndexOf(value) != -1) { - if (value == aThis) - { - return true; - } - else - { - return false; - } - } - else if (!(value.Length > aThis.Length) && (value.Length != aThis.Length)) - { - for (int i = 0; i < aThis.Length; i++) - { - if (di[i] == ci[0]) - { - for (int j = 1; j < value.Length; j++) - { - if (di[i + j] != ci[j]) - { - return false; - } - } - return true; - } - } + return true; } + return false; } @@ -612,31 +574,30 @@ public static bool EndsWith(string aThis, string aSubStr, StringComparison aComparison) { - Char[] di = aThis.ToCharArray(); - Char[] ci = aSubStr.ToCharArray(); - if (aThis.Length == aSubStr.Length) + if (aSubStr == null) { - if (aThis == aSubStr) - { - return true; - } - return false; + throw new ArgumentNullException("aSubStr"); } - else if (aThis.Length > aSubStr.Length) + + if (aThis == aSubStr) { - return false; - } - else - { - for (int i = aThis.Length - aSubStr.Length; i < aThis.Length; i++) - { - if (di[aThis.Length - aSubStr.Length + i] != ci[i]) - { - return false; - } - } return true; } + + if (aSubStr.Length == 0) + { + return true; + } + + int xLastIdx = aThis.Length - aSubStr.Length; + for (int i = 0; i < aSubStr.Length; i++) + { + if (aThis[xLastIdx + i] != aSubStr[i]) + { + return false; + } + } + return true; } // System.Int32 System.String.IndexOf(System.String, System.Int32, System.Int32, System.StringComparison) @@ -728,18 +689,11 @@ public static bool StartsWith(string aThis, string aSubStr, bool aIgnoreCase, CultureInfo aCulture) { - Char[] di = aThis.ToCharArray(); - Char[] ci = aSubStr.ToCharArray(); - if (aSubStr.Length > aThis.Length) + for (int i = 0; i < aSubStr.Length; i++) { - return false; - } - for (int i = 0; i < ci.Length; i++) - { - if (di[i] != ci[i]) + if (aThis[i] != aSubStr[i]) { return false; - } } return true; @@ -806,53 +760,6 @@ return new string(new char[length]); } - public static string dTrimStart(string aThis, string aSubStr) - { - char[] ci = aThis.ToCharArray(); - char[] di = aSubStr.ToCharArray(); - - if(aThis.StartsWith(aSubStr)) - { - if(!(aThis == aSubStr)) - { - char[] oi = new char[ci.Length - di.Length]; - for(int i=0; i < ci.Length - di.Length; i++) - { - oi[i] = ci[i + di.Length]; - } - return oi.ToString(); - } - else - { - return ""; - } - } - else - { - throw new ArgumentNullException(); - } - - } - - - - - - - - - - - - - - - - - - - - /*public int IndexOf(char c) { // TODO: We can't get 'this' From 05148af8cfbb1d00bfe6624437cc3f5c397e6323 Mon Sep 17 00:00:00 2001 From: Cyber Date: Thu, 17 Dec 2015 15:52:22 -0500 Subject: [PATCH 9/9] FINALLY fixed string stuff --- .../CustomImplementation/System/StringImpl.cs | 152 ++- .../Cosmos.System.Plugs/System/StringImpl.cs | 871 ++++++++++++++++++ 2 files changed, 994 insertions(+), 29 deletions(-) create mode 100644 source/Cosmos.System.Plugs/System/StringImpl.cs diff --git a/source/Cosmos.IL2CPU/CustomImplementation/System/StringImpl.cs b/source/Cosmos.IL2CPU/CustomImplementation/System/StringImpl.cs index 36aecce0a..7e0aab250 100644 --- a/source/Cosmos.IL2CPU/CustomImplementation/System/StringImpl.cs +++ b/source/Cosmos.IL2CPU/CustomImplementation/System/StringImpl.cs @@ -7,10 +7,10 @@ using global::System; using global::System.Globalization; - [Plug(Target = typeof(string), IsMicrosoftdotNETOnly = true)] + //[Plug(Target = typeof(string), IsMicrosoftdotNETOnly = true)] public static class StringImpl { - public static unsafe void Ctor( + /*public static unsafe void Ctor( string aThis, char[] aChars, [FieldAccess(Name = "System.Int32 System.String.m_stringLength")] ref int aStringLength, @@ -177,8 +177,21 @@ public static bool StartsWith(string aThis, string aSubstring, StringComparison aComparison) { - Console.WriteLine("String.StartsWith not working!"); - throw new NotImplementedException(); + Char[] di = aThis.ToCharArray(); + Char[] ci = aSubstring.ToCharArray(); + if (aSubstring.Length > aThis.Length) + { + return false; + } + for (int i = 0; i < ci.Length; i++) + { + if (di[i] != ci[i]) + { + return false; + + } + } + return true; } //String concatenation plugs @@ -559,11 +572,36 @@ public static bool Contains(string aThis, string value) { - if (aThis.IndexOf(value) != -1) + Char[] di = aThis.ToCharArray(); + Char[] ci = value.ToCharArray(); + if (value.Length == aThis.Length) { - return true; + if (value == aThis) + { + return true; + } + else + { + return false; + } + } + else if (!(value.Length > aThis.Length) && (value.Length != aThis.Length)) + { + for (int i = 0; i < aThis.Length; i++) + { + if (di[i] == ci[0]) + { + for (int j = 1; j < value.Length; j++) + { + if (di[i + j] != ci[j]) + { + return false; + } + } + return true; + } + } } - return false; } @@ -574,30 +612,31 @@ public static bool EndsWith(string aThis, string aSubStr, StringComparison aComparison) { - if (aSubStr == null) + Char[] di = aThis.ToCharArray(); + Char[] ci = aSubStr.ToCharArray(); + if (aThis.Length == aSubStr.Length) { - throw new ArgumentNullException("aSubStr"); - } - - if (aThis == aSubStr) - { - return true; - } - - if (aSubStr.Length == 0) - { - return true; - } - - int xLastIdx = aThis.Length - aSubStr.Length; - for (int i = 0; i < aSubStr.Length; i++) - { - if (aThis[xLastIdx + i] != aSubStr[i]) + if (aThis == aSubStr) { - return false; + return true; } + return false; + } + else if (aThis.Length > aSubStr.Length) + { + return false; + } + else + { + for (int i = aThis.Length - aSubStr.Length; i < aThis.Length; i++) + { + if (di[aThis.Length - aSubStr.Length + i] != ci[i]) + { + return false; + } + } + return true; } - return true; } // System.Int32 System.String.IndexOf(System.String, System.Int32, System.Int32, System.StringComparison) @@ -689,11 +728,18 @@ public static bool StartsWith(string aThis, string aSubStr, bool aIgnoreCase, CultureInfo aCulture) { - for (int i = 0; i < aSubStr.Length; i++) + Char[] di = aThis.ToCharArray(); + Char[] ci = aSubStr.ToCharArray(); + if (aSubStr.Length > aThis.Length) { - if (aThis[i] != aSubStr[i]) + return false; + } + for (int i = 0; i < ci.Length; i++) + { + if (di[i] != ci[i]) { return false; + } } return true; @@ -760,6 +806,53 @@ return new string(new char[length]); } + public static string dTrimStart(string aThis, string aSubStr) + { + char[] ci = aThis.ToCharArray(); + char[] di = aSubStr.ToCharArray(); + + if(aThis.StartsWith(aSubStr)) + { + if(!(aThis == aSubStr)) + { + char[] oi = new char[ci.Length - di.Length]; + for(int i=0; i < ci.Length - di.Length; i++) + { + oi[i] = ci[i + di.Length]; + } + return oi.ToString(); + } + else + { + return ""; + } + } + else + { + throw new ArgumentNullException(); + } + + } + + + + + + + + + + + + + + + + + + + + /*public int IndexOf(char c) { // TODO: We can't get 'this' @@ -771,5 +864,6 @@ //} return -1; }*/ + } } \ No newline at end of file diff --git a/source/Cosmos.System.Plugs/System/StringImpl.cs b/source/Cosmos.System.Plugs/System/StringImpl.cs new file mode 100644 index 000000000..e8a2b2704 --- /dev/null +++ b/source/Cosmos.System.Plugs/System/StringImpl.cs @@ -0,0 +1,871 @@ +using Cosmos.Common; +using Cosmos.Debug.Kernel; +using Cosmos.IL2CPU.Plugs; +using Sys = Cosmos.System; +using global::System; +using global::System.Globalization; + + +namespace Cosmos.System.Plugs.System +{ + [Plug(Target = typeof(string), IsMicrosoftdotNETOnly = true)] + public static class StringImpl + { + public static unsafe void Ctor( + string aThis, + char[] aChars, + [FieldAccess(Name = "System.Int32 System.String.m_stringLength")] ref int aStringLength, + [FieldAccess(Name = "System.Char System.String.m_firstChar")] char* aFirstChar) + { + aStringLength = aChars.Length; + for (int i = 0; i < aChars.Length; i++) + { + aFirstChar[i] = aChars[i]; + } + } + + public static unsafe void Ctor( + string aThis, + char[] aChars, + int start, + int length, + [FieldAccess(Name = "System.Int32 System.String.m_stringLength")] ref int aStringLength, + [FieldAccess(Name = "System.Char System.String.m_firstChar")] char* aFirstChar) + { + aStringLength = length; + for (int i = 0; i < length; i++) + { + aFirstChar[i] = aChars[start + i]; + } + } + + public static unsafe void Ctor( + string aThis, + char aChar, + int aLength, + [FieldAccess(Name = "System.Int32 System.String.m_stringLength")] ref int aStringLength, + [FieldAccess(Name = "System.Char System.String.m_firstChar")] char* aFirstChar) + { + aStringLength = aLength; + for (int i = 0; i < aLength; i++) + { + aFirstChar[i] = aChar; + } + } + + public static unsafe int get_Length( + int* aThis, + [FieldAccess(Name = "System.Int32 System.String.m_stringLength")] ref int aLength) + { + return aLength; + } + + public static unsafe char get_Chars(uint* aThis, int aIndex) + { + // todo: change to use a FieldAccessAttribute, to get the pointer to the first character and go from there + + // we first need to dereference the handle to a pointer. + uint xActualThis = aThis[0]; + var xCharIdx = (char*)(xActualThis + 16); + return xCharIdx[aIndex]; + } + + public static string Format(string aFormat, object aArg1) + { + if (aArg1 == null) + { + throw new ArgumentNullException(aFormat == null ? "aFormat" : "aArgs"); + } + var xO = new object[1]; + xO[0] = aArg1; + return FormatHelper(null, aFormat, xO); + } + + public static string Format(string aFormat, object aArg1, object aArg2) + { + if ((aArg1 == null) || (aArg2 == null)) + { + throw new ArgumentNullException(aFormat == null ? "aFormat" : "aArgs"); + } + var xO = new object[2]; + xO[0] = aArg1; + xO[1] = aArg2; + return FormatHelper(null, aFormat, xO); + } + + public static string Format(string aFormat, object aArg1, object aArg2, object aArg3) + { + if ((aArg1 == null) || (aArg2 == null) || (aArg3 == null)) + { + throw new ArgumentNullException(aFormat == null ? "aFormat" : "aArgs"); + } + var xO = new object[3]; + xO[0] = aArg1; + xO[1] = aArg2; + xO[2] = aArg3; + return FormatHelper(null, aFormat, xO); + } + + public static string Format(string aFormat, params object[] aArgs) + { + if (aArgs == null) + { + throw new ArgumentNullException(aFormat == null ? "aFormat" : "aArgs"); + } + return FormatHelper(null, aFormat, aArgs); + } + + public static string Format(IFormatProvider aFormatProvider, string aFormat, params object[] aArgs) + { + if (aArgs == null) + { + throw new ArgumentNullException(aFormat == null ? "aFormat" : "aArgs"); + } + return FormatHelper(aFormatProvider, aFormat, aArgs); + } + + public static string FormatHelper(IFormatProvider aFormatProvider, string aFormat, object[] aArgs) + { + char[] xCharArray = aFormat.ToCharArray(); + string xFormattedString = string.Empty; + bool xFoundPlaceholder = false; + string xParamNumber = string.Empty; + bool xParamNumberDone = true; + for (int i = 0; i < xCharArray.Length; i++) + { + if (xFoundPlaceholder) + { + if (xCharArray[i] == '{') + { + throw new FormatException("The format string provided is invalid."); + } + if (xCharArray[i] == '}') + { + int xParamIndex = StringHelper.GetStringToNumber(xParamNumber); + if ((xParamIndex < aArgs.Length - 1) && (aArgs[xParamIndex] != null)) + { + string xParamValue = aArgs[xParamIndex].ToString(); + xFormattedString = string.Concat(xFormattedString, xParamValue); + } + xFoundPlaceholder = false; + xParamNumberDone = true; + xParamNumber = string.Empty; + } + else if (xCharArray[i] == ':') + { + xParamNumberDone = true; + // TODO: Need to handle different formats. (X, N, etc) + } + else if ((char.IsDigit(xCharArray[i])) && (!xParamNumberDone)) + { + xParamNumber = string.Concat(xParamNumber, xCharArray[i]); + } + } + else if (xCharArray[i] == '{') + { + xFoundPlaceholder = true; + xParamNumberDone = false; + xParamNumber = string.Empty; + } + else + { + xFormattedString = string.Concat(xFormattedString, xCharArray[i]); + } + } + + return xFormattedString; + } + + public static bool StartsWith(this string aThis, string aSubstring, StringComparison aComparison) + { + Char[] di = aThis.ToCharArray(); + Char[] ci = aSubstring.ToCharArray(); + if (aSubstring.Length > aThis.Length) + { + return false; + } + for (int i = 0; i < ci.Length; i++) + { + if (di[i] != ci[i]) + { + return false; + + } + } + return true; + } + + //String concatenation plugs + public static string Concat(string str0) + { + return str0; + } + + public static string Concat(string str0, string str1) + { + return Concat(new[] { str0, str1 }); + } + + public static string Concat(string str0, string str1, string str2) + { + return Concat(new[] { str0, str1, str2 }); + } + + public static string Concat(string str0, string str1, string str2, string str3) + { + return Concat(new[] { str0, str1, str2, str3 }); + } + + //Object concatenation plugs + public static string Concat(object obj0) + { + return obj0?.ToString(); + } + + public static string Concat(object obj0, object obj1) + { + return Concat(obj0?.ToString(), obj1?.ToString()); + } + + public static string Concat(object obj0, object obj1, object obj2) + { + return Concat(obj0?.ToString(), obj1?.ToString(), obj2?.ToString()); + } + + public static string Concat(object obj0, object obj1, object obj2, object obj3) + { + return Concat(new[] { obj0?.ToString(), obj1?.ToString(), obj2?.ToString(), obj3?.ToString() }); + } + + //Array concatenation plugs + public static string Concat(params string[] values) + { + if (values != null) + { + int len = 0; + for (int i = 0; i < values.Length; i++) + { + string xValue = values[i]; + if (xValue != null) + { + len += values[i].Length; + } + } + return ConcatArray(values, len); + } + return string.Empty; + } + + public static string Concat(params object[] args) + { + if (args != null) + { + var values = new string[args.Length]; + for (int i = 0; i < args.Length; i++) + { + var xArg = args[i]; + if (xArg != null) + { + string xStrArg = xArg as string; + if (xStrArg != null) + { + values[i] = xStrArg; + } + else + { + values[i] = xArg.ToString(); + } + } + } + return Concat(values); + } + return string.Empty; + } + + public static string ConcatArray(string[] values, int totalLength) + { + if (values != null) + { + var xResultChars = new char[totalLength]; + int xCurPos = 0; + for (int i = 0; i < values.Length; i++) + { + string xStr = values[i]; + if (xStr != null) + { + for (int j = 0; j < xStr.Length; j++) + { + xResultChars[xCurPos] = xStr[j]; + xCurPos++; + } + } + } + string xResult = new string(xResultChars); + return xResult; + } + return string.Empty; + } + + public static string PadHelper(this string aThis, int totalWidth, char paddingChar, bool isRightPadded) + { + //Console.Write("PadHelper, totalWidth = "); + //WriteNumber((uint)totalWidth, 32); + //Console.WriteLine(""); + var cs = new char[totalWidth]; + + int pos = aThis.Length; + + if (isRightPadded) + { + for (int i = 0; i < aThis.Length; i++) + { + cs[i] = aThis[i]; + } + + for (int i = aThis.Length; i < totalWidth; i++) + { + cs[i] = paddingChar; + } + } + else + { + int offset = totalWidth - aThis.Length; + for (int i = 0; i < aThis.Length; i++) + { + cs[i + offset] = aThis[i]; + } + + for (int i = 0; i < offset; i++) + { + cs[i] = paddingChar; + } + } + + return new string(cs); + } + + public static string Substring(this string aThis, int startpos) + { + var cs = new char[aThis.Length - startpos]; + int j = 0; + for (int i = startpos; i < aThis.Length; i++) + { + cs[j++] = aThis[i]; + } + + return new string(cs); + } + + public static string Substring(this string aThis, int startpos, int length) + { + if (startpos + length > aThis.Length) + { + length = aThis.Length - startpos; + } + + var cs = new char[length]; + + int j = 0; + for (int i = startpos; i < startpos + length; i++) + { + cs[j++] = aThis[i]; + } + + return new string(cs); + } + + public static string Replace(this string aThis, char oldValue, char newValue) + { + var cs = new char[aThis.Length]; + + for (int i = 0; i < aThis.Length; i++) + { + if (aThis[i] != oldValue) + { + cs[i] = aThis[i]; + } + else + { + cs[i] = newValue; + } + } + + return new string(cs); + } + + // HACK: We need to redo this once char support is complete (only returns 0, -1). + public static int CompareTo(this string aThis, string other) + { + if (aThis.Length != other.Length) + { + return -1; + } + for (int i = 0; i < aThis.Length; i++) + { + if (aThis[i] != other[i]) + { + return -1; + } + } + return 0; + } + + public static int IndexOf(this string aThis, char value, int startIndex, int count) + { + int xEndIndex = aThis.Length; + if (startIndex + count < xEndIndex) + { + xEndIndex = startIndex + count; + } + for (int i = startIndex; i < xEndIndex; i++) + { + if (aThis[i] == value) + { + return i; + } + } + + return -1; + } + + // HACK: TODO - improve efficiency of this. + //How do we access the raw memory to copy it into a char array? + public static char[] ToCharArray(this string aThis) + { + var result = new char[aThis.Length]; + + for (int i = 0; i < aThis.Length; i++) + { + result[i] = aThis[i]; + } + + return result; + } + + [PlugMethod(Enabled = false)] + public static uint GetStorage(string aString) + { + return 0; + } + + //[PlugMethod(Enabled = false)] + //public static char[] GetStorageArray(string aString) { + // return null; + //} + + private static int[] BuildBadCharTable(char[] needle) + { + var badShift = new int[256]; + for (int i = 0; i < 256; i++) + { + badShift[i] = needle.Length; + } + int last = needle.Length - 1; + for (int i = 0; i < last; i++) + { + badShift[needle[i]] = last - i; + } + return badShift; + } + + private static int boyerMooreHorsepool(string pattern, string text) + { + var needle = pattern.ToCharArray(); + var haystack = text.ToCharArray(); + + if (needle.Length > haystack.Length) + { + return -1; + } + var badShift = BuildBadCharTable(needle); + int offset = 0; + int scan = 0; + int last = needle.Length - 1; + int maxoffset = haystack.Length - needle.Length; + while (offset <= maxoffset) + { + for (scan = last; needle[scan] == haystack[scan + offset]; scan--) + { + if (scan == 0) + { + //Match found + return offset; + } + } + offset += badShift[haystack[offset + last]]; + } + return -1; + } + + public static int IndexOf(this string aThis, string aSubstring, int aIdx, int aLength, StringComparison aComparison) + { + + return boyerMooreHorsepool(aSubstring, aThis.Substring(aIdx, aLength)); + } + + //private static void WriteNumber(uint aValue, + // byte aBitCount) + //{ + // uint xValue = aValue; + // byte xCurrentBits = aBitCount; + // Console.Write("0x"); + // while (xCurrentBits >= 4) + // { + // xCurrentBits -= 4; + // byte xCurrentDigit = (byte)((xValue >> xCurrentBits) & 0xF); + // string xDigitString = null; + // switch (xCurrentDigit) + // { + // case 0: + // xDigitString = "0"; + // goto default; + // case 1: + // xDigitString = "1"; + // goto default; + // case 2: + // xDigitString = "2"; + // goto default; + // case 3: + // xDigitString = "3"; + // goto default; + // case 4: + // xDigitString = "4"; + // goto default; + // case 5: + // xDigitString = "5"; + // goto default; + // case 6: + // xDigitString = "6"; + // goto default; + // case 7: + // xDigitString = "7"; + // goto default; + // case 8: + // xDigitString = "8"; + // goto default; + // case 9: + // xDigitString = "9"; + // goto default; + // case 10: + // xDigitString = "A"; + // goto default; + // case 11: + // xDigitString = "B"; + // goto default; + // case 12: + // xDigitString = "C"; + // goto default; + // case 13: + // xDigitString = "D"; + // goto default; + // case 14: + // xDigitString = "E"; + // goto default; + // case 15: + // xDigitString = "F"; + // goto default; + // default: + // Console.Write(xDigitString); + // break; + // } + // } + //} + + public static bool Contains(this string aThis, string value) + { + Char[] di = aThis.ToCharArray(); + Char[] ci = value.ToCharArray(); + if (value.Length == aThis.Length) + { + if (value == aThis) + { + return true; + } + else + { + return false; + } + } + else if (!(value.Length > aThis.Length) && (value.Length != aThis.Length)) + { + for (int i = 0; i < aThis.Length; i++) + { + if (di[i] == ci[0]) + { + for (int j = 1; j < value.Length; j++) + { + if (di[i + j] != ci[j]) + { + return false; + } + } + return true; + } + } + } + return false; + } + + public static bool EndsWith(this string aThis, string aSubStr, bool aIgnoreCase, CultureInfo aCulture) + { + return EndsWith(aThis, aSubStr, StringComparison.CurrentCulture); + } + + public static bool EndsWith(this string aThis, string aSubStr, StringComparison aComparison) + { + Char[] di = aThis.ToCharArray(); + Char[] ci = aSubStr.ToCharArray(); + if (aThis.Length == aSubStr.Length) + { + if (aThis == aSubStr) + { + return true; + } + return false; + } + else if (aThis.Length > aSubStr.Length) + { + return false; + } + else + { + for (int i = aThis.Length - aSubStr.Length; i < aThis.Length; i++) + { + if (di[aThis.Length - aSubStr.Length + i] != ci[i]) + { + return false; + } + } + return true; + } + } + + // System.Int32 System.String.IndexOf(System.String, System.Int32, System.Int32, System.StringComparison) + + public static bool Equals(this string aThis, string aThat, StringComparison aComparison) + { +#warning TODO: implement + if (aComparison == StringComparison.OrdinalIgnoreCase) + { + string xLowerThis = aThis.ToLower(); + string xLowerThat = aThat.ToLower(); + return EqualsHelper(xLowerThis, xLowerThat); + } + return EqualsHelper(aThis, aThat); + } + + public static bool EqualsHelper(string aStrA, string aStrB) + { + return aStrA.CompareTo(aStrB) == 0; + } + + private static bool CharArrayContainsChar(char[] aArray, char aChar) + { + for (int i = 0; i < aArray.Length; i++) + { + if (aArray[i] == aChar) + { + return true; + } + } + return false; + } + + public static int IndexOf(this string aThis, string aValue) + { + return aThis.IndexOf(aValue, 0, aThis.Length, StringComparison.CurrentCulture); + } + + public static int IndexOfAny(this string aThis, char[] aSeparators, int aStartIndex, int aLength) + { + if (aSeparators == null) + { + throw new ArgumentNullException("aSeparators"); + } + + int xResult = -1; + for (int i = 0; i < aSeparators.Length; i++) + { + int xValue = IndexOf(aThis, aSeparators[i], aStartIndex, aLength); + if (xValue < xResult || xResult == -1) + { + xResult = xValue; + } + } + return xResult; + } + + public static string Insert(this string aThis, int aStartPos, string aValue) + { + return aThis.Substring(0, aStartPos) + aValue + aThis.Substring(aStartPos); + } + + public static int LastIndexOf(this string aThis, char aChar, int aStartIndex, int aCount) + { + return LastIndexOfAny(aThis, new[] { aChar }, aStartIndex, aCount); + } + + public static int LastIndexOfAny(this string aThis, char[] aChars, int aStartIndex, int aCount) + { + for (int i = 0; i < aCount; i++) + { + if (CharArrayContainsChar(aChars, aThis[aStartIndex - i])) + { + return aStartIndex - i; + } + } + return -1; + } + + public static int nativeCompareOrdinalEx(string aStrA, int aIndexA, string aStrB, int aIndexB, int aCount) + { + //Just a basic implementation + if (aStrA == aStrB) + { + return 0; + } + return -1; + } + + public static bool StartsWith(this string aThis, string aSubStr, bool aIgnoreCase, CultureInfo aCulture) + { + Char[] di = aThis.ToCharArray(); + Char[] ci = aSubStr.ToCharArray(); + if (aSubStr.Length > aThis.Length) + { + return false; + } + for (int i = 0; i < ci.Length; i++) + { + if (di[i] != ci[i]) + { + return false; + + } + } + return true; + } + + + + public static string Remove(this string aThis, int aStart, int aCount) + { + return aThis.Substring(0, aStart) + aThis.Substring(aStart + aCount, aThis.Length - (aStart + aCount)); + } + + public static string Replace(this 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; + } + + public static string ToLower(this string aThis, CultureInfo aCulture) + { + return ChangeCasing(aThis, 65, 90, 32); + } + + public static string ToUpper(this string aThis, CultureInfo aCulture) + { + return ChangeCasing(aThis, 97, 122, -32); + } + + public static string ToUpper(this string aThis) + { + return ChangeCasing(aThis, 97, 122, -32); + } + + private static string ChangeCasing(string aValue, int lowerAscii, int upperAscii, int offset) + { + var xChars = new char[aValue.Length]; + + for (int i = 0; i < aValue.Length; i++) + { + int xAsciiCode = aValue[i]; + if ((xAsciiCode <= upperAscii) && (xAsciiCode >= lowerAscii)) + { + xChars[i] = (char)(xAsciiCode + offset); + } + else + { + xChars[i] = aValue[i]; + } + } + + return new string(xChars); + } + + public static string ToString(this string aThis) + { + return aThis; + } + + public static string FastAllocateString(int length) + { + return new string(new char[length]); + } + + public static string TrimStart(this char[] aThis, string aSubStr) + { + char[] ci = aThis; + char[] di = aSubStr.ToCharArray(); + + if (aThis[0] == (aSubStr.ToCharArray()[0])) + { + if (!(aThis.ToString() == aSubStr)) + { + char[] oi = new char[ci.Length - di.Length]; + for (int i = 0; i < ci.Length - di.Length; i++) + { + oi[i] = ci[i + di.Length]; + } + return oi.ToString(); + } + else + { + return ""; + } + } + else + { + throw new ArgumentNullException(); + } + + } + + + + + + + + + + + + + + + + + + + + + /*public int IndexOf(char c) + { + // TODO: We can't get 'this' + //string me = ToString(); + //for (int i = 0; i < me.Length; i++) + //{ + // if (me[i] == c) + // return i; + //} + return -1; + }*/ + } +}