Skip to content
This repository has been archived by the owner on Aug 24, 2022. It is now read-only.

Char based on number #938

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
JSIL.MakeClass("System.Object", "JSIL.StringEnumerator", true, ["T"], function ($) {
var T = new JSIL.GenericParameter("T", "JSIL.StringEnumerator");

$.RawMethod(false, "__CopyMembers__",
function ArrayEnumerator_CopyMembers(source, target) {
target._array = source._array;
target._length = source._length;
target._index = source._index;
}
);

$.Method({ Public: true, Static: false }, ".ctor",
new JSIL.MethodSignature(null, [$jsilcore.TypeRef("System.Array", ["!!0"]), $.Int32]),
function (array, startPosition) {
this._array = array;
this._length = array.length;
if (typeof (startPosition) !== "number")
JSIL.RuntimeError("ArrayEnumerator ctor second argument must be number");

this._index = startPosition;
}
);

$.Method({ Public: true, Static: false }, "Reset",
new JSIL.MethodSignature(null, []),
function () {
if (this._array === null)
JSIL.RuntimeError("Enumerator is disposed or not initialized");

this._index = -1;
}
);

$.Method({ Public: true, Static: false }, "MoveNext",
new JSIL.MethodSignature(System.Boolean, []),
function () {
return (++this._index < this._length);
}
);

$.Method({ Public: true, Static: false }, "Dispose",
new JSIL.MethodSignature(null, []),
function () {
this._array = null;
this._index = 0;
this._length = -1;
}
);

$.Method({ Public: false, Static: false }, null,
new JSIL.MethodSignature(System.Object, []),
function () {
return this._array.charCodeAt(this._index);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This matches .NET, right? re: surrogate pairs, etc.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll check it additionally, but really .Net strings (same as JS string) knows nothing about surrogate pairs.
So, it should just break them into two Chars (and Char is unable to represent surrogate pair, as it only Int16, and we need Int32 for surrogate pairs).

}
)
.Overrides("System.Collections.IEnumerator", "get_Current");

$.Method({ Public: true, Static: false }, "get_Current",
new JSIL.MethodSignature(T, []),
function () {
return this._array.charCodeAt(this._index);
}
)
.Overrides("System.Collections.Generic.IEnumerator`1", "get_Current");

$.Property({ Public: true, Static: false, Virtual: true }, "Current");

$.ImplementInterfaces(
/* 0 */ System.IDisposable,
/* 1 */ System.Collections.IEnumerator,
/* 2 */ $jsilcore.TypeRef("System.Collections.Generic.IEnumerator`1", [new JSIL.GenericParameter("T", "JSIL.StringEnumerator")])
);
});
4 changes: 2 additions & 2 deletions JSIL.Libraries/Includes/Bootstrap/Core/Classes/System.Char.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
JSIL.ImplementExternals(
"System.Char", function ($) {
$.RawMethod(true, "CheckType", function (value) {
return ((typeof (value) === "string") && (value.length == 1)) || JSIL.Box.IsBoxedOfType(value, $.Type);
return (typeof (value) === "number") || JSIL.Box.IsBoxedOfType(value, $.Type);
});

$.Constant({ Public: true, Static: true }, "MaxValue", "\uffff");
$.Constant({ Public: true, Static: true }, "MinValue", "\0");
}
);
JSIL.MakeNumericType(String, "System.Char", true, null, JSIL.MakeIConvertibleMethods);
JSIL.MakeNumericType(Number, "System.Char", true, null, JSIL.MakeIConvertibleMethods);
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@ JSIL.MakeArrayEnumerator = function (array, elementType) {
return new tArrayEnumerator(array, -1);
};

JSIL.MakeStringEnumerator = function (array, elementType) {
var tArrayEnumerator;

if (!elementType) {
if ($jsilcore.$tArrayEnumerator === null)
$jsilcore.$tArrayEnumerator = JSIL.StringEnumerator.Of(System.Object);

tArrayEnumerator = $jsilcore.$tArrayEnumerator;
} else {
tArrayEnumerator = JSIL.StringEnumerator.Of(elementType);
}

return new tArrayEnumerator(array, -1);
};

