Merge pull request #3208 from nspcc-dev/strlen

Support `strLen` native StdLib method
This commit is contained in:
Roman Khimov 2023-11-20 21:29:08 +03:00 committed by GitHub
commit f10d2d2be7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
34 changed files with 108 additions and 44 deletions

View file

@ -2,4 +2,4 @@ module github.com/nspcc-dev/neo-go/examples/engine
go 1.19
require github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120114907-e63a93b8a00e
require github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120162333-406c9f8b9214

View file

@ -1,2 +1,2 @@
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120114907-e63a93b8a00e h1:2lMbujHWDnACgVpjAY/Ci2OoFa4ZvGKGDaXt7H2mJSo=
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120114907-e63a93b8a00e/go.mod h1:J/Mk6+nKeKSW4wygkZQFLQ6SkLOSGX5Ga0RuuuktEag=
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120162333-406c9f8b9214 h1:v8XJTwSAAF4Wwp20yfW8Ihz6BBPXBOGmLloWVa5EuSM=
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120162333-406c9f8b9214/go.mod h1:J/Mk6+nKeKSW4wygkZQFLQ6SkLOSGX5Ga0RuuuktEag=

View file

@ -2,4 +2,4 @@ module github.com/nspcc-dev/neo-go/examples/events
go 1.19
require github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120114907-e63a93b8a00e
require github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120162333-406c9f8b9214

View file

@ -1,2 +1,2 @@
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120114907-e63a93b8a00e h1:2lMbujHWDnACgVpjAY/Ci2OoFa4ZvGKGDaXt7H2mJSo=
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120114907-e63a93b8a00e/go.mod h1:J/Mk6+nKeKSW4wygkZQFLQ6SkLOSGX5Ga0RuuuktEag=
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120162333-406c9f8b9214 h1:v8XJTwSAAF4Wwp20yfW8Ihz6BBPXBOGmLloWVa5EuSM=
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120162333-406c9f8b9214/go.mod h1:J/Mk6+nKeKSW4wygkZQFLQ6SkLOSGX5Ga0RuuuktEag=

View file

@ -2,4 +2,4 @@ module github.com/nspcc-dev/neo-go/examples/iterator
go 1.19
require github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120114907-e63a93b8a00e
require github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120162333-406c9f8b9214

View file

@ -1,2 +1,2 @@
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120114907-e63a93b8a00e h1:2lMbujHWDnACgVpjAY/Ci2OoFa4ZvGKGDaXt7H2mJSo=
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120114907-e63a93b8a00e/go.mod h1:J/Mk6+nKeKSW4wygkZQFLQ6SkLOSGX5Ga0RuuuktEag=
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120162333-406c9f8b9214 h1:v8XJTwSAAF4Wwp20yfW8Ihz6BBPXBOGmLloWVa5EuSM=
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120162333-406c9f8b9214/go.mod h1:J/Mk6+nKeKSW4wygkZQFLQ6SkLOSGX5Ga0RuuuktEag=

View file

@ -2,4 +2,4 @@ module github.com/nspcc-dev/neo-go/examples/nft
go 1.19
require github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120114907-e63a93b8a00e
require github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120162333-406c9f8b9214

View file

@ -1,2 +1,2 @@
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120114907-e63a93b8a00e h1:2lMbujHWDnACgVpjAY/Ci2OoFa4ZvGKGDaXt7H2mJSo=
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120114907-e63a93b8a00e/go.mod h1:J/Mk6+nKeKSW4wygkZQFLQ6SkLOSGX5Ga0RuuuktEag=
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120162333-406c9f8b9214 h1:v8XJTwSAAF4Wwp20yfW8Ihz6BBPXBOGmLloWVa5EuSM=
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120162333-406c9f8b9214/go.mod h1:J/Mk6+nKeKSW4wygkZQFLQ6SkLOSGX5Ga0RuuuktEag=

View file

@ -4,7 +4,7 @@ go 1.19
require (
github.com/nspcc-dev/neo-go v0.102.1-0.20231020181554-d89c8801d689
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120114907-e63a93b8a00e
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120162333-406c9f8b9214
github.com/stretchr/testify v1.8.4
)

View file

