MCPcopy
hub / github.com/google/mangle / String

Method String

ast/ast.go:562–648  ·  view source on GitHub ↗

String returns a string representation of the constant.

()

Source from the content-addressed store, hash-verified

560
561// String returns a string representation of the constant.
562func (c Constant) String() string {
563 switch c.Type {
564 case NameType:
565 return string(c.Symbol)
566 case StringType:
567 s, err := Escape(c.Symbol, false /* isBytes */)
568 if err != nil {
569 return "<bad>"
570 }
571 return fmt.Sprintf(`"%s"`, s)
572 case BytesType:
573 s, err := Escape(c.Symbol, true /* isBytes */)
574 if err != nil {
575 return "<bad>"
576 }
577 return fmt.Sprintf(`b"%s"`, s)
578 case NumberType:
579 return FormatNumber(c.NumValue)
580 case Float64Type:
581 return FormatFloat64(math.Float64frombits(uint64(c.NumValue)))
582 case TimeType:
583 return fmt.Sprintf("fn:time:parse_rfc3339(\"%s\")", FormatTime(c.NumValue))
584 case DurationType:
585 return fmt.Sprintf("fn:duration:parse(\"%s\")", FormatDuration(c.NumValue))
586 case PairShape:
587 fst := *c.fst
588 snd := *c.snd
589 return fmt.Sprintf("fn:pair(%s, %s)", fst.String(), snd.String())
590 case ListShape:
591 if c.IsListNil() {
592 return "[]"
593 }
594 var s strings.Builder
595 s.WriteRune('[')
596 s.WriteString((*c.fst).String())
597 c = *c.snd
598 for !c.IsListNil() {
599 s.WriteString(", ")
600 s.WriteString((*c.fst).String())
601 c = *c.snd
602 }
603 s.WriteRune(']')
604 return s.String()
605 case MapShape:
606 if c.IsMapNil() {
607 return "fn:map()"
608 }
609 var s strings.Builder
610 s.WriteRune('[')
611 s.WriteString((*c.fst.fst).String())
612 s.WriteString(" : ")
613 s.WriteString((*c.fst.snd).String())
614 c = *c.snd
615 for !c.IsMapNil() {
616 s.WriteString(", ")
617 s.WriteString((*c.fst.fst).String())
618 s.WriteString(" : ")
619 s.WriteString((*c.fst.snd).String())

Callers 1

DisplayStringMethod · 0.95

Calls 9

IsListNilMethod · 0.95
IsMapNilMethod · 0.95
IsStructNilMethod · 0.95
EscapeFunction · 0.85
FormatNumberFunction · 0.85
FormatFloat64Function · 0.85
FormatTimeFunction · 0.85
FormatDurationFunction · 0.85
StringMethod · 0.65

Tested by

no test coverage detected