JSIL.GetEnumerator = function (enumerable, elementType, fallbackMethodInvoke) {
if ((typeof (enumerable) === "undefined") || (enumerable === null))
JSIL.RuntimeError("Enumerable is null or undefined");
Expand All @@ -33,7 +48,7 @@ JSIL.GetEnumerator = function (enumerable, elementType, fallbackMethodInvoke) {
else if (enumerable.__IsArray__)
result = JSIL.MakeArrayEnumerator(enumerable.Items, elementType);
else if (typeof (enumerable) === "string")
result = JSIL.MakeArrayEnumerator(enumerable, elementType);
result = JSIL.MakeStringEnumerator(enumerable, elementType);
else if ((fallbackMethodInvoke !== true) && tIEnumerable$b1 && tIEnumerable$b1.$Is(enumerable))
result = tIEnumerable$b1.GetEnumerator.Call(enumerable);
else if ((fallbackMethodInvoke !== true) && tIEnumerable.$Is(enumerable))
Expand Down
1 change: 1 addition & 0 deletions JSIL.Libraries/Includes/Bootstrap/Core/Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ JSIL.DeclareNamespace("System.Runtime.InteropServices");
//? include("Helpers/JSIL.MakeIConvertibleMethods.js"); writeln();

//? include("Classes/JSIL.ArrayEnumerator.js"); writeln();
//? include("Classes/JSIL.StringEnumerator.js"); writeln();

//? include("Classes/System.Boolean.js"); writeln();
//? include("Classes/System.Char.js"); writeln();
Expand Down
32 changes: 13 additions & 19 deletions JSIL.Libraries/Includes/Bootstrap/Text/Classes/System.Char.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,33 @@ JSIL.ImplementExternals("System.Char", function ($) {
new JSIL.MethodSignature($.Boolean, [$.Char], []),
function IsControl(c) {
// FIXME: Unicode
var charCode = c.charCodeAt(0);
return (charCode <= 0x1F) || (charCode === 0x7F);
return (c <= 0x1F) || (c === 0x7F);
}
);

$.Method({ Static: true, Public: true }, "IsDigit",
new JSIL.MethodSignature($.Boolean, [$.Char], []),
function IsDigit(c) {
// FIXME: Unicode
var charCode = c.charCodeAt(0);
return (charCode >= 48) && (charCode <= 57);
return (c >= 48) && (c <= 57);
}
);

$.Method({ Static: true, Public: true }, "IsLetter",
new JSIL.MethodSignature($.Boolean, [$.Char], []),
function IsLetter(c) {
// FIXME: Unicode
var charCode = c.charCodeAt(0);
return (
((charCode >= 65) && (charCode <= 90)) ||
((charCode >= 97) && (charCode <= 122)));
((c >= 65) && (c <= 90)) ||
((c >= 97) && (c <= 122)));
}
);

$.Method({ Static: true, Public: true }, "IsNumber",
new JSIL.MethodSignature($.Boolean, [$.Char], []),
function IsNumeric(c) {
// FIXME: Unicode
var charCode = c.charCodeAt(0);
return (charCode >= 48) && (charCode <= 57);
return (c >= 48) && (c <= 57);
}
);

Expand All @@ -47,42 +43,40 @@ JSIL.ImplementExternals("System.Char", function ($) {
$.Method({ Static: true, Public: true }, "IsSurrogate",
new JSIL.MethodSignature($.Boolean, [$.Char], []),
function IsSurrogate(c) {
var charCode = c.charCodeAt(0);
return (charCode >= 0xD800) && (charCode <= 0xDFFF);
return (c >= 0xD800) && (c <= 0xDFFF);
}
);

$.Method({ Static: true, Public: true }, "IsWhiteSpace",
new JSIL.MethodSignature($.Boolean, [$.Char], []),
function IsWhiteSpace(c) {
// FIXME: Unicode
var charCode = c.charCodeAt(0);
return (
((charCode >= 0x09) && (charCode <= 0x13)) ||
(charCode === 0x20) ||
(charCode === 0xA0) ||
(charCode === 0x85));
((c >= 0x09) && (c <= 0x13)) ||
(c === 0x20) ||
(c === 0xA0) ||
(c === 0x85));
}
);

$.Method({ Static: true, Public: true }, "ToLowerInvariant",
new JSIL.MethodSignature($.Char, [$.Char], []),
function ToLowerInvariant(c) {
return c.toLowerCase();
return String.fromCharCode(c).toLowerCase().charCodeAt(0);
}
);

$.Method({ Static: true, Public: true }, "ToUpperInvariant",
new JSIL.MethodSignature($.Char, [$.Char], []),
function ToLowerInvariant(c) {
return c.toUpperCase();
return String.fromCharCode(c).toUpperCase().charCodeAt(0);
}
);

$.Method({ Static: true, Public: true }, "ToString",
new JSIL.MethodSignature($.String, [$.Char], []),
function ToString(c) {
return c;
return String.fromCharCode(c);
}
);
});
13 changes: 7 additions & 6 deletions JSIL.Libraries/Includes/Bootstrap/Text/Classes/System.String.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ JSIL.ImplementExternals(
$.Method({ Static: false, Public: true }, ".ctor",
new JSIL.MethodSignature(null, ["System.Char", "System.Int32"], [], $jsilcore),
function (ch, length) {
var symbol = String.fromCharCode(ch);
var arr = new Array(length);
for (var i = 0; i < length; i++)
arr[i] = ch;
arr[i] = symbol;

return new String(arr.join(""));
}
Expand Down Expand Up @@ -151,7 +152,7 @@ JSIL.ImplementExternals(
function (str, chars, startIndex) {
var result = null;
for (var i = startIndex || 0; i < chars.length; i++) {
var index = str.indexOf(chars[i]);
var index = str.indexOf(String.fromCharCode(chars[i]));
if ((result === null) || (index < result))
result = index;
}
Expand Down Expand Up @@ -198,7 +199,7 @@ JSIL.ImplementExternals(
function (str, chars) {
var result = null;
for (var i = 0; i < chars.length; i++) {
var index = str.lastIndexOf(chars[i]);
var index = str.lastIndexOf(String.fromCharCode(chars[i]));
if ((result === null) || (index > result))
result = index;
}
Expand Down Expand Up @@ -247,7 +248,7 @@ JSIL.ImplementExternals(
if (extraChars <= 0)
return str;

return makePadding(ch, extraChars) + str;
return makePadding(String.fromCharCode(ch), extraChars) + str;
}
);

Expand All @@ -258,7 +259,7 @@ JSIL.ImplementExternals(
if (extraChars <= 0)
return str;

return str + makePadding(ch, extraChars);
return str + makePadding(String.fromCharCode(ch), extraChars);
}
);

Expand All @@ -267,7 +268,7 @@ JSIL.ImplementExternals(
function (str, sourceIndex, destination, destinationIndex, count) {
if (count > 0) {
for (var i = 0; i < count; i++)
destination[destinationIndex + i] = str[sourceIndex + i];
destination[destinationIndex + i] = str.charCodeAt(sourceIndex + i);
}
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ JSIL.ImplementExternals("System.Text.StringBuilder", function ($) {
$.Method({ Static: false, Public: true }, "Append",
(new JSIL.MethodSignature($.Type, [$.Char, $.Int32], [])),
function Append(value, repeatCount) {
appendString(this, value, 0, value.length, repeatCount);
appendString(this, String.fromCharCode(value), 0, 1, repeatCount);
}
);

Expand All @@ -74,7 +74,7 @@ JSIL.ImplementExternals("System.Text.StringBuilder", function ($) {
], [])),
function Append(value, startIndex, charCount) {
for (var i = 0; i < charCount; i++)
this._str += value[startIndex + i];
this._str += String.fromCharCode(value[startIndex + i]);

this._capacity = Math.max(this._capacity, this._str.length);
}
Expand Down Expand Up @@ -130,7 +130,7 @@ JSIL.ImplementExternals("System.Text.StringBuilder", function ($) {
$.Method({ Static: false, Public: true }, "Append",
(new JSIL.MethodSignature($.Type, [$.Char], [])),
function Append(value) {
appendString(this, value, 0, value.length, 1);
appendString(this, String.fromCharCode(value), 0, 1, 1);
}
);

Expand Down Expand Up @@ -194,7 +194,7 @@ JSIL.ImplementExternals("System.Text.StringBuilder", function ($) {
(new JSIL.MethodSignature($.Type, [$jsilcore.TypeRef("System.Array", [$.Char])], [])),
function Append(value) {
for (var i = 0; i < value.length; i++)
this._str += value[i];
this._str += String.fromCharCode(value[i]);

this._capacity = Math.max(this._capacity, this._str.length);
}
Expand Down Expand Up @@ -322,7 +322,7 @@ JSIL.ImplementExternals("System.Text.StringBuilder", function ($) {
$.Method({ Static: false, Public: true }, "Replace",
(new JSIL.MethodSignature($.Type, [$.Char, $.Char], [])),
function Replace(oldChar, newChar) {
return replace(this, oldChar, newChar, 0, this._str.length);
return replace(this, String.fromCharCode(oldChar), String.fromCharCode(newChar), 0, this._str.length);
}
);

Expand All @@ -332,7 +332,7 @@ JSIL.ImplementExternals("System.Text.StringBuilder", function ($) {
$.Int32, $.Int32
], [])),
function Replace(oldChar, newChar, startIndex, count) {
return replace(this, oldChar, newChar, startIndex, count);
return replace(this, String.fromCharCode(oldChar), String.fromCharCode(newChar), startIndex, count);
}
);

Expand Down Expand Up @@ -385,7 +385,7 @@ JSIL.ImplementExternals("System.Text.StringBuilder", function ($) {
$.Method({ Static: false, Public: true }, "get_Chars",
(new JSIL.MethodSignature($.Char, [$.Int32], [])),
function get_Chars(i) {
return this._str[i];
return this._str.charCodeAt(i);
}
);

Expand All @@ -397,7 +397,7 @@ JSIL.ImplementExternals("System.Text.StringBuilder", function ($) {
}
this._str =
this._str.substr(0, i) +
value +
String.fromCharCode(value) +
this._str.substr(i + 1);
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ JSIL.ConcatString = function (/* ...values */) {
;
else if (typeof (arg) === "string")
result += arg;
else if ($jsilcore.JSIL.Box.IsBoxedOfType(arg, $jsilcore.System.Char))
result += String.fromCharCode(arg.valueOf());
else
result += String(arg);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ JSIL.SplitString = function (str, separators, options) {
if (!separators) {
// Whitespace characters from Unicode 6.0
separators = [
"\u0009", "\u000A", "\u000B", "\u000C", "\u000D", "\u0020", "\u0085", "\u00A0",
"\u1680", "\u180E", "\u2000", "\u2001", "\u2002", "\u2003", "\u2004", "\u2005",
"\u2006", "\u2007", "\u2008", "\u2009", "\u200A", "\u2028", "\u2029", "\u202F",
"\u205F", "\u3000"
0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x0020, 0x0085, 0x00A0,
0x1680, 0x180E, 0x2000, 0x2001, 0x2002, 0x2003, 0x2004, 0x2005,
0x2006, 0x2007, 0x2008, 0x2009, 0x200A, 0x2028, 0x2029, 0x202F,
0x205F, 0x3000
];
}

if (separators.length === 1) {
return str.split(separators[0]);
return str.split(String.fromCharCode(separators[0]));
} else {
var regexText = "";
for (var i = 0; i < separators.length; i++) {
if (i > 0)
regexText += "|"

regexText += JSIL.EscapeJSRegex(separators[i]);
regexText += JSIL.EscapeJSRegex(String.fromCharCode(separators[i]));
}
var regex = new RegExp(regexText, "g");

Expand Down
Loading