MCPcopy Create free account
hub / github.com/cel-expr/cel-go / parseFormatString

Function parseFormatString

ext/formatting.go:783–827  ·  view source on GitHub ↗

parseFormatString formats a string according to the string.format syntax, taking the clause implementations from the provided FormatCallback and the args from the given FormatList.

(formatStr string, callback formatStringInterpolator, list formatListArgs, locale string, maxPrecision int)

Source from the content-addressed store, hash-verified

781// parseFormatString formats a string according to the string.format syntax, taking the clause implementations
782// from the provided FormatCallback and the args from the given FormatList.
783func parseFormatString(formatStr string, callback formatStringInterpolator, list formatListArgs, locale string, maxPrecision int) (string, error) {
784 i := 0
785 argIndex := 0
786 var builtStr strings.Builder
787 for i < len(formatStr) {
788 if formatStr[i] == '%' {
789 if i+1 < len(formatStr) && formatStr[i+1] == '%' {
790 err := builtStr.WriteByte('%')
791 if err != nil {
792 return "", fmt.Errorf("error writing format string: %w", err)
793 }
794 i += 2
795 continue
796 } else {
797 argAny, err := list.Arg(int64(argIndex))
798 if err != nil {
799 return "", err
800 }
801 if i+1 >= len(formatStr) {
802 return "", errors.New("unexpected end of string")
803 }
804 if int64(argIndex) >= list.Size() {
805 return "", fmt.Errorf("index %d out of range", argIndex)
806 }
807 numRead, val, refErr := parseAndFormatClause(formatStr[i:], argAny, callback, list, locale, maxPrecision)
808 if refErr != nil {
809 return "", refErr
810 }
811 _, err = builtStr.WriteString(val)
812 if err != nil {
813 return "", fmt.Errorf("error writing format string: %w", err)
814 }
815 i += numRead
816 argIndex++
817 }
818 } else {
819 err := builtStr.WriteByte(formatStr[i])
820 if err != nil {
821 return "", fmt.Errorf("error writing format string: %w", err)
822 }
823 i++
824 }
825 }
826 return builtStr.String(), nil
827}
828
829// parseAndFormatClause parses the format clause at the start of the given string with val, and returns
830// how many characters were consumed and the substituted string form of val, or an error if one occurred.

Callers 2

ValidateMethod · 0.85
CompileOptionsMethod · 0.85

Calls 5

parseAndFormatClauseFunction · 0.85
NewMethod · 0.80
ArgMethod · 0.65
SizeMethod · 0.65
StringMethod · 0.65

Tested by

no test coverage detected