Skip to content

Commit

Permalink
Adding more string wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
Frank Laub committed Mar 3, 2010
1 parent 8b97cff commit 682ed03
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 82 deletions.
94 changes: 69 additions & 25 deletions src/DotWeb.System/String.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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")]
Expand All @@ -116,11 +157,26 @@ public string Substring(int startIndex, int length) {
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);
Expand Down Expand Up @@ -325,23 +381,11 @@ private int ParseDecimal() {
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
{
Expand Down
36 changes: 18 additions & 18 deletions src/DotWeb.Translator/AttributeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@ public static string GetJsName(MemberReference memberRef) {
}

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) {
Expand All @@ -72,25 +73,24 @@ public static bool IsIntrinsic(PropertyReference propertyRef, TypeSystem typeSys

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) {
Expand Down
21 changes: 20 additions & 1 deletion test/DotWeb.Functional.Test/Client/StringTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,27 @@ private void RunTest() {
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]);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
H8.DecorationTests.prototype.TestJsCamelCase = function() {
var x = new H8.CamelCaseTest().$ctor();
x.foo();
x.camelCase();
x.NotCamelCase();
this.camelCase();
};
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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__) {
Expand All @@ -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;
};

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
};

Expand Down
Loading

0 comments on commit 682ed03

Please sign in to comment.