2018-01-30 01:29:38 +08:00
|
|
|
package eval
|
|
|
|
|
|
|
|
import "strings"
|
|
|
|
|
2019-10-23 05:49:32 +08:00
|
|
|
// SplitVariableRef splits a variable reference into the sigil and the
|
|
|
|
// (qualified) name.
|
|
|
|
func SplitVariableRef(ref string) (sigil string, qname string) {
|
|
|
|
if ref == "" {
|
|
|
|
return "", ""
|
|
|
|
}
|
|
|
|
switch ref[0] {
|
|
|
|
case '@':
|
|
|
|
// TODO(xiaq): Support % later.
|
|
|
|
return ref[:1], ref[1:]
|
|
|
|
default:
|
|
|
|
return "", ref
|
|
|
|
}
|
2018-01-30 02:12:14 +08:00
|
|
|
}
|
|
|
|
|
2020-12-25 01:39:51 +08:00
|
|
|
func SplitQName(qname string) (first, rest string) {
|
|
|
|
colon := strings.IndexByte(qname, ':')
|
|
|
|
if colon == -1 {
|
|
|
|
return qname, ""
|
|
|
|
}
|
|
|
|
return qname[:colon+1], qname[colon+1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
// SplitQNameSegs splits a qualified name into namespace segments.
|
|
|
|
func SplitQNameSegs(qname string) []string {
|
|
|
|
segs := strings.SplitAfter(qname, ":")
|
|
|
|
if len(segs) > 0 && segs[len(segs)-1] == "" {
|
|
|
|
segs = segs[:len(segs)-1]
|
|
|
|
}
|
|
|
|
return segs
|
|
|
|
}
|
|
|
|
|
2019-10-23 05:49:32 +08:00
|
|
|
// SplitQNameNs splits a qualified variable name into the namespace part and the
|
|
|
|
// name part.
|
|
|
|
func SplitQNameNs(qname string) (ns, name string) {
|
|
|
|
if qname == "" {
|
|
|
|
return "", ""
|
2018-01-30 01:29:38 +08:00
|
|
|
}
|
2019-10-23 05:49:32 +08:00
|
|
|
colon := strings.LastIndexByte(qname[:len(qname)-1], ':')
|
|
|
|
// If colon is -1, colon+1 will be 0, rendering an empty ns.
|
|
|
|
return qname[:colon+1], qname[colon+1:]
|
2018-01-30 01:29:38 +08:00
|
|
|
}
|
|
|
|
|
2020-11-14 12:14:03 +08:00
|
|
|
// SplitQNameNsIncomplete splits an incomplete qualified variable name into
|
|
|
|
// the namespace part and the name part.
|
2019-10-23 05:49:32 +08:00
|
|
|
func SplitQNameNsIncomplete(qname string) (ns, name string) {
|
|
|
|
colon := strings.LastIndexByte(qname, ':')
|
|
|
|
// If colon is -1, colon+1 will be 0, rendering an empty ns.
|
|
|
|
return qname[:colon+1], qname[colon+1:]
|
2018-01-30 02:12:14 +08:00
|
|
|
}
|
|
|
|
|
2020-11-14 12:14:03 +08:00
|
|
|
// SplitQNameNsFirst splits a qualified variable name into the first part and
|
|
|
|
// the rest.
|
2019-10-23 05:49:32 +08:00
|
|
|
func SplitQNameNsFirst(qname string) (ns, rest string) {
|
|
|
|
colon := strings.IndexByte(qname, ':')
|
|
|
|
if colon == len(qname)-1 {
|
|
|
|
// Unqualified variable ending with colon ($name:).
|
|
|
|
return "", qname
|
|
|
|
}
|
|
|
|
// If colon is -1, colon+1 will be 0, rendering an empty ns.
|
|
|
|
return qname[:colon+1], qname[colon+1:]
|
2018-01-30 02:12:14 +08:00
|
|
|
}
|
|
|
|
|
2020-11-14 12:14:03 +08:00
|
|
|
// SplitIncompleteQNameFirstNs splits an incomplete qualified variable name
|
|
|
|
// into the first part and the rest.
|
2019-10-23 05:49:32 +08:00
|
|
|
func SplitIncompleteQNameFirstNs(qname string) (ns, rest string) {
|
|
|
|
colon := strings.IndexByte(qname, ':')
|
|
|
|
// If colon is -1, colon+1 will be 0, rendering an empty ns.
|
|
|
|
return qname[:colon+1], qname[colon+1:]
|
2018-01-30 01:29:38 +08:00
|
|
|
}
|
|
|
|
|
2019-10-23 05:49:32 +08:00
|
|
|
// SplitQNameNsSegs splits a qualified name into namespace segments.
|
|
|
|
func SplitQNameNsSegs(qname string) []string {
|
|
|
|
segs := strings.SplitAfter(qname, ":")
|
|
|
|
if len(segs) > 0 && segs[len(segs)-1] == "" {
|
|
|
|
segs = segs[:len(segs)-1]
|
2018-01-30 01:29:38 +08:00
|
|
|
}
|
2019-10-23 05:49:32 +08:00
|
|
|
return segs
|
2018-01-30 01:29:38 +08:00
|
|
|
}
|