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

Function parseFormatStringV2

ext/formatting_v2.go:673–717  ·  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 formatStringInterpolatorV2, list formatListArgs, maxPrecision int)

Source from the content-addressed store, hash-verified

671// parseFormatString formats a string according to the string.format syntax, taking the clause implementations
672// from the provided FormatCallback and the args from the given FormatList.
673func parseFormatStringV2(formatStr string, callback formatStringInterpolatorV2, list formatListArgs, maxPrecision int) (string, error) {
674 i := 0
675 argIndex := 0
676 var builtStr strings.Builder
677 for i < len(formatStr) {
678 if formatStr[i] == '%' {
679 if i+1 < len(formatStr) && formatStr[i+1] == '%' {
680 err := builtStr.WriteByte('%')
681 if err != nil {
682 return "", fmt.Errorf("error writing format string: %w", err)
683 }
684 i += 2
685 continue
686 } else {
687 argAny, err := list.Arg(int64(argIndex))
688 if err != nil {
689 return "", err
690 }
691 if i+1 >= len(formatStr) {
692 return "", errors.New("unexpected end of string")
693 }
694 if int64(argIndex) >= list.Size() {
695 return "", fmt.Errorf("index %d out of range", argIndex)
696 }
697 numRead, val, refErr := parseAndFormatClauseV2(formatStr[i:], argAny, callback, list, maxPrecision)
698 if refErr != nil {
699 return "", refErr
700 }
701 _, err = builtStr.WriteString(val)
702 if err != nil {
703 return "", fmt.Errorf("error writing format string: %w", err)
704 }
705 i += numRead
706 argIndex++
707 }
708 } else {
709 err := builtStr.WriteByte(formatStr[i])
710 if err != nil {
711 return "", fmt.Errorf("error writing format string: %w", err)
712 }
713 i++
714 }
715 }
716 return builtStr.String(), nil
717}
718
719// parseAndFormatClause parses the format clause at the start of the given string with val, and returns
720// 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

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

Tested by

no test coverage detected