mirror of
https://github.com/danbulant/Cosmos
synced 2026-06-12 11:11:45 +00:00
Added plugs for Convert.ToString
Fixed int.ToString("x")
This commit is contained in:
parent
ec36f1cbb0
commit
e62eae1414
4 changed files with 97 additions and 0 deletions
|
|
@ -39,6 +39,7 @@ namespace Cosmos.Compiler.Tests.Bcl
|
|||
DelegatesTest.Execute();
|
||||
EventsTest.Execute();
|
||||
RandomTests.Execute();
|
||||
ConvertTests.Execute();
|
||||
|
||||
// System.Collections
|
||||
HashtableTest.Execute();
|
||||
|
|
|
|||
|
|
@ -0,0 +1,23 @@
|
|||
using Cosmos.TestRunner;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Cosmos.Compiler.Tests.Bcl.System
|
||||
{
|
||||
public static class ConvertTests
|
||||
{
|
||||
public static void Execute()
|
||||
{
|
||||
Assert.AreEqual("1010", Convert.ToString(10, 2), "Convert.ToString(int, 2) works");
|
||||
Assert.AreEqual("12", Convert.ToString(10, 8), "Convert.ToString(int, 8) works");
|
||||
Assert.AreEqual("10", Convert.ToString(10, 10), "Convert.ToString(int, 10) works");
|
||||
Assert.AreEqual("A", Convert.ToString(10, 16), "Convert.ToString(int, 16) works");
|
||||
Assert.AreEqual("11000100000", Convert.ToString(1568, 2), "Convert.ToString(int, 2) works");
|
||||
Assert.AreEqual("3040", Convert.ToString(1568, 8), "Convert.ToString(int, 8) works");
|
||||
Assert.AreEqual("1568", Convert.ToString(1568, 10), "Convert.ToString(int, 10) works");
|
||||
Assert.AreEqual("620", Convert.ToString(1568, 16), "Convert.ToString(int, 16) works");
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -20,6 +20,11 @@ namespace Cosmos.System_Plugs.System
|
|||
{
|
||||
string result = "";
|
||||
|
||||
if(aThis == 0)
|
||||
{
|
||||
result = "0";
|
||||
}
|
||||
|
||||
while (aThis != 0)
|
||||
{
|
||||
if ((aThis % 16) < 10)
|
||||
|
|
|
|||
68
source/Cosmos.System2_Plugs/System/ParseNumbersImpl.cs
Normal file
68
source/Cosmos.System2_Plugs/System/ParseNumbersImpl.cs
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using Cosmos.Common.Extensions;
|
||||
using IL2CPU.API.Attribs;
|
||||
|
||||
namespace Cosmos.System_Plugs.System
|
||||
{
|
||||
[Plug(TargetName = "System.ParseNumbers, System.Private.CoreLib")]
|
||||
class ParseNumbersImpl
|
||||
{
|
||||
public static string IntToString(int value, int radix, int width, char paddingChar, int flags)
|
||||
{
|
||||
if (flags != 0)
|
||||
{
|
||||
throw new NotImplementedException("IntToString with non-zero flags is not supported");
|
||||
}
|
||||
string valueString = "";
|
||||
|
||||
if (radix == 2 || radix == 8 || radix == 16)
|
||||
{
|
||||
int shiftRightAmount = 1;
|
||||
if (radix == 8)
|
||||
{
|
||||
shiftRightAmount = 3;
|
||||
}
|
||||
else if (radix == 16)
|
||||
{
|
||||
shiftRightAmount = 4;
|
||||
}
|
||||
if (value < 0)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
while (value > 0)
|
||||
{
|
||||
valueString = (value % radix).ToString("X") + valueString;
|
||||
value >>= shiftRightAmount;
|
||||
}
|
||||
}
|
||||
else if (radix == 10)
|
||||
{
|
||||
valueString = value.ToString();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException(nameof(radix));
|
||||
}
|
||||
|
||||
if (width == -1)
|
||||
{
|
||||
return valueString;
|
||||
}
|
||||
|
||||
if (valueString.Length > width)
|
||||
{
|
||||
throw new NotImplementedException("IntToString Case not handled when value is longer than width");
|
||||
}
|
||||
|
||||
int count = width - valueString.Length;
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
valueString = paddingChar + valueString;
|
||||
}
|
||||
return valueString;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue