1
0
Fork 0
mirror of https://github.com/kubernetes-sigs/node-feature-discovery.git synced 2024-12-14 11:57:51 +00:00

source/kernel: ditch regexp in kconfig parsing

Regexp is just a resource hog here and does not simplify the code.
This commit is contained in:
Markus Lehtonen 2021-11-30 14:41:03 +02:00
parent ffe12cb1e4
commit d4efc2ce0b

View file

@ -23,7 +23,6 @@ import (
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
"k8s.io/apimachinery/pkg/util/validation"
@ -98,22 +97,27 @@ func parseKconfig(configPath string) (map[string]string, error) {
return nil, fmt.Errorf("failed to read kernel config from %+v", append([]string{configPath}, searchPaths...))
}
// Regexp for matching kconfig flags
re := regexp.MustCompile(`^CONFIG_(?P<flag>\w+)=(?P<value>.+)`)
// Process data, line-by-line
lines := bytes.Split(raw, []byte("\n"))
for _, line := range lines {
if m := re.FindStringSubmatch(string(line)); m != nil {
if m[2] == "y" || m[2] == "m" {
kconfig[m[1]] = "true"
} else {
value := strings.Trim(m[2], `"`)
if len(value) > validation.LabelValueMaxLength {
klog.Warningf("ignoring kconfig option '%s': value exceeds max length of %d characters", m[1], validation.LabelValueMaxLength)
str := string(line)
if strings.HasPrefix(str, "CONFIG_") {
split := strings.SplitN(str, "=", 2)
if len(split) != 2 {
continue
}
kconfig[m[1]] = value
// Trim the "CONFIG_" prefix
name := split[0][7:]
if split[1] == "y" || split[1] == "m" {
kconfig[name] = "true"
} else {
value := strings.Trim(split[1], `"`)
if len(value) > validation.LabelValueMaxLength {
klog.Warningf("ignoring kconfig option '%s': value exceeds max length of %d characters", name, validation.LabelValueMaxLength)
continue
}
kconfig[name] = value
}
}
}