Skip to content

Commit

Permalink
String element length (#2854)
Browse files Browse the repository at this point in the history
* Create codeql.yml

* implement multiple string length

* Delete codeql.yml

* remove byte length

* remove `stringCharLength` and add more test cases for stringElementLength

* Update src/Neo/SmartContract/Native/StdLib.cs

Co-authored-by: Shargon <[email protected]>

* Update tests/Neo.UnitTests/SmartContract/Native/UT_StdLib.cs

Co-authored-by: Shargon <[email protected]>

* Remove empty line

* add bad utf-8 unit test

---------

Co-authored-by: Shargon <[email protected]>
  • Loading branch information
Jim8y and shargon authored Sep 15, 2023
1 parent 5bccb94 commit 5207956
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
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");

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

0 comments on commit 5207956

Please sign in to comment.