Skip to content

Commit

Permalink
fix: ufmt multi-byte fix. (#1889)
Browse files Browse the repository at this point in the history
<!-- please provide a detailed description of the changes made in this
pull request. -->

<details><summary>Contributors' checklist...</summary>

- [x] Added new tests, or not needed, or not feasible
- [ ] Provided an example (e.g. screenshot) to aid review or the PR is
self-explanatory
- [ ] Updated the official documentation or not needed
- [ ] No breaking changes were made, or a `BREAKING CHANGE: xxx` message
was included in the description
- [ ] Added references to related issues and PRs
- [ ] Provided any useful hints for running manual tests
- [ ] Added new benchmarks to [generated
graphs](https://gnoland.github.io/benchmarks), if any. More info
[here](https://github.com/gnolang/gno/blob/master/.benchmarks/README.md).
</details>
  • Loading branch information
MalekLahbib authored Apr 8, 2024
1 parent 831bb6f commit 1f14e7b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
18 changes: 10 additions & 8 deletions examples/gno.land/p/demo/ufmt/ufmt.gno
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,26 @@ func Println(args ...interface{}) {
// %t: formats a boolean value to "true" or "false".
// %%: outputs a literal %. Does not consume an argument.
func Sprintf(format string, args ...interface{}) string {
end := len(format)
// we use runes to handle multi-byte characters
sTor := []rune(format)
end := len(sTor)
argNum := 0
argLen := len(args)
buf := ""

for i := 0; i < end; {
isLast := i == end-1
c := format[i]
c := string(sTor[i])

if isLast || c != '%' {
if isLast || c != "%" {
// we don't check for invalid format like a one ending with "%"
buf += string(c)
i++
continue
}

verb := format[i+1]
if verb == '%' {
verb := string(sTor[i+1])
if verb == "%" {
buf += "%"
i += 2
continue
Expand All @@ -82,7 +84,7 @@ func Sprintf(format string, args ...interface{}) string {
argNum++

switch verb {
case 's':
case "s":
switch v := arg.(type) {
case interface{ String() string }:
buf += v.String()
Expand All @@ -91,7 +93,7 @@ func Sprintf(format string, args ...interface{}) string {
default:
buf += "(unhandled)"
}
case 'd':
case "d":
switch v := arg.(type) {
case int:
buf += strconv.Itoa(v)
Expand All @@ -116,7 +118,7 @@ func Sprintf(format string, args ...interface{}) string {
default:
buf += "(unhandled)"
}
case 't':
case "t":
switch v := arg.(type) {
case bool:
if v {
Expand Down
3 changes: 3 additions & 0 deletions examples/gno.land/p/demo/ufmt/ufmt_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ func TestSprintf(t *testing.T) {
{"no args", nil, "no args"},
{"finish with %", nil, "finish with %"},
{"stringer [%s]", []interface{}{stringer{}}, "stringer [I'm a stringer]"},
{"â", nil, "â"},
{"Hello, World! 😊", nil, "Hello, World! 😊"},
{"unicode formatting: %s", []interface{}{"😊"}, "unicode formatting: 😊"},
}

for _, tc := range cases {
Expand Down

0 comments on commit 1f14e7b

Please sign in to comment.