@ -200,8 +200,8 @@ github.com/nspcc-dev/go-ordered-json v0.0.0-20220111165707-25110be27d22/go.mod h
github.com/nspcc-dev/hrw v1.0.9 h1:17VcAuTtrstmFppBjfRiia4K2wA/ukXZhLFS8Y8rz5Y=
github.com/nspcc-dev/neo-go v0.102.1-0.20231020181554-d89c8801d689 h1:WnEdGAQwaW0C8wnNnQZ+rM/JfFKZDSTOqwm8cS0TOdk=
github.com/nspcc-dev/neo-go v0.102.1-0.20231020181554-d89c8801d689/go.mod h1:x+wmcYqpZYJwLp1l/pHZrqNp3RSWlkMymWGDij3/OPo=
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120114907-e63a93b8a00e h1:2lMbujHWDnACgVpjAY/Ci2OoFa4ZvGKGDaXt7H2mJSo=
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120114907-e63a93b8a00e/go.mod h1:J/Mk6+nKeKSW4wygkZQFLQ6SkLOSGX5Ga0RuuuktEag=
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120162333-406c9f8b9214 h1:v8XJTwSAAF4Wwp20yfW8Ihz6BBPXBOGmLloWVa5EuSM=
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120162333-406c9f8b9214/go.mod h1:J/Mk6+nKeKSW4wygkZQFLQ6SkLOSGX5Ga0RuuuktEag=
github.com/nspcc-dev/neofs-api-go/v2 v2.14.0 h1:jhuN8Ldqz7WApvUJRFY0bjRXE1R3iCkboMX5QVZhHVk=
github.com/nspcc-dev/neofs-crypto v0.4.0 h1:5LlrUAM5O0k1+sH/sktBtrgfWtq1pgpDs09fZo+KYi4=
github.com/nspcc-dev/neofs-sdk-go v1.0.0-rc.11 h1:QOc8ZRN5DXlAeRPh5QG9u8rMLgoeRNiZF5/vL7QupWg=

View file

@ -2,4 +2,4 @@ module github.com/nspcc-dev/neo-go/examples/nft-nd
go 1.19
require github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120114907-e63a93b8a00e
require github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120162333-406c9f8b9214

View file

@ -1,2 +1,2 @@
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120114907-e63a93b8a00e h1:2lMbujHWDnACgVpjAY/Ci2OoFa4ZvGKGDaXt7H2mJSo=
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120114907-e63a93b8a00e/go.mod h1:J/Mk6+nKeKSW4wygkZQFLQ6SkLOSGX5Ga0RuuuktEag=
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120162333-406c9f8b9214 h1:v8XJTwSAAF4Wwp20yfW8Ihz6BBPXBOGmLloWVa5EuSM=
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120162333-406c9f8b9214/go.mod h1:J/Mk6+nKeKSW4wygkZQFLQ6SkLOSGX5Ga0RuuuktEag=

View file

@ -2,4 +2,4 @@ module github.com/nspcc-dev/neo-go/examples/oracle
go 1.19
require github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120114907-e63a93b8a00e
require github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120162333-406c9f8b9214

View file

@ -1,2 +1,2 @@
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120114907-e63a93b8a00e h1:2lMbujHWDnACgVpjAY/Ci2OoFa4ZvGKGDaXt7H2mJSo=
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120114907-e63a93b8a00e/go.mod h1:J/Mk6+nKeKSW4wygkZQFLQ6SkLOSGX5Ga0RuuuktEag=
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120162333-406c9f8b9214 h1:v8XJTwSAAF4Wwp20yfW8Ihz6BBPXBOGmLloWVa5EuSM=
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120162333-406c9f8b9214/go.mod h1:J/Mk6+nKeKSW4wygkZQFLQ6SkLOSGX5Ga0RuuuktEag=

View file

@ -2,4 +2,4 @@ module github.com/nspcc-dev/neo-go/examples/runtime
go 1.19
require github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120114907-e63a93b8a00e
require github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120162333-406c9f8b9214

View file

@ -1,2 +1,2 @@
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120114907-e63a93b8a00e h1:2lMbujHWDnACgVpjAY/Ci2OoFa4ZvGKGDaXt7H2mJSo=
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120114907-e63a93b8a00e/go.mod h1:J/Mk6+nKeKSW4wygkZQFLQ6SkLOSGX5Ga0RuuuktEag=
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120162333-406c9f8b9214 h1:v8XJTwSAAF4Wwp20yfW8Ihz6BBPXBOGmLloWVa5EuSM=
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120162333-406c9f8b9214/go.mod h1:J/Mk6+nKeKSW4wygkZQFLQ6SkLOSGX5Ga0RuuuktEag=

