github.com/markusbkk/elvish@v0.0.0-20231204143114-91dc52438621/pkg/eval/var_parse.go (about)

     1  package eval
     2  
     3  import "strings"
     4  
     5  // SplitSigil splits any leading sigil from a qualified variable name.
     6  func SplitSigil(ref string) (sigil string, qname string) {
     7  	if ref == "" {
     8  		return "", ""
     9  	}
    10  	// TODO: Support % (and other sigils?) if https://b.elv.sh/584 is implemented for map explosion.
    11  	switch ref[0] {
    12  	case '@':
    13  		return ref[:1], ref[1:]
    14  	default:
    15  		return "", ref
    16  	}
    17  }
    18  
    19  // SplitQName splits a qualified name into the first namespace segment and the
    20  // rest.
    21  func SplitQName(qname string) (first, rest string) {
    22  	colon := strings.IndexByte(qname, ':')
    23  	if colon == -1 {
    24  		return qname, ""
    25  	}
    26  	return qname[:colon+1], qname[colon+1:]
    27  }
    28  
    29  // SplitQNameSegs splits a qualified name into namespace segments.
    30  func SplitQNameSegs(qname string) []string {
    31  	segs := strings.SplitAfter(qname, ":")
    32  	if len(segs) > 0 && segs[len(segs)-1] == "" {
    33  		segs = segs[:len(segs)-1]
    34  	}
    35  	return segs
    36  }
    37  
    38  // SplitIncompleteQNameNs splits an incomplete qualified variable name into the
    39  // namespace part and the name part.
    40  func SplitIncompleteQNameNs(qname string) (ns, name string) {
    41  	colon := strings.LastIndexByte(qname, ':')
    42  	// If colon is -1, colon+1 will be 0, rendering an empty ns.
    43  	return qname[:colon+1], qname[colon+1:]
    44  }