Add/improve function "show"

This commit is contained in:
Maisa 2021-06-07 19:11:33 -03:00
parent ce3a246cd7
commit 2341e36255
5 changed files with 41 additions and 5 deletions

View File

@ -0,0 +1,2 @@
Bits.show_reverse(bits: Bits): String
Bits.show(Bits.reverse(bits))

View File

@ -0,0 +1,15 @@
// Show pairs of hex values ignoring "00"
Bits.to_buf_string(x: Bits): String
let full = Bits.to_hex_string(x)
Bits.to_buf_string.clean(String.reverse(full), "")
Bits.to_buf_string.clean(xs: String, str: String): String
case xs {
nil : str
cons:
let {val, res} = String.take_pair(2, xs)
let new = String.concat(
if String.eql(val, "00") then "" else " " | val,
str)
Bits.to_buf_string.clean(res, new)
}

View File

@ -1,8 +1,8 @@
Buffer32.show(buf: Buffer32): String
let str = "<Buffer32 "
let str = "<Buffer32"
for i: U32 from 0 to Nat.to_u32(buf@depth) with str:
let crunk = Buffer32.get(i, buf)// group of 4 bytes
let bits = U32.to_bits(crunk)
let hex = Bits.to_hex_string(bits)
let crunk = Buffer32.get(i, buf) // group of 4 bytes
let bits = U32.to_bits(crunk)
let hex = Bits.to_buf_string(bits)
str | " " | hex
str | " >"
str | ">"

7
base/Buffer8/show.kind Normal file
View File

@ -0,0 +1,7 @@
Buffer8.show(buf: Buffer8): String
let str = "<Buffer8 "
for i from 0 to buf@depth with str:
let bits = U32.to_bits(Buffer32.get(i, buf)) // group of 4 bytes
let hex = Bits.to_hex_string(bits)
str | " " | hex
str | " >"

View File

@ -0,0 +1,12 @@
String.take_pair(n: Nat, xs: String): Pair<String, String>
String.take_pair.go(n, xs, "")
String.take_pair.go(n: Nat, xs: String, take_aux: String): Pair<String, String>
case xs {
nil : {take_aux, xs}
cons: case n {
zero : {take_aux, xs}
succ:
String.take_pair.go(n.pred, xs.tail, String.cons(xs.head, take_aux))
}
}