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

String element length #2854

Merged
merged 20 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 18 additions & 0 deletions src/Neo/SmartContract/Native/StdLib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using System;
using System.Globalization;
using System.Numerics;
using System.Text;

namespace Neo.SmartContract.Native
{
Expand Down Expand Up @@ -222,5 +223,22 @@ private static string[] StringSplit([MaxLength(MaxInputLength)] string str, stri
StringSplitOptions options = removeEmptyEntries ? StringSplitOptions.RemoveEmptyEntries : StringSplitOptions.None;
return str.Split(separator, options);
}

[ContractMethod(CpuFee = 1 << 8)]
private static int StrLen([MaxLength(MaxInputLength)] string str)
{
// return the length of the string in elements
// it should return 1 for both "🦆" and "ã"

TextElementEnumerator enumerator = StringInfo.GetTextElementEnumerator(str);
int count = 0;

while (enumerator.MoveNext())
{
count++;
}

return count;
}
}
}
41 changes: 41 additions & 0 deletions tests/Neo.UnitTests/SmartContract/Native/UT_StdLib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,47 @@ public void StringSplit()
Assert.AreEqual("b", arr[1].GetString());
}

[TestMethod]
public void StringElementLength()
{
var snapshot = TestBlockchain.GetTestSnapshot();

using var script = new ScriptBuilder();
script.EmitDynamicCall(NativeContract.StdLib.Hash, "strLen", "🦆");
script.EmitDynamicCall(NativeContract.StdLib.Hash, "strLen", "ã");
script.EmitDynamicCall(NativeContract.StdLib.Hash, "strLen", "a");

Jim8y marked this conversation as resolved.
Show resolved Hide resolved
using var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, settings: TestBlockchain.TheNeoSystem.Settings);
engine.LoadScript(script.ToArray());

Assert.AreEqual(engine.Execute(), VMState.HALT);
Assert.AreEqual(3, engine.ResultStack.Count);
Assert.AreEqual(1, engine.ResultStack.Pop().GetInteger());
Assert.AreEqual(1, engine.ResultStack.Pop().GetInteger());
Assert.AreEqual(1, engine.ResultStack.Pop().GetInteger());
}

[TestMethod]
public void TestInvalidUtf8Sequence()
{
// Simulating invalid UTF-8 byte (0xff) decoded as a UTF-16 char
const char badChar = (char)0xff;
var badStr = badChar.ToString();
var snapshot = TestBlockchain.GetTestSnapshot();

using var script = new ScriptBuilder();
script.EmitDynamicCall(NativeContract.StdLib.Hash, "strLen", badStr);
script.EmitDynamicCall(NativeContract.StdLib.Hash, "strLen", badStr + "ab");

using var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, settings: TestBlockchain.TheNeoSystem.Settings);
engine.LoadScript(script.ToArray());

Assert.AreEqual(engine.Execute(), VMState.HALT);
Assert.AreEqual(2, engine.ResultStack.Count);
Assert.AreEqual(3, engine.ResultStack.Pop().GetInteger());
Assert.AreEqual(1, engine.ResultStack.Pop().GetInteger());
}

[TestMethod]
public void Json_Deserialize()
{
Expand Down