From 33f6d4c6501b90728a107e917af6f63671f7443f Mon Sep 17 00:00:00 2001 From: Hiroyuki Kuromiya <15026387+kromiii@users.noreply.github.com> Date: Tue, 2 Jul 2024 23:23:19 +0900 Subject: [PATCH] Add function to check if string contains any substrings --- internal/modifier/modifier.go | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/internal/modifier/modifier.go b/internal/modifier/modifier.go index 28305b8..25afcb5 100644 --- a/internal/modifier/modifier.go +++ b/internal/modifier/modifier.go @@ -2,6 +2,8 @@ package modifier import ( "os" + "regexp" + "strings" "github.com/kromiii/unleash-checker-ai/pkg/openai" ) @@ -22,10 +24,26 @@ func (m *Modifier) ModifyFile(filePath string, unusedFlags []string) error { return err } - modifiedContent, err := m.openaiClient.ModifyCode(string(content), unusedFlags) - if err != nil { - return err + modifiedContent := string(content) + lines := strings.Split(modifiedContent, "\n") + for i, line := range lines { + if containsAny(line, unusedFlags) { + modifiedLine, err := m.openaiClient.ModifyCode(line, unusedFlags) + if err != nil { + return err + } + + lines[i] = modifiedLine + } } + modifiedContent = strings.Join(lines, "\n") + return os.WriteFile(filePath, []byte(modifiedContent), 0644) } + +func containsAny(s string, substrs []string) bool { + pattern := strings.Join(substrs, "|") + matched, _ := regexp.MatchString(pattern, s) + return matched +}