-
Notifications
You must be signed in to change notification settings - Fork 1k
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
String element length #2854
Changes from 5 commits
7570cec
0aceab8
503a7cd
97e656f
47c471a
7f5ef10
eba4b5d
f73d159
af687e4
9d17e23
af18ffc
6784cf0
8d356ea
d14643e
f390735
1037124
a61b822
715c640
aeedf50
b55e479
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
using System; | ||
using System.Globalization; | ||
using System.Numerics; | ||
using System.Text; | ||
|
||
namespace Neo.SmartContract.Native | ||
{ | ||
|
@@ -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) | ||
{ | ||
// 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 "ã" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need it? It's tied to .NET-specific definition of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will remove this one then. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @roman-khimov updated, may you please review again There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
|
||
} | ||
} |
There was a problem hiding this comment.
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
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exactly, just thought it would be better to make the naming style consistent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is not the same because this take in care about the unicode chars