Permalink
Browse files

Adding more string wrappers

  • Loading branch information...
1 parent 8b97cff commit 682ed036b77efeca08fff09282e2bc5df44270cf Frank Laub committed Mar 3, 2010
@@ -37,7 +37,9 @@ public class String
{
#if !HOSTED_MODE
public static readonly string Empty = "";
-
+
+ #region Native methods
+
/// <summary>
/// The number of characters in the String value represented by this String object.
/// </summary>
@@ -76,32 +78,71 @@ public class String
[JsMacro("({0} == {1})")]
public extern override bool Equals(object obj);
- [JsMacro("StringHelper.GetHashCode({0})")]
- public override int GetHashCode() {
- return StringHelper.GetHashCode(this);
- }
+ public extern int IndexOf(string searchString);
+
+ public extern int IndexOf(string searchString, int position);
+
+ public extern int LastIndexOf(string searchString);
- public extern bool Contains(string value);
+ public extern int LastIndexOf(string searchString, int position);
public extern string Replace(string oldValue, string newValue);
public extern string Replace(char oldValue, char newValue);
public extern string Trim();
+ [JsName("toLocaleUpperCase")]
+ public extern string ToUpper();
+
+ [JsName("toLocaleLowerCase")]
+ public extern string ToLower();
+
+ [JsName("toUpperCase")]
+ public extern string ToUpperInvariant();
+
+ [JsName("toLowerCase")]
+ public extern string ToLowerInvariant();
+
+ [JsName("substring")]
+ private extern string NativeSubstring(int start, int end);
+
+ [JsName("split")]
+ private extern string[] NativeSplit(object separator);
+
+ [JsName("split")]
+ private extern string[] NativeSplit(object separator, int count);
+ #endregion
+
+ #region .NET Methods
+
[IndexerName("Chars")]
public extern char this[int index] {
[JsMacro("{0}.charAt({1})")]
get;
}
+ [JsCamelCase(false)]
+ public override int GetHashCode() {
+ int hash = 0;
+ for (int i = 0; i < this.Length; i++) {
+ int ch = this.CharCodeAt(i);
+ hash = (hash << 5) - hash + ch;
+ }
+ return hash;
+ }
+
+ public bool Contains(string value) {
+ return IndexOf(value) != -1;
+ }
+
[JsName("_Substring")]
public string Substring(int startIndex) {
if (startIndex == 0)
return this;
if (startIndex < 0 || startIndex > this.Length)
throw new ArgumentOutOfRangeException("startIndex");
- return InternalSubstring(startIndex, this.Length);
+ return NativeSubstring(startIndex, this.Length);
}
[JsName("_Substring")]
@@ -116,11 +157,26 @@ public class String
throw new ArgumentOutOfRangeException("length", "startIndex + length > this.length");
if (startIndex == 0 && length == this.Length)
return this;
- return InternalSubstring(startIndex, startIndex + length);
+ return NativeSubstring(startIndex, startIndex + length);
+ }
+
+ [JsName("_Split")]
+ public string[] Split(params char[] seperator) {
+ if (seperator.Length == 1) {
+ return this.NativeSplit(seperator);
+ }
+ throw new NotSupportedException();
}
- [JsMacro("{0}.substring({1}, {2})")]
- internal extern string InternalSubstring(int start, int end);
+ [JsName("_Split")]
+ public string[] Split(char[] seperator, int count) {
+ if (seperator.Length == 1) {
+ return this.NativeSplit(seperator, count);
+ }
+ throw new NotSupportedException();
+ }
+
+ #region Format
public static string Format(string format, object arg0) {
var sb = FormatHelper(null, format, arg0);
@@ -325,23 +381,11 @@ class FormatSpecifier
return n;
}
}
-#endif
- }
+ #endregion
-#if !HOSTED_MODE
- [JsNamespace]
- internal static class StringHelper
- {
- internal static int GetHashCode(String str) {
- int hash = 0;
- for (int i = 0; i < str.Length; i++) {
- int ch = str.CharCodeAt(i);
- hash = (hash << 5) - hash + ch;
- }
- return hash;
- }
- }
+ #endregion
#endif
+ }
public static class StringExtensions
{
@@ -50,12 +50,13 @@ public static class AttributeHelper
}
public static bool IsCamelCase(MemberReference memberRef, TypeSystem typeSystem) {
- if (IsCamelCase((ICustomAttributeProvider)memberRef.DeclaringType, typeSystem))
- return true;
var provider = GetProvider(memberRef);
if (provider == null)
return false;
- return IsCamelCase(provider, typeSystem);
+ var ret = GetBooleanFromAttribute(provider, JsCamelCase);
+ if (ret.HasValue)
+ return ret.Value;
+ return GetBooleanFromAttribute(memberRef.DeclaringType, JsCamelCase) ?? false;
}
public static bool IsAnonymous(TypeReference type, TypeSystem typeSystem) {
@@ -72,25 +73,24 @@ public static class AttributeHelper
private static string GetStringFromAttribute(ICustomAttributeProvider provider, string attributeName) {
var customAttr = FindByName(provider, attributeName);
- if (customAttr != null) {
- var args = customAttr.ConstructorParameters;
- if (args.Count == 1)
- return (string)args[0];
- return "";
- }
- return null;
+ if (customAttr == null)
+ return null;
+
+ var args = customAttr.ConstructorParameters;
+ if (args.Count == 1)
+ return (string)args[0];
+ return "";
}
- private static bool IsCamelCase(ICustomAttributeProvider provider, TypeSystem typeSystem) {
- var customAttr = FindByName(provider, JsCamelCase);
+ private static bool? GetBooleanFromAttribute(ICustomAttributeProvider provider, string attributeName) {
+ var customAttr = FindByName(provider, attributeName);
if (customAttr == null)
- return false;
-
- if (customAttr.ConstructorParameters.Count == 0)
- return true;
+ return null;
- var enabled = customAttr.Properties["Enabled"];
- return (bool)enabled;
+ var args = customAttr.ConstructorParameters;
+ if (args.Count == 1)
+ return (bool)args[0];
+ return true;
}
private static CustomAttribute FindByName(ICustomAttributeProvider provider, string typeName) {
@@ -38,8 +38,27 @@ class StringTest : JsScript
this.view.AreStringsEqual("str.Substring(0)", str, str.Substring(0));
this.view.AreStringsEqual("string.Format(\"Test: {0}\")", "Test: arg0", string.Format("Test: {0}", "arg0"));
this.view.AreStringsEqual("string.Format(\"/{0, 10}/\")", "/ 1/", string.Format("/{0, 10}/", 1));
- //this.view.AreEqual("str.GetHashCode()", 0, str.GetHashCode());
+ // This only works in web-mode because we are using Mono's hashcode implementation
+ this.view.AreEqual("str.GetHashCode()", 5894048900, str.GetHashCode());
+ this.view.AreEqual("str.Contains(\"is\")", true, str.Contains("is"));
+ this.view.AreEqual("str.Contains(\"not\")", false, str.Contains("not"));
+ this.view.AreEqual("str.IndexOf(\"is\")", 2, str.IndexOf("is"));
+ this.view.AreEqual("str.IndexOf(\"is\", 4)", 5, str.IndexOf("is", 4));
+ this.view.AreEqual("str.IndexOf(\"not\")", -1, str.IndexOf("not"));
+ this.view.AreEqual("str.LastIndexOf(\"is\")", 5, str.LastIndexOf("is"));
+ this.view.AreEqual("str.LastIndexOf(\"not\")", -1, str.LastIndexOf("not"));
+ this.view.AreStringsEqual("str.ToUpper()", "THIS IS A STRING", str.ToUpper());
+ this.view.AreStringsEqual("str.ToLower()", "this is a string", str.ToLower());
+ var str3 = " trim ";
+ this.view.AreStringsEqual("str3", " trim ", str3);
+ this.view.AreStringsEqual("str3.Trim()", "trim", str3.Trim());
+
+ var parts = str.Split(' ');
+ this.view.AreStringsEqual("str.Split(' ')[0]", "This", parts[0]);
+ this.view.AreStringsEqual("str.Split(' ')[1]", "is", parts[1]);
+ this.view.AreStringsEqual("str.Split(' ')[2]", "a", parts[2]);
+ this.view.AreStringsEqual("str.Split(' ')[3]", "string", parts[3]);
}
}
}
@@ -1,5 +1,6 @@
H8.DecorationTests.prototype.TestJsCamelCase = function() {
var x = new H8.CamelCaseTest().$ctor();
- x.foo();
+ x.camelCase();
+ x.NotCamelCase();
this.camelCase();
};
@@ -134,12 +134,12 @@ System.Text.StringBuilder.prototype.Append$5 = function(value, startIndex, count
$Class(null, 'System', 'String_FormatSpecifier');
-System.String_FormatSpecifier.prototype.ParseDecimal = function(str) {
+System.String_FormatSpecifier.prototype.ParseDecimal = function() {
var V_0 = this.ptr;
var V_1 = 0;
while (true) {
var V_4 = true;
- var V_2 = str.charCodeAt(V_0);
+ var V_2 = this.str.charCodeAt(V_0);
if (V_2 >= 48) {
var R_1 = 57 >= V_2;
}
@@ -164,9 +164,9 @@ System.String_FormatSpecifier.prototype.ParseDecimal = function(str) {
return V_3;
};
-System.String_FormatSpecifier.prototype.IsWhiteSpace = function(str, ptr) {
- var V_0 = str.charCodeAt(ptr);
- if (((V_0 < 9) || (V_0 > 13)) && (V_0 != 133)) {
+System.String_FormatSpecifier.prototype.IsWhiteSpace = function() {
+ var V_0 = this.str.charCodeAt(this.ptr);
+ if ((((V_0 < 9) || (V_0 > 13)) && (V_0 != 32)) && (V_0 != 133)) {
var R_1 = V_0 == 8287;
}
else {
@@ -220,65 +220,66 @@ System.IndexOutOfRangeException.prototype.$ctor$0 = function() {
return this;
};
-System.String_FormatSpecifier.prototype.ParseFormatSpecifier = function(str) {
+System.String_FormatSpecifier.prototype.ParseFormatSpecifier = function() {
try {
- this.n = this.ParseDecimal(str);
- var V_1 = this.n >= 0;
- if (!V_1) {
- throw new System.FormatException().$ctor$1("Input string was not in a correct format.");
+ this.n = this.ParseDecimal();
+ var V_2 = this.n >= 0;
+ if (!V_2) {
+ throw new System.FormatException().$ctor$1("Invalid argument specifier.");
}
- V_1 = str.charAt(this.ptr) != ',';
- if (!V_1) {
+ V_2 = this.str.charAt(this.ptr) != ',';
+ if (!V_2) {
this.ptr = this.ptr + 1;
while (true) {
- V_1 = this.IsWhiteSpace(str, this.ptr);
- if (!V_1) {
+ V_2 = this.IsWhiteSpace();
+ if (!V_2) {
break;
}
this.ptr = this.ptr + 1;
}
var V_0 = this.ptr;
- this.format = str._Substring$1(V_0, this.ptr - V_0);
- this.left_align = str.charAt(this.ptr) == '-';
- V_1 = !this.left_align;
- if (!V_1) {
+ var V_1 = this.ptr - V_0;
+ this.format = this.str._Substring$1(V_0, V_1);
+ this.left_align = this.str.charAt(this.ptr) == '-';
+ V_2 = !this.left_align;
+ if (!V_2) {
this.ptr = this.ptr + 1;
}
- this.width = this.ParseDecimal(str);
- V_1 = this.width >= 0;
- if (!V_1) {
- throw new System.FormatException().$ctor$1("Input string was not in a correct format.");
+ this.width = this.ParseDecimal();
+ V_2 = this.width >= 0;
+ if (!V_2) {
+ throw new System.FormatException().$ctor$1("Invalid width specifier.");
}
}
else {
this.width = 0;
this.left_align = false;
this.format = "";
}
- V_1 = str.charAt(this.ptr) != ':';
- if (!V_1) {
+ V_2 = this.str.charAt(this.ptr) != ':';
+ if (!V_2) {
var D_0 = this.ptr + 1;
- var V_2 = D_0;
+ var V_3 = D_0;
this.ptr = D_0;
- V_0 = V_2;
+ V_0 = V_3;
while (true) {
- V_1 = str.charAt(this.ptr) != '}';
- if (!V_1) {
+ V_2 = this.str.charAt(this.ptr) != '}';
+ if (!V_2) {
break;
}
this.ptr = this.ptr + 1;
}
- this.format = this.format + str._Substring$1(V_0, this.ptr - V_0);
+ this.format = this.format + this.str._Substring$1(V_0, this.ptr - V_0);
}
else {
this.format = null;
}
var D_1 = this.ptr;
- V_2 = D_1;
+ V_3 = D_1;
this.ptr = D_1 + 1;
- V_1 = str.charAt(V_2) == '}';
- if (!V_1) {
- throw new System.FormatException().$ctor$1("Input string was not in a correct format.");
+ V_2 = this.str.charAt(V_3) == '}';
+ if (!V_2) {
+ throw new System.FormatException().$ctor$1("Missing end characeter.");
}
}
catch (__ex__) {
@@ -292,8 +293,9 @@ System.String_FormatSpecifier.prototype.ParseFormatSpecifier = function(str) {
};
System.String_FormatSpecifier.prototype.$ctor = function(str, ptr) {
+ this.str = str;
this.ptr = ptr;
- this.ParseFormatSpecifier(str);
+ this.ParseFormatSpecifier();
return this;
};
@@ -336,7 +338,6 @@ String.formatHelper = function(result, format, args) {
V_8 = V_2 != '{';
if (!V_8) {
result.Append$5(format, V_1, (V_0 - V_1) - 1);
- console.log(result.toString());
V_8 = format.charAt(V_0) != '{';
if (!V_8) {
var D_2 = V_0;
@@ -396,6 +397,10 @@ String.formatHelper = function(result, format, args) {
}
}
}
+ V_8 = V_1 >= format.length;
+ if (!V_8) {
+ result.Append$5(format, V_1, format.length - V_1);
+ }
return result;
};
Oops, something went wrong.

0 comments on commit 682ed03

Please sign in to comment.