Merge pull request #604 from xushiwei/q

library: encoding/{binary, hex, base32, base64}
This commit is contained in:
xushiwei
2024-07-30 00:38:12 +08:00
committed by GitHub
3 changed files with 53 additions and 0 deletions

View File

@@ -291,6 +291,10 @@ Here are the Go packages that can be imported correctly:
* [fmt](https://pkg.go.dev/fmt) (partially)
* [reflect](https://pkg.go.dev/reflect) (partially)
* [time](https://pkg.go.dev/time) (partially)
* [encoding/binary](https://pkg.go.dev/encoding/binary)
* [encoding/hex](https://pkg.go.dev/encoding/hex)
* [encoding/base32](https://pkg.go.dev/encoding/base32)
* [encoding/base64](https://pkg.go.dev/encoding/base64)
* [regexp](https://pkg.go.dev/regexp)
* [regexp/syntax](https://pkg.go.dev/regexp/syntax)

View File

@@ -0,0 +1,48 @@
package main
import (
"encoding/base32"
"encoding/base64"
"encoding/hex"
"fmt"
"log"
)
func base64Demo() {
msg := "Hello, 世界"
encoded := base64.StdEncoding.EncodeToString([]byte(msg))
fmt.Println(encoded)
decoded, err := base64.StdEncoding.DecodeString(encoded)
if err != nil {
fmt.Println("decode error:", err)
return
}
fmt.Println(string(decoded))
}
func base32Demo() {
str := "JBSWY3DPFQQHO33SNRSCC==="
dst := make([]byte, base32.StdEncoding.DecodedLen(len(str)))
n, err := base32.StdEncoding.Decode(dst, []byte(str))
if err != nil {
fmt.Println("decode error:", err)
return
}
dst = dst[:n]
fmt.Printf("%q\n", dst)
}
func hexDemo() {
const s = "48656c6c6f20476f7068657221"
decoded, err := hex.DecodeString(s)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", decoded)
}
func main() {
base64Demo()
base32Demo()
hexDemo()
}

View File

@@ -699,6 +699,7 @@ func (b Builder) ChangeType(t Type, x Expr) (ret Expr) {
case vkFuncDecl:
ret.impl = checkExpr(x, t.raw.Type, b).impl
case vkClosure:
// TODO(xsw): change type should be a noop instruction
convType := func() Expr {
r := Expr{llvm.CreateAlloca(b.impl, t.ll), b.Prog.Pointer(t)}
b.Store(r, x)