elvish/eval/builtin_fn_str.go

171 lines
3.7 KiB
Go
Raw Normal View History

2017-12-17 13:20:03 +08:00
package eval
import (
"bytes"
"errors"
"regexp"
"strconv"
"strings"
"github.com/elves/elvish/eval/types"
2017-12-17 13:20:03 +08:00
"github.com/elves/elvish/util"
)
// String operations.
var ErrInput = errors.New("input error")
func init() {
addToReflectBuiltinFns(map[string]interface{}{
"<s": func(a, b string) bool { return a < b },
"<=s": func(a, b string) bool { return a <= b },
"==s": func(a, b string) bool { return a == b },
"!=s": func(a, b string) bool { return a != b },
">s": func(a, b string) bool { return a > b },
">=s": func(a, b string) bool { return a >= b },
"to-string": toString,
"ord": ord,
"base": base,
"wcswidth": util.Wcswidth,
"-override-wcwidth": util.OverrideWcwidth,
"has-prefix": strings.HasPrefix,
"has-suffix": strings.HasSuffix,
})
2017-12-17 13:20:03 +08:00
addToBuiltinFns([]*BuiltinFn{
{"joins", joins},
{"splits", splits},
{"replaces", replaces},
{"eawk", eawk},
})
}
// toString converts all arguments to strings.
func toString(fm *Frame, args ...interface{}) {
out := fm.OutputChan()
2017-12-17 13:20:03 +08:00
for _, a := range args {
out <- types.ToString(a)
2017-12-17 13:20:03 +08:00
}
}
// joins joins all input strings with a delimiter.
func joins(ec *Frame, args []interface{}, opts map[string]interface{}) {
var sepv string
2017-12-17 13:20:03 +08:00
iterate := ScanArgsOptionalInput(ec, args, &sepv)
sep := sepv
2017-12-17 13:20:03 +08:00
TakeNoOpt(opts)
var buf bytes.Buffer
iterate(func(v interface{}) {
if s, ok := v.(string); ok {
2017-12-17 13:20:03 +08:00
if buf.Len() > 0 {
buf.WriteString(sep)
}
buf.WriteString(s)
2017-12-17 13:20:03 +08:00
} else {
throwf("join wants string input, got %s", types.Kind(v))
2017-12-17 13:20:03 +08:00
}
})
out := ec.ports[1].Chan
out <- buf.String()
2017-12-17 13:20:03 +08:00
}
// splits splits an argument strings by a delimiter and writes all pieces.
func splits(ec *Frame, args []interface{}, opts map[string]interface{}) {
2017-12-21 06:40:33 +08:00
var (
s, sep string
2017-12-21 06:40:33 +08:00
optMax int
)
2017-12-17 13:20:03 +08:00
ScanArgs(args, &sep, &s)
ScanOpts(opts, OptToScan{"max", &optMax, "-1"})
2017-12-17 13:20:03 +08:00
out := ec.ports[1].Chan
parts := strings.SplitN(s, sep, optMax)
2017-12-17 13:20:03 +08:00
for _, p := range parts {
out <- p
2017-12-17 13:20:03 +08:00
}
}
func replaces(ec *Frame, args []interface{}, opts map[string]interface{}) {
2017-12-17 13:20:03 +08:00
var (
old, repl, s string
2017-12-17 13:20:03 +08:00
optMax int
)
ScanArgs(args, &old, &repl, &s)
ScanOpts(opts, OptToScan{"max", &optMax, "-1"})
2017-12-17 13:20:03 +08:00
ec.ports[1].Chan <- strings.Replace(s, old, repl, optMax)
2017-12-17 13:20:03 +08:00
}
func ord(fm *Frame, s string) {
out := fm.ports[1].Chan
2017-12-17 13:20:03 +08:00
for _, r := range s {
out <- "0x" + strconv.FormatInt(int64(r), 16)
2017-12-17 13:20:03 +08:00
}
}
// ErrBadBase is thrown by the "base" builtin if the base is smaller than 2 or
// greater than 36.
var ErrBadBase = errors.New("bad base")
func base(fm *Frame, b int, nums ...int) error {
2017-12-17 13:20:03 +08:00
if b < 2 || b > 36 {
return ErrBadBase
2017-12-17 13:20:03 +08:00
}
out := fm.ports[1].Chan
2017-12-17 13:20:03 +08:00
for _, num := range nums {
out <- strconv.FormatInt(int64(num), b)
2017-12-17 13:20:03 +08:00
}
return nil
2017-12-17 13:20:03 +08:00
}
var eawkWordSep = regexp.MustCompile("[ \t]+")
// eawk takes a function. For each line in the input stream, it calls the
// function with the line and the words in the line. The words are found by
// stripping the line and splitting the line by whitespaces. The function may
// call break and continue. Overall this provides a similar functionality to
// awk, hence the name.
func eawk(ec *Frame, args []interface{}, opts map[string]interface{}) {
var f Callable
2017-12-17 13:20:03 +08:00
iterate := ScanArgsOptionalInput(ec, args, &f)
TakeNoOpt(opts)
broken := false
iterate(func(v interface{}) {
2017-12-17 13:20:03 +08:00
if broken {
return
}
line, ok := v.(string)
2017-12-17 13:20:03 +08:00
if !ok {
throw(ErrInput)
}
args := []interface{}{line}
for _, field := range eawkWordSep.Split(strings.Trim(line, " \t"), -1) {
args = append(args, field)
2017-12-17 13:20:03 +08:00
}
newec := ec.fork("fn of eawk")
// TODO: Close port 0 of newec.
ex := newec.PCall(f, args, NoOpts)
ClosePorts(newec.ports)
if ex != nil {
switch ex.(*Exception).Cause {
case nil, Continue:
// nop
case Break:
broken = true
default:
throw(ex)
}
}
})
}