View file

@ -2,4 +2,4 @@ module github.com/nspcc-dev/neo-go/examples/storage
go 1.19
require github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120114907-e63a93b8a00e
require github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120162333-406c9f8b9214

View file

@ -1,2 +1,2 @@
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120114907-e63a93b8a00e h1:2lMbujHWDnACgVpjAY/Ci2OoFa4ZvGKGDaXt7H2mJSo=
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120114907-e63a93b8a00e/go.mod h1:J/Mk6+nKeKSW4wygkZQFLQ6SkLOSGX5Ga0RuuuktEag=
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120162333-406c9f8b9214 h1:v8XJTwSAAF4Wwp20yfW8Ihz6BBPXBOGmLloWVa5EuSM=
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120162333-406c9f8b9214/go.mod h1:J/Mk6+nKeKSW4wygkZQFLQ6SkLOSGX5Ga0RuuuktEag=

View file

@ -2,4 +2,4 @@ module github.com/nspcc-dev/neo-go/examples/timer
go 1.19
require github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120114907-e63a93b8a00e
require github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120162333-406c9f8b9214

View file

@ -1,2 +1,2 @@
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120114907-e63a93b8a00e h1:2lMbujHWDnACgVpjAY/Ci2OoFa4ZvGKGDaXt7H2mJSo=
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120114907-e63a93b8a00e/go.mod h1:J/Mk6+nKeKSW4wygkZQFLQ6SkLOSGX5Ga0RuuuktEag=
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120162333-406c9f8b9214 h1:v8XJTwSAAF4Wwp20yfW8Ihz6BBPXBOGmLloWVa5EuSM=
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120162333-406c9f8b9214/go.mod h1:J/Mk6+nKeKSW4wygkZQFLQ6SkLOSGX5Ga0RuuuktEag=

View file

@ -2,4 +2,4 @@ module github.com/nspcc-dev/neo-go/examples/token
go 1.19
require github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120114907-e63a93b8a00e
require github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120162333-406c9f8b9214

View file

@ -1,2 +1,2 @@
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120114907-e63a93b8a00e h1:2lMbujHWDnACgVpjAY/Ci2OoFa4ZvGKGDaXt7H2mJSo=
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120114907-e63a93b8a00e/go.mod h1:J/Mk6+nKeKSW4wygkZQFLQ6SkLOSGX5Ga0RuuuktEag=
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120162333-406c9f8b9214 h1:v8XJTwSAAF4Wwp20yfW8Ihz6BBPXBOGmLloWVa5EuSM=
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120162333-406c9f8b9214/go.mod h1:J/Mk6+nKeKSW4wygkZQFLQ6SkLOSGX5Ga0RuuuktEag=

View file

@ -214,7 +214,7 @@ github.com/nspcc-dev/dbft v0.0.0-20230515113611-25db6ba61d5c h1:uyK5aLbAhrnZtnvo
github.com/nspcc-dev/go-ordered-json v0.0.0-20220111165707-25110be27d22 h1:n4ZaFCKt1pQJd7PXoMJabZWK9ejjbLOVrkl/lOUmshg=
github.com/nspcc-dev/go-ordered-json v0.0.0-20220111165707-25110be27d22/go.mod h1:79bEUDEviBHJMFV6Iq6in57FEOCMcRhfQnfaf0ETA5U=
github.com/nspcc-dev/hrw v1.0.9 h1:17VcAuTtrstmFppBjfRiia4K2wA/ukXZhLFS8Y8rz5Y=
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120114907-e63a93b8a00e h1:2lMbujHWDnACgVpjAY/Ci2OoFa4ZvGKGDaXt7H2mJSo=
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120162333-406c9f8b9214 h1:v8XJTwSAAF4Wwp20yfW8Ihz6BBPXBOGmLloWVa5EuSM=
github.com/nspcc-dev/neofs-api-go/v2 v2.14.0 h1:jhuN8Ldqz7WApvUJRFY0bjRXE1R3iCkboMX5QVZhHVk=
github.com/nspcc-dev/neofs-crypto v0.4.0 h1:5LlrUAM5O0k1+sH/sktBtrgfWtq1pgpDs09fZo+KYi4=
github.com/nspcc-dev/neofs-sdk-go v1.0.0-rc.11 h1:QOc8ZRN5DXlAeRPh5QG9u8rMLgoeRNiZF5/vL7QupWg=

View file

