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 5 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
35 changes: 35 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,39 @@ 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 StringByteLength([MaxLength(MaxInputLength)] string str)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is same as OpCode.SIZE?

Copy link
Contributor Author

@Jim8y Jim8y Mar 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is same as OpCode.SIZE?

Exactly, just thought it would be better to make the naming style consistent.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is same as OpCode.SIZE?

Exactly, just thought it would be better to make the naming style consistent.

Is not the same because this take in care about the unicode chars

{
// return the length of the string in bytes
// it should return 4 for "🦆" and 2 for "ã"
return Encoding.UTF8.GetByteCount(str);
}

[ContractMethod(CpuFee = 1 << 8)]
private static int StringCharLength([MaxLength(MaxInputLength)] string str)
{
// return the length of the string in characters
// it should return 2 for "🦆" and 1 for "ã"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need it? It's tied to .NET-specific definition of Char and UTF-16 which is very niche. At the moment I just don't know how it could be implemented in Go. utf8.RuneCountInString works fine or StringElementLength, but StringCharLength looks very much C#/.NET specific and not very useful at the first glance.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will remove this one then.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@roman-khimov updated, may you please review again

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks OK, just one simple method.

return str.Length;
}

[ContractMethod(CpuFee = 1 << 8)]
private static int StringElementLength([MaxLength(MaxInputLength)] string str)
Jim8y marked this conversation as resolved.
Show resolved Hide resolved
{
// 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;
}

}
}
53 changes: 53 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,59 @@ public void StringSplit()
Assert.AreEqual("b", arr[1].GetString());
}

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

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

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(1, engine.ResultStack.Count);

var res = engine.ResultStack.Pop<Integer>();
Assert.AreEqual(4, res);
}

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

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

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(1, engine.ResultStack.Count);

var res = engine.ResultStack.Pop<Integer>();
Assert.AreEqual(2, res);
}
[TestMethod]
public void StringElementLength()
{
var snapshot = TestBlockchain.GetTestSnapshot();

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

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(1, engine.ResultStack.Count);

var res = engine.ResultStack.Pop<Integer>();
Assert.AreEqual(1, res);
}

[TestMethod]
public void Json_Deserialize()
{
Expand Down