mirror of
https://github.com/go-sylixos/elvish.git
synced 2024-12-12 17:27:50 +08:00
Add a "base" builtin.
This commit is contained in:
parent
5bbb8bef13
commit
9793da932c
|
@ -83,6 +83,8 @@ func init() {
|
|||
&BuiltinFn{"lt", wrapFn(lt)},
|
||||
&BuiltinFn{"gt", wrapFn(gt)},
|
||||
|
||||
&BuiltinFn{"base", wrapFn(base)},
|
||||
|
||||
&BuiltinFn{"=", eq},
|
||||
&BuiltinFn{"deepeq", deepeq},
|
||||
|
||||
|
@ -506,6 +508,20 @@ func gt(ec *EvalCtx, nums ...float64) {
|
|||
}
|
||||
}
|
||||
|
||||
var ErrBadBase = errors.New("bad base")
|
||||
|
||||
func base(ec *EvalCtx, b int, nums ...int) {
|
||||
if b < 2 || b > 36 {
|
||||
throw(ErrBadBase)
|
||||
}
|
||||
|
||||
out := ec.ports[1].Chan
|
||||
|
||||
for _, num := range nums {
|
||||
out <- String(strconv.FormatInt(int64(num), b))
|
||||
}
|
||||
}
|
||||
|
||||
var ErrNotEqual = errors.New("not equal")
|
||||
|
||||
func eq(ec *EvalCtx, args []Value) {
|
||||
|
|
|
@ -184,6 +184,7 @@ var evalTests = []struct {
|
|||
String("a"): NewList(strs("1", "2")...)}),
|
||||
String("foo"),
|
||||
}, nomore},
|
||||
{"base 16 42 233", strs("2a", "e9"), nomore},
|
||||
}
|
||||
|
||||
func strs(ss ...string) []Value {
|
||||
|
|
Loading…
Reference in New Issue
Block a user