@ -2,4 +2,4 @@ module github.com/nspcc-dev/neo-go/examples/zkp/xor
go 1.19
require github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120114907-e63a93b8a00e
require github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120162333-406c9f8b9214

View file

@ -1,2 +1,2 @@
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120114907-e63a93b8a00e h1:2lMbujHWDnACgVpjAY/Ci2OoFa4ZvGKGDaXt7H2mJSo=
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120114907-e63a93b8a00e/go.mod h1:J/Mk6+nKeKSW4wygkZQFLQ6SkLOSGX5Ga0RuuuktEag=
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120162333-406c9f8b9214 h1:v8XJTwSAAF4Wwp20yfW8Ihz6BBPXBOGmLloWVa5EuSM=
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120162333-406c9f8b9214/go.mod h1:J/Mk6+nKeKSW4wygkZQFLQ6SkLOSGX5Ga0RuuuktEag=

2
go.mod
View file

@ -14,7 +14,7 @@ require (
github.com/mr-tron/base58 v1.2.0
github.com/nspcc-dev/dbft v0.0.0-20230515113611-25db6ba61d5c
github.com/nspcc-dev/go-ordered-json v0.0.0-20220111165707-25110be27d22
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120114907-e63a93b8a00e
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120162333-406c9f8b9214
github.com/nspcc-dev/neofs-sdk-go v1.0.0-rc.11
github.com/nspcc-dev/rfc6979 v0.2.0
github.com/pierrec/lz4 v2.6.1+incompatible

4
go.sum
View file

@ -229,8 +229,8 @@ github.com/nspcc-dev/go-ordered-json v0.0.0-20220111165707-25110be27d22 h1:n4ZaF
github.com/nspcc-dev/go-ordered-json v0.0.0-20220111165707-25110be27d22/go.mod h1:79bEUDEviBHJMFV6Iq6in57FEOCMcRhfQnfaf0ETA5U=
github.com/nspcc-dev/hrw v1.0.9 h1:17VcAuTtrstmFppBjfRiia4K2wA/ukXZhLFS8Y8rz5Y=
github.com/nspcc-dev/hrw v1.0.9/go.mod h1:l/W2vx83vMQo6aStyx2AuZrJ+07lGv2JQGlVkPG06MU=
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120114907-e63a93b8a00e h1:2lMbujHWDnACgVpjAY/Ci2OoFa4ZvGKGDaXt7H2mJSo=
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120114907-e63a93b8a00e/go.mod h1:J/Mk6+nKeKSW4wygkZQFLQ6SkLOSGX5Ga0RuuuktEag=
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120162333-406c9f8b9214 h1:v8XJTwSAAF4Wwp20yfW8Ihz6BBPXBOGmLloWVa5EuSM=
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120162333-406c9f8b9214/go.mod h1:J/Mk6+nKeKSW4wygkZQFLQ6SkLOSGX5Ga0RuuuktEag=
github.com/nspcc-dev/neofs-api-go/v2 v2.14.0 h1:jhuN8Ldqz7WApvUJRFY0bjRXE1R3iCkboMX5QVZhHVk=
github.com/nspcc-dev/neofs-api-go/v2 v2.14.0/go.mod h1:DRIr0Ic1s+6QgdqmNFNLIqMqd7lNMJfYwkczlm1hDtM=
github.com/nspcc-dev/neofs-crypto v0.4.0 h1:5LlrUAM5O0k1+sH/sktBtrgfWtq1pgpDs09fZo+KYi4=

View file

@ -2,4 +2,4 @@ module github.com/nspcc-dev/neo-go/internal/examples/oracle
go 1.19
require github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120114907-e63a93b8a00e
require github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120162333-406c9f8b9214

View file

@ -1,2 +1,2 @@
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120114907-e63a93b8a00e h1:2lMbujHWDnACgVpjAY/Ci2OoFa4ZvGKGDaXt7H2mJSo=
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120114907-e63a93b8a00e/go.mod h1:J/Mk6+nKeKSW4wygkZQFLQ6SkLOSGX5Ga0RuuuktEag=
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120162333-406c9f8b9214 h1:v8XJTwSAAF4Wwp20yfW8Ihz6BBPXBOGmLloWVa5EuSM=
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231120162333-406c9f8b9214/go.mod h1:J/Mk6+nKeKSW4wygkZQFLQ6SkLOSGX5Ga0RuuuktEag=

View file

@ -277,6 +277,29 @@ func TestCurrentSigners(t *testing.T) {
})
}
func TestStdLib_StrLen(t *testing.T) {
bc, acc := chain.NewSingle(t)
e := neotest.NewExecutor(t, bc, acc, acc)
src := `package foo
import (
"github.com/nspcc-dev/neo-go/pkg/interop/native/std"
)
func Main(s string) int {
return std.StrLen(s)
}`
ctr := neotest.CompileSource(t, e.CommitteeHash, strings.NewReader(src), &compiler.Options{Name: "Helper"})
e.DeployContract(t, ctr, nil)
c := e.CommitteeInvoker(ctr.Hash)
expected := stackitem.Make(1)
c.Invoke(t, expected, "main", "🦆")
c.Invoke(t, expected, "main", "ã")
c.Invoke(t, expected, "main", "a")
expected = stackitem.Make(7)
c.Invoke(t, expected, "main", "abc 123")
}
func spawnVM(t *testing.T, ic *interop.Context, src string) *vm.VM {
b, di, err := compiler.CompileWithOptions("foo.go", strings.NewReader(src), nil)
require.NoError(t, err)

View file

@ -7,6 +7,7 @@ import (
"errors"
"math/big"
"strings"
"unicode/utf8"
"github.com/mr-tron/base58"
"github.com/nspcc-dev/neo-go/pkg/config"
@ -159,6 +160,11 @@ func newStd() *Std {
md = newMethodAndPrice(s.stringSplit3, 1<<8, callflag.NoneFlag)
s.AddMethod(md, desc)
desc = newDescriptor("strLen", smartcontract.IntegerType,
manifest.NewParameter("str", smartcontract.StringType))
md = newMethodAndPrice(s.strLen, 1<<8, callflag.NoneFlag)
s.AddMethod(md, desc)
return s
}
@ -421,6 +427,12 @@ func (s *Std) stringSplitAux(str, sep string, removeEmpty bool) []stackitem.Item
return result
}
func (s *Std) strLen(_ *interop.Context, args []stackitem.Item) stackitem.Item {
str := s.toLimitedString(args[0])
return stackitem.NewBigInteger(big.NewInt(int64(utf8.RuneCountInString(str))))
}
// Metadata implements the Contract interface.
func (s *Std) Metadata() *interop.ContractMD {
return &s.ContractMD

View file

@ -558,3 +558,25 @@ func TestStringSplit(t *testing.T) {
func() { s.stringSplit2(ic, []stackitem.Item{s1, s2}) })
})
}
func TestStd_StrLen(t *testing.T) {
s := newStd()
ic := &interop.Context{VM: vm.New()}
check := func(t *testing.T, expected int64, str string) {
args := []stackitem.Item{stackitem.Make(str)}
actual := s.strLen(ic, args)
l, ok := actual.Value().(*big.Int)
require.True(t, ok)
require.Equal(t, expected, l.Int64())
}
// These tests are taken from https://github.com/neo-project/neo/pull/2854/files.
check(t, 1, "🦆")
check(t, 1, "ã")
check(t, 1, "a")
bad := string(rune(0xff))
check(t, 1, bad)
check(t, 3, bad+"ab")
}

