Skip to content

Commit

Permalink
Add Itoa atoi syscalls (neo-project#2043)
Browse files Browse the repository at this point in the history
* Itoa atoi

* add base

* Update src/neo/SmartContract/ApplicationEngine.Binary.cs

Co-authored-by: Erik Zhang <[email protected]>

* Update src/neo/SmartContract/ApplicationEngine.Binary.cs

Co-authored-by: Erik Zhang <[email protected]>

* Fix ut

* Clean code

* Update src/neo/SmartContract/ApplicationEngine.Binary.cs

Co-authored-by: Erik Zhang <[email protected]>

* Update src/neo/SmartContract/ApplicationEngine.Binary.cs

Co-authored-by: Erik Zhang <[email protected]>

* Fix ut

Co-authored-by: Erik Zhang <[email protected]>
  • Loading branch information
2 people authored and Shawn committed Jan 8, 2021
1 parent f1e782a commit c8b4e39
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
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

0 comments on commit c8b4e39

Please sign in to comment.