github.com/hoop33/elvish@v0.0.0-20160801152013-6d25485beab4/eval/string.go (about)

     1  package eval
     2  
     3  import "github.com/elves/elvish/parse"
     4  
     5  // String is just a string.
     6  type String string
     7  
     8  func (String) Kind() string {
     9  	return "string"
    10  }
    11  
    12  func (s String) Repr(int) string {
    13  	return quote(string(s))
    14  }
    15  
    16  func (s String) String() string {
    17  	return string(s)
    18  }
    19  
    20  func (s String) Len() int {
    21  	return len(string(s))
    22  }
    23  
    24  // Call resolves a command name to either a Fn variable or external command and
    25  // calls it.
    26  func (s String) Call(ec *EvalCtx, args []Value) {
    27  	resolve(string(s), ec).Call(ec, args)
    28  }
    29  
    30  func resolve(s string, ec *EvalCtx) FnValue {
    31  	// Try variable
    32  	splice, ns, name := ParseVariable(string(s))
    33  	if !splice {
    34  		if v := ec.ResolveVar(ns, FnPrefix+name); v != nil {
    35  			if caller, ok := v.Get().(FnValue); ok {
    36  				return caller
    37  			}
    38  		}
    39  	}
    40  
    41  	// External command
    42  	return ExternalCmd{string(s)}
    43  }
    44  
    45  // ToString converts a Value to String. When the Value type implements
    46  // String(), it is used. Otherwise Repr(NoPretty) is used.
    47  func ToString(v Value) string {
    48  	if s, ok := v.(Stringer); ok {
    49  		return s.String()
    50  	}
    51  	return v.Repr(NoPretty)
    52  }
    53  
    54  func quote(s string) string {
    55  	return parse.Quote(s)
    56  }