Skip to content

Commit

Permalink
feat: ufmt package to print all numeric primitive (#1784)
Browse files Browse the repository at this point in the history
  • Loading branch information
r3v4s authored Mar 19, 2024
1 parent 2ddce90 commit 5c5d9ef
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
12 changes: 12 additions & 0 deletions examples/gno.land/p/demo/ufmt/ufmt.gno
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,22 @@ func Sprintf(format string, args ...interface{}) string {
switch v := arg.(type) {
case int:
buf += strconv.Itoa(v)
case int8:
buf += strconv.Itoa(int(v))
case int16:
buf += strconv.Itoa(int(v))
case int32:
buf += strconv.Itoa(int(v))
case int64:
buf += strconv.Itoa(int(v))
case uint:
buf += strconv.FormatUint(uint64(v), 10)
case uint8:
buf += strconv.FormatUint(uint64(v), 10)
case uint16:
buf += strconv.FormatUint(uint64(v), 10)
case uint32:
buf += strconv.FormatUint(uint64(v), 10)
case uint64:
buf += strconv.FormatUint(v, 10)
default:
Expand Down
10 changes: 8 additions & 2 deletions examples/gno.land/p/demo/ufmt/ufmt_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,15 @@ func TestSprintf(t *testing.T) {
{"hi %%%s!", []interface{}{"worl%d"}, "hi %worl%d!"},
{"string [%s]", []interface{}{"foo"}, "string [foo]"},
{"int [%d]", []interface{}{int(42)}, "int [42]"},
{"int8 [%d]", []interface{}{int8(8)}, "int8 [8]"},
{"int16 [%d]", []interface{}{int16(16)}, "int16 [16]"},
{"int32 [%d]", []interface{}{int32(32)}, "int32 [32]"},
{"int64 [%d]", []interface{}{int64(64)}, "int64 [64]"},
{"uint [%d]", []interface{}{uint(42)}, "uint [42]"},
{"int64 [%d]", []interface{}{int64(42)}, "int64 [42]"},
{"uint64 [%d]", []interface{}{uint64(42)}, "uint64 [42]"},
{"uint8 [%d]", []interface{}{uint8(8)}, "uint8 [8]"},
{"uint16 [%d]", []interface{}{uint16(16)}, "uint16 [16]"},
{"uint32 [%d]", []interface{}{uint32(32)}, "uint32 [32]"},
{"uint64 [%d]", []interface{}{uint64(64)}, "uint64 [64]"},
{"bool [%t]", []interface{}{true}, "bool [true]"},
{"bool [%t]", []interface{}{false}, "bool [false]"},
{"invalid bool [%t]", []interface{}{"invalid"}, "invalid bool [(unhandled)]"},
Expand Down

0 comments on commit 5c5d9ef

Please sign in to comment.