@@ -545,7 +545,10 @@ func extractNestedYAMLValue(yamlContent, parentKey, childKey string) string {
545545 if err != nil {
546546 return ""
547547 }
548- escapedChild := regexp .QuoteMeta (childKey )
548+ scalarMatchers , err := buildNestedYAMLScalarMatchers (regexp .QuoteMeta (childKey ))
549+ if err != nil {
550+ return ""
551+ }
549552
550553 parentIndent := - 1
551554 childIndent := - 1 // indent of direct children (set on first non-blank line inside the block)
@@ -581,28 +584,39 @@ func extractNestedYAMLValue(yamlContent, parentKey, childKey string) string {
581584 continue
582585 }
583586
584- if value := extractNestedYAMLScalar (line , escapedChild ); value != "" {
587+ if value := extractNestedYAMLScalar (line , scalarMatchers ); value != "" {
585588 return value
586589 }
587590 }
588591
589592 return ""
590593}
591594
592- func extractNestedYAMLScalar (line , escapedChild string ) string {
595+ // buildNestedYAMLScalarMatchers compiles the scalar value patterns for a child key once,
596+ // so they can be reused across every candidate line inside the parent block.
597+ func buildNestedYAMLScalarMatchers (escapedChild string ) ([]* regexp.Regexp , error ) {
593598 childPrefix := `^\s+` + escapedChild + `[ \t]*:[ \t]*`
594599 valuePatterns := []string {
595600 childPrefix + `'([^'\n]+)'` ,
596601 childPrefix + `"([^"\n]+)"` ,
597602 childPrefix + `([^'"\n#][^\n#]*?)(?:[ \t]*#.*)?$` ,
598603 }
599604
605+ matchers := make ([]* regexp.Regexp , 0 , len (valuePatterns ))
600606 for _ , valuePattern := range valuePatterns {
601607 //nolint:regexpdynamicpattern // The child key is quoted before compilation.
602608 valueRegexp , err := regexp .Compile (valuePattern )
603609 if err != nil {
604- return ""
610+ return nil , err
605611 }
612+ matchers = append (matchers , valueRegexp )
613+ }
614+
615+ return matchers , nil
616+ }
617+
618+ func extractNestedYAMLScalar (line string , matchers []* regexp.Regexp ) string {
619+ for _ , valueRegexp := range matchers {
606620 if match := valueRegexp .FindStringSubmatch (line ); len (match ) >= 2 {
607621 return strings .TrimSpace (match [1 ])
608622 }
0 commit comments