Skip to content

Commit

Permalink
support uint128/uint256 toString
Browse files Browse the repository at this point in the history
  • Loading branch information
douyixuan committed Sep 13, 2024
1 parent 88a47d0 commit 7c2dab0
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ test/example:
${CELL} -d -t riscv tests/examples/if-cond.cell && ckb-debugger --bin if-cond${exe} | grep "100:0:ss"
${CELL} -d -t riscv tests/examples/switch.cell && ckb-debugger --bin switch${exe} | grep "five"
${CELL} -d -t riscv tests/examples/number-literal.cell && ckb-debugger --bin number-literal${exe}
${CELL} -d -t riscv tests/examples/to-string.cell && ckb-debugger --bin to-string${exe}
${CELL} -d -t riscv tests/examples/return.cell && ckb-debugger --bin return${exe}
${CELL} -d -t riscv tests/examples/named-ret-type.cell && ckb-debugger --bin named-ret-type${exe} | grep "0"
${CELL} -d -t riscv tests/examples/func.cell && ckb-debugger --bin func${exe} | grep "999"
Expand Down
35 changes: 35 additions & 0 deletions pkg/builtin/builtin.cell
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,38 @@ func (e *error) NotNone() bool {
return !e.none
}

type uint256 uint256
type uint128 uint128

func (x uint256) toString() string {
digits := []uint8{}
for i := x; i > 0u256; i /= 10u256 {
mod := byte(i % 10u256)
digits = append(digits, mod + '0')
}
length := uint64(len(digits))
digits = append(digits, byte(0))
str := make(string, digits[0:length])
for i := 0; i < length - length / 2; i++ {
v := str[i]
str[i] = str[length - 1 - i]
str[length - 1 - i] = v
}
return str
}

func (x uint128) toString() string {
digits := []uint8{}
for i := x; i > 0u128; i /= 10u128 {
mod := byte(i % 10u128)
digits = append(digits, mod + '0')
}
str := make(string, digits)
length := len(str)
for i := 0; i < length - length / 2; i++ {
v := str[i]
str[i] = str[length - 1 - i]
str[length - 1 - i] = v
}
return str
}
12 changes: 12 additions & 0 deletions tests/examples/to-string.cell
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

func main() {
str := 100348u256.toString()
if str != "100348" {
return 1
}
str = 1003249u128.toString()
if str != "1003249" {
return 2
}
return 0
}

0 comments on commit 7c2dab0

Please sign in to comment.