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

Added some conversions #474

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 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
3 changes: 1 addition & 2 deletions src/Neo.Compiler.MSIL/MSIL/Conv_Multi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -670,8 +670,7 @@ private int ConvertCall(OpCode src, NeoMethod to, List<MethodToken> methodTokens
}
else if (src.tokenMethod == "System.Char System.String::get_Chars(System.Int32)")
{
ConvertPushNumber(1, src, to);
Convert1by1(VM.OpCode.SUBSTR, null, to);
Convert1by1(VM.OpCode.PICKITEM, src, to);
return 0;
}
else if (src.tokenMethod == "System.String System.String::Substring(System.Int32)")
Expand Down
10 changes: 10 additions & 0 deletions src/Neo.SmartContract.Framework/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,22 @@ public static byte ToByte(this int source)
[OpCode(OpCode.LEFT)]
public extern static byte[] Take(this byte[] source, int count);
erikzhang marked this conversation as resolved.
Show resolved Hide resolved

[OpCode(OpCode.SWAP)]
[OpCode(OpCode.LEFT)]
[OpCode(OpCode.CONVERT, StackItemType.ByteString)]
public extern static string Take(this string source, int count);

/// <summary>
/// Returns byte[] with last 'count' elements from 'source'. Faults if count < 0
/// </summary>
[OpCode(OpCode.RIGHT)]
public extern static byte[] Last(this byte[] source, int count);

[OpCode(OpCode.SWAP)]
[OpCode(OpCode.RIGHT)]
[OpCode(OpCode.CONVERT, StackItemType.ByteString)]
public extern static string Last(this string source, int count);

/// <summary>
/// Returns a reversed copy of parameter 'source'.
/// Example: [0a,0b,0c,0d,0e] -> [0e,0d,0c,0b,0a]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Text;
using System.Numerics;
using System.Runtime.CompilerServices;
using Neo.SmartContract.Framework;

namespace Neo.Compiler.MSIL.UnitTests.TestClasses
{
class Contract_Types_String : SmartContract.Framework.SmartContract
{
public static char checkIndex(string cad, int pos) { return cad[pos]; }
public static string checkRange(string cad, int pos, int count) { return cad.Substring(pos, count); }
public static string checkTake(string cad, int count) { return cad.Take(count); }
public static string checkLast(string cad, int count) { return cad.Last(count); }
}
}
34 changes: 34 additions & 0 deletions tests/Neo.Compiler.MSIL.UnitTests/UnitTest_Types.cs
Original file line number Diff line number Diff line change
Expand Up @@ -521,5 +521,39 @@ public void UInt160_byte_array_construct()
var received = new UInt160(((ByteString)item).GetSpan());
Assert.AreEqual(received, notZero);
}

[TestMethod]
public void String_Methods()
{
using var testengine = new TestEngine();
testengine.AddEntryScript("./TestClasses/Contract_Types_String.cs");

var result = testengine.ExecuteTestCaseStandard("checkIndex", "hello", 4);
Assert.AreEqual(1, result.Count);
var item = result.Pop();
Assert.IsTrue(item is Integer);
Assert.AreEqual((byte)'o', item.GetInteger());

testengine.Reset();
result = testengine.ExecuteTestCaseStandard("checkTake", "hello", 4);
Assert.AreEqual(1, result.Count);
item = result.Pop();
Assert.IsTrue(item is ByteString);
Assert.AreEqual("hell", item.GetString());

testengine.Reset();
result = testengine.ExecuteTestCaseStandard("checkLast", "hello", 2);
Assert.AreEqual(1, result.Count);
item = result.Pop();
Assert.IsTrue(item is ByteString);
Assert.AreEqual("lo", item.GetString());

testengine.Reset();
result = testengine.ExecuteTestCaseStandard("checkRange", "hello", 1, 2);
Assert.AreEqual(1, result.Count);
item = result.Pop();
Assert.IsTrue(item is ByteString);
Assert.AreEqual("el", item.GetString());
}
}
}