Skip to content

Commit

Permalink
Merge pull request CosmosOS#2861 from Guillermo-Santos/int32_tostring
Browse files Browse the repository at this point in the history
Updates on int.ToString(`format`)
  • Loading branch information
quajak authored Jan 2, 2024
2 parents d0f59d5 + 99b0070 commit 2d1f688
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 32 deletions.
28 changes: 21 additions & 7 deletions Tests/Kernels/Cosmos.Compiler.Tests.Bcl.System/System/Int32Test.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;

using System.Linq;
using Cosmos.Common.Extensions;
using Cosmos.TestRunner;

namespace Cosmos.Compiler.Tests.Bcl.System
Expand Down Expand Up @@ -37,13 +38,26 @@ public static void Execute()
// actually the Hash Code of an Int32 is the same value
Assert.IsTrue((resultAsInt == value), "Int32.GetHashCode() doesn't work");

#if false
// Now let's try ToString() again but printed in hex (this test fails for now!)
result = value.ToString("X2");
expectedResult = "0x7FFFFFFF";
// Now let's try ToString() again but printed in hex
result = value.ToString("X");
expectedResult = "7FFFFFFF";

Assert.IsTrue((result == expectedResult), "Int32.ToString(X) brings incorrect result.");

// Ensure value is not overrided
Assert.IsTrue((value != 0), "Int32.ToString(X) overrides the value of the variable.");

// Hex with padding
value = 255;
result = value.ToString("X4");
expectedResult = "00FF";
Assert.IsTrue((result == expectedResult), "Int32.ToString(X4) brings incorrect result.");

Assert.IsTrue((result == expectedResult), "Int32.ToString(X2) doesn't work");
#endif
// Test Decimal format wit padding
value = 10;
expectedResult = "0010";
result = value.ToString("D4");
Assert.IsTrue((result == expectedResult), "Int32.ToString(D4) brings incorrect result.");

// basic bit operations

Expand Down
61 changes: 36 additions & 25 deletions source/Cosmos.System2_Plugs/System/Int32Impl.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Text;
using Cosmos.Common;
using IL2CPU.API.Attribs;

Expand All @@ -13,46 +14,56 @@ public static string ToString(ref int aThis)

public static string ToString(ref int aThis, string format)
{
if (format.Equals("X"))
if (string.IsNullOrEmpty(format))
{
string result = "";
return "0";
}

if(aThis == 0)
{
result = "0";
}
StringBuilder sb = new();

while (aThis != 0)
{
if (aThis % 16 < 10)
switch (format[0])
{
case 'X':
if (aThis == 0)
{
result = aThis % 16 + result;
sb.Append('0');
break;
}
else
{
string temp = "";

switch (aThis % 16)
int value = aThis;

while (value != 0)
{
int remainder = value % 16;
if (remainder < 10)
{
case 10: temp = "A"; break;
case 11: temp = "B"; break;
case 12: temp = "C"; break;
case 13: temp = "D"; break;
case 14: temp = "E"; break;
case 15: temp = "F"; break;
sb.Insert(0, remainder);
}
else
{
char temp = (char)('A' + (remainder - 10));
sb.Insert(0, temp);
}

result = temp + result;
value /= 16;
}
break;
case 'D':
sb.Append(aThis);
break;
default:
return aThis.ToString();
}

aThis /= 16;
}
var result = sb.ToString();

return result;
if (format.Length > 1)
{
return int.TryParse(format.AsSpan(1), out int number) ? result.PadLeft(number, '0') : aThis.ToString();
}
else
{
return aThis.ToString();
return result;
}
}

Expand Down

0 comments on commit 2d1f688

Please sign in to comment.