Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Itoa atoi syscalls #2043

Merged
merged 12 commits into from
Nov 8, 2020
25 changes: 25 additions & 0 deletions src/neo/SmartContract/ApplicationEngine.Binary.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using Neo.Cryptography;
using Neo.VM.Types;
using System;
using System.Globalization;
using System.Numerics;
using static System.Convert;

namespace Neo.SmartContract
Expand All @@ -12,6 +15,8 @@ partial class ApplicationEngine
public static readonly InteropDescriptor System_Binary_Base64Decode = Register("System.Binary.Base64Decode", nameof(Base64Decode), 0_00100000, CallFlags.None, true);
public static readonly InteropDescriptor System_Binary_Base58Encode = Register("System.Binary.Base58Encode", nameof(Base58Encode), 0_00100000, CallFlags.None, true);
public static readonly InteropDescriptor System_Binary_Base58Decode = Register("System.Binary.Base58Decode", nameof(Base58Decode), 0_00100000, CallFlags.None, true);
public static readonly InteropDescriptor System_Binary_Itoa = Register("System.Binary.Itoa", nameof(Itoa), 0_00100000, CallFlags.None, true);
public static readonly InteropDescriptor System_Binary_Atoi = Register("System.Binary.Atoi", nameof(Atoi), 0_00100000, CallFlags.None, true);

protected internal byte[] BinarySerialize(StackItem item)
{
Expand All @@ -23,6 +28,26 @@ protected internal StackItem BinaryDeserialize(byte[] data)
return BinarySerializer.Deserialize(data, Limits.MaxStackSize, Limits.MaxItemSize, ReferenceCounter);
}

protected internal string Itoa(BigInteger value, int @base)
{
return @base switch
{
10 => value.ToString(),
16 => value.ToString("x"),
_ => throw new ArgumentOutOfRangeException(nameof(@base))
};
}

protected internal BigInteger Atoi(string value, int @base)
{
return @base switch
{
10 => BigInteger.Parse(value),
16 => BigInteger.Parse(value, NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture),
_ => throw new ArgumentOutOfRangeException(nameof(@base))
};
}

protected internal string Base64Encode(byte[] data)
{
return ToBase64String(data);
Expand Down
17 changes: 17 additions & 0 deletions tests/neo.UnitTests/SmartContract/UT_ApplicationEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Neo.Ledger;
using Neo.SmartContract;
using Neo.VM.Types;
using System.Numerics;

namespace Neo.UnitTests.SmartContract
{
Expand Down Expand Up @@ -38,6 +39,22 @@ public void TestBinary()
Assert.AreEqual("2VfUX", engine.Base58Encode(new byte[] { 1, 2, 3, 4 }));
}

[TestMethod]
public void TestItoaAtoi()
{
using var engine = ApplicationEngine.Create(TriggerType.Application, null, null);

Assert.AreEqual("1", engine.Itoa(BigInteger.One, 10));
Assert.AreEqual("1", engine.Itoa(BigInteger.One, 16));
Assert.AreEqual("-1", engine.Itoa(BigInteger.MinusOne, 10));
Assert.AreEqual("f", engine.Itoa(BigInteger.MinusOne, 16));
Assert.AreEqual(-1, engine.Atoi("-1", 10));
Assert.AreEqual(1, engine.Atoi("+1", 10));
Assert.AreEqual(-1, engine.Atoi("ff", 16));
Assert.ThrowsException<System.FormatException>(() => engine.Atoi("a", 10));
Assert.ThrowsException<System.ArgumentOutOfRangeException>(() => engine.Atoi("a", 11));
}

[TestMethod]
public void TestNotify()
{
Expand Down