View file

@ -173,3 +173,10 @@ func StringSplitNonEmpty(s, sep string) []string {
return neogointernal.CallWithToken(Hash, "stringSplit", int(contract.NoneFlag),
s, sep, true).([]string)
}
// StrLen returns length of the string in Utf- characters.
// It uses `strLen` method of StdLib native contract.
func StrLen(s string) int {
return neogointernal.CallWithToken(Hash, "strLen", int(contract.NoneFlag),
s).(int)
}

View file

@ -87,7 +87,7 @@ const (
faultedTxHashLE = "82279bfe9bada282ca0f8cb8e0bb124b921af36f00c69a518320322c6f4fef60"
faultedTxBlock uint32 = 23
invokescriptContractAVM = "VwIADBQBDAMOBQYMDQIODw0DDgcJAAAAAErZMCQE2zBwaEH4J+yMqiYEEUAMFA0PAwIJAAIBAwcDBAUCAQAOBgwJStkwJATbMHFpQfgn7IyqJgQSQBNA"
block20StateRootLE = "ae445869283f8d7e0debc3f455014c73cde21b9802db99e80248da9f393bce14"
block20StateRootLE = "cc8315751c5b6edf39c14f1637bfa0595d6ca86b8ed4d41cb495d572b680504f"
)
var (